Skip to content

Instantly share code, notes, and snippets.

@Michael-F-Ellis
Created December 2, 2021 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Michael-F-Ellis/26e8197a6acdb815ff79be8764583bd1 to your computer and use it in GitHub Desktop.
Save Michael-F-Ellis/26e8197a6acdb815ff79be8764583bd1 to your computer and use it in GitHub Desktop.
Arete Workshop code examples
# The machine library contains functions you can use control the Pico hardware.
# In this example program, we're using the Pin class to configure and control
# the GPIO pins.
from machine import Pin
# The utime library contains functions that allow us to read the current time
# and to delay (aka 'sleep') for seconds, milliseconds, or microseconds.
import utime
# main runs until stopped, blinking the Pico built-in LED at 1 second intervals.
def main():
# The picoLED is GPIO #25. Before using it we need to configure it for output.
picoLed = Pin(25, Pin.OUT)
# Print a message to the console
print("Hello")
# Enclose the loop in a try/finally clause to allow for clean-up actions
# when the program is stopped.
try:
# A while True loop runs forever.
while True:
picoLed.high() # LED on
utime.sleep_ms(1000) # Wait 1 second
picoLed.low() # LED off
utime.sleep_ms(1000) # Wait 1 second
finally:
# Clean up. Ensure the LED is turned off when we exit.
picoLed.low()
# Invoke main when the program is loaded
main()
# The machine library contains functions you can use control the Pico hardware.
# In this example program, we're using the Pin class to configure and control
# the GPIO pins.
from machine import Pin
# The utime library contains functions that allow us to read the current time
# and to delay (aka 'sleep') for seconds, milliseconds, or microseconds.
import utime
# main runs until stopped, blinking 3 LEDS in sequence.
def main():
# Note that the pin numbers on the printed on the Pico board are NOT
# the same as the GPIO numbers needed by the Pin function. Use the
# Pico pinout diagram at https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf
# to see which GPIO pin goes with which physical pin.
led14 = Pin(14, Pin.OUT) # Pico pin 19
led15 = Pin(15, Pin.OUT) # Pico pin 20
led16 = Pin(16, Pin.OUT) # Pico pin 21
# Print a message to the console
print("Look Ma, 3 LEDS at a time!")
# Make a list we can used to control the LEDs in sequence
ledList = [led14, led15, led16]
# Each led will blink quickly (20 milliseconds) and the sleep for a longer time
# (380 ms). The timing is arbitrary, I like the effect better than equal on/off times.
onMs = 20
offMs = 380
# Enclose the loop in a try/finally clause to allow for clean-up actions
# when the program is stopped.
try:
# A while True loop runs forever.
while True:
# blink each led in sequence
for led in ledList:
led.on() # LED on
utime.sleep_ms(onMs) # Wait 1 second
led.off() # LED off
utime.sleep_ms(offMs) # Wait 1 second
finally:
# Clean up. Ensure the each LED is turned off when we exit.
for led in ledList:
led.off()
# Invoke main when the program is loaded
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment