Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor -- OLED Display

by PugazhM in Circuits > Raspberry Pi

19153 Views, 14 Favorites, 0 Comments

Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor -- OLED Display

Circuit_V01.jpeg

This instruct-able is about interfacing DHT22 temperature sensor and 128x32 OLED display with Raspberry Pi Pico, and using Python programming, for reading 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...

Abstract

The Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040 features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM and support for up to 16MB of off-chip Flash. It provides wide range of flexible I/O options includes I2C, SPI, and uniquely Programmable I/O (GPIO) pins.

The DHT22 (AM2302) is a high precision temperature sensor module, provide calibrated temperature and humidity which is connected to digital IO pin of RPi Pico. 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 Raspberry Pi Pico, and using Python programming, for reading Celsius and humidity, and prints on OLED display.

Reference

Raspberry Pi Pico, Getting Started on Board Blink LED” Instruct-able / You-tube by PugazhM

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/).

“Raspberry Pi Pico -- 128x32 OLED Display Interface (SSD1306)” Instruct-able / You-tube by PugazhM https://www.instructables.com/Raspberry-Pi-Pico-1...

The “DHT22.py” driver library

https://github.com/danjperron/PicoDHT22

Components

Component.jpg

Raspberry Pi Pico

Micro USB Cable

128x32 OLED Display Module

DHT22 (AM2302) = 1 No

10K Resistor = 1 No

Schematic

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 RPi Pico 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 RPi Pico GP2 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 is16 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

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

Python Program – DHT22 Sensor

TaskEvent_V01.jpeg

Download MicroPython SSD1306 OLED driver, I2C and SPI interfaces library. “ssd1306.py” driver library from “https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py”.

Open the RPi Pico 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 Temperature / Humidity icon (32x32 pixels) is 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.

Download DHT22 library, “DHT22.py” driver library from “https://github.com/danjperron/PicoDHT22/DHT22.py”.

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

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 RPI Pico DHT22 (AM2302) temperature / Humidity sensor measurement Displays / updates the sensor readings on OLED at 5 seconds interval # DHT22 library is available at # https://github.com/danjperron/PicoDHT22 OLED interface on Instruct-able and You-tube: https://www.instructables.com/Raspberry-Pi-Pico-128x32-OLED-Display-Interface-SS/ https://www.youtube.com/watch?v=GDt1EYGkpjs * The Raspberry Pi Pico 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 GPIO5 * OLED SDA pin to GPIO4 # DHT22 Sensor pins * DHT22 Pin 1 to 3V3 * DHT22 Pin 2 to GPIO2 * DHT22 Pin 3 to NC * DHT22 Pin 4 to GND Name:- M.Pugazhendi Date:- 05thAug2021 Version:- V0.1 e-mail:- muthuswamy.pugazhendi@gmail.com ''' from machine import Pin, I2C from DHT22 import DHT22 import time from ssd1306 import SSD1306_I2C import framebuf import utime
# OLED pixel definition (WxH)
WIDTH  = 128 
HEIGHT = 32
# I2C0 pin assignments
SCL = 5
SDA = 4
# 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)
#Initialize the onboard LED as output
led = machine.Pin(25,machine.Pin.OUT)
# Toggle LED functionality
def BlinkLED(timer_one):
    led.toggle()
# 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
dht22 = DHT22(Pin(2,Pin.IN,Pin.PULL_UP))
# Initialize OLED
display = SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Initialize the onboard LED as output
led = machine.Pin(25,machine.Pin.OUT)
# Initialize timer_one. Used for toggling the on board LED
timer_one = machine.Timer()
# Timer one initialization for on board blinking LED at 200mS interval
timer_one.init(freq=5, mode=machine.Timer.PERIODIC, callback=BlinkLED)
while True:
    T, H = dht22.read()
    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

Conclusion.jpg

The project is successfully completed with Raspberry Pi Pico, DHT22 (AM2302) and 128x32 OLED shield

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

Video Link

Conclusion_OLED.jpg

Result_Video.mp4

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

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

If you enjoyed this instruct-able, then make sure you subscribe

The DHT22 and 128x32 OLED video link