loading

From the minds of http://arduinotronics.blogspot.com

UPDATE: Added our SpeakEasy Text-To-Speech (TTS) Shield to make this a "Talking" Weather Station!

UPDATE: Added a 4 port relay board for device control

UPDATE: Added Dew Point and Heat Index calculations

SainSmart sent me a 1.8" TFT LCD (ST7735R), and I decided to upgrade my Weather Station / Thermostat. This time around, I used the slightly less accurate but much less expensive DHT-22 (AM2302) temperature / humidity sensor. I also received a BMP-085 barometric pressure sensor, but it appears dead. More on that in the near future. Tighten your seat belt, and here we go:

Parts:

Arduino Uno
1.8 TFT LCD
DHT-22
10k Ohm Resistor
Relay Board (optional)
Male / Female Jumpers (very helpful)


Step 1: Connections

Connect the DHT-22 sensor and the LCD display as per the schematic. There are also connections on the left side of the display for the SD card, and we will address that at a future edit for logging purposes. Just use the connections on the right side of the display for now.

DHT-22:

(S)ignal to Arduino Pin 2
+5 to Arduino +5vdc
G to Arduino Ground
10k Resistor between S and +5vdc

ST7735R LCD:

VCC to Arduino +5vdc
Gnd to Arduino Ground
SCL to Ardiuino Pin 13
SDA to Arduino Pin 11
CS to Arduino Pin 10
DC to Arduino Pin 9
Reset to Arduino Pin 8

Step 2: Code

The following code is still a work in progress, but is functional. Let us know if you find problems or have suggestions for improvements.

/*
Arduino TFT text example

This example demonstrates how to draw text on the
TFT with an Arduino.

This example code is in the public domain

Created 15 April 2013 by Scott Fitzgerald

http://arduino.cc/en/Tutorial/TFTDisplayText

Modded by Steve Spence of http://arduinotronics.blogspot.com


*/

#include "SPI.h"

#include "ST7735.h"


// Pins SCLK and MOSI are fixed in hardware, and pin 10 (or 53)
// must be an output
//#define sclk 13 // for MEGAs use pin 52
//#define mosi 11 // for MEGAs use pin 51
#define cs 10 // for MEGAs you probably want this to be pin 53
#define dc 9
#define rst 8 // you can also connect this to the Arduino reset

// pin definition for the Leonardo
// #define cs 7
// #define dc 0
// #define rst 1

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char tempPrintout[6];
char humPrintout[6];




// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Fahrenheit conversion added by Steve Spence, http://arduinotronics.blogspot.com

#include "DHT.h" //https://learn.adafruit.com/dht/downloads



#define DHTPIN 2 // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin + (middle) of the sensor to +5V
// Connect pin S (on the right) of the sensor to whatever your DHTPIN is
// Connect pin - (on the left) of the sensor to GROUND
// Connect 10k resistor between S and +

int cycleTime = 2000;

DHT dht(DHTPIN, DHTTYPE);

float h;
float t;

void setup() {

// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255,255,255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temp (F)",0,0);
// write the text to the top left corner of the screen
TFTscreen.text("Humidity (%)",0,60);
// ste the font size very large for the loop
TFTscreen.setTextSize(4);

dht.begin();

}

void loop() {

// Read the value of the temp/humidity sensor on D2

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
t = (t*1.8)+32; //C to F conversion

String tempVal = doubleToString(t, 2);
String humVal = doubleToString(h, 0);
// String sensorVal = String(1.234);

// convert the reading to a char array
tempVal.toCharArray(tempPrintout, 6);
humVal.toCharArray(humPrintout, 6);

// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);
// wait for a moment
delay(cycleTime);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);
}

//Rounds down (via intermediary integer conversion truncation)
String doubleToString(double input,int decimalPlaces){
if(decimalPlaces!=0){
String string = String((int)(input*pow(10,decimalPlaces)));
if(abs(input)<1){
if(input>0)
string = "0"+string;
else if(input<0)
string = string.substring(0,1)+"0"+string.substring(1);
}
return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
}
else {
return String((int)input);
}
}

Step 3: Now With Text-To-Speech

We added our prototype TTS Shield, and made some slight modifications to the code. Now the temperature and humidity is spoken out loud. This can get annoying, so you may want to add a button for speech on demand! We changed the DHT-22 pin to 6 as the original pin 2 interferes with the SpeakEasy Shield. The SpeakEasy Shield (produced for us by http://www.hacktronics.com) is based on the SpeakJet and TTS256 chips, with an onboard amplifier.



//Speak Easy Setup


#include <SoftwareSerial.h>

#define txPin 2
#define rxPin 5
SoftwareSerial sjSerial = SoftwareSerial(rxPin, txPin);

int bufferPin = 4; // monitor buffer half full, or busy (jmp2)


/*
Arduino TFT text example

This example demonstrates how to draw text on the
TFT with an Arduino. The Arduino reads the value
of an analog sensor attached to pin A0, and writes
the value to the LCD screen, updating every
quarter second.

This example code is in the public domain

Created 15 April 2013 by Scott Fitzgerald

http://arduino.cc/en/Tutorial/TFTDisplayText

*/

#include // Arduino LCD library
#include

// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8

// pin definition for the Leonardo
// #define cs 7
// #define dc 0
// #define rst 1

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char tempPrintout[6];
char humPrintout[6];




// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Fahrenheit conversion added by Steve Spence, http://arduinotronics.blogspot.com

#include


#define DHTPIN 6 // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin + (middle) of the sensor to +5V
// Connect pin S (on the right) of the sensor to whatever your DHTPIN is
// Connect pin - (on the left) of the sensor to GROUND
// Connect 10k resistor between S and +

int cycleTime = 2000;

DHT dht(DHTPIN, DHTTYPE);

float h;
float t;

void setup() {

// initialize the serial communications with the SpeakJet-TTS256
Serial.begin (9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(bufferPin, INPUT);
sjSerial.begin(9600);// set the data rate for the SoftwareSerial port

// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255,255,255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temp (F)",0,0);
// write the text to the top left corner of the screen
TFTscreen.text("Humidity (%)",0,60);
// ste the font size very large for the loop
TFTscreen.setTextSize(4);

dht.begin();

}

void loop() {

// Read the value of the temp/humidity sensor on D2

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
t = (t*1.8)+32; //C to F conversion

String tempVal = doubleToString(t, 0);
String humVal = doubleToString(h, 0);
// String sensorVal = String(1.234);

// convert the reading to a char array
tempVal.toCharArray(tempPrintout, 6);
humVal.toCharArray(humPrintout, 6);

// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);
// wait for a moment
delay(cycleTime);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);

sjSerial.print("temprature ");
delay(250);
sjSerial.print(tempPrintout);
delay(500);
sjSerial.print(" humidity ");
delay(250);
sjSerial.println(humPrintout);
}

//Rounds down (via intermediary integer conversion truncation)
String doubleToString(double input,int decimalPlaces){
if(decimalPlaces!=0){
String string = String((int)(input*pow(10,decimalPlaces)));
if(abs(input)<1){
if(input>0)
string = "0"+string;
else if(input<0)
string = string.substring(0,1)+"0"+string.substring(1);
}
return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
}
else {
return String((int)input);
}
}

Step 4: Adding a Relay Board

In this step, we add a 4 port relay board, that controls an air conditioner, heater, fan, and dehumidifier. If the temperature goes above 70 degrees F, then the fan and AC come on. If the temp drops below 65 and the humidity is above 50%, then the heat, fan, and dehumidifier come on. You can customize your switch points and logic in the code.

We are using a SainSmart 4 port relay board, and connecting the 4 input lines to analog ports repurposed as digital outputs. For more information, see http://arduinotronics.blogspot.com/2013/01/working-with-sainsmart-5v-relay-board.html

//SainSmart 4 port relay board setup
//We are out of digital ports, so repurposing
//analog inputs as digital outputs

// GND connected to Arduino GND
int relayPin1 = A0; // IN1 connected to digital pin A0
int relayPin2 = A1; // IN2 connected to digital pin A1
int relayPin3 = A2; // IN3 connected to digital pin A2
int relayPin4 = A3; // IN4 connected to digital pin A3
// VCC connected to Arduino +5v


//Speak Easy Setup



#include
#define txPin 2
#define rxPin 5
SoftwareSerial sjSerial = SoftwareSerial(rxPin, txPin);

int bufferPin = 4; // monitor buffer half full, or busy (jmp2)


/*
Arduino TFT text example

This example demonstrates how to draw text on the
TFT with an Arduino. The Arduino reads the value
of an analog sensor attached to pin A0, and writes
the value to the LCD screen, updating every
quarter second.

This example code is in the public domain

Created 15 April 2013 by Scott Fitzgerald

http://arduino.cc/en/Tutorial/TFTDisplayText

*/

#include // Arduino LCD library
#include

// pin definition for the Uno
// SCL Hardware 13
// SDA Hardware 11
#define cs 10
#define dc 9
#define rst 8


// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char tempPrintout[6];
char humPrintout[6];




// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Fahrenheit conversion added by Steve Spence, http://arduinotronics.blogspot.com

#include


#define DHTPIN 6 // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin + (middle) of the sensor to +5V
// Connect pin S (on the right) of the sensor to whatever your DHTPIN is
// Connect pin - (on the left) of the sensor to GROUND
// Connect 10k resistor between S and +

int cycleTime = 2000;

DHT dht(DHTPIN, DHTTYPE);

float h;
float t;

void setup() {

//Initialize SainSmart Relay Board
pinMode(relayPin1, OUTPUT); // sets the analog pin as digital output
pinMode(relayPin2, OUTPUT); // sets the analog pin as digital output
pinMode(relayPin3, OUTPUT); // sets the analog pin as digital output
pinMode(relayPin4, OUTPUT); // sets the sets the analog pin as digital output

digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin3, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin4, HIGH); // Prevents relays from starting up engaged

// initialize the serial communications with the SpeakJet-TTS256
Serial.begin (9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(bufferPin, INPUT);
sjSerial.begin(9600);// set the data rate for the SoftwareSerial port

// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255,255,255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temp (F)",0,0);
// write the text to the top left corner of the screen
TFTscreen.text("Humidity (%)",0,60);
// ste the font size very large for the loop
TFTscreen.setTextSize(4);

dht.begin();

}

void loop() {

// Read the value of the temp/humidity sensor on D2

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
t = (t*1.8)+32; //C to F conversion

String tempVal = doubleToString(t, 0);
String humVal = doubleToString(h, 0);
// String sensorVal = String(1.234);

// convert the reading to a char array
tempVal.toCharArray(tempPrintout, 6);
humVal.toCharArray(humPrintout, 6);

// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);
// wait for a moment
delay(cycleTime);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);

sjSerial.print("temprature ");
delay(250);
sjSerial.print(tempPrintout);
delay(500);
sjSerial.print(" humidity ");
delay(250);
sjSerial.println(humPrintout);

if(t>70){
digitalWrite(relayPin1, LOW); // turn on AC);
digitalWrite(relayPin2, HIGH); // turn off Heat);
digitalWrite(relayPin3, LOW); // turn on FAN);
digitalWrite(relayPin4, HIGH); // turn off Dehumidifier);
}

if(t<65 && h<50){
digitalWrite(relayPin1, HIGH); // turn off AC);
digitalWrite(relayPin2, LOW); // turn on Heat);
digitalWrite(relayPin3, LOW); // turn on FAN);
digitalWrite(relayPin4, HIGH); // turn off Dehumidifier);
}

if(t<65 && h>50){
digitalWrite(relayPin1, HIGH); // turn off AC);
digitalWrite(relayPin2, LOW); // turn on Heat);
digitalWrite(relayPin3, LOW); // turn on FAN);
digitalWrite(relayPin4, LOW); // turn on Dehumidifier);
}

if(t>65 && t< 70 && h>50){
digitalWrite(relayPin1, HIGH); // turn off AC);
digitalWrite(relayPin2, HIGH); // turn off Heat);
digitalWrite(relayPin3, LOW); // turn on FAN);
digitalWrite(relayPin4, LOW); // turn on Dehumidifier);
}

if(t>65 && t< 70 && h<50){
digitalWrite(relayPin1, HIGH); // turn off AC);
digitalWrite(relayPin2, HIGH); // turn off Heat);
digitalWrite(relayPin3, HIGH); // turn off FAN);
digitalWrite(relayPin4, HIGH); // turn off Dehumidifier);
}
}

//Rounds down (via intermediary integer conversion truncation)
String doubleToString(double input,int decimalPlaces){
if(decimalPlaces!=0){
String string = String((int)(input*pow(10,decimalPlaces)));
if(abs(input)<1){
if(input>0)
string = "0"+string;
else if(input<0)
string = string.substring(0,1)+"0"+string.substring(1);
}
return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
}
else {
return String((int)input);
}
}

Step 5: Future Upgrades

Future upgrades for this are using the included SD card socket on the display for logging, and web interface with a Ethernet shield. We will also add the ability to request the target temperature, using a potentiometer or up/down buttons.

See:

http://arduinotronics.blogspot.com/2012/03/arduino-thermostat.html

http://arduinotronics.blogspot.com/2013/01/working-with-sainsmart-5v-relay-board.html

http://arduinotronics.blogspot.com/2012/11/more-ethernet-read-analog-set-digital.html

http://arduinotronics.blogspot.com/2012/06/building-heating-air-conditioning.html

Add Dew Point and Heat Index calculations to your temperature and humidity project (works with any sensor) - http://arduinotronics.blogspot.com/2013/12/temp-humidity-w-dew-point-calcualtions.html


<p>This looks like a great item that will be used a lot Thanks</p>
<p>Hi, </p><p>I was having the same problem as madmoonfish was having, and I don't understand where to get the DHT library. If you could help me out, that would be great!</p>
Just updated the code page with the link to the DHT library, and fixed the 3 includes that did not paste properly.
can you please tell me what to include, sketch is saying error class tft has no member named background, stroke, text in various sections of t he code.<br>Ive installed the gfx amd st7735 libraries but im just not too sure how to make them work, the errors look like to not me, im missing a library or something! ? im not sure.<br> Please help!<br>Im going to use this for an automated greenhouse. :-)<br>my screen came in the mail today but all I get is a white backlight :-(
<p>&lt; and &gt; symbols get dropped when pasting in the code. Went back and fixed.</p><p>#include &lt;SPI.h&gt;</p><p>#include &lt;ST7735.h&gt;</p>
I forgot to ask, in the code for your screen I noticed your using BLACKTAB witch is the S version, but you talk about using the R version witch is the screen I ordered wich is GREENTAB I think there is also a BLUETAB version that they sell.<br>do I just replace BLACKTAB with GREENTAB in the code?<br>thank you for your help, its very much appreciated. :-)
<p>I went back and looked at my code, and don't see &quot;blacktab&quot;, &quot;greentab&quot; etc. what line of code are you referring to? The code should work on any ST7735</p>
im sorry that is from another code, ignore that. very sorry for the confusion. <br>ill try the updated code soon. if it still doesn't work ill reinstall the IDE
thank you very much for replying, ill try it in a few hours.<br>my back went out thismorning so cant sit, wish there was a sketch download link.<br>if its not too much to ask can you please email me the code?<br>thank you. once again
Yep, <br>That did the trick! <br>Glad to see that my display is working properly. <br>Many thanks for the write up!
Sorry for the confusion. I need a version control system :-)
<p>I'm trying to put this together and have a bit of a problem in the wiring. </p><p>In the code talking about the TFT the following pin assignments are mentioned:</p><p>CS to Arduino Pin 10<br><br>DC to Arduino Pin 9<br><br>Reset to Arduino Pin 8</p><p><br></p><p>and from this link talking about the relay <a href="http://arduinotronics.blogspot.com/2013/01/working-with-sainsmart-5v-relay-board.html" rel="nofollow">http://arduinotronics.blogspot.com/2013/01/working-with-sainsmart-5v-relay-board.html</a></p><p><br></p><p>the following code is given:</p><p>int relayPin2 = 8; // IN2 connected to digital pin 8</p><p>int relayPin3 = 9; // IN3 connected to digital pin 9</p><p>int relayPin4 = 10; // IN3 connected to digital pin 10</p><p>are these two sharing the same pins?</p>
<p>Change the relay pins to whatever pins you have available.</p>
<p>Okay, how would I hook the pressure sensor up to this project. Where would it go into the sketch?</p>
<p>Hello again,</p><p>Although my sensor is working and I am receiving accurate info, the screen is real dim, nothing like in your video. Is this something you would know how to fix?</p><p>Many thanks</p>
check your brightness and contrast resistors.
<p>Hi. I was wondering if you can help me. I am planning on using this (minus talking part) for a homemade incubator. Am trying to set-up the code, but I get an error. The part hi-lighted is &quot;DHT dht(DHTPIN, DHTTYPE);&quot; with the error &quot;'DHT' does not name the type.&quot; I would be grateful for your thought, as search this problem has come up with no easy fix.</p><p> Great and inspiring instructable!</p>
Sounds like you are missing the dht library.
<p>Updated the code with a link to the libray, and fixed the 3 includes that did not paste properly.</p>
<p>Aargh! I thought they were ok as they were showing up in my library. I even tried 3 different DHT libraries previously (which all showed up!) So what did was updated my arduino software to current version. Re-done the dht sensor files and now all works fab! Thanks very much! Just need to setup my relays and all will be up and running. Thanks again :D</p>
Ok, something seems to be wrong! <br>You are showing CS, DC an RST to be pins 10,9 and 8. <br>In your code CS=10, DC=11 and RST =13 <br>Also ,are the SCL clock and SDA data line coded from the libraries. <br>When I scope 13 it does not seem to be reset because it popping like a clock signal! <br>Very confused!!
Good catch. Originally, we did software SPI (any pins can be used), then switched to hardware SPI (for faster performance), and forgot to update parts of the instructable. All should be fixed now.
Do I need to call the different code examples any particular name....kinda new at this...thanks
any name you would like, within arduino naming conventions.
Ask previous question about naming requirements of programs as haven't done this much and now I'm confused with how do you load all 4 programs to get everything to work? Any help appreciated...thanks
to load a sketch on an arduino, you use the arduino IDE, open the sketch to upload or type the code, and click the upload button. you can only have one sketch loaded at a time. Learning to use the Arduino IDE is beyond the scope of this tutorial. See <a href="http://arduino.cc/en/Guide/HomePage" rel="nofollow">http://arduino.cc/en/Guide/HomePage</a>
Thanks sspence for the reply....I do understand that only one sketch can be loaded at a time but I'm trying to use all the features of this project with the relay and don't know how one accomplishes this.....any suggestions?
yes, you add the various features to one big sketch, like I did in the tutorial.
<p>I've looked at doing this but I'm just not a programmer so I don't know how to pull this off.....thanks for your replies</p>
tank you veryyyyyyyyyyyyyyy much

About This Instructable

48,180views

144favorites

License:

Bio: Professionally, I'm an IT Engineer (Executive Level) and Electronics Tech. I'm a Amateur Radio Operator (KK4HFJ). I lived off grid, with Solar (PV ... More »
More by sspence:DIY I2C LCD Display with Inputs Raspberry Pi & the Neo 6M GPS Earthquake / Vibration Sensor 
Add instructable to: