Once you've finished setting up your QT Py ESP32-S2 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import alarm from digitalio import DigitalInOut, Direction, Pull # setup pins for the traffic light LEDs red_light = DigitalInOut(board.A1) yellow_light = DigitalInOut(board.A2) green_light = DigitalInOut(board.A3) # array of LEDs lights = [red_light, yellow_light, green_light] # the traffic light is common anode # the pins will need to be pulled down to ground # to turn on the LEDs. they are setup as inputs # so that the pull can be toggled # Pull.UP turns the LEDs off to start for light in lights: light.direction = Direction.INPUT light.pull = Pull.UP # button pin setup pin_alarm = alarm.pin.PinAlarm(pin=board.A0, value=False, pull=True) # count to track which light is on count = 2 # tracks the last light last_count = 1 while True: # increase count by 1, loop through 0-2 count = (count+1) % 3 # turn off the last LED lights[last_count].pull = Pull.UP # turn on the current LED lights[count].pull = Pull.DOWN # print(count) # delay to keep count time.sleep(1) # reset last LED for next loop last_count = count # go into light sleep mode until button is pressed again alarm.light_sleep_until_alarms(pin_alarm)
Upload the Code and Libraries to the QT Py ESP32-S2
After downloading the Project Bundle, plug your QT Py ESP32-S2 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the QT Py ESP32-S2's CIRCUITPY drive.
- lib folder
- code.py
Your QT Py ESP32-S2 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
![circuitpy](https://adafruit.github.io/Adafruit_Learning_System_Guides/Traffic_Light_Wearable.png)
The traffic light model's LEDs are common anode, meaning that the three LEDs share power (the black wire). The red, yellow and green wires need to be tied to ground to turn on the LEDs.
In the code, the three LEDs' pins are set as inputs so that their pull
direction can be toggled either UP
or DOWN
. When a pin's pull
is set to Pull.UP
, the LED is off. When a pin's pull
is set to Pull.DOWN
, the pin is tied to ground and the LED turns on.
# setup pins for the traffic light LEDs red_light = DigitalInOut(board.A1) yellow_light = DigitalInOut(board.A2) green_light = DigitalInOut(board.A3) # array of LEDs lights = [red_light, yellow_light, green_light] # the traffic light is common anode # the pins will need to be pulled down to ground # to turn on the LEDs. they are setup as inputs # so that the pull can be toggled # Pull.UP turns the LEDs off to start for light in lights: light.direction = Direction.INPUT light.pull = Pull.UP
The ESP32-S2 has the ability to use deep_sleep
and light_sleep
to save battery life. sleep
can be exited with alarms using the alarm
library. One type of alarm
is a PinAlarm
where if a pin's state changes then sleep
is interrupted. In this case, the button's pin is setup as pin_alarm
.
# button pin setup pin_alarm = alarm.pin.PinAlarm(pin=board.A0, value=False, pull=True)
count
and last_count
keep track of the button presses. The count
determines which LED in the traffic light is turned on.
# count to track which light is on count = 2 # tracks the last light last_count = 1
In the loop, count
increases by 1
for a range of 0 to 2. The last LED that was turned on has its pull
set to Pull.UP
to turn it off. The current LED is turned on by setting its pull
to Pull.DOWN
. Finally, the ESP32-S2 goes into light_sleep
to preserve battery life. It will stay in this state until the button is pressed again. Once the button is pressed, the loop will repeat.
while True: # increase count by 1, loop through 0-2 count = (count+1) % 3 # turn off the last LED lights[last_count].pull = Pull.UP # turn on the current LED lights[count].pull = Pull.DOWN # print(count) # delay to keep count time.sleep(1) # reset last LED for next loop last_count = count # go into light sleep mode until button is pressed again alarm.light_sleep_until_alarms(pin_alarm)
Text editor powered by tinymce.