Ahhh... Yes, I am Gollum. Don't worry about the name... I am the code master, but only when I want to be. I wrote this script, a little code to measure the temperature and humidity of my greenhouse using the DHT11 sensor. Why? Because the greenhouse has to be under control, everything has to be perfect, and with this code I can monitor and protect the environment where the plants grow--and the greenhouse is mine, you see? Only mine!
With Arduino and the DHT11 sensor, I display temperature and humidity on an LCD screen. Oh, yes, numbers are everything-but there's more. When the temperature is too high or too low, the red LED lights up, a signal that something is wrong, that the environment might be in danger. We cannot let anything go wrong. Everything must be under control, always.
What does the code do?
- It reads the temperature and humidity with the DHT11 sensor and displays them on an LCD.
- If the temperature exceeds 29 degrees or falls below 10 degrees, the red LED lights up.
- Protecting the environment.
// Variable and library declarations
#include <DHT.h>
#include <LiquidCrystal.h>
int sensor = 4;
#define DHTTYPE DHT11
DHT dht(sensor, DHTTYPE);
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
//SETUP
void setup() {
lcd.begin(16, 2);
lcd.print(“TEMPERATURE:”);
lcd.setCursor(0,1);
lcd.print("HUMIDITY: ”);
pinMode(2, OUTPUT);
}
//LOOP
void loop() {
int t = dht.readTemperature();
int h = dht.readHumidity();
lcd.setCursor(14,0);
lcd.print(t);
lcd.setCursor (14,1);
lcd.print(h);
delay(10);
//if the temperature is above 29 degrees, the red led (pin 2) is activated
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/posx3tywxhqb2b8clm1h.png)
if (t>29) {
digitalWrite(2, HIGH);
}
else {
digitalWrite (2, LOW);
}
//if the temperature is less than 10 degrees, the red LED (pin 2) is activated.
if (t<10) {
digitalWrite (2, HIGH);
}
else {
digitalWrite (2, LOW);
}
}
Components... Everything must be perfect:
- Arduino Uno (or similar, the best friend for my project)
- DHT11 sensor (helps me measure temperature and humidity)
- 16x2 LCD (because I want to see everything clearly!)
- Red LED (to alert me when something goes wrong)
The greenhouse is mine, it's important. The temperature has to stay right, always, if I don't want everything to go wrong. I can't allow that to happen! The code is my tool, my control. If something goes wrong, I will be ready!
written with ♥ by Gollum!
Top comments (0)