Arduino Temperature display

Written by

in

Part 1

This project is for a temperature logger that will record the temperature and humidity over an extended period of time.

Introduction


I want to monitor the temperature and humidity in my loft for a period of least one year. These are some of the initial features I need:

  • Temperature & Humidity sensor such as DHT11 or DHT22
  • Small display to show readings
  • Capture Highest reading, Lowest reading and Average reading
  • Display current readings by default

Nice to have features:

  • Web server so I can monitor the readings without going into the loft
  • Save readings to EEPROM so they are remembered during power loss.
  • Use real time clock or get time from internet so readings can be logged by day

I am learning to develop more with Arduino so I want to use either a R4 Nano ore ESP32 Nano, the latter having built in wifi.


Prototype

Using parts I already had, I created a prototype on breadboard.

I only had a DHT11 sensor so I tested with that. I will use the DHT22 in the final project as it is faster, more accurate and has an extended temperature range.

I need to add a button to scroll through different displays, Current values, Maximum, Minimum, Average and blank display. The default will be to display a blank screen after 30s of no activity.


Program

This code is not perfect and is just one way to implement my project. I use comments within the code to explain what the code does for learning and for me to remember why I did it this way.

Arduino Code
/*
*
*   Copyright (c) 2026, Perry Andrews https://www.pelnet.co.uk
*   All rights reserved.
*
*   Title: DHT11 Demo
*
*   Description: Program for testing a DHT11/22 Temperature & Humidity Sensor
*
*   Device:        Arduino Nano ESP32 (3V3) or R4
*   Clock:         
*   Filename:      dht11_demo.ino
*   Date:          23/06/2026
*   File Version:  1.0
*   C Version:     Arduino Version 2.3.10
*   Author:        Perry Andrews
*   Company:       https://www.pelnet.co.uk
*
*
*   Libraries:     Adafruit SSD1306, Adafruit GFX
*                  DHT Sensor Library: 
*                  https://github.com/adafruit/DHT-sensor-library
*                  Adafruit Unified Sensor Lib:  
*                  https://github.com/adafruit/Adafruit_Sensor
*
*/

/*
*
*    Revision:        Date:                 Comment:
*    1.0              23/06/2026            First revision tested.
*   
*/

/*
*
*    I/O Assignment:
*    A0  - Analog LDR
*    D2  - DHT11/22
*    D3  - LED (PWM)
*    A4/A5 - I2C Display
*    D6 - Buzzer
*
*/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DIYables_OLED_SSD1309.h>
#include "DHT.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
#define DHTPIN 2     // Digital pin connected to the DHT sensor
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
const int analogInPin = A0;
//const int analogOutPin = 3;
const int ledPin = LED_BUILTIN;  // the number of the LED pin
const int leftButton = D7;       // Left Menu Button
const int middleButton = D8;     // Middle / Enter Button
const int rightButton = D9;      // Right Menu Button
const int buzzer = D6;           // buzzer to arduino pin 6
int menuSelected = 0;            // Currently Selected Menu Item
int readDelay = 20;
float h;
float t;
float hmin = 100;
float tmin = 100;
float hmax;
float tmax;
int val;

// Create an SSD1309 or SSD1306 display instance on the default I2C bus
//DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(middleButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT); // Set buzzer - pin as an output
  Serial.begin(115200); //Set serial baud rate to 115200 bps
  // initialize the OLED object
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  //if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("Display allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  dht.begin();
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(32, 0);
  display.setTextSize(1);
  display.println("DHT11 Demo");
  display.setCursor(0, 16);
  display.setTextSize(2);
  display.println("(C)2026");
  display.println("PMRA");
  display.display();
  beep(1000);
  delay(2000);
 }

void beep(int l)
{
  //Sound Buzzer
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(l);
  noTone(buzzer);     // Stop sound...
  //delay(l);
}

void loop()
{
 
  if(readDelay > 19){
    val=analogRead(analogInPin);    //Read value from analog input

    // Reading temperature or humidity takes about 250 milliseconds!
    h = dht.readHumidity();
    // Read temperature as Celsius
    t = dht.readTemperature();
  
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }
    readDelay = 0;
    if (h > hmax){
      hmax = h;
    }
    if (t > tmax){
      tmax = t;
    }
    if (h < hmin){
      hmin = h;
    }
    if (t < tmin){
      tmin = t;
    }
  }
  
  
  // Check Button
  int button = digitalRead(middleButton);
  if (button == 0){
    beep(100);
    menuSelected = menuSelected + 1;
    if (menuSelected == 4){
      menuSelected = 0;
    }

  }
  //Uncomment to ouput values to serial port
  /*
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print("Light Value: ");
  Serial.println(val,DEC);
  */
  switch(menuSelected){
    case 0:
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(32, 0);
      display.setTextSize(1);
      display.println("DHT22 Live");
      display.setCursor(0, 16);
      display.setTextSize(2);
      display.print("T:");
      display.print(t);
      display.println("C");
      display.print("H:");
      display.print(h);
      display.println("%");
      display.print("L:");
      display.println(val);
      digitalWrite(ledPin, LOW);
      display.display();
      break;
    case 1:
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(32, 0);
      display.setTextSize(1);
      display.println("DHT22 Max");
      display.setCursor(0, 16);
      display.setTextSize(2);
      display.print("T:");
      display.print(tmax);
      display.println("C");
      display.print("H:");
      display.print(hmax);
      display.println("%");
      //display.print("L:");
      //display.println(val);
      digitalWrite(ledPin, LOW);
      display.display();
      break;
    case 2:
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(32, 0);
      display.setTextSize(1);
      display.println("DHT22 Min");
      display.setCursor(0, 16);
      display.setTextSize(2);
      display.print("T:");
      display.print(tmin);
      display.println("C");
      display.print("H:");
      display.print(hmin);
      display.println("%");
      //display.print("L:");
      //display.println(val);
      digitalWrite(ledPin, LOW);
      display.display();
      break;
    case 3:
      display.clearDisplay();
      display.display();
      digitalWrite(ledPin, HIGH);
      break;
  }
 //Serial.println(readDelay); 
 delay(100);
 readDelay ++;
}

The code above includes some extras. I included a light dependent resistor to show the ambient light level, a button to show live, max and min readings and a piezo buzzer to provide audio feedback when the button is pressed.


Conclusion

This is just the start.

Next to add the RTC using the I2C bus and also a SD card reader to store the temperature readings.

To be continued…

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *