Skip to content

OLED PING

Circuit

Coder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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