Arduino, DHT22, and a LCD: Colour to Temperature

In this project we do an adaptation of rotating colour sketch and instead we use range of temperatures and assign each range a colour. So my idea is to do 15 Celsius or less would be blue, 15 to 25 degrees would be assigned the colour green and above 25 Celsius would be red.
I’d like to thank the folks at the Arduino and adafruits forums for all there help giving these sketches up and running.

There is the code:

#include <LiquidCrystal.h> //lcd library
#include "DHT.h"  //sensor library
#define DHTPIN 2   //  pin 32 (TQFP-32)
#define DHTTYPE DHT22   // type of sensor DHT 22  (AM2302)
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
byte degreeChar[8] = 
{ B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000, };
DHT dht(DHTPIN, DHTTYPE);

// LCD
// LCD RS=7, EN=8, DS4=9, DS5=10, DS6=11, DS7=12
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12 );

// you can change the overall brightness by range 0 -> 255
int brightness = 255;



void setup() {
  lcd.begin(20, 4);
  analogReference(INTERNAL);
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH); // power on
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); // backlight on
  dht.begin(); //  start sensor 
    lcd.setCursor(0,0);


  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT);
 
  brightness = 100;  
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h)) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SENSOR");    
    lcd.setCursor(0,1);
    lcd.print("ERROR");
  } 
  else {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("TEMPERATURE ");
    lcd.print(t);
    lcd.createChar(0, degreeChar);
    lcd.setCursor(18,1);
    lcd.write(byte(0));
    lcd.print("C");
    lcd.setCursor(0,2);
    lcd.print("HUMIDITY    ");
    lcd.print(h);
    lcd.print(" %");
 }
if (t < 15)
{
    setBacklight(0, 0, 255);  // set to blue
}
else if (t < 25)
{
    setBacklight(0, 255, 0);  // set to green
}
else 
{
    setBacklight(255, 0, 0);  // set to red
}
{
  digitalWrite(13, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(13, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
}
  
void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);
 
  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);
 
  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  Serial.print("R = "); Serial.print(r, DEC);
  Serial.print(" G = "); Serial.print(g, DEC);
  Serial.print(" B = "); Serial.println(b, DEC);
  analogWrite(REDLITE, r);
  analogWrite(GREENLITE, g);
  analogWrite(BLUELITE, b);
}

Leave a Reply

Your email address will not be published. Required fields are marked *