Robert Schreiner
Published

Raspberry Pi Pico Shows Distance on OLED, Toggled by Button

Raspberry Pi Pico measures distance with HC-SR04 and displays on a 0.96 OLED. Measuring can be toggled by a button (interrupt request).

IntermediateProtip1 hour2,019
Raspberry Pi Pico Shows Distance on OLED, Toggled by Button

Things used in this project

Hardware components

Raspberry Pi Pico
Raspberry Pi Pico
×1
OLED Display 128x64 (I2C)
×1
Button with 4 legs
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

ssd1306 micropython driver

Story

Read more

Schematics

Layout

Code

code.py

MicroPython
from machine import Pin, I2C, Timer
from ssd1306 import SSD1306_I2C
import utime


# global toggle button variable
measure_on = False

# debounce for button
def debounce(pin):
    timer.init(mode=Timer.ONE_SHOT, period=200, callback=on_pressed)

# if button pressed, toggle measure_on
def on_pressed(timer):
    global measure_on
    measure_on = not measure_on

# Init button
button = Pin(16, Pin.IN, Pin.PULL_DOWN)
timer = Timer()
button.irq(debounce, Pin.IRQ_RISING)

# Init Display
i2c = I2C(0,sda=Pin(0),scl=Pin(1),freq=40000)
oled = SSD1306_I2C(128,64,i2c)

# Init HC-SR04 pins
trigger = Pin(14, Pin.OUT)
echo = Pin(13, Pin.IN)


def ultra():
    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
    distance = (timepassed * 0.0343) / 2
    return distance

try:
    while True:
        oled.fill(0)
        if measure_on:
            result = ultra()
            oled.text("Distance:",0,0)
            oled.text(str(result) + " cm",0,10)
        oled.show()
        utime.sleep(1)            
except KeyboardInterrupt:
    pass

Credits

Robert Schreiner

Robert Schreiner

3 projects • 4 followers

Comments