Python Tutorials – Lesson 11 – Ultrasonic Sensor Module with OLED using Raspberry Pi Pico

To control Ultrasonic sensor with OLED using Raspberry Pi Pico

Required Components

  1. Ultrasonic sensor board_1 no
  2. OLED Display_1 no
  3. Raspberry Pi Pico board_1 no
  4. Data Cable_1 no
  5. Connecting Wires_8 no

Circuit

Steps

  1. Make sure the components are working properly.
  2. Ultrasonic Sensor board VCC, GND connected to VBus, GND of Raspberry Pi Pico Board.
  3. Connect Ultrasonic sensor Board echo, trigger pin to the Raspberry Pi Pico Board GP2, GP3 pin.
  4. OLED board VCC, GND connected to 3V3 (OUT), GND of Raspberry Pi Pico Board.
  5. OLED board SCL, SDA connected to GP16, GP17 of Raspberry Pi Pico Board.
  6. Check the Python program.
  7. Check the Electrical Circuit.
  8. 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)  

Leave a Reply

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