Program the Trinket M0 w/ CircuitPython

This is the flow of our project, which informs the flow of our code:

  1. Power on the system, light power switch LED
  2. Pour beverage into the chilling vessel
  3. Press button to begin chilling process, light up button LED
  4. When chilling is finished, pump liquid from chilling vessel into drinking glass
  5. Turn off button LED

If you're new to Trinket M0 and CircuitPython, check out this guide for getting started! Once you're ready, and have the latest CircuitPython installed on your board, you can proceed.

Copy the code below, and paste it into your favorite text editor.

You can adjust the code for different chilling and pumping times by changing the values of chillTime in minutes and pumpTime in seconds.

You can see that we turn on and off the button's integrated LED which is connected to pin D2 at the start and finish of the cycle, respectively. What about the power LEDs on the board and the toggle switch? Those bypass the Trinket M0 and run directly off of the 12V supply to indicate that it is live.

Next, save the file as main.py on your Trinket M0, which shows up on your computer as the CIRCUITPY USB drive.

# Chilled Drinkibot

from digitalio import DigitalInOut, Direction, Pull
import board
import time

led = DigitalInOut(board.D2)  # Button LED
led.direction = Direction.OUTPUT

button = DigitalInOut(board.D0)
button.direction = Direction.INPUT
button.pull = Pull.UP

chiller = DigitalInOut(board.D3)  # Pin to control the chiller and fan
chiller.direction = Direction.OUTPUT

pump = DigitalInOut(board.D4)  # Pin to control the pump
pump.direction = Direction.OUTPUT


chillTime = 5  # How many _minutes_ of cooling

pumpTime = 35  # How many seconds of pumping

while True:
    # we could also just do "led.value = not button.value" !
    if button.value:
        print('not')
        led.value = False      # turn OFF LED
        chiller.value = False  # turn OFF chiller
        pump.value = False     # turn OFF pump
    else:
        print('pressed')
        led.value = True       # turn ON LED
        chiller.value = True   # turn ON chiller
        time.sleep(chillTime * 60)  # wait chiller time (in seconds)
        chiller.value = False  # turn OFF chiller
        pump.value = True      # turn ON pump
        time.sleep(pumpTime)   # wait pump time
        pump.value = False     # turn OFF pump
        led.value = False      # turn OFF LED

    time.sleep(0.01)  # debounce delay

Next, with the chiller assembly and pump connected, you can test out your system, and then finalize assembly!

Last updated on 2017-11-02 at 02.14.15 PM Published on 2017-11-03 at 12.25.04 PM