Skip to content

Commit e6d9cf1

Browse files
committed
add initial files
0 parents  commit e6d9cf1

File tree

7 files changed

+168
-0
lines changed

7 files changed

+168
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Welcome!
2+
3+
This 2 hour workshop will introduce you to using CircuitPython to bridge the
4+
digital and physical worlds. You'll learn how to program a small, specialized
5+
computer (a microcontroller) to control devices in the physical world. This
6+
workshop is hands on and you will:
7+
- wire up a circuit
8+
- program a microcontroller
9+
- build an electronic instrument to measure reaction time
10+
- use the instrument to generate data on your reaction time
11+
12+
The course material is divided into:
13+
- a list of parts to order for each participant
14+
- a project template (used for all examples)
15+
- lecture slides
16+
- example code
17+
18+
To aid initial learning the microcontrollers are preloaded with CircuitPython
19+
(and a few libraries). For instructors (or those following independent study,
20+
see 'template' for setup instructions).

examples/led_button/code.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import board
2+
import digitalio
3+
import neopixel
4+
5+
6+
# the following globals and functions could be included as a module
7+
8+
# make an object to control the NEOPIXEL, multi-color LED
9+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
10+
11+
# configure the button pin (digital pin 5, D5)
12+
button = digitalio.DigitalInOut(board.D5)
13+
14+
# define button pin as an input
15+
button.direction = digitalio.Direction.INPUT
16+
17+
# add a 'pull up' to set the default value of this pin to True
18+
button.pull = digitalio.Pull.UP
19+
20+
21+
def set_brightness(value):
22+
# set the LED brightness to a value from 0 to 1
23+
pixel.brightness = value
24+
25+
26+
def set_color(red, green, blue):
27+
# set the LED color by defining the red, green and blue
28+
# components using values from 0 to 255
29+
# (255, 0, 0) will be red
30+
# (0, 255, 0) will be green
31+
# (255, 255, 0) will be yellow
32+
# (255, 255, 255) will be white
33+
pixel.fill((red, green, blue))
34+
35+
36+
def is_button_pressed():
37+
# the pin was configured with a 'pull up' so it's default value is True
38+
# pressing the button connects the button pin to ground and changes
39+
# the value to False
40+
# this function should return True when the button is pressed so we need
41+
# to invert the value
42+
return not button.value
43+
44+
# set the pixel color to red
45+
set_color(255, 0, 0)
46+
47+
# run this code forever (until powered down)
48+
while True:
49+
# if the button is pressed..
50+
if is_button_pressed():
51+
# turn on the light, LED, to full brightness
52+
set_brightness(1.0)
53+
else:
54+
# else, turn off the LED
55+
set_brightness(0.0)

examples/reaction_time/code.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import board
2+
import digitalio
3+
import neopixel
4+
5+
import time
6+
7+
8+
# the following globals and functions could be included as a module
9+
10+
# make an object to control the NEOPIXEL, multi-color LED
11+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
12+
13+
# configure the button pin (digital pin 5, D5)
14+
button = digitalio.DigitalInOut(board.D5)
15+
16+
# define button pin as an input
17+
button.direction = digitalio.Direction.INPUT
18+
19+
# add a 'pull up' to set the default value of this pin to True
20+
button.pull = digitalio.Pull.UP
21+
22+
23+
def set_brightness(value):
24+
# set the LED brightness to a value from 0 to 1
25+
pixel.brightness = value
26+
27+
28+
def set_color(red, green, blue):
29+
# set the LED color by defining the red, green and blue
30+
# components using values from 0 to 255
31+
# (255, 0, 0) will be red
32+
# (0, 255, 0) will be green
33+
# (255, 255, 0) will be yellow
34+
# (255, 255, 255) will be white
35+
pixel.fill((red, green, blue))
36+
37+
38+
def is_button_pressed():
39+
# the pin was configured with a 'pull up' so it's default value is True
40+
# pressing the button connects the button pin to ground and changes
41+
# the value to False
42+
# this function should return True when the button is pressed so we need
43+
# to invert the value
44+
return not button.value
45+
46+
47+
# start trial count at 0
48+
trial_count = 0
49+
50+
# set led color to pure red
51+
set_color(255, 0, 0)
52+
53+
# run trials
54+
while True:
55+
# turn on light
56+
set_brightness(1.0)
57+
58+
# record starting time
59+
start_time = time.monotonic()
60+
61+
# wait for the button to be pressed
62+
while not is_button_pressed():
63+
# do nothing if the button is not pressed
64+
pass
65+
66+
# record time end time (when button was pressed)
67+
end_time = time.monotonic()
68+
69+
# turn off light
70+
set_brightness(0.0)
71+
72+
# compute time between light onset and button press
73+
reaction_time = end_time - start_time
74+
75+
# report reaction time as comma separated text
76+
print("%s,%s" % (trial_count, reaction_time))
77+
78+
# increment trial count
79+
trial_count += 1
80+
81+
# wait a short time between trials
82+
time.sleep(5)

template/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Plug in the RP2040 feather to your computer. Next, hold down 'boot sel'
2+
button and press the 'reset' button (while holding 'boot sel'). Release
3+
'boot sel' and the RP2040 feather should appear as a USB flash/thumb
4+
drive. Copy the .uf2 file in this directory to the drive. The RP2040
5+
feather should reset and reappear as a drive with a 'code.py' file and
6+
a 'libs' directory. Copy the files in the 'libs' directory in this
7+
directory (the 'neopixel.mpy' and 'adafruit_pixelbuf.mpy' files)
8+
to the 'libs' directory on the RP2040 feather.
9+
10+
You should now be ready to load any of the examples for this workshop
11+
onto the RP2040 feather!
Binary file not shown.

template/libs/adafruit_pixelbuf.mpy

2.94 KB
Binary file not shown.

template/libs/neopixel.mpy

1.28 KB
Binary file not shown.

0 commit comments

Comments
 (0)