Raspberry Pi Pico

Raspberry Pi Pico Ultrasonic Sensor Interfacing and programming

Raspberry Pi Pico Ultrasonic Sensor:

 

Raspberry Pi Pico Ultrasonic Sensor Interfacing and programming- In this article, I am going to use the most popular HC-SR04 Ultrasonic Sensor with the Raspberry Pi Pico. You know ultrasonic sensor is one of the most commonly used sensors and is usually used for obstacle detection in robotics, it is also used for monitoring the water level, and so on. For now, I will make a simple distance meter and you will be able to see the measured distance on the Oled display module. So, let’s go ahead and take a look at the circuit diagram.

Previous Articles:

Raspberry Pi Pico Vs Arduino.

Raspberry Pi Pico Pinout & Specs

Raspberry Pi Pico MicroPython and Thonny IDE Installation.

Raspberry Pi Pico Led examples.

Raspberry Pi Pico Digital Input.

Raspberry Pi Pico Oled Display Module SSD1306

Raspberry Pi Pico ADC Analog Sensor

Raspberry Pi Pico Temperature Sensor.

Raspberry Pi Pico PIR Motion Sensor.

Raspberry Pi Pico LDR Sensor, Day & Night Detection.


Amazon Links:

Raspberry Pi Pico

SSD1306 Oled Display Module

HC-SR04 Ultrasonic Sensor

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



Ultrasonic Sensor Interfacing with Raspberry Pi Pico:

Raspberry Pi Pico

The SSD1306 Oled display module connections with the Raspberry Pi Pico remains exactly the same.  The VCC pin is connected with the VBus which is Pin40 of the Raspberry Pi Pico. Trigger pin is connected with GP3, Echo pin connected with GP2, and the GND pin of the ultrasonic sensor is connected with the GND pin of the Raspberry Pi Pico board.


Ultrasonic Sensor Raspberry Pi Pico Programming:

from machine import Pin, I2C

from ssd1306 import SSD1306_I2C

from oled import Write, GFX, SSD1306_I2C

from oled.fonts import ubuntu_mono_15, ubuntu_mono_20

import utime




trigger = Pin(3, Pin.OUT)

echo = Pin(2, Pin.IN)

def distance():

timepassed=0

trigger.low()

utime.sleep_us(2)

trigger.high()

utime.sleep_us(5)

trigger.low()

while echo.value() == 0:

signaloff = utime.ticks_us()

while echo.value() == 1:

signalon = utime.ticks_us()

timepassed = signalon - signaloff

return timepassed




WIDTH  = 128

HEIGHT = 64

i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000)

oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)

while True:

oled.fill(0)

measured_time = distance()

distance_cm = (measured_time * 0.0343) / 2




oled.fill(0)

write20 = Write(oled, ubuntu_mono_20)

write20.text("Distance: ", 0, 0)

write20.text(str(round(distance_cm,1))+" cm",0,20)




oled.show()

The Oled display code remains exactly the same, this time I defined the trigger and echo pins which are connected with GP3 and GP2. I defined the trigger pin as the output and the echo pin as the input.


The actual code is placed inside the distance() function which measure the time duration of the signal as it travels from the transmitter to the receiver. Finally, the measured time is converted into centimeters and then divided by 2 to get the actual distance. While these other lines of code simply display the text and measured distance on the Oled display module. Now, let’s go ahead and run this code.

Raspberry Pi Pico

I just built myself a distance meter, now I can use this for obstacle detection, for controlling an electronic Door lock, for measuring the water level inside a tank, for controlling lights, and so on.

Next Article, Raspberry Pi Pico PIR Sensor Interfacing, and programming

Watch Video:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button