from machine import Pin, ADC, I2C
from ssd1306 import SSD1306_I2C
import time
# Initialize I2C for OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
# Initialize ADC for Pulse Sensor
pulse_sensor = ADC(Pin(34))
pulse_sensor.atten(ADC.ATTN_11DB) # Set attenuation for wider range
# Function to read heartbeat sensor
def read_pulse():
return pulse_sensor.read()
# Display initialization message
oled.fill(0)
oled.text("Initializing...", 0, 0)
oled.show()
time.sleep(2)
# Main loop
while True:
heartbeat_value = read_pulse()
# Debug: print raw sensor value to serial monitor
print("Heartbeat:", heartbeat_value)
# Clear the display
oled.fill(0)
# Display the heartbeat value
oled.text("Heartbeat:", 0, 0)
oled.text(str(heartbeat_value), 0, 10)
# Update the display
oled.show()
# Wait before taking another reading
time.sleep(0.5)