Hey, do you want to create something fun and practical with Arduino? Today, we will build a simple smart greenhouse monitoring system. Don’t worry, even if this is your first time with Arduino, you can easily manage it by following this tutorial! Imagine creating a small system with your own hands that can automatically detect temperature and humidity, and respond to environmental changes. Isn’t that cool?
Materials You Need
Before we start, let’s take a look at the “weapons” you need to prepare:
-
Arduino UNO development board (a great companion for beginners)
-
DHT11 temperature and humidity sensor (our “eyes”)
-
Breadboard (not the edible kind!)
-
LED lights (one red and one blue)
-
Small fan module
-
Several connecting wires
-
USB data cable (to power the Arduino)
These materials can be purchased at electronics markets or online, and they are inexpensive, suitable for entry-level players.
Quick Supplement of Basic Knowledge
Don’t rush to connect the wires; first, let’s understand the basic structure of Arduino. The Arduino UNO is a small circuit board with 14 digital input/output pins and 6 analog input pins.
Digital pins can be set to high (5V) or low (0V), just like a switch with only two states: on and off. Meanwhile, analog pins can read different voltage values, such as data from sensors.
Arduino is programmed using C/C++ language, but don’t be afraid; its syntax is much simpler than you might think.
Tip: The Arduino program mainly consists of two parts: setup() and loop(). The setup() runs only once for initialization, while loop() continuously executes. It’s like brushing your teeth and washing your face in the morning (setup), but breathing continues all the time (loop).
Connecting Hardware
Now, let’s connect all the components together, just like building with blocks:
- Connecting the DHT11 sensor:
-
VCC to Arduino’s 5V
-
GND to Arduino’s GND (ground)
-
DATA to Arduino’s digital pin 2
- Connecting the LED lights:
-
The long leg (anode) of the red LED connects to digital pin 7 through a 220-ohm resistor
-
The long leg (anode) of the blue LED connects to digital pin 6 through a 220-ohm resistor
-
Both short legs of the LEDs connect to GND
- Fan module:
-
The positive terminal connects to digital pin 9
-
The negative terminal connects to GND
You may wonder why we need resistors for the LEDs. This is to protect the LEDs from burning out, just like adding a pressure relief valve to a water pipeline.
Writing Code
Once the hardware is connected, it’s time to write the code! Open the Arduino IDE and enter the following code:
1#include <DHT.h>
2
3
4
5#define DHTPIN 2 // Pin connected to DHT11
6
7#define DHTTYPE DHT11 // Type of sensor used
8
9#define RED_LED 7 // Red LED pin
10
11#define BLUE_LED 6 // Blue LED pin
12
13#define FAN_PIN 9 // Fan pin
14
15
16
17// Set temperature and humidity thresholds
18
19const float TEMP_HIGH = 28.0;
20
21const float HUMIDITY_HIGH = 80.0;
22
23
24
25DHT dht(DHTPIN, DHTTYPE);
26
27
28
29void setup() {
30
31 Serial.begin(9600); // Initialize serial communication
32
33 dht.begin(); // Initialize DHT sensor
34
35
36
37 // Set pin modes
38
39 pinMode(RED_LED, OUTPUT);
40
41 pinMode(BLUE_LED, OUTPUT);
42
43 pinMode(FAN_PIN, OUTPUT);
44
45
46
47 Serial.println("Smart Greenhouse Monitoring System Started!");
48
49}
50
51
52
53void loop() {
54
55 // Wait for 2 seconds (DHT11 reading interval cannot be too short)
56
57 delay(2000);
58
59
60
61 // Read temperature and humidity data
62
63 float humidity = dht.readHumidity();
64
65 float temperature = dht.readTemperature();
66
67
68
69 // Check if reading was successful
70
71 if (isnan(humidity) || isnan(temperature)) {
72
73 Serial.println("Failed to read data from DHT sensor!");
74
75 return;
76
77 }
78
79
80
81 // Output current temperature and humidity
82
83 Serial.print("Temperature: ");
84
85 Serial.print(temperature);
86
87 Serial.print(" °C, Humidity: ");
88
89 Serial.print(humidity);
90
91 Serial.println(" %");
92
93
94
95 // Control devices based on environmental conditions
96
97 if (temperature > TEMP_HIGH) {
98
99 digitalWrite(RED_LED, HIGH); // Temperature too high, red light on
100
101 digitalWrite(FAN_PIN, HIGH); // Turn on fan to cool down
102
103 } else {
104
105 digitalWrite(RED_LED, LOW); // Temperature normal, red light off
106
107 digitalWrite(FAN_PIN, LOW); // Turn off fan
108
109 }
110
111
112
113 if (humidity > HUMIDITY_HIGH) {
114
115 digitalWrite(BLUE_LED, HIGH); // Humidity too high, blue light on
116
117 } else {
118
119 digitalWrite(BLUE_LED, LOW); // Humidity normal, blue light off
120
121 }
122
123}
It looks a bit long, but this code is actually quite simple. We first include the DHT library, define the various pins, and then initialize them in setup(). In loop(), the code reads the temperature and humidity every two seconds and controls the LEDs and fan based on the preset thresholds.
Upload Code and Test the System
Once the code is written, click the upload button in the Arduino IDE to transfer the code to the development board. After uploading, your smart greenhouse monitoring system will start working!
Open the Serial Monitor (Tools > Serial Monitor), and you will see real-time temperature and humidity data. When the temperature exceeds 28°C, the red LED will light up, and the fan will start spinning; when the humidity exceeds 80%, the blue LED will light up, reminding you to pay attention to high humidity.
Tip: If the upload fails, first check if the board and port are selected correctly. It may also be that the DHT library is not installed; search and install the DHT library through Sketch > Include Library > Manage Libraries.
Improve Your System
With the basic system built, you can add more features:
-
Connect a soil moisture sensor for automatic watering
-
Add a light sensor to control LED strips simulating sunlight
-
Use an OLED display to show temperature and humidity data
-
Add a buzzer to sound an alarm when the environment is abnormal
The principle of the system is actually like this: sensors collect environmental data → Arduino processes this data → controls other devices based on set conditions. Once you grasp this idea, you can design more complex smart systems.
Practical Application Scenarios
This small system may seem simple, but the concept can be expanded to larger projects. For example:
-
Home plant care system
-
Small greenhouse automation control
-
Reptile enclosure environmental monitoring
-
Food storage environmental monitoring
Try to collect data in practical use, analyze environmental change patterns, and then adjust your code and thresholds to make the system smarter.
Alright, give it a try! Remember to observe the system’s response during testing, and don’t be afraid if you encounter problems; solving problems is the essence of learning. Try more, debug more, and you will discover the joy of Arduino!
After completing this project, challenge yourself: can you add a real-time clock module to set different temperature and humidity standards at different times?