teig
Published © MPL-2.0

Light alarm clock with raspberry pi pico

Easy and useful: An introduction project to get to know the raspberry pi pico.

BeginnerFull instructions provided3 hours5,902
Light alarm clock with raspberry pi pico

Things used in this project

Hardware components

Raspberry Pi Pico
Raspberry Pi Pico
×1

Software apps and online services

ObjectBlocks Openscad

Story

Read more

Custom parts and enclosures

enclosure stl

cover for enclosure stl

raw openscad file

scad file to adjust things.

Schematics

light alarm clock schematics

Code

Micro Pyhton code for light alarm clock

MicroPython
from machine import Pin, PWM, Timer
import utime # utime is the micropython brother of time

# Setup
onboard_led = Pin(25, Pin.OUT)
# blink onboard led 3 times to make sure the pico works
for i in range(3):
    onboard_led.value(1)
    utime.sleep(0.2)
    onboard_led.value(0)
    utime.sleep(0.2)
    

# see https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf chapter 3.1
tim = Timer()

# the timer works with seconds. To make it more human readable I calculate hours
m = 60 # 60 seconds = 1 minute
h = 60*m # 60 minutes = 1 hour
sleeptime = 7.7*h # my preferred sleeping duration is 7.7 hours

# Pulse Width Modulation setup
duty = 0
pwm = PWM(Pin(15))
pwm.freq(1000)

# flash external led once to make sure it still works
duty = 40000
pwm.duty_u16(duty)
utime.sleep(1)
duty = 0
pwm.duty_u16(duty)

def tick(timer):
    global duty
    global pwm
    
    while duty < 255:
        duty +=1
        pwm.duty_u16(duty*duty)
        utime.sleep(0.3)
    
tim.init(freq=1/sleeptime, mode=Timer.ONE_SHOT, callback=tick)

Credits

teig

teig

2 projects • 5 followers

Comments