ESP32 -- DHT22 (AM2302) Temperature Sensor -- OLED Display

by PugazhM in Circuits > Wireless

5167 Views, 4 Favorites, 0 Comments

ESP32 -- DHT22 (AM2302) Temperature Sensor -- OLED Display

ESP32_Circuit_V01.jpeg

This experimentation is about interfacing DHT22 temperature sensor and 128x32 OLED display with ESP32, and using Python programming, for reading temperature in Celsius and humidity, and prints on OLED display

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.

https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...

ESP32 -- DHT22 (AM2302) Temperature Sensor -- OLED Display You tube Video

Abstract

The ESP32 is constructed with Xtensa dual-core (or single-core) 32-bit RISC architecture, operating at 160MHz or 240MHz, with up to 520KiB internal SRAM, integrated with Wi-Fi (802.11 b/g/n) and Bluetooth (v4.2 BR/EDR and BLE (shares the radio with Wi-Fi). The ESP32 provides peripheral interfaces like ADC, DAC, touch sensors, SPI, I2C, I2S, UART, CAN, PWM etc. The ESP32 supports all IEEE 802.11 standard security feature (WFA, WPA/WPA2 and WAPI), and cryptographic hardware acceleration (AES, RSA, SHA-2, ECC, RNG etc). It supports wake up from GPIO / Sensor interrupts, timers and 5uA deep sleep current consumption.

The DHT22 (AM2302) is a high precision temperature sensor module, provide calibrated temperature and humidity which is connected to digital IO pin of ESP32. The DHT22 provides the temperature in Celsius format.

An OLED display works without a backlight because it emits visible light. In low ambient light conditions (such as a dark room), an OLED screen can achieve a higher contrast ratio than an LCD, regardless of whether the LCD. OLEDs represent the future of display technology.

This experimentation is about interfacing DHT22 temperature sensor and 128x32 OLED display with ESP32, and using Python programming, for reading temperature in Celsius and humidity, and prints on OLED display

Reference

"ESP32 – Getting Started MicroPython -- on Board Blink LED” Instruct-able / You-tube by PugazhM

https://www.instructables.com/ESP32-Getting-Start...

The “ssd1306.py” driver library. (https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py)

LCD Assistant tool. (http://en.radzio.dxp.pl/bitmap_converter/).

“ESP32-- 128x32 OLED – SSD1306 Display Interface” Instruct-able / You-tube by PugazhM https://www.instructables.com/ESP32-Getting-Start...

“Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor -- OLED Display” Instruct-able / You-tube by PugazhM

https://www.instructables.com/ESP32-Getting-Start...

Components

Components.jpg

ESP32 Development Board

Micro USB Cable

128x32 OLED Display Module

DHT22 (AM2302) = 1 No

10K Resistor = 1 No

Schematic

ESP32_Circuit_V01.jpeg

Organic Light Emitting Diodes (OLED) is a flat light emitting technology, made by placing a series of organic thin films between two conductors.

When electrical current is applied, then OLED emits a bright light.

OLEDs are available in many colors. (Red, Green, Blue, Yellow, Amber, White, etc.).

OLEDs are emissive displays that do not require a backlight, self-illumination and so, they are thinner and more efficient than LCD displays OLEDs represent the future of display technology.

OLED provide the best image quality ever and they can also be made transparent, flexible, foldable and even roll-able and stretchable in the future.

The 128x32 OLED uses SSD1306 device and supports I2C communication interface

OLED displays consumes low power and can functional at 3.3VDC.

The SSD1306 is a single chip common cathode type driver, and drives up to 128 segments of 64 commons.

The SSD1306 embeds with display RAM, oscillator and 256 steps of contrast / brightness control. It provides either I2C interface or SPI interface.

The OLED module is connected / communicates with ESP32 I2C0 bus.

MicroPython provides device driver library, that includes frame buffer, line-shape drawing functionalities, ASCII font decoding, text string handling etc.

DHT22 (AM2302) -- Temperature / Humidity Sensor

Signal.jpeg

DHT22 digital temperature / humidity sensor delivers temperatures between -40°C and +80°C and humidity between 0% to 100%.

The temperature accuracy is ±0.1°C (maximum).

The DHT22 data pin is connected with ESP32 GPIO23 pin, and pulled up to VCC, via 10K ohm resistor.

Every DHT22 sensor is temperature compensated and calibrated in an “accurate calibration chamber” and then calibration-coefficient is saved in internal OTP memory.

The sensor supports long transmission distance. DHT22 (AM2302) outputs calibrated digital data signal.

DHT22 sensor uses 1-wire proprietary protocol. The 1-wire protocol has start signal, response signal from sensor and then follows 40 bits of sensor information.

If power is applied, then the DHT22 sensor takes around 2 seconds for initializing the system, after power up sequence, it starts measuring the first temperature and humidity value.

Initially, the uC configures the DATA pin as input, which is connected to DHT22 sensor. The DATA pin is connected to 3V3 via 10K pull up resistor.

Usually, the uC pulls the data line as HIGH.

Whenever uC needs to collect data, then it issues “start signal” by pulling down the “1-wire bus” into “LOW” for 0.8mS to 29mS. Usually 1mS.

Then uC releases the “1-wire bus” to high.

After the start signal, the DHT22 sensor takes around 30uS to issue the response signal to uC, by pulling the “1-wire bus” to “LOW” for about 80uS, and then release the “1 wire bus” to high for 80uS.

Then the sensor issues 40 bits of data.

The sensor represents “LOGIC ONE”, by “LOW” the “1-wire bus” for 50uS and releases to “HIGH” for 70uS

The sensor represents “LOGIC ZERO”, by “LOW” the “1-wire bus” for 50uS and releases to “HIGH” for 26uS

After issuing 40 data bits, the sensor enters into sleep mode, until it gets the next “start pulse”.

The humidity resolution is 16 Bits. First bit is MSB and then follows the next bits.

The temperature resolution is16 Bits. MSB first out.

When the MSB(Bit15) is “1”, it indicates a negative temperature.

When the MSB (Bit15) is “0”, it indicates a positive temperature.

The other bits (Bit14 ~ bit 0) indicate the detected temperature value.

Parity bit = humidity high + humidity low + temperature high + temperature low

ESP32 reads the temperature and humidity at 2 second interval and displays on OLED.

DHT22 Sensor MicroPython Program

TaskEvent_V01.jpeg

Download MicroPython SSD1306 OLED driver, I2C and SPI interfaces library, “ssd1306.py” driver library from

https://github.com/micropython/micropython/blob/ma...

Open the ESP32 as drive, and then copy the library into root directory.

The timer_one is initialized and callbacks the “BlinkLED” functionality for toggling on board LED at 200mS duration. (frequency = 5)

Black and White icons (Temperature / Humidity) of 32x32 pixels are created by using MS Paint and converted into byte array by using LCD assistant tool.

By using MS paint, draw a bigger size icon and then resize into 32x32 pixels. Save the icon as BMP picture. Select option as “Monochrome Bitmap”.

"LCD Assistant” tool can be downloaded from “http://en.radzio.dxp.pl/bitmap_converter/”

Open the icon.bmp at “LCD Assistant” tool. Select byte orientation as “Horizontal” and size as 32x32 and Size endian as “little”, pixel/bytes 8. Save output as byte array.

Later, the icon byte array can be copied into main python program.

Each 32x32 icon contains / needs 1024 pixels or 1024 bits or 128 byte per icon.

OLED is initialized and welcome screen of “ESP32, OLED Display” is displayed in two rows, for about 5 seconds.

The program initializes the OLED and DHT22 devices.

The Program reads the DHT22 sensor at 5 Sec interval and updates the OLED screen.

Temperature / Humidity Icon is populated and 2 rows of temperature / humidity sensor reading are displays on OLED screen

'''
Demonstrates ESP32 DHT22 (AM2302) temperature / Humidity sensor measurement Displays / updates the sensor readings on OLED at 5 seconds interval * The ESP32 pin connections for OLED I2C # OLED Power Pins * OLED VCC pin to 3V3 * OLED GND pin to GND # OLED I2C Pins * OLED SCL pin to GPI22 * OLED SDA pin to GPI21 # DHT22 Sensor pins * DHT22 Pin 1 to 3V3 * DHT22 Pin 2 to GPI23 * DHT22 Pin 3 to NC * DHT22 Pin 4 to GND Name:- M.Pugazhendi Date:- 28thSep2021 Version:- V0.1 e-mail:- muthuswamy.pugazhendi@gmail.com ''' import machine from machine import Pin, I2C import dht import time from ssd1306 import SSD1306_I2C import framebuf import utime import dht
# OLED pixel definition (WxH)
WIDTH  = 128 
HEIGHT = 32
# I2C0 pin assignments
SCL = 22
SDA = 21
DHT_PIN = 23
# 32x32 apple icon pixel array
h = [
0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x00, 0x00, 0x07, 0xE0, 0x00, 0x18, 0x04, 0x20, 0x00, 0x18,
0x04, 0x38, 0x00, 0x3C, 0x04, 0x20, 0x00, 0x24, 0x04, 0x30, 0x00, 0x66, 0x04, 0x30, 0x00, 0x24,
0x04, 0x20, 0x00, 0x3C, 0x05, 0xB8, 0x00, 0x00, 0x05, 0xA0, 0x06, 0x00, 0x05, 0xA0, 0x0E, 0x00,
0x05, 0xB0, 0x0B, 0x00, 0x05, 0xA0, 0x19, 0x00, 0x05, 0xB8, 0x11, 0x80, 0x05, 0xA0, 0x11, 0x80,
0x05, 0xA0, 0x1B, 0x00, 0x0D, 0xA0, 0x0E, 0x00, 0x19, 0xB8, 0x00, 0x18, 0x31, 0x8C, 0x00, 0x18,
0x21, 0x84, 0x00, 0x24, 0x67, 0xC6, 0x00, 0x66, 0x47, 0xE2, 0x00, 0x42, 0x47, 0xE2, 0x00, 0x42,
0x47, 0xE2, 0x00, 0x42, 0x47, 0xC6, 0x00, 0x7E, 0x61, 0x84, 0x00, 0x18, 0x30, 0x0C, 0x00, 0x00,
0x18, 0x18, 0x00, 0x00, 0x0F, 0xF0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]
humid = bytearray(h)
toggle = 0
#Initialize the onboard LED as output
led = machine.Pin(2,machine.Pin.OUT)
# Toggle LED functionality
def BlinkLED(timer_one):
    global toggle
    if toggle == 1:
        led.value(0)
        toggle = 0
    else:
        led.value(1)
        toggle = 1
# Initialize I2C0, Scan and Debug print of SSD1306 I2C device address
i2c = I2C(0, scl=Pin(SCL), sda=Pin(SDA), freq=200000)
print("Device Address      : "+hex(i2c.scan()[0]).upper())
# Initialize DHT22
sensor = dht.DHT22(Pin(DHT_PIN))
# Initialize OLED
display = SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Initialize the onboard LED as output
led = machine.Pin(2,machine.Pin.OUT)
# Initialize timer_one. Used for toggling the on board LED
timer_one = machine.Timer(0)
# Timer one initialization for on board blinking LED at 200mS interval
timer_one.init(freq=5, mode=machine.Timer.PERIODIC, callback=BlinkLED)
while True:
    sensor.measure()
    T = sensor.temperature()
    H = sensor.humidity()
    display.fill(0)
    
    # Write the Temperature and Humidity ICON
    fb = framebuf.FrameBuffer(humid, 32, 32, framebuf.MONO_HLSB)
    display.blit(fb, 0, 0)
    
    #Write Humidity value. Convert the humidity into two decimal places.
    display.text(str('H: ' +"{:0.2f}".format(H)+ "  %",2),40,5)
    #display.text("H: " + str(H) + " %",40,5)
    
    #Write Temperature value. Convert the temperature into two decimal places.
    display.text(str('T: ' +"{:0.2f}".format(T)+ "  C",2),40,19)
    #display.text("T: " + str(T) + "  C",40,19)
    
    #Draw the degree symbol. Its 4x4 pixel squire.
    display.fill_rect(113, 18, 4,4, 1)
    
    #Show display
    display.show()
    
    # Wait for Five seconds. Then proceed to collect next sensor reading.
    time.sleep_ms(5000)

Conclusion

C1.jpg
Conclusion_OLED.jpg

The project is successfully completed with ESP32, DHT22 sensor and 128x32 OLED shield

OLEDs represent the future of display technology and can be used for many embedded projects as mixed image and alpha numerical display. It consumes less power.

The humidity and temperature are read at 5 seconds interval, and updated on OLED.

Results and Video Links

Please Visit You Tube for the Videos:,

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.

https://www.youtube.com/channel/UCY4mekPLfeFinbQH...

ESP32 -- DHT22 (AM2302) Temperature Sensor -- OLED Display You tube Video