Arduino DHT11 Sensor tutorial |
Component requirements:
1. Arduino UnoArduino DHT11 wiring diagram:
Pin Connection:
1. Firstly, make a connection between the Arduino Uno and breadboard.
- Connect Arduino GND pin to breadboard GND side.
- Connect Arduino 5v pin to breadboard 5v side.
Next, connect the breadboard GND side to another GND side of breadboard.
2. Make a connection between the Arduino Uno and DHT11 sensor.
- The DHT11 data pin should be connected to the Arduino pin 4.
- The DHT11 GND pin should be connected to breadboard GND side.
- The DHT11 VCC pin should be connected to the breadboard 5v side.
3.Buzzer connection to arduino.
- Connect the buzzer positive pin to pin 5 of the Arduino.
- Connnect negative pins of buzzer to breadboard GND side.
4. Now, Its time to add a 12v fan, but the problem is arduino can only supply 12 volts. SO the solution for the problem is to use TIP41C transistor, which can be use as load switch.
- Connect TIP41C Emitter pin to breadboard GND side.
- Connect a 1k resistor to the base pin of the TIP41C transistor. Now connect the end of the resistor to arduino pin 6.
Connect the fan GND pin to TIP41C collector pin and the fan positive pin to 12v power supply.
Make sure the power suppy GND pin is connected to arduino GND pin.
5. Now, Make a connection between arduino and DIY mini heater. The heater is made of nichrome wire which also works on 12 volts.
- Connect TIP41C Emitter pin to breadboard GND side.
- Connect a 1k resistor to the base pin of the TIP41C transistor. Now connect the end of the resistor to arduino pin 7.
Connect the heater GND pin to TIP41C collector pin and the heater positive pin to 12v power supply.
Make sure the power suppy GND pin is connected to arduino GND pin.
6. Now, Its time to connect the RGB LEDs to the arduino.
- The RGB Led R pin is protected with a 220 ohm resistor, and the resistor end is attached to the Arduino pin 9.
- The RGB Led G pin is protected with a 220 ohm resistor, and the resistor end is attached to the Arduino pin 10.
- The RGB Led B pin is protected with a 220 ohm resistor, and the resistor end is attached to the Arduino pin 11.
- Connnect RGB led GND pin to breadboard GND side.
In order to protect the LEDs, a 220 ohm resistor is used as a current limiter.
5. Last component is 16x2 LCD display with I2C driver.
- The Arduino A5 pin should be connected to the LCD SCL pin.
- The Arduino A4 pin should be connected to the LCD SDA pin.
- The Arduino 5v pin should be connected to the LCD VCC pin.
- The Arduino GND pin should be connected to the LCD GND pin.
Now the circuit is completed. Lets jump to the coding part.
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float temp = 0;
float humi = 0;
int lcdColumns = 16;
int lcdRows = 2;
//If 0x3f address is not work, then change the address to 0x27
LiquidCrystal_I2C lcd (0x3f, lcdColumns, lcdRows);
int buzzer = 5;
int fan = 6;
int heater = 7;
int rgb[3] = {9, 10, 11};
uint8_t buzz[8] = {B00001, B00011, B00111, B11111, B11111, B00111, B00011, B00001};
uint8_t degree[8] = {B00000, B11000, B11000, B00000, B00000, B00000, B00000, B00000};
uint8_t tem[8] = {B00100, B01010, B01010, B01110, B01110, B11111, B11111, B01110};
uint8_t hum[8] = {B00100, B00100, B01010, B01010, B10001, B10001, B10001, B01110};
uint8_t fanEmoji[8] = {B00000, B11011, B11011, B00100, B11011, B11011, B00000, B00000};
uint8_t heaterEmoji[8] = {B00000, B10001, B10001, B11111, B10001, B10001, B10001, B00000};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
lcd.createChar(0, buzz);
lcd.createChar(1, degree);
lcd.createChar(2, tem);
lcd.createChar(3, hum);
lcd.createChar(4, fanEmoji);
lcd.createChar(5, heaterEmoji);
pinMode(fan, OUTPUT);
pinMode(heater, OUTPUT);
pinMode(buzzer, OUTPUT);
for (int i = 0; i < 3; i++) {
pinMode(rgb[i], OUTPUT);
}
int t = 100;
delay(500);
for (int r = 0; r < 3; r++) {
setColor(148, 0, 211);
delay(t);
setColor(0, 0, 255);
delay(t);
setColor(0, 191, 255);
delay(t);
setColor(0, 255, 0);
delay(t);
setColor(255, 255, 0);
delay(t);
setColor(255, 127, 0);
delay(t);
setColor(255, 0, 0);
delay(t);
}
}
void loop() {
// put your main code here, to run repeatedly:
printData();
if (temp < 15 ) {
setColor(0, 0, 255);
analogWrite(fan, 0);
digitalWrite(heater, HIGH);
lcd.setCursor(14, 0);
lcd.write(0);
lcd.setCursor(15, 0);
lcd.write(5);
tone(buzzer, 1000);
delay(400);
noTone(buzzer);
delay(400);
}
else if (temp > 20 && temp < 25) {
setColor(255, 255, 255);
analogWrite(fan, 51);
lcd.setCursor(15, 0);
lcd.write(4);
noTone(buzzer);
}
else if (temp > 25 && temp < 30) {
setColor(0, 255, 0);
analogWrite(fan, 102);
lcd.setCursor(15, 0);
lcd.write(4);
noTone(buzzer);
}
else if (temp > 30 && temp < 35) {
setColor(255, 255, 0);
analogWrite(fan, 153);
lcd.setCursor(15, 0);
lcd.write(4);
noTone(buzzer);
}
else if (temp > 35 && temp < 40) {
setColor(255, 90, 0);
analogWrite(fan, 204);
lcd.setCursor(15, 0);
lcd.write(4);
noTone(buzzer);
}
else if (temp > 40 && temp < 50) {
setColor(255, 0, 0);
analogWrite(fan, 255);
lcd.setCursor(14, 0);
lcd.write(0);
lcd.setCursor(15, 0);
lcd.write(4);
tone(buzzer, 1000);
}
}
void printData() {
delay(2000);
lcd.clear();
temp = dht.readTemperature();
humi = dht.readHumidity();
if (isnan(temp) || isnan(humi)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("TEMP: "));
Serial.print(temp);
Serial.print(" ");
Serial.print(F("HUMI: "));
Serial.print(humi);
Serial.println(F(" %"));
lcd.setCursor(0, 0);
lcd.print(F("Temp: "));
lcd.print(temp);
lcd.write(1);
lcd.print(F("C"));
lcd.setCursor(0, 1);
lcd.print(F("Hum : "));
lcd.print(humi);
lcd.print(F(" %"));
}
void setColor(int r, int g, int b) {
analogWrite(rgb[0], r);
analogWrite(rgb[1], g);
analogWrite(rgb[2], b);
}