Adds

25 June 2015

Arduino with a DHT11 and RGB LED which changes colour based on temperature - Prints temperature and humidity to serial monitor

Code gets:

- Temperature and Humidity from a DHT11 sensor

and

 RGB LED changes colour depending on the Temperature

Prints the data to the Serial Monitor

Please let me know if you experience any problems with this code. Be sure to check back for more Arduino code.

Schematic:




Code:


 /*  
   
 Oliver Holden  
 Created 28/10/2014  
   
 Components:  
 * Arduino UNO  
 * DHT11  
 * 4.7K resistor  
 * RGB LED  
 * 3x 220 ohm resistors  
   
 - Gets Temperature and Humidity from a DHT11 sensor  
   
 - RGB LED changes colour depending on the Temperature   
   
 Prints the data to the Serial Monitor  
   
 Schematic for this project is on  
 http://breadboarder.blogspot.co.uk/  
   
 */  
   
 #include <dht.h>   // Including libary to read DHT11   
    
 dht DHT;   // Need this   
   
 int redPin = 11;   // Red LED,  connected to digital pin 11  
 int grnPin = 10;  // Green LED, connected to digital pin 10  
 int bluPin = 9;  // Blue LED, connected to digital pin 9  
   
 int dht_dpin = A0;   // DHT11 data pin attached to A2   
    
 void setup(){     
    
  Serial.begin(9600);   // Opening serial connection at 9600bps   
  Serial.println("Humidity and temperature");   // Printing Serial Monitor header  
  delay(1000);   // Wait 1 second before printing data to Serial Monitor  
    
  pinMode(redPin, OUTPUT);   // Sets the pins as output for RGB LED  
  pinMode(grnPin, OUTPUT);  
  pinMode(bluPin, OUTPUT);  
    
  }  
    
 void loop(){     
    
  DHT.read11(dht_dpin);   // Reading data from DHT11  
    
   Serial.print("Current humidity = ");   // Printing text  
    
   Serial.print(DHT.humidity);     
   Serial.print("% ");     
   Serial.print("temperature = ");     
   Serial.print(DHT.temperature);     
   Serial.println("C ");     
    
  // Setting the colours of the RGB LED based on temperature,  
 // Colour is shown for 0.1s before checking temperature again.  
   
 if((DHT.temperature < 26) && (DHT.temperature >= 23.2))   // Writing the LED colour pins HIGH or LOW to set colours  
  {  
   digitalWrite(redPin, HIGH);  // yellow  
   digitalWrite(grnPin, HIGH);  
   delay(100);  
     
   digitalWrite(bluPin, LOW);  
   
  }  
    
  if((DHT.temperature < 23) && (DHT.temperature > 20.2))  
  {  
   digitalWrite(grnPin, HIGH);  // green  
   delay(100);  
     
   digitalWrite(redPin, LOW);  
   digitalWrite(bluPin, LOW);  
    
  }  
   
 if((DHT.temperature < 20) && (DHT.temperature > 17.2))  
  {  
   digitalWrite(grnPin, HIGH);  // aqua  
   digitalWrite(bluPin, HIGH);  
   delay(100);  
     
   digitalWrite(redPin, LOW);  
     
   
  }  
   
  if(DHT.temperature <= 17)  
  {  
   digitalWrite(bluPin, HIGH);  // blue  
   delay(100);  
     
   digitalWrite(grnPin, LOW);  
   digitalWrite(redPin, LOW);  
    
  }  
    
  delay(1000);   // Sensor shouldn't be read too frequently so delay of 1s  
    
  }  

No comments:

Post a Comment