Arduino, DHT22, and a LCD: Rotate That Colour

Welcome to my latest Arduino project. This project is based on the DHT22 temperature and humidity sensor. I’m using a 20×4 RGB LCD screen to output the data to. I got the idea that since the LCD can do RGB why not make it rotate through these colours while dislaying the data from the sensor. All parts used here are or where available from . the full sketch is below and if you have any comments leave them in the comments section below.

// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
#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);


 
// initialize the library with the numbers of the interface pins
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("HUMIDTY     ");
    lcd.print(h);
    lcd.print(" %");
}  
  for (int i = 0; i < 255; i++) {
    setBacklight(i, 0, 255-i);
    delay(15);
  }
  for (int i = 0; i < 255; i++) {
    setBacklight(255-i, i, 0);
    delay(15);
  }
  for (int i = 0; i < 255; i++) {
    setBacklight(0, 255-i, i);
    delay(15);
  }
}
 
 
 
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 *