"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Raspberry Pi Pico SSD1306 OLED Display (MicroPython) ┃
┃ ┃
┃ A program to display Raspberry Pi logo, text, and a ┃
┃ simple timer animation on an SSD1306 OLED display ┃
┃ connected to a Raspberry Pi Pico. ┃
┃ ┃
┃ Copyright (c) 2023 Anderson Costa ┃
┃ GitHub: github.com/arcostasi ┃
┃ License: MIT ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import Pin, PWM, I2C
import ssd1306
import time
import onewire, ds18x20
#--- I2C setup / OLED setup: 128x64 pixels ---
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.show()
#--- Living room ( Light ) ---
pir = Pin(15, Pin.IN) # PIR motion sensor
led = Pin(16, Pin.OUT) # LED light
#--Toilet--
trig = Pin(2, Pin.OUT) # Ultrasonic trigger
echo = Pin(3, Pin.IN) # Ultrasonic echo
motor = PWM(Pin(4))
motor.freq(50)
#--- Temperature and Buzzer ---
ow = onewire.OneWire(Pin(6)) # DS18B20 data pin
ds = ds18x20.DS18X20(ow)
roms = ds.scan()
buzzer = PWM(Pin(17)) # Buzzer
# --- Support Functions ---
def set_angle(angle):
# Map angle (0–180) to duty cycle
duty = int(2000 + (angle / 180) * 6000)
motor.duty_u16(duty)
#time.sleep(0) / wait for motor to move
def distance():
# Ultrasonic distance measurement
trig.value(1)
time.sleep_us(10)
trig.value(0)
start = time.ticks_us()
while echo.value() == 0:
if time.ticks_diff(time.ticks_us(), start) >1000000:
return 999
while echo.value() == 1:
end = time.ticks_us()
if time.ticks_diff(end, start) >1000000:
return 999
return (end - start) * 0.0343 / 2
def beep(frequency=2000, duration=500):
buzzer.freq(frequency)
buzzer.duty_u16(30000)
time.sleep_ms(duration)
buzzer.duty_u16(1)
def siren(repeat=3):
"""
Play a siren-style warning sound.
repeat: how many cycles to play
"""
for _ in range(repeat):
# Low tone
beep(1000, 200)
time.sleep_ms(50)
# High tone
beep(3000, 200)
time.sleep_ms(50)
#--- Mian Loop ---
while True:
# Living room motion + LED
if pir.value() == 1: # Motion detected
print("Living room : Motion detected")
led.value(0)
for count in range(10, 0, -1):
print("LED Turning ON in "+ str(count) + "seconds")
oled.fill(0)
oled.text("Living:", 0, 0)
oled.text("Motion detected!", 0, 20)
oled.text("LED turn ON in:" + str (count), 0, 40)
oled.show()
time.sleep(1)
print(" LED ON ")
led.value(1)
time.sleep(3)
else:
print("LED turned : OFF ")
led.value(0)
# Toilet + DC motor
d = distance()
if d < 15: # Person detected near toilet
print(f"Toilet : Person near toilet bowl distance : {d:.0f} CM")
# print(f"distance : {d:.1f} CM")
oled.fill(0)
oled.text("Toilet:", 0, 0)
oled.text("Person detected !!", 0, 15) #---stop the lib----
oled.text("Distance: " + str(round(d))+ "CM" , 0, 30)
oled.text("Lid OPEN...", 0, 50)
oled.show()
set_angle(180) #---Lid open---
else:
print("Toilet : Nobody ")
oled.fill(0)
oled.text("Toilet:", 0, 0)
oled.text("No detection !", 0, 20)
oled.text("Lid Closed...!", 0, 40)
oled.show()
set_angle(90) # Lid closed
#time.sleep(0)
# Temperature monitoring
ds.convert_temp()
time.sleep_ms(750)
for rom in roms:
temp = ds.read_temp(rom)
print(f"Temperature : {temp:.1f}°c")
if temp > 40:
print("Temperature High : Alert Elderly !! ")
oled.fill(0)
oled.text("Living Room:", 0, 0)
oled.text("Temp : High!", 0, 20)
oled.text(f"Temp : {temp:.1f}c" , 0, 35)
oled.text(" Alert Elderly! ", 0, 50)
oled.show()
siren(repeat=5)
print(".")
else:
print("Temperature : Normal")
oled.fill(0)
oled.text("Living Room:", 0, 0)
oled.text("Temp : Normal", 0, 20)
oled.text(f"Temp : {temp:.1f}c" , 0, 35)
oled.show()
print(".")
time.sleep(1.5)