Components and supplies
LCD 16x2 Backlit Display
Arduino Nano R3
Single Turn Potentiometer- 10k ohms
DHT11 Temperature Humidity Sensor Module
3.7V 1000mAh Lithium Rechargable Battery
AZDelivery I2C 0.96-inch OLED Display SSD1306 128x64 Pixels IIC 3.3V 5V White Character Display
Tactile Switch, Top Actuated
Arduino Pro Mini
TP4056 5V 1A TYPE C Micro USB Board Charging Module for 18650 Lithium Battery
Hook Up wire
Project description
Code
Temp/Humid monitor LCD version code
arduino
1/* Paul Brace April 2021 2 * Very low power consumption temperature and humidity monitor 3 * with minimum and maximum monitoring 4 * Board - Arduino Nano 3 5 * Display - LCD 16x2 6 * 7 * Aim was to reduce power consumption as much as possible so 8 * can be run on a rechargeable battery for long periods 9 * This is achieved by: 10 * 1. Turning the backlight off after a set period 11 * 2. Using the rocketscream low power library to put the processor 12 * to sleep waking every 24 seconds to check the temperature and 13 * humidity, which takes less than a second. 14 * The mode button is used to wake the processor for a set period , turn 15 * the backlight on and switch between the temperature and humidity displays. 16 * The processor remains awake for the period specified in keepAwakeFor. 17 * The backlight remains on for the period specified in timeLightTurnedOn 18 * (Note: this should be less than keepAwakeFor or the light will not be 19 * turned off until the next awake cycle). 20 * The reset button is used to reset the minimum and maximum values 21 * when the backlight is on. 22 * Power consumption with the suggested components: 23 * Asleep: 24 seconds 5.6mA 24 * Checking temp./humid. between sleeps <1 second 14mA 25 * So on average 6.0mA 26 * 27 * With backlight on 28.5mA 28 * 29 * Using a 1,000 mAh rechargeable battery the system should last for up 30 * to 7 days between charges – frequently turning on the backlight will obviously 31 * reduce this. 32 * 33 * Note on using the 3.7v rechargeable battery. 34 * The Nano specification is that it requires a 5v supply, however 35 * for this project it runs perfectly well on a 3.7v rechargeable battery 36 * with the supply connected to the 5v pin. 37 * It is also safe to recharge when in use as the voltage will only increase to 4.2v 38 */ 39 40// Include driver for the DHT 41// This is the DHT sensor library by Adafruit 42#include <DHT.h> 43// Include the library driver for display: 44#include <LiquidCrystal.h> 45// Include Low-Power library from rocketscream 46#include "LowPower.h" 47 48// Pin used to collect data from the DHT 49#define DATA_PIN 4 50 51// DHT Type. If you have a DHT22 comment the DHT11 and 52// uncomment DHT22 53#define DHTTYPE DHT11 54//#define DHTTYPE DHT22 55 56// Create the sensor object and assign pin 57DHT dht(DATA_PIN, DHTTYPE); 58 59// Create an lcd object and assign the pins 60// LiquidCrystal lcd( RS, EN, D4,D5, D6, D7) 61LiquidCrystal lcd(12, 13, 6, 7, 8, 9); 62 63#define MODE_BUTTON 2 // Wake processor/turn backlight on and switch display between temperature and humidity 64#define RESET_BUTTON 3 // If pressed when light on then resets min and max settings 65#define BACKLIGHT_PIN 5 // Connected to backlight on LCD 66 67float temp; //Current temperature 68float humid; //Current humidity 69float minTemp; //The minimum temperature recorded 70float maxTemp; //The maximum temperature recorded 71float minHumid; //The minimum humidity recorded 72float maxHumid; //The maximum humidity recorded 73 74enum mode { 75 dispTemp, // Temperature currently displayed 76 dispHumid // Humidity currently displayed 77}; 78 79mode currentMode; // Current display setting 80 81bool awake; // True if system woken by pressing Mode button 82bool justWoken; // True on activation of interrupt so system knows to wait for button to be released. 83bool lightOn; // True if backlight on 84unsigned long timeLightTurnedOn; // Time backlight was last turned on 85int lightTimeOut = 10; // Number of seconds light remains on (should be less than keepAwakeFor) 86unsigned long timeWoken; // Time last woken or a button press 87int keepAwakeFor = 15; // Number of seconds before allow to go back to sleep 88 89const int wakeUpPin = MODE_BUTTON; // Set the pin that will wake up from power saving mode 90 91// Arrows used to indicate maximum and minimum 92byte upArrow[8] = { 93 B00000, 94 B00100, 95 B01110, 96 B10101, 97 B00100, 98 B00100, 99 B00000, 100}; 101 102byte downArrow[8] = { 103 B00000, 104 B00100, 105 B00100, 106 B10101, 107 B01110, 108 B00100, 109 B00000, 110}; 111 112void setup() { 113 minTemp = 99; //Set to a figure that is going to be too high 114 maxTemp = -9; //Set to a figure that is going to be too low 115 minHumid = 99; //Set to a figure that is going to be too high 116 maxHumid = -9; //Set to a figure that is going to be too low 117 pinMode(MODE_BUTTON, INPUT_PULLUP); // Use INPUT_PULLUP - will go LOW when pressed 118 pinMode(RESET_BUTTON, INPUT_PULLUP); 119 pinMode(BACKLIGHT_PIN, OUTPUT); 120 // Start sensor 121 dht.begin(); 122 // Initalise diplay 123 lcd.begin(16, 2); 124 lcd.noAutoscroll(); 125 lcd.display(); 126 lcd.clear(); 127 // Create up and down arrow characters 128 lcd.createChar(0, upArrow); 129 lcd.createChar(1, downArrow); 130 131 TurnBacklightOn(); 132 currentMode = dispTemp; 133 awake = true; 134 timeWoken = millis(); // Set so initially remains awake for set period 135} 136 137void loop() { 138 // Check to see if has been awake for the keepAwakeFor period 139 // if so allow to go to sleep 140 if ((millis() - timeWoken) > (keepAwakeFor * 1000)) { 141 awake = false; 142 // Sleep for 24 seconds 143 for (int i = 0; i < 3; i++){ 144 // Allow wake up pin to trigger interrupt on low. 145 attachInterrupt(0, wakeUp, LOW); 146 // Enter power down state with ADC and BOD disabled. 147 // Wake up when wake up pin goes low or after 8 seconds. 148 // If wake up button pressed then as long as button held down for more 149 // than a few milliseconds it will fall through any remaining sleep loops 150 LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 151 // Disable external pin interrupt on wake up pin. 152 detachInterrupt(0); 153 } 154 } 155 156 // If woken by Mode button press then default to temperature display 157 if (justWoken) 158 currentMode = dispTemp; 159 160 // Repeat read 3 times, once every 1/10th second, to ensure get up to date readings 161 // as the system may go back to sleep on next loop 162 for (int i = 0; i < 3; i++) { 163 delay(100); 164 165 // Check if need to turn backlight off 166 if (lightOn && ((millis() - timeLightTurnedOn) > (lightTimeOut * 1000))) 167 TurnBacklightOff(); 168 // get readings, update minimum and maximum values and 169 // refresh display 170 humid = dht.readHumidity(); // read humidity 171 temp = dht.readTemperature(); // read temperature 172 // Set min and max values if required 173 if (temp < minTemp) 174 minTemp = temp; 175 if (temp > maxTemp) 176 maxTemp = temp; 177 if (humid < minHumid) 178 minHumid = humid; 179 if (humid > maxHumid) 180 maxHumid = humid; 181 if (currentMode == dispTemp) 182 ShowTemperature(); 183 else 184 ShowHumidity(); 185 // If awake due to Mode button press the check if any buttons have been pressed 186 if (awake) 187 CheckButtons(); 188 } 189} 190 191// To ensure numbers align add a space if required 192String formatNumber(float value){ 193 if (value < 10.0 && value > 0.0) 194 return " " + String(value); 195 else 196 return String(value); 197} 198 199void ShowTemperature() { 200 lcd.setCursor(0, 0); 201 lcd.print("Temp: " + String(temp) + "c "); 202 lcd.setCursor(0, 1); 203 lcd.write(byte(1)); 204 lcd.print(String(minTemp) + "c "); 205 lcd.setCursor(9, 1); 206 lcd.write(byte(0)); 207 lcd.print(String(maxTemp) + "c "); 208} 209 210void ShowHumidity() { 211 // Display current temp on line 0 212 lcd.setCursor(0, 0); 213 lcd.print("Humid: " + String(humid) + "% "); 214 lcd.setCursor(0, 1); 215 lcd.write(byte(1)); 216 lcd.print(String(minHumid) + "% "); 217 lcd.setCursor(9, 1); 218 lcd.write(byte(0)); 219 lcd.print(String(maxHumid) + "% "); 220} 221 222void CheckButtons() { 223 // If just woken up then pause for 1/2 second to allow button to be released 224 if (justWoken){ 225 delay(500); 226 justWoken = false; 227 } 228 // If the mode or reset button has been pressed the associated pin will go LOW 229 // Check if mode button pressed if so switch mode between displays 230 if (digitalRead(MODE_BUTTON) == LOW) { 231 timeWoken = millis(); // Reset so remains awake for the awake period after switching mode 232 TurnBacklightOn(); // In case off and so also remains on for set period 233 // Switch displays 234 if (currentMode == dispTemp) { 235 currentMode = dispHumid; 236 ShowHumidity(); 237 delay(500); // Give time to release button 238 } 239 else { 240 currentMode = dispTemp; 241 ShowTemperature(); 242 delay(500); // Give time to release button 243 } 244 245 } 246 247 // Check if reset button pressed 248 // Is light currently on? 249 // If so the reset min and max values 250 if (digitalRead(RESET_BUTTON) == LOW) { 251 if (lightOn) { 252 maxTemp = -9; 253 minTemp = 99; 254 maxHumid = -9; 255 minHumid = 99; 256 delay(350); // Give time to release button 257 } 258 } 259} 260 261void TurnBacklightOn() { 262 // Turn backlight on 263 lightOn = true; 264 timeLightTurnedOn = millis(); 265 digitalWrite(BACKLIGHT_PIN, HIGH); 266} 267 268void TurnBacklightOff() { 269 // Turn backlight off 270 lightOn = false; 271 digitalWrite(BACKLIGHT_PIN, LOW); 272} 273 274void wakeUp() 275{ 276 // Handler for the pin interrupt. 277 timeWoken = millis(); // Reset so remains awake for awake period 278 TurnBacklightOn(); 279 awake = true; 280 justWoken = true; 281}
Temo/Humid monitor OLED version code
arduino
1/* Paul Brace April 2021 2 * Very low power consumption temperature and humidity monitor 3 * with minimum and maximum monitoring 4 * Board - Arduino Pro Mini 5 * Display - OLED 128x64 IC2 6 * Aim was to reduce power consumption as much as possible so 7 * can be run on a rechargeable battery for long periods 8 * This is achieved by: 9 * 1. Using the rocketscream low power library to put the processor 10 * to sleep waking every 24 seconds to check the temperature and 11 * humidity, which takes less than a second. 12 * 2. Displaying a small sleep icon to reduce the number of lit pixels 13 * The sleep icon also moves every 8 seconds to avoid screen burn-in 14 * The mode button is used to wake the processor for a set period, display 15 * the readings and switch between the temperature and humidity displays. 16 * The processor remains awake for the period specified in keepAwakeFor. 17 * The reset button is used to reset the minimum and maximum values 18 * when the processor has been woken by the mode button being pressed. 19 * Power consumption with the suggested components: 20 * Asleep: 24 seconds 3.23mA 21 * Checking temp./humid. between sleeps <1 second 6.8mA 22 * So on average 3.4mA 23 * 24 * Using a 1,000 mAh rechargeable battery the system should last for up 25 * to 12 days between charges – frequently turning on the display will reduce this. 26 * 27 * Note on using the 3.7v rechargeable battery. 28 * The Nano specification is that it requires a 5v supply, however 29 * for this project it runs perfectly well on a 3.7v rechargeable battery 30 * with the supply connected to the 5v pin. 31 * It is also safe to recharge when in use as the voltage will only increase to 4.2v 32 */ 33 34// Include drivers 35#include <DHT.h> // This is the DHT sensor library by Adafruit 36#include "LowPower.h" // Low-Power library from rocketscream 37 // required for the OLED display 38#include <SPI.h> // Synchronous serial data protocol library 39#include <Wire.h> // IC2 communications library 40#include <Adafruit_GFX.h> // Adafruit Graphics Core Library 41#include <Adafruit_SSD1306.h> // SSD1306 library for Monochrome 128x64 and 128x32 OLEDs 42#include <Fonts/FreeMonoBold9pt7b.h> // Include fonts to be used 43#include <Fonts/FreeSans9pt7b.h> 44 45 46#define DATA_PIN 4 // Pin used to collect data from the DHT 47#define DHTTYPE DHT11 // DHT Type 48//#define DHTTYPE DHT22 // If you have a DHT22 comment the DHT11 and 49 // uncomment DHT22 50// Create the sensor object and assign pin 51DHT dht(DATA_PIN, DHTTYPE); 52 53 54#define SCREEN_WIDTH 128 // OLED display width, in pixels 55#define SCREEN_HEIGHT 64 // OLED display height, in pixels 56 57// The pins for I2C are defined by the Wire-library. 58// On an arduino UNO/Nano and Pro Mini: A4(SDA), A5(SCL) 59// On an arduino MEGA 2560: 20(SDA), 21(SCL) 60// On an arduino LEONARDO: 2(SDA), 3(SCL), ... 61#define OLED_RESET -1 // Reset pin # (or -1 if no reset on display) 62#define SCREEN_ADDRESS 0x3C // See datasheet for Address (Yours could be 0x3D) 63 64// Creat eobject for the SSD1306 display connected to I2C (SDA, SCL pins) 65Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 66 67#define MODE_BUTTON 2 // Activate display and switched between temperature and humidity 68#define RESET_BUTTON 3 // If pressed when awake resets min and max settings 69 70float temp; //Current temperature 71float humid; //Current humidity 72float minTemp; //The minimum temperature recorded 73float maxTemp; //The maximum temperature recorded 74float minHumid; //The minimum humidity recorded 75float maxHumid; //The maximum humidity recorded 76 77enum mode { 78 dispTemp, // Temperature currently displayed 79 dispHumid // Humidity currently displayed 80}; 81 82mode currentMode; // Current display setting 83 84bool awake; // True if system woken by pressing Mode button 85bool justWoken; // True on activation of interrupt so system knows to wait for button to be released. 86unsigned long timeWoken; // Time last woken or a button press 87int keepAwakeFor = 15; // Number of seconds before allow to go back to sleep 88 89const int wakeUpPin = MODE_BUTTON; // Set the pin that will wake up from power saving mode 90 91// Icon displayed when system is asleep 92static const unsigned char PROGMEM snooze[] = 93{ B00000000, B00001110, 94 B00000000, B00000100, 95 B00000000, B00001110, 96 B00000011, B11000000, 97 B00000011, B11000000, 98 B00000000, B10000000, 99 B00000001, B00000000, 100 B00000011, B11000000, 101 B00000011, B11000000, 102 B11111000, B00000000, 103 B11111000, B00000000, 104 B00010000, B00000000, 105 B00100000, B00000000, 106 B01000000, B00000000, 107 B11111000, B00000000, 108 B11111000, B00000000, 109}; 110 111// Arrows used to indicate maximum and minimum 112static const unsigned char PROGMEM upArrow[]{ 113 B00000000, 114 B00011000, 115 B00111100, 116 B01111110, 117 B11111111, 118 B00011000, 119 B00011000, 120 B00011000 121}; 122 123static const unsigned char PROGMEM downArrow[]{ 124 B00011000, 125 B00011000, 126 B00011000, 127 B11111111, 128 B01111110, 129 B00111100, 130 B00011000, 131 B00000000 132}; 133 134void setup() { 135 minTemp = 99; //Set to a figure that is going to be too high 136 maxTemp = -9; //Set to a figure that is going to be too low 137 minHumid = 99; //Set to a figure that is going to be too high 138 maxHumid = -9; //Set to a figure that is going to be too low 139 pinMode(MODE_BUTTON, INPUT_PULLUP); // Use INPUT_PULLUP - will go LOW when pressed 140 pinMode(RESET_BUTTON, INPUT_PULLUP); 141 // Start sensor 142 dht.begin(); 143 // Initialise display 144 display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); 145 display.setTextColor(WHITE); 146 currentMode = dispTemp; 147 awake = true; 148 timeWoken = millis(); // Set so initially remains awake for set period 149} 150 151void loop() { 152 // Check to see if has been awake for the keepAwakeFor period 153 // if so allow to go to sleep 154 if ((millis() - timeWoken) > (keepAwakeFor * 1000)) { 155 awake = false; 156 // Sleep for 24 seconds 157 for (int i = 0; i < 3; i++){ 158 display.clearDisplay(); 159 display.drawBitmap(random(10, 110), random(10, 48), snooze, 16, 16, WHITE); 160 display.display(); 161 // Allow wake up pin to trigger interrupt on low. 162 attachInterrupt(0, wakeUp, LOW); 163 // Enter power down state with ADC and BOD module disabled. 164 // Wake up when wake up pin is low or after 8 seconds. 165 // If wake up button pressed then as long as button held down for more 166 // than a few milliseconds it will fall through any remaining sleep loops 167 LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 168 // Disable external pin interrupt on wake up pin. 169 detachInterrupt(0); 170 } 171 } 172 173 // If woken by Mode button press then default to temperature display 174 if (justWoken) 175 currentMode = dispTemp; 176 177 // Repeat read and check buttons 3 times once every 1/10th second to ensure get up to date readings 178 // as the system may go back to sleep on next loop 179 for (int i = 0; i < 3; i++) { 180 delay(100); 181 // Get readings, update minimum and maximum values 182 // and display if system is awake as a result of Mode button press 183 humid = dht.readHumidity(); // read humidity 184 temp = dht.readTemperature(); // read temperature 185 if (temp < minTemp) 186 minTemp = temp; 187 if (temp > maxTemp) 188 maxTemp = temp; 189 if (humid < minHumid) 190 minHumid = humid; 191 if (humid > maxHumid) 192 maxHumid = humid; 193 // Update display if awake 194 if ((currentMode == dispTemp) && (awake)) 195 ShowTemperature(); 196 if ((currentMode == dispHumid) && (awake)) 197 ShowHumidity(); 198 199 // If awake due to Mode button press the check if any buttons have been pressed 200 if (awake) 201 CheckButtons(); 202 } 203} 204 205// To ensure numbers align add a space if required 206String FormatNumber(float value){ 207 if (value < 10.0 && value > 0.0) 208 return " " + String(value); 209 else 210 return String(value); 211} 212 213void ShowTemperature() { 214 display.clearDisplay(); 215 display.setTextSize(1); 216 display.setFont(&FreeSans9pt7b); // Font used for text 217 display.setCursor(0, 15); 218 display.print("Temp:"); 219 display.drawBitmap(20, 35-9, upArrow, 8, 8, WHITE); 220 display.drawBitmap(20, 55-9, downArrow, 8, 8, WHITE); 221 display.setFont(&FreeMonoBold9pt7b); // Font used for numbers 222 display.setCursor(60, 15); 223 display.print(FormatNumber(temp) + "c"); 224 display.setCursor(60, 35); 225 display.print(FormatNumber(maxTemp) + "c"); 226 display.setCursor(60, 55); 227 display.print(FormatNumber(minTemp) + "c"); 228 display.display(); 229} 230 231void ShowHumidity() { 232 display.clearDisplay(); 233 display.setTextSize(1); 234 display.setFont(&FreeSans9pt7b); // Font used for text 235 display.setCursor(0, 15); 236 display.print("Humid:"); 237 display.drawBitmap(20, 35-9, upArrow, 8, 8, WHITE); 238 display.drawBitmap(20, 55-9, downArrow, 8, 8, WHITE); 239 display.setFont(&FreeMonoBold9pt7b); // Font used for numbers 240 display.setCursor(60, 15); 241 display.print(FormatNumber(humid) + "%"); 242 display.setCursor(60, 35); 243 display.print(FormatNumber(maxHumid) + "%"); 244 display.setCursor(60, 55); 245 display.print(FormatNumber(minHumid) + "%"); 246 display.display(); 247} 248 249void CheckButtons() { 250 // If just woken up then pause for 1/2 second to allow button to be released 251 if (justWoken){ 252 delay(500); 253 justWoken = false; 254 } 255 // If the mode or reset button has been pressed pin will go LOW 256 // Check if mode button pressed if so switch mode between displays 257 if (digitalRead(MODE_BUTTON) == LOW) { 258 timeWoken = millis(); // Reset so remains awake for set period after switching mode 259 //awake = true; 260 if (currentMode == dispTemp) { 261 currentMode = dispHumid; 262 ShowHumidity(); 263 delay(500); // Give time to release button 264 } 265 else { 266 currentMode = dispTemp; 267 ShowTemperature(); 268 delay(500); // Give time to release button 269 } 270 } 271 272 // Check if reset button pressed 273 // Is the system awake? 274 // If so the reset min and max values 275 if (digitalRead(RESET_BUTTON) == LOW) { 276 if (awake) { 277 maxTemp = -9; 278 minTemp = 99; 279 maxHumid = -9; 280 minHumid = 99; 281 delay(500); // Give time to release button 282 } 283 } 284} 285 286void wakeUp() 287{ 288 // Handler for the pin interrupt. 289 timeWoken = millis(); // Reset so remains awake for awake period 290 awake = true; 291 justWoken = true; 292}
Temo/Humid monitor OLED version code
arduino
1/* Paul Brace April 2021 2 * Very low power consumption temperature and humidity monitor 3 * with minimum and maximum monitoring 4 * Board - Arduino Pro Mini 5 * Display - OLED 128x64 IC2 6 * Aim was to reduce power consumption as much as possible so 7 * can be run on a rechargeable battery for long periods 8 * This is achieved by: 9 * 1. Using the rocketscream low power library to put the processor 10 * to sleep waking every 24 seconds to check the temperature and 11 * humidity, which takes less than a second. 12 * 2. Displaying a small sleep icon to reduce the number of lit pixels 13 * The sleep icon also moves every 8 seconds to avoid screen burn-in 14 * The mode button is used to wake the processor for a set period, display 15 * the readings and switch between the temperature and humidity displays. 16 * The processor remains awake for the period specified in keepAwakeFor. 17 * The reset button is used to reset the minimum and maximum values 18 * when the processor has been woken by the mode button being pressed. 19 * Power consumption with the suggested components: 20 * Asleep: 24 seconds 3.23mA 21 * Checking temp./humid. between sleeps <1 second 6.8mA 22 * So on average 3.4mA 23 * 24 * Using a 1,000 mAh rechargeable battery the system should last for up 25 * to 12 days between charges – frequently turning on the display will reduce this. 26 * 27 * Note on using the 3.7v rechargeable battery. 28 * The Nano specification is that it requires a 5v supply, however 29 * for this project it runs perfectly well on a 3.7v rechargeable battery 30 * with the supply connected to the 5v pin. 31 * It is also safe to recharge when in use as the voltage will only increase to 4.2v 32 */ 33 34// Include drivers 35#include <DHT.h> // This is the DHT sensor library by Adafruit 36#include "LowPower.h" // Low-Power library from rocketscream 37 // required for the OLED display 38#include <SPI.h> // Synchronous serial data protocol library 39#include <Wire.h> // IC2 communications library 40#include <Adafruit_GFX.h> // Adafruit Graphics Core Library 41#include <Adafruit_SSD1306.h> // SSD1306 library for Monochrome 128x64 and 128x32 OLEDs 42#include <Fonts/FreeMonoBold9pt7b.h> // Include fonts to be used 43#include <Fonts/FreeSans9pt7b.h> 44 45 46#define DATA_PIN 4 // Pin used to collect data from the DHT 47#define DHTTYPE DHT11 // DHT Type 48//#define DHTTYPE DHT22 // If you have a DHT22 comment the DHT11 and 49 // uncomment DHT22 50// Create the sensor object and assign pin 51DHT dht(DATA_PIN, DHTTYPE); 52 53 54#define SCREEN_WIDTH 128 // OLED display width, in pixels 55#define SCREEN_HEIGHT 64 // OLED display height, in pixels 56 57// The pins for I2C are defined by the Wire-library. 58// On an arduino UNO/Nano and Pro Mini: A4(SDA), A5(SCL) 59// On an arduino MEGA 2560: 20(SDA), 21(SCL) 60// On an arduino LEONARDO: 2(SDA), 3(SCL), ... 61#define OLED_RESET -1 // Reset pin # (or -1 if no reset on display) 62#define SCREEN_ADDRESS 0x3C // See datasheet for Address (Yours could be 0x3D) 63 64// Creat eobject for the SSD1306 display connected to I2C (SDA, SCL pins) 65Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 66 67#define MODE_BUTTON 2 // Activate display and switched between temperature and humidity 68#define RESET_BUTTON 3 // If pressed when awake resets min and max settings 69 70float temp; //Current temperature 71float humid; //Current humidity 72float minTemp; //The minimum temperature recorded 73float maxTemp; //The maximum temperature recorded 74float minHumid; //The minimum humidity recorded 75float maxHumid; //The maximum humidity recorded 76 77enum mode { 78 dispTemp, // Temperature currently displayed 79 dispHumid // Humidity currently displayed 80}; 81 82mode currentMode; // Current display setting 83 84bool awake; // True if system woken by pressing Mode button 85bool justWoken; // True on activation of interrupt so system knows to wait for button to be released. 86unsigned long timeWoken; // Time last woken or a button press 87int keepAwakeFor = 15; // Number of seconds before allow to go back to sleep 88 89const int wakeUpPin = MODE_BUTTON; // Set the pin that will wake up from power saving mode 90 91// Icon displayed when system is asleep 92static const unsigned char PROGMEM snooze[] = 93{ B00000000, B00001110, 94 B00000000, B00000100, 95 B00000000, B00001110, 96 B00000011, B11000000, 97 B00000011, B11000000, 98 B00000000, B10000000, 99 B00000001, B00000000, 100 B00000011, B11000000, 101 B00000011, B11000000, 102 B11111000, B00000000, 103 B11111000, B00000000, 104 B00010000, B00000000, 105 B00100000, B00000000, 106 B01000000, B00000000, 107 B11111000, B00000000, 108 B11111000, B00000000, 109}; 110 111// Arrows used to indicate maximum and minimum 112static const unsigned char PROGMEM upArrow[]{ 113 B00000000, 114 B00011000, 115 B00111100, 116 B01111110, 117 B11111111, 118 B00011000, 119 B00011000, 120 B00011000 121}; 122 123static const unsigned char PROGMEM downArrow[]{ 124 B00011000, 125 B00011000, 126 B00011000, 127 B11111111, 128 B01111110, 129 B00111100, 130 B00011000, 131 B00000000 132}; 133 134void setup() { 135 minTemp = 99; //Set to a figure that is going to be too high 136 maxTemp = -9; //Set to a figure that is going to be too low 137 minHumid = 99; //Set to a figure that is going to be too high 138 maxHumid = -9; //Set to a figure that is going to be too low 139 pinMode(MODE_BUTTON, INPUT_PULLUP); // Use INPUT_PULLUP - will go LOW when pressed 140 pinMode(RESET_BUTTON, INPUT_PULLUP); 141 // Start sensor 142 dht.begin(); 143 // Initialise display 144 display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); 145 display.setTextColor(WHITE); 146 currentMode = dispTemp; 147 awake = true; 148 timeWoken = millis(); // Set so initially remains awake for set period 149} 150 151void loop() { 152 // Check to see if has been awake for the keepAwakeFor period 153 // if so allow to go to sleep 154 if ((millis() - timeWoken) > (keepAwakeFor * 1000)) { 155 awake = false; 156 // Sleep for 24 seconds 157 for (int i = 0; i < 3; i++){ 158 display.clearDisplay(); 159 display.drawBitmap(random(10, 110), random(10, 48), snooze, 16, 16, WHITE); 160 display.display(); 161 // Allow wake up pin to trigger interrupt on low. 162 attachInterrupt(0, wakeUp, LOW); 163 // Enter power down state with ADC and BOD module disabled. 164 // Wake up when wake up pin is low or after 8 seconds. 165 // If wake up button pressed then as long as button held down for more 166 // than a few milliseconds it will fall through any remaining sleep loops 167 LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 168 // Disable external pin interrupt on wake up pin. 169 detachInterrupt(0); 170 } 171 } 172 173 // If woken by Mode button press then default to temperature display 174 if (justWoken) 175 currentMode = dispTemp; 176 177 // Repeat read and check buttons 3 times once every 1/10th second to ensure get up to date readings 178 // as the system may go back to sleep on next loop 179 for (int i = 0; i < 3; i++) { 180 delay(100); 181 // Get readings, update minimum and maximum values 182 // and display if system is awake as a result of Mode button press 183 humid = dht.readHumidity(); // read humidity 184 temp = dht.readTemperature(); // read temperature 185 if (temp < minTemp) 186 minTemp = temp; 187 if (temp > maxTemp) 188 maxTemp = temp; 189 if (humid < minHumid) 190 minHumid = humid; 191 if (humid > maxHumid) 192 maxHumid = humid; 193 // Update display if awake 194 if ((currentMode == dispTemp) && (awake)) 195 ShowTemperature(); 196 if ((currentMode == dispHumid) && (awake)) 197 ShowHumidity(); 198 199 // If awake due to Mode button press the check if any buttons have been pressed 200 if (awake) 201 CheckButtons(); 202 } 203} 204 205// To ensure numbers align add a space if required 206String FormatNumber(float value){ 207 if (value < 10.0 && value > 0.0) 208 return " " + String(value); 209 else 210 return String(value); 211} 212 213void ShowTemperature() { 214 display.clearDisplay(); 215 display.setTextSize(1); 216 display.setFont(&FreeSans9pt7b); // Font used for text 217 display.setCursor(0, 15); 218 display.print("Temp:"); 219 display.drawBitmap(20, 35-9, upArrow, 8, 8, WHITE); 220 display.drawBitmap(20, 55-9, downArrow, 8, 8, WHITE); 221 display.setFont(&FreeMonoBold9pt7b); // Font used for numbers 222 display.setCursor(60, 15); 223 display.print(FormatNumber(temp) + "c"); 224 display.setCursor(60, 35); 225 display.print(FormatNumber(maxTemp) + "c"); 226 display.setCursor(60, 55); 227 display.print(FormatNumber(minTemp) + "c"); 228 display.display(); 229} 230 231void ShowHumidity() { 232 display.clearDisplay(); 233 display.setTextSize(1); 234 display.setFont(&FreeSans9pt7b); // Font used for text 235 display.setCursor(0, 15); 236 display.print("Humid:"); 237 display.drawBitmap(20, 35-9, upArrow, 8, 8, WHITE); 238 display.drawBitmap(20, 55-9, downArrow, 8, 8, WHITE); 239 display.setFont(&FreeMonoBold9pt7b); // Font used for numbers 240 display.setCursor(60, 15); 241 display.print(FormatNumber(humid) + "%"); 242 display.setCursor(60, 35); 243 display.print(FormatNumber(maxHumid) + "%"); 244 display.setCursor(60, 55); 245 display.print(FormatNumber(minHumid) + "%"); 246 display.display(); 247} 248 249void CheckButtons() { 250 // If just woken up then pause for 1/2 second to allow button to be released 251 if (justWoken){ 252 delay(500); 253 justWoken = false; 254 } 255 // If the mode or reset button has been pressed pin will go LOW 256 // Check if mode button pressed if so switch mode between displays 257 if (digitalRead(MODE_BUTTON) == LOW) { 258 timeWoken = millis(); // Reset so remains awake for set period after switching mode 259 //awake = true; 260 if (currentMode == dispTemp) { 261 currentMode = dispHumid; 262 ShowHumidity(); 263 delay(500); // Give time to release button 264 } 265 else { 266 currentMode = dispTemp; 267 ShowTemperature(); 268 delay(500); // Give time to release button 269 } 270 } 271 272 // Check if reset button pressed 273 // Is the system awake? 274 // If so the reset min and max values 275 if (digitalRead(RESET_BUTTON) == LOW) { 276 if (awake) { 277 maxTemp = -9; 278 minTemp = 99; 279 maxHumid = -9; 280 minHumid = 99; 281 delay(500); // Give time to release button 282 } 283 } 284} 285 286void wakeUp() 287{ 288 // Handler for the pin interrupt. 289 timeWoken = millis(); // Reset so remains awake for awake period 290 awake = true; 291 justWoken = true; 292}
Downloadable files
Temp/Humid monitor LCD Schematic
Temp/Humid monitor LCD Schematic
Temp/Humid monitor LCD Breadboard
Temp/Humid monitor LCD Breadboard
Temp/Humid monitor OLED Schematic
Temp/Humid monitor OLED Schematic
Temp/Humid monitor OLED Breadbaord
Temp/Humid monitor OLED Breadbaord
Temp/Humid monitor LCD Breadboard
Temp/Humid monitor LCD Breadboard
Temp/Humid monitor OLED Schematic
Temp/Humid monitor OLED Schematic
Temp/Humid monitor LCD Schematic
Temp/Humid monitor LCD Schematic
Temp/Humid monitor OLED Breadbaord
Temp/Humid monitor OLED Breadbaord
Comments
Only logged in users can leave comments
paulsb
0 Followers
•0 Projects
Table of contents
Intro
4
0