These assignments were completed on a Raspberry Pi Pico microcontroller, running Circuit Python 7.3.2
The purpose of this assignment is to print a “countdown from 10 seconds to 0 (liftoff)” to the serial monitor.
There’s no wiring in this assignment. Just plug the pico into the computer.
# 2022-09-02, Em Heisig (hheisig51)
# Creates a countdown from 10 seconds to 0 seconds (liftoff) and prints it to the serial monitor.
import time
for x in range(10, 0, -1): # Moves from 10 to 0 in increments of 1
if x > 0:
print(f"{x} seconds left!") # prints the number of seconds left
time.sleep(1)
print(f"Liftoff!") # since x is not greater than 0, this prints
My biggest challenge was figuring out the loop. I first tried making two while.True()
loops, which didn’t work. Then, I tried using if
and else
inside a while.True()
, which works, albeit clunky. Finally, I got to using a for
loop, which makes much more sense in this use-case.
As the countdown ticks down, a red light will blink every second. At 0 seconds (liftoff), a green LED will turn on.
# 2022-09-09, Em Heisig (hheisig51)
# As the countdown (from 01_countdown.py) ticks down, a red light will blink every second. At 0 seconds (liftoff), a green LED will turn on.
import board
import digitalio
import time
g_led = digitalio.DigitalInOut(board.GP18)
r_led = digitalio.DigitalInOut(board.GP13)
g_led.direction = digitalio.Direction.OUTPUT
r_led.direction = digitalio.Direction.OUTPUT
for x in range(10, 0, -1): # Moves from 10 to 0 in increments of 1
if x > 0:
print(f"{x} seconds left!") # Prints the number of seconds left
r_led.value = True # Turns the red LED on
time.sleep(.3)
r_led.value = False # Turns red LED off 0.3 seconds into the 1.0 second cycle.
time.sleep(.7)
ppschakelrint(f"Liftoff!") # Since x is not greater than 0, this prints
g_led.value = True
time.sleep(2)
g_led.value = False
This one was simple. Just remember, the long leg of the LED is the positive leg, and always use a resistor. I’ve forgotten that before, and it completely fries your LED.
In addition to the previous segments (see 01_countdown.py and 02_lights.py), this assignment adds a button to start the countdown.
Let’s import our libraries and set up our LEDs’ like before.
import board
import digitalio
import time
g_led = digitalio.DigitalInOut(board.GP18) # assigns a board.pin (GP18) to a variable (g_led)
r_led = digitalio.DigitalInOut(board.GP13)
g_led.direction = digitalio.Direction.OUTPUT # assigns the variable, with a pin now attached to it, as an output
r_led.direction = digitalio.Direction.OUTPUT
Now, we’ll set up the button, as an INPUT
rather than an OUTPUT
, and as a pull-up resistor.
button = digitalio.DigitalInOut(board.GP16) # see above
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP # sets the button to function as a "pull-up" resistor, with a default value of true.
Same code as before, except the for
loop is now nested inside if butto.value == False:
, making it only trigger when the button is pressed.
while True:
if button.value == False:
for x in range(10, 0, -1): # Moves from 10 to 0 in increments of 1
if x > 0:
print(f"{x} seconds left!") # Prints the number of seconds left
r_led.value = True # Turns the red LED on
time.sleep(.3)
r_led.value = False # Turns red LED off 0.3 seconds into the 1.0 second cycle.
time.sleep(.7)
print(f"Liftoff!") # Since x is not greater than 0, this prints
g_led.value = True
time.sleep(2)
g_led.value = False
Figuring out the difference between a pull-up and a pull-down configuration for the button was very tricky. So, I made this little diagram to help myself out:
Pull-up | Pull-down | |
---|---|---|
Default value | True | False |
“Pressed” value | False | True |
Wired to | GND | 3V3(OUT) |
It was very frustrating figuring that out. My advice is to ask whatever question you need to ask to actually understand the wiring. Nobody’s going to judge you unless they’re a jerk.
In addition to the previous segments (see 03_button.py), this assignment adds a servo that simulates “the launch tower disconnecting.” The servo will start at a position of 0 degrees, then move to 180 degrees when “liftoff” is achieved.
There is some new code relating to the servo, but it’s pretty standard. I recommend looking at the code file and its comments.
This reflection is less about this singular assignment, but more about this Launch Pad assignment as a whole. I think it highlights the importance of segmenting your process when making something. Not fragmenting! You need to make sure things work together. But segmenting, and building one on top of the other, rather than building a servo and a button and lights and a countdown.
This assignment just prints x, y, and z acceleration values from an MPU-6050 chip to the serial monitor.
# 2022-09-29, Em Heisig (hheisig51)
# This assignment just prints x, y, and z acceleration
# values from an MPU-6050 chip to the serial monitor.
import board
import time
import adafruit_mpu6050
import busio
sda_pin = board.GP14 # the sda_pin and scl_pin will be used for data
scl_pin = board.GP15
i2c = busio.I2C(scl_pin, sda_pin) # pairs the two pins together
mpu = adafruit_mpu6050.MPU6050(i2c) # creates a variable we can reference for values
while True: # cleans up and prints the x, y, and z values
print(f"x = {round(mpu.acceleration[0],3)}, y = {round(mpu.acceleration[1],3)}, z = {round(mpu.acceleration[2],3)}")
time.sleep(.5)
This assigment introduced a whole new part, one I hadn’t used before, which introduced some new difficulties. Working with the I2C clock working with only specific pins was hard, but it got figured out. Also, condensing multiple lines of variables into one massive print statement took some time, but f-strings are awesome.
In this assignment, a red LED lights up if {x} is within a certain range (Acceleration of 0 to -4, which rougly corresponds with a rotation of 90 degrees).
# 2022-10-03, Em Heisig (hheisig51)
# In this assignment, a red LED lights up if {x} is within
# a certain range (Acceleration of 0 to -4, which rougly
# corresponds with a rotation of 90 degrees)
import board
import time
import adafruit_mpu6050
import busio
import digitalio
sda_pin = board.GP14 # the sda_pin and scl_pin will be used for data
scl_pin = board.GP15
i2c = busio.I2C(scl_pin, sda_pin) # pairs the two pins together
mpu = adafruit_mpu6050.MPU6050(i2c) # creates a variable we can reference for values
led = digitalio.DigitalInOut(board.GP13)
led.direction = digitalio.Direction.OUTPUT
x = 1
while True:
for x in range(1, 5, 1): # Loops a count from 1 to 5
if x == 4: # 1 out of 5 times, the values are printed
print(f"x = {round(mpu.acceleration[0],3)}, y = {round(mpu.acceleration[1],3)}, z = {round(mpu.acceleration[2],3)}")
x = 1
if mpu.acceleration[2] < 0 and mpu.acceleration[2] > -4: # turns the LED on if x falls within this range
led.value = True
time.sleep(.1)
else:
led.value = False
time.sleep(.1)
There was not a lot of complexity with this assignment. Most of the heavy lifting was done already (in setting up the accelerometer). But, I didn’t want the LED to just flicker and be done, but I also didn’t want the serial monitor to be spammed with messages. So, I had to tackle a for
loop, and adjust time.sleep
values constantly. I used the for loop to print the values every 5th time, but check them and trigger the LED every time. This assignment was very trial and error.
In addition to the previous assignments, this adds an OLED screen to print the values.
This assignment is rather long, so I recommend looking at it and its comments at the code file: 07-oled.py
This was a very fun assignment if you’re just looking at the result. If you’re looking at the process, it’s very not-fun. I had quite a bit of trouble understanding how/what/when the display rendered (helped by asking others, trial and error, and using tutorials straight from the manufacturer), which led to quite a few things getting cut off. But, eventually it came together, to make a very nice little display
This assignment takes 6 inputs (of 3 coordinate pairs) and returns the area of their triangle, or returns an error.
No wiring was necessary for this assignment. Only the pico was used.
This assignment is lengthy, as its only code, no wiring. I recomend looking at it in its code file: 08_functions.py
Being such a code-heavy assigment, this was quite different. Each component was really basic! But, learning how to plug them into eachother was quite a struggle. Properly naming variables was helpful, alongside using comments to mark different sections. It was a very fun assignment, especially being able to go back and optimize it, and watch the code shrink.
You have successfully written a script to calculate and return the area of each triangle. Now, your commander has asked that you include a small OLED screen to improve visualization of where the landing area is relative to the base.
(Description taken from Paul Schakel (pschake34), with permission)
%^
%^
%^
%^
To design a 3D-printed beam to withstand the most force before breaking, or bending more than 35 mm.
“Somebody is thinking today” - Mr. Miller
Hi! I’m Em Heisig, and I’m a junior at Charlottesville High School. You can reach me at hheisig51@charlottesvilleschools.org or github@eheisig.com.