MJRoBot (Marcelo Rovai)
Published © LGPL

ESP32 and OLED Display: Internet Clock - DHT22

This is the 2nd tutorial on my ESP32 learning journey. Here, an OLED shows time and captured data by DHT sensor (temperature & humidity).

BeginnerFull instructions provided8 hours14,899
ESP32 and OLED Display: Internet Clock - DHT22

Things used in this project

Hardware components

ESP32S
Espressif ESP32S
×1
Pmod OLED
Digilent Pmod OLED
×1
DHT22 Humidity and Temperature digital sensor
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1

Story

Read more

Schematics

Electrical Diagram

Code

Code snippet #4

Plain text
void loop() 
{
  getDHT();
  displayData();
  delay(2000);
}

Code snippet #5

Plain text
/***************************************************
* Display Data
****************************************************/
void displayData() 
{
  display.clear();   // clear the display

  display.drawString(0, 0,  "temp: ");
  display.drawString(40, 0,  String(localTemp));
  display.drawString(90, 0,  "oC");
  display.drawString(0, 32, "hum:  ");
  display.drawString(40, 32,  String(localHum));
  display.drawString(90, 32,  "%");

  display.display();   // write the buffer to the display
  delay(10);
}

Code snippet #9

Plain text
/***************************************************
* Get display
****************************************************/
int getDisplay()
{
  int displayNum = 0;
  int analog_value = analogRead(ANALOG_PIN_0);
  if      (analog_value <= 999)                         displayNum = 0;
  else if (analog_value > 1000 && analog_value <= 1999) displayNum = 1;
  else if (analog_value > 2000 && analog_value <= 2999) displayNum = 2;
  else if (analog_value > 3000 && analog_value <= 3999) displayNum = 3;
  else if (analog_value > 4000 )                        displayNum = 4;
 
  return displayNum;
}

Code snippet #10

Plain text
/***************************************************
* Display Data
****************************************************/
void displayData(int displayNum) 
{
  String formattedTime = timeClient.getFormattedTime();
  display.clear();   // clear the display
  switch (displayNum) 
  {
    case 0:
      display.clear();
      break;
    case 1:
      display.setFont(ArialMT_Plain_24);
      display.drawString(20, 31,  String(formattedTime));
      break;
    case 2:
      display.setFont(ArialMT_Plain_24);
      display.drawString(0, 31,  "T:");
      display.drawString(30, 31,  String(localTemp));
      display.drawString(100, 31,  "oC");
      break;
    case 3:
      display.setFont(ArialMT_Plain_24);
      display.drawString(0, 31,  "H:");
      display.drawString(30, 31,  String(localHum));
      display.drawString(100, 31,  "%");
      break;
    case 4:
      display.setFont(Open_Sans_Condensed_Light_20);
      display.drawString(0, 0,  "t:");
      display.drawString(10, 0,  String(localTemp));
      display.drawString(47, 0,  "oC");
      display.drawString(75, 0, "h:");
      display.drawString(85, 0,  String(localHum));
      display.drawString(120, 0 ,  "%");
      display.setFont(ArialMT_Plain_24);
      display.drawString(20, 31,  String(formattedTime));
      break;
    default: 
      display.clear();
      break;
  }
  display.display();   // write the buffer to the display
  delay(10);
}

Code snippet #11

Plain text
void loop() 
{
  getDHT();
  timeClient.update();
  displayData(getDisplay());
  delay(2000);
}

Github

https://github.com/espressif/arduino-esp32

Github file

https://github.com/Mjrovai/ESP32/tree/master/Playing%20with%20ESP32%20and%20Arduino%20IDE/ESP32_Blink

Github

https://github.com/adafruit/DHT-sensor-library

Credits

MJRoBot (Marcelo Rovai)

MJRoBot (Marcelo Rovai)

66 projects • 949 followers
Professor, Engineer, MBA, Master in Data Science. Writes about Electronics with a focus on Physical Computing, IoT, ML, TinyML and Robotics.

Comments