from machine import Pin, I2C, ADC
import time
import ssd1306
# Initialize the I2C interface (GP0 is SCL, GP1 is SDA)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
# Initialize the SSD1306 OLED display (address 0x3C)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize components
led = Pin(2, Pin.OUT) # LED on GP2
button = Pin(4, Pin.IN, Pin.PULL_UP) # Button on GP4 (with pull-up resistor)
potentiometer = ADC(Pin(28)) # Potentiometer on GP28
# Debounce variables
last_state = button.value() # Start with button state
last_time = time.ticks_ms() # Milliseconds timer
debounce_delay = 50 # 50ms debounce delay
# Timer variables for periodic "- - -" update
last_display_time = time.ticks_ms()
display_interval = 500 # Update every 500ms
# Function to display multiple lines
def display_lines(line1, line2, line3):
oled.fill(0) # Clear display
oled.text(line1, 0, 0) # First line (empty for spacing)
oled.text(line2, 0, 16) # Second line (potentiometer value)
oled.text(line3, 0, 32) # Third line (BEAT or - - -)
oled.show() # Update display
# Default beat display state
beat_display = "- - -"
# Print a welcome mssg on the Terminal
print("Hello from your Pulse and heartbeat Pico measurement device!")
print("")
print("This is a prototype of a device that captures heartbeat and measures blood pressure. Both values are displayed. Bloodpressure is measured in digits while the heartbeat is simply registered and displayed as a BEAT event.")
print("")
print("To simulate the bloodpressure use the potentiometer.")
print("To simulate heartbeat, press the button. Each heartbeat is also signalled by the LED light on. ")
# Main loop
while True:
# Read potentiometer value (0 to 65535)
pot_value = potentiometer.read_u16()
pot_value_scaled = int((pot_value / 65535) * 1023) # Scale to 0-1023
# Get the current time
current_time = time.ticks_ms()
# Handle button press (debounce)
current_state = button.value()
if current_state != last_state and time.ticks_diff(current_time, last_time) > debounce_delay:
last_time = current_time # Reset debounce timer
last_state = current_state # Update last state
if current_state == 0: # Button pressed
beat_display = "BEAT" # Override with "BEAT"
led.value(1) # Turn on LED
else: # Button released
beat_display = "- - -" # Set back to "- - -"
led.value(0) # Turn off LED
# Ensure "- - -" is displayed every 500ms (unless overridden by button press)
if time.ticks_diff(current_time, last_display_time) > display_interval:
last_display_time = current_time # Reset interval timer
if last_state == 1: # If button is not pressed
beat_display = "- - -" # Update automatically every 500ms
# Display values with empty first line
display_lines("", f"Pot: {pot_value_scaled}", beat_display)
time.sleep(0.05) # Small delay to avoid overloading CPU
Loading
ssd1306
ssd1306