ADJUSTABLE THERMOSTAT WITH RELAY
WHAT DO I GET WITH THIS TUTORIAL?
First of all, the aim of this tutorial is to combine more modules with Dasduino, and show it in a specific example. To combine means to make mutual conditions and correlations of certain readings and display them, i.e. control them. The modules we will use are in the table on the right, and most of them can be found in the CSP Hobby set.
Finally, we will get a device which will, depending on the temperature, turn the relay (heater) on, or off. The temperature at which this happens will be adjustable using keypad, and everything mentioned will be read on the LCD display.
List of components and individual tutorials:
Dasduino Core – driver installation
DHT11 – HUM
Matrix membrane keyboard – HUM
LCD 16×2 – HUM
Relay
MODULE CONNECTING
All modules have already been individually processed in HUM – How to use module tutorials. Therefore, we will not do the same again. In the previous chapter, you can find links for individual tutorials. Now connect the module, as shown in the picture below, and we will immediately begin with programming.
ALL MODULES IN ONE SKETCH
The plan is to copy a part of the code from the HUM tutorials needed for each module separately. After that, we will write the necessary conditions and fuctions for certain variables. So let’s go..
DHT11
NOTE: make sure you have previously installed the necessary libraries, as written in the HUM tutorials
#include "dht.h" // adding dht library// needs to be defined for readings off the DHT dht DHT; #define dht_dpin A0 //data pin DHT11 on the A0 Croduino pin int ocitanaTemp = 0; void setup(){ } void loop(){ // reading the DHT11 condition // this command saves the temperature reading to "DHT.temperature" DHT.read11(dht_dpin); // saving the temperature reading in the ocitanaTemp variable ocitanaTemp = DHT.temperature; delay(800); // the minimum clearance between readings from the DHT11 is 800ms}Matrix Membrane Keypad
NOTE: later on, we will only add new parts to the existing code, as indicated in the comments.
// PREDEFINING#include "Keypad.h" // adding library Keypad// needs to be defined for the Matrix keypad const byte red = 4; // keypad has 4 rows const byte stupac = 4; // and 4 columns char tipke[red][stupac] = { // define position of the keys on the keypad {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte red_pinovi[red] = {7,6,5,4}; // we connect the row pins to PIN9, PIN8, PIN7, PIN6, one after the other byte stupac_pinovi[stupac] = {3,2,1,0}; // and the column pins to PIN5, PIN4, PIN3, PIN2, one after the other Keypad tipkovnica = Keypad(makeKeymap(tipke), red_pinovi, stupac_pinovi, red, stupac); // we will listen to the pressed key using: "tipkovnica.getKey()"// VOID SETUP() // nothing here // VOID LOOP() // nothing here // we will listen to the pressed key using "tipkovnica.getKey()"LCD 16×2
WRITING CONDITIONS
For starters, let’s print the temperature reading from the DHT11 sensor. We will make a special function which will do it. The function “Ispis()” can be written down before or after “setup()” and “void()” functions.
THE WHOLE, FINAL CODE
The whole, final code is a bit messy and you can copy it here. It contains a bunch of comments so you can skip the passage above. If you have a problem understanding it, you can always take a peek there for further clarification.
The aim of this tutorial is to, in the example, show logic after which we encourage you to write the code yourselves. The same menu can be written in many ways and is a great practice. If you have any trouble, feel free to contact us with specific questions (please comment the code). Do not forget to install the necessary libraries.
#include "dht.h" // adding dht library#include "Keypad.h" // adding Keypad library#include "LiquidCrystal_I2C.h" // adding the necessary library for the LCD#include "Wire.h" // since we use I2C for the LCD#include "EEPROM.h"// needs to be defined for readings off the DHTa dht DHT; #define dht_dpin A0 //data pin DHT11 on the A0 Croduino pin int ocitanaTemp = 0;// needs to be defined for the Matrix keypad const byte red = 4; // keypad has 4 rows const byte stupac = 4; // and 4 columns char tipke[red][stupac] = { // define key positions on the keypad {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte red_pinovi[red] = {7,6,5,4}; // we connect the row pins to PIN9, PIN8, PIN7, PIN6, one after the other byte stupac_pinovi[stupac] = {3,2,1,0}; // we connect the column pins to PIN5, PIN4, PIN3, PIN2, one after the other Keypad tipkovnica = Keypad(makeKeymap(tipke), red_pinovi, stupac_pinovi, red, stupac); // we will listen to the pressed key using: "tipkovnica.getKey()"// needs to be defined for the LCDLiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);// needs to be defined for the relayint relej = 8; // relej spajamo na D8// additional menu settingsint podesenaTemp = 0; // the variable in which we will save the set temperature// reading the value saved to the EEPROM memory 10 address// to that address we will save every change made in this variableString unosString = String(EEPROM.read(10)); // string variable in which we "build" the keypad entryboolean boolUnosTemp = false; // the condition of entry and exit from the menu void setup(){ pinMode(relej, OUTPUT); // defining relay as an output component lcd.begin(16,2); // adjust the LCD to 16x2 lcd.backlight(); // turn the backlighting of the LCD on // write out a greeting message lcd.setCursor(0,0); // set the writing cursor to the first column and first row lcd.print("Termostat"); // make sure that the String does not exceed 16 characters lcd.setCursor(0,1); // set the cursor to the first column, second row lcd.print("e-radionica.com");} void loop(){ // reading the DHT11 condition // this command saves the temperature reading to "DHT.temperature" DHT.read11(dht_dpin); // we save the temperature reading to the variable ocitanaTemp ocitanaTemp = DHT.temperature; char pritisnuto = tipkovnica.getKey(); // the pressed key is saved to the variable pressed // because of the delay (800) which is necessary for reading off the DHT11 // it is possible that the key will have to be held pressed for 800ms // in order for it to be registered as this part of sketch// entering the menu for setting temperatureif( pritisnuto == '#' && boolUnosTemp == false) { // if the "#" key is pressed and we have not already entered the menu boolUnosTemp = true; // we have entered the Menu // print the text of the Menu lcd.clear(); lcd.setCursor(0,0); lcd.print("postavi temp:"); unosString = ""; // let's make a loop which will listen to the entry of temperature, up until we have pressed the key '#' again // after which we will exit the Menu while( boolUnosTemp == false) { // save the pressed key to "unos" char unos = tipkovnica.getKey(); // since we only want numbers in temperature, every other character must be ignored: // if one of the above conditions is satisfied, the entry is a "blank" char if( unos == 'A' || unos == 'B' || unos == 'C' || unos == 'D' || unos == '*' ) unos = '\0'; // yet, if the '#' key is pressed, we will exit the Menu, then print out and compare the entered temperature else if( unos == '#' ) { boolUnosTemp = false; // this will come out of the given while unos = '\0'; // we do not want anything to be added to our unosString } // we save every individual key to one string, which will later be used for comparison to the current temperature // and taking that into consideration, turn the relay on or off unosString += String(unos); // printing out what we are writing lcd.setCursor(0,1); lcd.print(unosString); // saving changes to EEPROM memory address 10 EEPROM.write(10,unosString.toInt()); }} // other condition, for keys A,B,C i D // if the key 'A' is pressed, set the temperature to 15 degrees else if( pritisnuto == 'A' ) unosString = "15"; // if the key 'B' is pressed, set the temperature to 20 degrees else if( pritisnuto == 'B' ) unosString = "20"; // if the key 'C' is pressed, set the temperature to 25 degrees else if( pritisnuto == 'C' ) unosString = "25"; // if the key 'D' is pressed, set the temperature to 30 degrees else if( pritisnuto == 'D' ) unosString = "30";// the condition under which the relay turns on or off// in case the set temperature is lower than the read temperature, turn the relay on if( unosString.toInt() > ocitanaTemp ) digitalWrite(relej, HIGH);// turn the relay off in every other case else digitalWrite(relej, LOW); delay(800); // the minimum clearance between readings off the DHT11 is 800ms}// a function which prints out the readingsvoid Ispis() { // DHT11 sensor temperature reading print lcd.clear(); // delete everything previously written on the LCD lcd.setCursor(0,0); // set the cursor to the first column, first row lcd.print("Ocitano:" + String(ocitanaTemp)); // printing out the temperature set in the Menu lcd.setCursor(0,1); // we set the cursor to the first column, second row lcd.print("Podeseno:" + unosString);}

