[Lab] LCD 20x4 colour, DHT22 Tempature sensor, Arduino Uno
Bruce Harding
bsharding at rogers.com
Fri Nov 14 20:49:49 EST 2014
Well I was able to sort this out on my own. It is alway more satisfying to
figure something out ones self.
I used the following code. One of the key differences b/t my implementation and
other on the net, was I'm using a 3 colour 20x4 screen so the pinning was just
a wee bit different. It also helps if the brightness is almost at full or the
character are hard to see. I'm sure there is someway to fix that in the code,
but that is for another day.
Also on the list of things to learn is, adding a Ethernet shield and
broadcasting to the web.
===================================================
#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)
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 );
unsigned int bat = 0; // ADC value
byte volt = 0; // battery %
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);
lcd.print("Battery");
for(byte i=0; i<=10; i++)
{
bat=analogRead(1); // battery sensor pin 17 (TQFP-32)
if (bat<=800)
{ bat = 800; }
if (bat>=900)
{ bat = 900; }
volt = bat-800;
lcd.setCursor(0,1);
lcd.print(volt);
lcd.print("%");
delay(200);
}
ADCSRA = 0; // Disable ADC
}
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(" %");
}
delay(2000);
if (millis()>=8000)
{ digitalWrite(13, LOW); } // backlight off
if (millis()>=16000)
{ digitalWrite(0, LOW); } // power off
}
On Friday, November 14, 2014 12:16:23 PM Bruce Harding wrote:
> Need some help.
>
> Lets assume that I've got all the pinning right. And further assume for the
> time being that the problem is with the code. Can anyone see why I'm not
> seeing a print out on my LCD for temperature information from my sensor
> (DHT22). I've just run a the DHT.test code and confirm it is working and
> pinned properly. I'm trying to get this working with my Arduino Uno.
>
>
>
> Project I'm Trying to Copy-ish
> http://mchp.blogspot.ca/2014/08/arduino-pocket-hygrometerthermometer.html
>
>
>
> adafruit instructions
> https://learn.adafruit.com/character-lcds/wiring-a-character-lcd
>
>
>
> LCD Specs
> http://www.adafruit.com/datasheets/WH2004A-CFH-JT%23.pdf
>
>
>
> 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)
> byte degreeChar[8] =
> { B01100,
> B10010,
> B10010,
> B01100,
> B00000,
> B00000,
> B00000,
> B00000, };
> DHT dht(DHTPIN, DHTTYPE);
> LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //we need 6 pins: RS, EN, D7, D6,
> D5, and D4 to talk to the LCD
> unsigned int bat = 0; // ADC value
> byte volt = 0; // battery %
>
> void setup() {
> lcd.begin(16, 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);
> lcd.print("Battery");
> for(byte i=0; i<=10; i++)
> {
> bat=analogRead(1); // battery sensor pin 17 (TQFP-32)
> if (bat<=800)
> { bat = 800; }
> if (bat>=900)
> { bat = 900; }
> volt = bat-800;
> lcd.setCursor(0,1);
> lcd.print(volt);
> lcd.print("%");
> delay(200);
> }
> ADCSRA = 0; // Disable ADC
> }
> 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,0);
> lcd.print("T");
> lcd.print(t);
> lcd.createChar(0, degreeChar);
> lcd.setCursor(6,0);
> lcd.write(byte(0));
> lcd.print("C");
> lcd.setCursor(0,1);
> lcd.print("H");
> lcd.print(h);
> lcd.print(" %");
> }
> delay(2000);
> if (millis()>=8000)
> { digitalWrite(13, LOW); } // backlight off
> if (millis()>=16000)
> { digitalWrite(0, LOW); } // power off
> }
--
bruce
More information about the Lab
mailing list