Imagine coming home in the summer, and the air conditioning has just adjusted the room temperature to the most comfortable level—this smart experience is something we can create ourselves! Today, we will use the most common Arduino UNO development board, along with a temperature sensor, to build a mini air conditioning system that can autonomously adjust itself. Don’t worry about needing expensive equipment; the material cost is less than 50 yuan. Get ready to enjoy the joy of hardware programming for the first time!
Hardware Preparation and Basic Circuit
Arduino UNO R3 acts like the brain of the robot, responsible for processing data and sending commands. We also need a DHT11 Temperature and Humidity Sensor (which looks like a button battery with three legs) and a small DC motor (to simulate the air conditioning fan).
When connecting the sensor to the development board, pay attention: VCC connects to the 5V pin, DATA connects to digital pin 2, and GND connects to GND. This “three-pin connection” is the standard setup for the sensor; if connected incorrectly, it may not work!
1// Basic wiring test code
2
3#include <DHT.h>
4
5#define DHTPIN 2
6
7#define DHTTYPE DHT11
8
9
10
11DHT dht(DHTPIN, DHTTYPE);
12
13
14
15void setup() {
16
17 Serial.begin(9600);
18
19 dht.begin();
20
21}
22
23
24
25void loop() {
26
27 float temp = dht.readTemperature();
28
29 Serial.print("Current temperature:");
30
31 Serial.println(temp);
32
33 delay(1000);
34
35}
Open the Serial Monitor (the telescope icon in the upper right corner), and if you see the numbers changing, congratulations on taking the first step! The most common issue at this stage is poor sensor contact; if you see abnormal values like “-999”, first check if the connections are loose.
Conditional Judgement of Temperature Data
Now we need to add some “intelligence” to the system—when the temperature exceeds the set value, the fan will start. We will use conditional statements, just like traffic lights: if the temperature is above 28 degrees, the red light turns on (motor starts), and if it is below 25 degrees, the green light turns on (motor stops).
1// Temperature control logic
2
3int motorPin = 9; // Motor connected to pin 9
4
5
6
7void setup() {
8
9 pinMode(motorPin, OUTPUT);
10
11 // Keep the previous sensor initialization code
12
13}
14
15
16
17void loop() {
18
19 float currentTemp = dht.readTemperature();
20
21
22
23 if(currentTemp > 28) {
24
25 digitalWrite(motorPin, HIGH); // Start the motor
26
27 Serial.println("Cooling started!");
28
29 } else if(currentTemp < 25) {
30
31 digitalWrite(motorPin, LOW); // Stop the motor
32
33 Serial.println("Temperature is suitable");
34
35 }
36
37 delay(5000); // Check every 5 seconds
38
39}
Friendly reminder: <span>if</span> statements are like security checkpoints; data must pass inspection to execute the corresponding operation. A common mistake is forgetting to use two equal signs <span>==</span> for equality checks; using a single equal sign will cause the program to malfunction.
System Optimization and Function Expansion
After completing the basic functionality, we can add some “humanized” designs to the system. For example, use the onboard LED for status indication (the LED on pin 13) or emit a sound when the temperature changes.
Try this enhanced version of the code:
1// Advanced version with sound and light prompts
2
3#include <DHT.h>
4
5// Initialization part remains unchanged
6
7
8
9void loop() {
10
11 float temp = dht.readTemperature();
12
13
14
15 if(temp > 28) {
16
17 digitalWrite(9, HIGH);
18
19 digitalWrite(13, HIGH); // Turn on the onboard LED
20
21 tone(8, 1000, 200); // Pin 8 connected to the buzzer
22
23 } else {
24
25 digitalWrite(9, LOW);
26
27 digitalWrite(13, LOW);
28
29 }
30
31
32
33 // Add temperature fluctuation warning
34
35 static float lastTemp = 0;
36
37 if(abs(temp - lastTemp) > 3) {
38
39 Serial.println("Temperature fluctuating drastically!");
40
41 }
42
43 lastTemp = temp;
44
45
46
47 delay(3000);
48
49}
Now the system will light up and beep when cooling starts, and it can also monitor drastic temperature changes. If you encounter issues, remember to print key variable values to the Serial Monitor; this is the ultimate tool for hardware debugging. For example, if it suddenly stops working, add a line <span>Serial.println(temp);</span> to see if the sensor is malfunctioning.
Try making these modifications:
-
Add humidity monitoring functionality (the DHT11 supports this)
-
Connect a mobile app via Bluetooth module
-
Set different temperature preset values for different time periods
-
Record the temperature change curve over 24 hours
If the motor does not turn, first try breathing near the sensor to raise the temperature and confirm that the temperature reading is normal. If the code is fine, it is likely a power supply issue—try connecting the motor to a separate 5V power supply. This is how hardware projects work; sometimes the problem is not in the code but in the fascinating reactions of the physical world.