Raspberry Pi Pico -- Ultrasonic Distance Sensor (HC-SR04), HVGA 320x480 LCD-- Object Monitoring

by PugazhM in Circuits > Raspberry Pi

206 Views, 0 Favorites, 0 Comments

Raspberry Pi Pico -- Ultrasonic Distance Sensor (HC-SR04), HVGA 320x480 LCD-- Object Monitoring

Circuit_V01.jpeg

This experimentation is about interfacing Ultrasonic sensor and 320x480 HVGA LCD with Raspberry Pi Pico. Python programming, is used for triggering the sensor and reading the object distance. The program finds the object distance in cm and inches and prints on HVGA LCD display. If the object is too nearer (less than 50 cm), then the program screens a warning message on the display.

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

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

The Ultrasonic Object 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.

Ultrasonic sensor HC-SR04 provides 2cm to 40 cm non-contact distance measurements, between the object and sensor. The module includes ultrasonic transmitter, receiver and control circuit. Ultrasonic sensors are mainly used for Proximity detection, Avoiding Obstacles, Range finder and measurement of distance.

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 Ultrasonic sensor and 320x480 HVGA LCD with Raspberry Pi Pico. Python programming, is used for triggering the sensor and reading the object distance. The program finds the object distance in cm and inches and prints on HVGA LCD display. If the object is too nearer (less than 50 cm), then the program screens a warning message on the 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...

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

https://www.instructables.com/Raspberry-Pi-Pico-D...

“RPi Pico – 3.5 Inch (320x480) HVGA HVGA LCD (ILI9488) – Bitmap Image Photo Frame – Internal Flash” Instruct-able / You-tube by PugazhM

https://www.instructables.com/Raspberry-Pi-Pico-D...

Components

Component.jpg

Raspberry Pi Pico

Micro USB Cable

320x480, ILI9488 SPI HVGA LCD

Ultrasonic Sensor HR-SC04

1K2 and 2K Resistors = 1 No each

Schematic

Circuit_V01.jpeg

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. T

he 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.

Ultrasonic Sensor HC-SR04

UltrasonicSensor.png

The HC-SR04 is powered by +5VDC. The “Echo” pin produces the +5VDC signal output.

RPi Pico uses 3V3 GPIO. Input pins are 3V3 voltage tolerance.

Since “Echo” pin is connected to the RPi Pico as input, it should be connected via level translator (+5VDC to +3.3VDC) or potential divider network.

In this experiment uses 1K2 and 2K resistors as potential divider.

Vout=5VDC×2000/(2000+1200)= 3.125 VDC

The HC-SR04 ultrasonic sensor consists of two ultrasonic transducers.

One transducer act as transmitter and generates the 40KHz ultrasonic sound pulse.

The RPi Pico applies a 10uS trigger pulse at “Trig” pin, for generating the ultrasonic sound pulse.

The sensor generates sonic burst of 8 pulse pattern @40KHz, makes the “ultrasonic signature” from HC-SR04, allowing the receiver to differentiate the transmitted pattern from the ambient ultrasonic noise.

Immediately after issuing 8 pulses, the sensor “Echo” pin goes HIGH to start collecting the beginning of the echo-back signal.

If the ultrasonic sound pulse is intercepted by an object, then the pulses will echo back to the sensor. Both object and sensor should be aligned in line of sight, for accurate distance measurement.

The second transducer act as receiver and receives the echoed pulse.

If it receives them, then the sensor “Echo” pin goes to LOW, to end the measurement. The RPi Pico, which is connected on “Echo” pin, captures the output pulse width, in micro seconds

The RPi Pico uses the pulse width time, for calculating the distance between the object and sensor.

Object Distance Calculation

Signal.jpeg

Ultrasonic sound travels at the speed of 331.23 meters / second at air, at 0 °C (32 °F)

The travelling speed of sound, strongly depends on the medium as well as temperature.

If increase in room temperature, then sound wave travelling speed will increase or vice versa.

At room temperature, say 30.0 °C, the sound wave travels at speed of 348.95 meters / sec.

Reference https://www.weather.gov/epz/wxcalc_speedofsound

The accurate distance measurement, strongly depends on medium and room temperature.

The sensor transmits the sound wave to one direction and intercept by the object. The sound wave, then echo back to sensor. Hence ultrasonic sound travels two times, before reaching the sensor, and it should be adopted into distance calculation.

The distance between object and sensor is calculated by substituting the measured echo time width into following equation. For example, if the measured width is 5mS, then

Object Distance = (0.034895 ×5000) / 2 = 87.23 cm

The RPi Pico measures the time in uS and object distance is calculated in cm.

Python Program – Ultrasonic Sensor Object Distance Monitoring

TaskEvent_V01.jpeg

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

ILI9488_V01.py – 320x480 HVGA LCD library

Roboto24_Med.py – Roboto Medium 24 fonts

RobotoBlack32_180.py – Roboto Black 32 font file

Ultrasonic_iconpy – Ultrasonic sensor icon file

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 Ultrasonic Sensor GPIO pins.

The python program reads and calculates the distance between object and sensor at 5 seconds interval. The calculated distance is displayed on the HVGA LCD, at 5 Sec interval.

If the distance between object and sensor is less than 5cm, then “TOO CLOSE” warning is displayed on the screen.

If the distance between object and sensor is greater than 5cm, and less than 50cm, then “NEAR” warning is displayed on the screen.

If the distance between object and sensor is greater than 50cm, and less than 400cm, then “AWAY” warning is displayed on the screen.

If the distance between object and sensor is greater than 400cm, then “NONE” and “NO OBJECT” warning is displayed on the screen.

'''<br>Demonstrates the use of Ultrasonic sensor HR-SC04 and 320x480 TFT.
 
* 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
# Ultrasonic Sensor HC-SR04 pins
* VCC Pin 1 to VBUS
* Trig Pin 2 to GPIO12
* Echo Pin 3 to GPIO13
* GND Pin 4 to GND
 
Name:- M.Pugazhendi
Date:-  01stSep2021
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 Ultrasonic_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)
ULTRASONIC_TRIGGER = const(12)
ULTRASONIC_ECHO = const(13)
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 the Ultrsonic sensor
ultrasonic_trigger = machine.Pin(ULTRASONIC_TRIGGER,machine.Pin.OUT)
ultrasonic_echo = machine.Pin(ULTRASONIC_ECHO,machine.Pin.IN)
#Initialize SPI
spi = SPI(0,baudrate=40000000,miso=Pin(TFT_MISO_PIN),mosi=Pin(TFT_MOSI_PIN),sck=Pin(TFT_CLK_PIN))
#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()
OBJECT_MINIMUM_DISTANCE = const(5) # 5cm
OBJECT_NEAR = const(50) # 50 CM. Close.
OBJECT_AWAY = const(400) # 400 CM AWAY. Maximum distance
OBJECT_NONE = const(0)
safe_present = 0
safe_previous = 0
# Read the sensor and calculate the distance
def MeasureDistance():
    ultrasonic_trigger.low()
    utime.sleep_us(2)
    ultrasonic_trigger.high()
    utime.sleep_us(5)
    ultrasonic_trigger.low()
    while ultrasonic_echo.value() == 0:
       signaloff = utime.ticks_us()
    while ultrasonic_echo.value() == 1:
       signalon = utime.ticks_us()
    timepassed = signalon - signaloff
    # distance = (timepassed * 0.0343) / 2 # @21 C
    distance = (timepassed * 0.034895) / 2 # @30 C
    return distance
# Read the sensor and display the values
def ReadUltrasonicSensor(timer_two):
    global safe_present
    global safe_previous
    
    distance = MeasureDistance()
    if distance < OBJECT_MINIMUM_DISTANCE:
        safe_present = OBJECT_MINIMUM_DISTANCE
    elif distance < OBJECT_NEAR:
        safe_present = OBJECT_NEAR
    elif distance < OBJECT_AWAY:
        safe_present = OBJECT_AWAY
    else:
        safe_present = OBJECT_NONE
    display.SetFont(RobotoBlack32_180)
    display.SetFgBgColor(COLOR_BLACK, SECONDARY_NORMAL_C)
    display.FillRectangle(cx+80,cy+125,cw-12,45,SECONDARY_NORMAL_C)
    if safe_present != OBJECT_NONE:
        display.WriteString(cx+80,cy+130,"{:0.2f}".format(distance) + " cm")
    else:
        display.WriteString(cx+80,cy+130,"NO OBJECT")
    
    # If the object is less than 50 centimeter, say Display object is too close
    # else Normal
    if safe_present != safe_previous:
        safe_previous = safe_present
        display.SetFont(RobotoBlack32_180)
        if safe_present == OBJECT_MINIMUM_DISTANCE:
            display.SetFgBgColor(COLOR_WHITE, COLOR_RED)
            display.FillRectangle(cx,cy+210,cw,45,COLOR_RED)
            display.WriteString(cx+90,cy+220,"TOO CLOSE")
        elif safe_present == OBJECT_NEAR:
            display.SetFgBgColor(COLOR_WHITE, COLOR_RED)
            display.FillRectangle(cx,cy+210,cw,45,COLOR_RED)
            display.WriteString(cx+120,cy+220,"NEAR")
        elif safe_present == OBJECT_AWAY:
            display.SetFgBgColor(COLOR_WHITE, COLOR_BLUE)
            display.FillRectangle(cx,cy+210,cw,45,COLOR_BLUE)
            display.WriteString(cx+120,cy+220,"AWAY")
        else:
            display.SetFgBgColor(COLOR_WHITE, COLOR_RED)
            display.FillRectangle(cx,cy+210,cw,45,COLOR_RED)
            display.WriteString(cx+120,cy+220,"NONE")
       
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+70,y11+10,"ULTRASONIC DISTANCE")
# Draw icon window
x12 = 4; y12 = 55; w12 = 150
h12 = 320 - y12 - 10
display.FillRectangle(x12,y12,w12,h12,PRIMARY_NORMAL_C)
# Draw Distance window and set Roboto24 font for headings
cx = x12 + w12 + 3; cy = 55; cw = 320; ch = 258
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+110,cy+3,"DISTANCE")
display.SetFont(RobotoBlack32_180)
display.SetFgBgColor(COLOR_BLACK, SECONDARY_NORMAL_C)
display.WriteString(cx+30,cy+80,"OBJECT DISTANCE")
# Draw Ultrsonic icon
display.SetFgBgColor(COLOR_WHITE, PRIMARY_NORMAL_C)
k = bytearray(ultrasonic)
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 Ultrsonic Sensor.
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=ReadUltrasonicSensor)
ReadUltrasonicSensor(timer_two)

Conclusion

C1.jpg

The project is successfully completed with Raspberry Pi Pico Ultrasonic Distance Sensor HC-SR05 and 320x480

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.

In expensive ultrasonic sensor can be used for contact less obstacle detection / distance measurements etc.

The calculated distance between sensor and object is displayed on HVGA LCD.

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 Ultrasound Object Monitoring video link