To control Ultrasonic sensor with OLED using Raspberry Pi Pico
Required Components
- Ultrasonic sensor board_1 no
- OLED Display_1 no
- Raspberry Pi Pico board_1 no
- Data Cable_1 no
- Connecting Wires_8 no
Circuit
Steps
- Make sure the components are working properly.
- Ultrasonic Sensor board VCC, GND connected to VBus, GND of Raspberry Pi Pico Board.
- Connect Ultrasonic sensor Board echo, trigger pin to the Raspberry Pi Pico Board GP2, GP3 pin.
- OLED board VCC, GND connected to 3V3 (OUT), GND of Raspberry Pi Pico Board.
- OLED board SCL, SDA connected to GP16, GP17 of Raspberry Pi Pico Board.
- Check the Python program.
- Check the Electrical Circuit.
- Run the Python program.
Python Program
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import utime
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
def ultrasonnic():
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 # oled display width
HEIGHT = 64 # oled display height
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000) # Init I2C using pins GP8 & GP9 (default I2C0 pins)
print("I2C Address : "+hex(i2c.scan()[0]).upper()) # Display device address
print("I2C Configuration: "+str(i2c)) # Display I2C config
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Init oled display
# Clear the oled display in case it has junk on it.
oled.fill(0)
# Add some text
oled.text("CIRCUIT DIGEST",5,8)
oled.text("INTERFACING THE",5,30)
oled.text("ULTRASONNIC",28,40)
oled.text("SENSOR",50,50)
# Finally update the oled display so the image & text is displayed
oled.show()
utime.sleep(4)
while True:
oled.fill(0)
measured_time = ultrasonnic()
distance_cm = (measured_time * 0.0343) / 2
distance_cm = round(distance_cm,2)
oled.text("<ObjectDistance>",0,8)
oled.text(">> "+str(distance_cm)+" cm",2,25)
oled.show()
utime.sleep(1)