How to create a simple Focus Timer using Raspberry Pi Pico
In this guide we will create a simple yet extremely useful focus timer using Raspberry Pi Pico development board.
You can use this timer to focus on tasks at hand. The technique also known as the Pomodoro Technique.
What you need
- Raspberry Pi Pico or compatible board with CircuitPython installed
- LED strip (WS2812, also known as NeoPixel)
- 1M Ohm Resistor
- Jumper wires
- USB cable to connect to the power source
You can use any Raspberry Pi Pico board. For this guide we chose a Pimoroni Pico Lipo since it has a handy battery charger if we wanna make our timer mobile.
- Connect Pi Pico Ground and 3v3 to LED strip Ground and VIN
- Connect GP16 to LED strip data in
- Connect our 1M Ohm resistor between GP15 and Ground
- Connect a piece or wire to GP15, this will be our touch sensor which will control the timer
Thats it, we can now code our awesome focus timer.
Copy the following code in code.py file on your development board. Be sure to check out the inline comments for the explanation.
import time, os, board import neopixel, touchio from rainbowio import colorwheel OFF = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) # Settings pixels_count = 8 # How many LEDs on a strip to glow on each touch pixels_increment = 1 # Brightness, max is 1, go nuts if you like pixels_brightness = 0.1 # Each LED is a 5 minutes timer timer_seconds = (60 * 5) # See the color constants above, define a new color if you like color = WHITE # Initialise lights pixel_pin = board.GP16 pixels = neopixel.NeoPixel(pixel_pin, pixels_count, brightness=pixels_brightness, auto_write=True) # Initialise touch pin touch_pin = touchio.TouchIn(board.GP15) on_count = 0 prev_count = 0 # Helper to get rounded time monotonic def time_monotonic(): return round(time.monotonic()) # A cool rainbow effect, we will use this on startup and when the timer ends def rainbow_cycle(wait, count): for color in range(count): for pixel in range(len(pixels)): # pylint: disable=consider-using-enumerate pixel_index = (pixel * 256 // len(pixels)) + color * 5 pixels[pixel] = colorwheel(pixel_index & 255) pixels.show() time.sleep(wait) pixels.fill(OFF) # Glows or switches off the LEDs def update_pixels(): pixels.fill(OFF) if on_count: for p in range(on_count): pixels[p] = color rainbow_cycle(0.01, 80) start_time = time_monotonic() while True: prev_count = on_count now_time = time_monotonic() if touch_pin.value: # Detected a touch if on_count >= pixels_count: # Still less than 'pixels_count', increase the timer count on_count = 0 update_pixels() else: # Cant go above 'pixels_count', reset to 0 on_count += pixels_increment update_pixels() # Touch will always reset the start time start_time = time_monotonic() elapsed_min = now_time - start_time if elapsed_min >= timer_seconds and on_count: # Each tick (timer_seconds) will trigger this # Reduce 'pixels_increment' which will switch off 'pixels_increment' number of LEDs start_time = time_monotonic() on_count -= pixels_increment update_pixels() if on_count is 0: # A little show for you time.sleep(1) rainbow_cycle(0.01, 80) time.sleep(0.2)
Simply run code.py or you can disconnect/connect your board which will automatically run your code.
On startup you would see a nice rainbow light show for few seconds. To set the timer, touch the wire connected on pin GP15, each touch should increment the timer.
Sit back and focus on the given task, once the timer ends it will again show a little rainbow to celebrate.
Download all files
A little celebration before you go. You can increase the brightness to 1 to see this effect on startup and when the timer ends.
If you have any questions or would like clarification on any of the steps, don't hesitate to leave a comment.
Hope you have learned something new today.