Raspberry Pi Pico – DHT22 Sensor – 3.5 Inch (320x480) HVGA LCD – Weather Monitoring
by PugazhM in Circuits > Raspberry Pi
2241 Views, 9 Favorites, 0 Comments
Raspberry Pi Pico – DHT22 Sensor – 3.5 Inch (320x480) HVGA LCD – Weather Monitoring
This experimentation is about interfacing DHT22 temperature sensor and 320x480 HVGA LCD with Raspberry Pi Pico. Using Python programming, for reading temperature in Celsius and humidity. The program converts the temperature into Fahrenheit, Kelvin and Rankine, prints on HVGA LCD display
Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.
https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...
The Weather Monitoring Video
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.
Nowadays beautiful HVGA LCD screens are getting cheaper and using it in an embedded design, makes it more user friendly. In this instruct-able, explains about connecting the 320x480, 3.5Inch HVGA LCD, with ILI9488 driver and SPI interfacing into Raspberry Pi Pico. The HVGA LCD can be connected to the Raspberry Pi Pico SPI bus. It needs minimum number of port pins (4).
This experimentation is about interfacing DHT22 temperature sensor and 320x480 HVGA LCD with Raspberry Pi Pico. Using Python programming, for reading temperature in Celsius and humidity. The program converts the temperature into Fahrenheit, Kelvin and Rankine, prints on HVGA LCD display.
Reference
“Raspberry Pi Pico, Getting Started on Board Blink LED” Instruct-able / You-tube by PugazhM
https://www.instructables.com/Raspberry-Pi-Pico-G...
The “DHT22.py” driver library
https://github.com/danjperron/PicoDHT22
“Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor -- OLED Display” Instruct-able / You-tube by PugazhM
https://github.com/danjperron/PicoDHT22
“RPi Pico – 3.5 Inch (320x480) HVGA TFT LCD (ILI9488) – Bitmap Image Photo Frame – Internal Flash” Instruct-able / You-tube by PugazhM
Components
Raspberry Pi Pico
Micro USB Cable
320x480, ILI9488 SPI HVGA LCD
DHT22 (AM2302) = 1 No
10K Resistor = 1 No
Schematic
The HVGA LCD (3.5 inch, 320x480 pixel, ILI9488 LCD controller), is used for this instruct-able.
The LCD is easily interfaced with RPi Pico SPI bus, and it needs minimum of four Digital IO lines.
The ILI9488 LCD Controller is a 16.7M single-chip SoC driver for a-Si TFT liquid crystal display panels with a resolution of 320(RGB) x 480 dots.
The ILI9488 is comprised of a 960-channel source driver, a 480-channel gate driver, 345,600 bytes of on chip GRAM for graphic data of 320 (RGB) (H) x 480 (V) x 18 dots. The LCD operates at 3.3 Volt Logic.
The ILI9488 supports 8-colors display and sleep mode power management functions, ideal for portable products where battery power conservation is desirable, such as digital cellular phones, smart phones, MP3 players, personal media players and similar devices with color graphics displays.
RPi Pico SPI port is connected to the LCD (GPIO6 - SCLK, and GPIO7 – MOSI).
RPi Pico GPIO pin GPIO0, GPIO1 and GPIO2 are connected to CS, RST and DC\RS pin of HVGA LCD.
DHT22(AM2302) Temperature / Humidity Sensor
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 5 second interval and displays on HVGA LCD.
The conversion formula for Celsius to other format are given below.
Fahrenheit:- T(°F) = T(°C) × 9/5 + 32
Kelvin:- T(K) = T(°C) + 273.15
Rankine:- T(°R) = (T(°C) + 273.15) × 9/5
Python Program – DHT22 and 320x480 HVGA LCD, Weather Monitoring
Open the RPi Pico as drive, and then copy the following files into root directory.
DHT22.py – DHT22 library
ILI9488_V01.py – 320x480 HVGA LCD library
Roboto24_Med.py – Roboto Medium 24 fonts
RobotoBlack32_180.py – Roboto Black 32 font file
Temperature_icon.py – Temperature and humidity icon file
By using MS paint, draw a bigger size icon and then resize into 128x128 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 128x128 and Size endian as “little”, pixel/bytes 8. Save output as byte array.
Later, the icon byte array can be copied into Temperature_icon.py.
Download and extract Peter Hinch "micropython-font-to-py-master.zip" from "https://github.com/peterhinch/micropython-font-to-py"
Execute at windows cmd shell "python font_to_py.py Roboto.ttf 24 Roboto24_Med.py", for encoding the ttf font file into one-bit mono font file for micro python
The timer_one is initialized and callbacks the “BlinkLED” functionality for toggling on board LED at 500mS duration. (frequency = 1)
The HVGA LCD class provides basic firmware functionalities like Init, ResetDevice, WriteDevice, WriteDataToDevice, WriteBlock and FillRectangle, WriteString etc.
The python program, initializes ILI9488 HVGA LCD with SPI interface, and the DHT22 sensor.
The python program reads the temperature and humidity values at 5 seconds interval.
The temperature is in Celsius format, which is converted into Fahrenheit, Kelvin and Rankine format by the software. Converted formats of temperature and humidity are displayed on the HVGA LCD, at 5 Sec interval.
'''<br>Demonstrates the use of 320x480 TFT and DHT22 sensor. * The Raspberry Pi Pico pin connections for TFT is given below: # TFT Power Pins * TFT VCC pin to 3V3 * TFT GND pin to GND # TFT SPI Pins * TFT SCLK pin to GPIO6 * TFT MOSI pin to GPIO7 * TFT CS pin to GPIO0 # TFT Control Pins * TFT RST pin to GPI01 * TFT RS\DC pin to GPIO2
# DHT22 Sensor pins * DHT22 Pin 1 to 3V3 * DHT22 Pin 2 to GPIO13 * DHT22 Pin 3 to NC * DHT22 Pin 4 to GND Name:- M.Pugazhendi Date:- 30thAug2021 Version:- V0.1 e-mail:- muthuswamy.pugazhendi@gmail.com ''' from ILI9488_V01 import ILI9488 from machine import Pin, SPI from micropython import const import Roboto24_Med, RobotoBlack32_180 import time,utime,framebuf from DHT22 import DHT22 from Temperature_icon import *
SCREEN_WIDTH = const(480) SCREEN_HEIGHT = const(320) SCREEN_ROT = const(1)
TFT_CLK_PIN = const(6) TFT_MOSI_PIN = const(7) TFT_MISO_PIN = const(4)
TFT_CS_PIN = const(0) TFT_RST_PIN = const(1) TFT_DC_PIN = const(2)
COLOR_WHITE = const(0xFFFF) COLOR_BLACK = const(0x0000) COLOR_DARK_GRAY = const(0xC618) COLOR_LIGHT_GRAY = const(0xEF7D) COLOR_RED = const(0xF100) COLOR_ORANGE = const(0xFCC0) COLOR_YELLOW = const(0xFF47) #FEB3B COLOR_GREEN = const(0x0770) COLOR_BLUE = const(0x001F)
PRIMARY_DARK_C = const(0x0017) PRIMARY_NORMAL_C = const (0x601D) PRIMARY_LITE_C = const(0x9A5F)
SECONDARY_DARK_C = const(0x8C71) SECONDARY_LITE_C = const(0xEF7D) SECONDARY_NORMAL_C = const(0xBDD7)
#Initialize the onboard LED as output led = machine.Pin(25,machine.Pin.OUT)
#Initialize SPI spi = SPI(0,baudrate=40000000,miso=Pin(TFT_MISO_PIN),mosi=Pin(TFT_MOSI_PIN),sck=Pin(TFT_CLK_PIN))
# DHT22 libray is available at # https://github.com/danjperron/PicoDHT22 # Initialize DHT22 dht22 = DHT22(Pin(13,Pin.IN,Pin.PULL_UP))
#Initialize TFT Display display = ILI9488(spi,cs=Pin(TFT_CS_PIN),dc=Pin(TFT_DC_PIN),rst=Pin(TFT_RST_PIN), w=SCREEN_WIDTH, h=SCREEN_HEIGHT,r=SCREEN_ROT)
display.FillRectangle(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,PRIMARY_DARK_C) #Clear Screen
# Toggle LED funtionality def BlinkLED(timer_one): led.toggle() # Read the sensor and calculate / screen the temperature -- humidity values def ReadDHT22Sensor(timer_two): T, H = dht22.read() # Read Sensor if T is None: print(" sensor error") else: F = ( T * 1.8 ) + 32 # Fahrenheit # T(°F) = T(°C) × 9/5 + 32 K = T + 273.15 # Kelvin # T(K) = T(°C) + 273.15 R = (T + 273.15) * 1.8 # Rankine # T(°R) = (T(°C) + 273.15) × 9/5
display.SetFont(RobotoBlack32_180) display.SetFgBgColor(COLOR_BLACK, SECONDARY_NORMAL_C) display.FillRectangle(cx+30,cy+40,cw-32,35,SECONDARY_NORMAL_C) display.WriteString(cx+30,cy+40,"{:0.2f}".format(T)+ "°C") display.FillRectangle(fx+30,fy+40,fw-32,35,SECONDARY_NORMAL_C) display.WriteString(fx+30,fy+40,"{:0.2f}".format(F)+ "°F") display.FillRectangle(kx+10,ky+40,kw-12,35,SECONDARY_NORMAL_C) display.WriteString(kx+10,ky+40,"{:0.2f}".format(K)+ "°K") display.FillRectangle(rx+10,ry+40,rw-12,35,SECONDARY_NORMAL_C) display.WriteString(rx+10,ry+40,"{:0.2f}".format(R)+ "°R") display.FillRectangle(hx+110,hy+40,hw-112,35,SECONDARY_NORMAL_C) display.WriteString(hx+110,hy+40,"{:0.2f}".format(H)+ " %") x1 = 5; y1 = 5; w1 = 470; h1 = 50 display.FillRectangle(x1,y1,w1,h1,SECONDARY_DARK_C)
# Draw header and set Roboto32 font for headings display.SetFgBgColor(COLOR_WHITE, PRIMARY_DARK_C) display.SetFont(RobotoBlack32_180) x11 = 0; y11 = 0; w11 = 480; h11 = 50 display.FillRectangle(x11,y11,w11,h11,PRIMARY_DARK_C) display.FillRectangle(x11,y11+h11,w11,320-h11,SECONDARY_DARK_C) display.FillRectangle(x11,y11+315,w11,5,PRIMARY_DARK_C) display.WriteString(x11+150,y11+10,"DH22 SENSOR")
# Draw icon window x12 = 4; y12 = 55; w12 = 150 h12 = 320 - y12 - 10 display.FillRectangle(x12,y12,w12,h12,PRIMARY_NORMAL_C)
# Draw Celsius window and set Roboto24 font for headings cx = x12 + w12 + 3; cy = 55; cw = 158; ch = 82 display.FillRectangle(cx,cy,cw,ch-2,SECONDARY_NORMAL_C) display.FillRectangle(cx,cy,cw,30,PRIMARY_NORMAL_C) display.SetFgBgColor(COLOR_WHITE, PRIMARY_NORMAL_C) display.SetFont(Roboto24_Med) display.WriteString(cx+30,cy+3,"CELSIUS")
# Draw Fahernheit window fx = cx+cw+3; fy = 55; fw = 158; fh = 82 display.FillRectangle(fx,fy,fw,fh-2,SECONDARY_NORMAL_C) display.FillRectangle(fx,fy,fw,30,PRIMARY_NORMAL_C) display.WriteString(fx+10,fy+3,"FAHRENHEIT")
# Draw Kelvin window kx = x12 + w12 + 3; ky = cy + ch + 2; kw = 158; kh = 80 display.FillRectangle(kx,ky,kw,kh-2,SECONDARY_NORMAL_C) display.FillRectangle(kx,ky,kw,30,PRIMARY_NORMAL_C) display.WriteString(kx+30,ky+3,"KELVIN")
# Draw Rankine window rx = kx + kw + 3; ry = ky; rw = 158; rh = 80 display.FillRectangle(rx,ry,rw,rh-2,SECONDARY_NORMAL_C) display.FillRectangle(rx,ry,rw,30,PRIMARY_NORMAL_C) display.WriteString(rx+30,ry+3,"RANKINE")
# Draw Humidity window hx = kx; hy = ky + kh + 2; hw = 320; hh = 90 display.FillRectangle(hx,hy,hw,hh,SECONDARY_NORMAL_C) display.FillRectangle(hx,hy,hw,30,PRIMARY_NORMAL_C) display.WriteString(hx+110,hy+3,"HUMIDITY")
# Draw temperature / Humidity icon k = bytearray(temperature) fb = framebuf.FrameBuffer(k, 128, 128, framebuf.MONO_HLSB) display.DrawBlit(fb,x12+10,y12+65,128,128)
#Initialize timer_one. Used for toggling the on board LED timer_one = machine.Timer()
#Timer one initialization for on board blinking LED at 500mS interval timer_one.init(freq=2, mode=machine.Timer.PERIODIC, callback=BlinkLED)
# Initialize timer_two. Used for reading DHT22. timer_two = machine.Timer()
# Timer two initialization for reading sensor at 5 Seconds interval timer_two.init(freq=0.2, mode=machine.Timer.PERIODIC, callback=ReadDHT22Sensor) ReadDHT22Sensor(timer_two)
Conclusion
The project is successfully completed with Raspberry Pi Pico and 320x480 and DHT22 sensor
HVGA LCD shield is in expensive, and can be used for many embedded projects as mixed image and alpha numerical display. It consumes less power. The converted temperature is displayed at HVGA LCD screen as given below.
Video Link
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 Weather Monitoring video link