Examples for Getting Started with Pico guide.
This commit is contained in:
parent
75dabd7c9f
commit
15fa3569e4
26 changed files with 544 additions and 0 deletions
|
|
@ -0,0 +1,9 @@
|
|||
"""Example for Pico. Turns on the built-in LED."""
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.LED)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
while True:
|
||||
led.value = True
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
"""Example for Pico. Turns the built-in LED on and off with no delay."""
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.LED)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
while True:
|
||||
led.value = True
|
||||
led.value = False
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
"""Example for Pico. Blinks the built-in LED."""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.LED)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
while True:
|
||||
led.value = True
|
||||
time.sleep(0.5)
|
||||
led.value = False
|
||||
time.sleep(0.5)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
"""Example for Pico. Blinks the built-in LED."""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.LED)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
while True:
|
||||
led.value = not led.value
|
||||
time.sleep(0.5)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
"""
|
||||
A burglar alarm example for Pico. Quick flashing LED indicates alarm has been triggered.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* PIR sensor on pin GP28.
|
||||
* LED on pin GP13.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
pir = digitalio.DigitalInOut(board.GP28)
|
||||
pir.direction = digitalio.Direction.INPUT
|
||||
led = digitalio.DigitalInOut(board.GP13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
motion_detected = False
|
||||
while True:
|
||||
if pir.value and not motion_detected:
|
||||
print("ALARM! Motion detected!")
|
||||
motion_detected = True
|
||||
|
||||
if pir.value:
|
||||
led.value = True
|
||||
time.sleep(0.1)
|
||||
led.value = False
|
||||
time.sleep(0.1)
|
||||
|
||||
motion_detected = pir.value
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
"""
|
||||
A burglar alarm example for Pico. Slow flashing LED indicates alarm is ready. Quick flashing LED
|
||||
indicates alarm has been triggered.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* PIR sensor on pin GP28.
|
||||
* LED on pin GP13.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
pir = digitalio.DigitalInOut(board.GP28)
|
||||
pir.direction = digitalio.Direction.INPUT
|
||||
led = digitalio.DigitalInOut(board.GP13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
motion_detected = False
|
||||
while True:
|
||||
if pir.value and not motion_detected:
|
||||
print("ALARM! Motion detected!")
|
||||
motion_detected = True
|
||||
|
||||
if pir.value:
|
||||
led.value = True
|
||||
time.sleep(0.1)
|
||||
led.value = False
|
||||
time.sleep(0.1)
|
||||
|
||||
else:
|
||||
motion_detected = False
|
||||
led.value = True
|
||||
time.sleep(0.5)
|
||||
led.value = False
|
||||
time.sleep(0.5)
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
"""
|
||||
A burglar alarm example for Pico. Slow flashing LED indicates alarm is ready. Quick flashing LED
|
||||
and beeping buzzer indicate alarm has been triggered.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* PIR sensor on pin GP28.
|
||||
* LED on pin GP13.
|
||||
* Piezo buzzer on pin GP14.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
import pwmio
|
||||
|
||||
pir = digitalio.DigitalInOut(board.GP28)
|
||||
pir.direction = digitalio.Direction.INPUT
|
||||
led = digitalio.DigitalInOut(board.GP13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
buzzer = pwmio.PWMOut(board.GP14, frequency=660, duty_cycle=0, variable_frequency=True)
|
||||
|
||||
motion_detected = False
|
||||
while True:
|
||||
if pir.value and not motion_detected:
|
||||
print("ALARM! Motion detected!")
|
||||
motion_detected = True
|
||||
|
||||
if pir.value:
|
||||
led.value = True
|
||||
buzzer.duty_cycle = 2 ** 15
|
||||
time.sleep(0.1)
|
||||
led.value = False
|
||||
buzzer.duty_cycle = 0
|
||||
time.sleep(0.1)
|
||||
|
||||
else:
|
||||
motion_detected = False
|
||||
led.value = True
|
||||
time.sleep(0.5)
|
||||
led.value = False
|
||||
time.sleep(0.5)
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
"""
|
||||
A burglar alarm with two motion sensors example for Pico. Slow flashing LED indicates alarm is
|
||||
ready. Quick flashing LED and beeping buzzer indicate alarm has been triggered.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* PIR sensor on pin GP28.
|
||||
* PIR sensor on pin GP22.
|
||||
* LED on pin GP13.
|
||||
* Piezo buzzer on pin GP14.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
import pwmio
|
||||
|
||||
pir_one = digitalio.DigitalInOut(board.GP28)
|
||||
pir_one.direction = digitalio.Direction.INPUT
|
||||
pir_two = digitalio.DigitalInOut(board.GP22)
|
||||
pir_two.direction = digitalio.Direction.INPUT
|
||||
led = digitalio.DigitalInOut(board.GP13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
buzzer = pwmio.PWMOut(board.GP14, frequency=660, duty_cycle=0, variable_frequency=True)
|
||||
|
||||
motion_detected_one = False
|
||||
motion_detected_two = False
|
||||
while True:
|
||||
if pir_one.value and not motion_detected_one:
|
||||
print("ALARM! Motion detected in bedroom!")
|
||||
motion_detected_one = True
|
||||
|
||||
if pir_two.value and not motion_detected_two:
|
||||
print("ALARM! Motion detected in living room!")
|
||||
motion_detected_two = True
|
||||
|
||||
if pir_one.value or pir_two.value:
|
||||
led.value = True
|
||||
buzzer.duty_cycle = 2 ** 15
|
||||
time.sleep(0.1)
|
||||
led.value = False
|
||||
buzzer.duty_cycle = 0
|
||||
time.sleep(0.1)
|
||||
|
||||
else:
|
||||
motion_detected_one = False
|
||||
motion_detected_two = False
|
||||
|
||||
led.value = True
|
||||
time.sleep(0.5)
|
||||
led.value = False
|
||||
time.sleep(0.5)
|
||||
16
Getting_Started_With_Raspberry_Pi_Pico/button.py
Normal file
16
Getting_Started_With_Raspberry_Pi_Pico/button.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
Button example for Pico. Prints button pressed state to serial console.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* Button switch on pin GP13.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
button = digitalio.DigitalInOut(board.GP13)
|
||||
button.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
while True:
|
||||
print(button.value)
|
||||
time.sleep(0.5)
|
||||
19
Getting_Started_With_Raspberry_Pi_Pico/button_and_led.py
Normal file
19
Getting_Started_With_Raspberry_Pi_Pico/button_and_led.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
"""
|
||||
Button and LED example for Pico. Turns on LED when button is pressed.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* Button switch on pin GP13.
|
||||
* LED on pin GP14.
|
||||
"""
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.GP14)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
button = digitalio.DigitalInOut(board.GP13)
|
||||
button.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
while True:
|
||||
if button.value:
|
||||
led.value = True
|
||||
led.value = False
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
"""
|
||||
Button and LED example for Pico. Turns on LED when button is pressed.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* Button switch on pin GP13.
|
||||
* LED on pin GP14.
|
||||
"""
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.GP14)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
button = digitalio.DigitalInOut(board.GP13)
|
||||
button.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
while True:
|
||||
led.value = button.value
|
||||
17
Getting_Started_With_Raspberry_Pi_Pico/button_pressed.py
Normal file
17
Getting_Started_With_Raspberry_Pi_Pico/button_pressed.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
"""
|
||||
Button example for Pico. Prints message to serial console when button is pressed.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* Button switch on pin GP13.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
button = digitalio.DigitalInOut(board.GP13)
|
||||
button.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
while True:
|
||||
if button.value:
|
||||
print("You pressed the button!")
|
||||
time.sleep(0.5)
|
||||
7
Getting_Started_With_Raspberry_Pi_Pico/conditional.py
Normal file
7
Getting_Started_With_Raspberry_Pi_Pico/conditional.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
"""Example of assigning a variable and comparing it to a value."""
|
||||
user_name = input ("What is your name? ")
|
||||
|
||||
if user_name == "Clark Kent":
|
||||
print("You are Superman!")
|
||||
else:
|
||||
print("You are not Superman!")
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
"""Example of assigning a variable, and comparing it to a value in a loop."""
|
||||
user_name = input ("What is your name? ")
|
||||
|
||||
while user_name != "Clark Kent":
|
||||
print("You are not Superman - try again!")
|
||||
user_name = input ("What is your name? ")
|
||||
print("You are Superman!")
|
||||
5
Getting_Started_With_Raspberry_Pi_Pico/definite_loop.py
Normal file
5
Getting_Started_With_Raspberry_Pi_Pico/definite_loop.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"""Example of definite loop."""
|
||||
print("Loop starting!")
|
||||
for i in range(10):
|
||||
print("Loop number", i)
|
||||
print("Loop finished!")
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
"""
|
||||
LED example for Pico. Blinks external LED on and off.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* LED on pin GP14.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.GP14)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
while True:
|
||||
led.value = True
|
||||
time.sleep(0.5)
|
||||
led.value = False
|
||||
time.sleep(0.5)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
"""Example of infinite loop. Final print statement is never reached."""
|
||||
print("Loop starting!")
|
||||
while True:
|
||||
print("Loop running!")
|
||||
print("Loop finished!")
|
||||
17
Getting_Started_With_Raspberry_Pi_Pico/motion_sensor.py
Normal file
17
Getting_Started_With_Raspberry_Pi_Pico/motion_sensor.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
"""
|
||||
Simple motion sensor example for Pico. Prints to serial console when PIR sensor is triggered.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* PIR sensor on pin GP28.
|
||||
"""
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
pir = digitalio.DigitalInOut(board.GP28)
|
||||
pir.direction = digitalio.Direction.INPUT
|
||||
|
||||
while True:
|
||||
if pir.value:
|
||||
print("ALARM! Motion detected!")
|
||||
while pir.value:
|
||||
pass
|
||||
29
Getting_Started_With_Raspberry_Pi_Pico/neopixels_rainbow.py
Normal file
29
Getting_Started_With_Raspberry_Pi_Pico/neopixels_rainbow.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""
|
||||
NeoPixel example for Pico. Displays a rainbow on the NeoPixels.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* RGB NeoPixel LEDs connected to pin GP0.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import neopixel
|
||||
from _pixelbuf import colorwheel
|
||||
|
||||
# Update this to match the number of NeoPixel LEDs connected to your board.
|
||||
num_pixels = 30
|
||||
|
||||
pixels = neopixel.NeoPixel(board.GP0, num_pixels, auto_write=False)
|
||||
pixels.brightness = 0.5
|
||||
|
||||
|
||||
def rainbow(speed):
|
||||
for j in range(255):
|
||||
for i in range(num_pixels):
|
||||
pixel_index = (i * 256 // num_pixels) + j
|
||||
pixels[i] = colorwheel(pixel_index & 255)
|
||||
pixels.show()
|
||||
time.sleep(speed)
|
||||
|
||||
|
||||
while True:
|
||||
rainbow(0)
|
||||
17
Getting_Started_With_Raspberry_Pi_Pico/neopixels_red.py
Normal file
17
Getting_Started_With_Raspberry_Pi_Pico/neopixels_red.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
"""
|
||||
NeoPixel example for Pico. Turns the NeoPixels red.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* RGB NeoPixel LEDs connected to pin GP0.
|
||||
"""
|
||||
import board
|
||||
import neopixel
|
||||
|
||||
# Update this to match the number of NeoPixel LEDs connected to your board.
|
||||
num_pixels = 30
|
||||
|
||||
pixels = neopixel.NeoPixel(board.GP0, num_pixels)
|
||||
pixels.brightness = 0.5
|
||||
|
||||
while True:
|
||||
pixels.fill((255, 0, 0))
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
"""
|
||||
NeoPixel example for Pico. Turns the NeoPixels red, green, and blue in sequence.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* RGB NeoPixel LEDs connected to pin GP0.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import neopixel
|
||||
|
||||
# Update this to match the number of NeoPixel LEDs connected to your board.
|
||||
num_pixels = 30
|
||||
|
||||
pixels = neopixel.NeoPixel(board.GP0, num_pixels)
|
||||
pixels.brightness = 0.5
|
||||
|
||||
while True:
|
||||
pixels.fill((255, 0, 0))
|
||||
time.sleep(0.5)
|
||||
pixels.fill((0, 255, 0))
|
||||
time.sleep(0.5)
|
||||
pixels.fill((0, 0, 255))
|
||||
time.sleep(0.5)
|
||||
27
Getting_Started_With_Raspberry_Pi_Pico/reaction_game.py
Normal file
27
Getting_Started_With_Raspberry_Pi_Pico/reaction_game.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
"""
|
||||
Reaction game example for Pico. LED turns on for between 5 and 10 seconds. Once it turns off, try
|
||||
to press the button as quickly as possible to measure reaction timm.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* LED on pin GP13.
|
||||
* Button switch on pin GP14.
|
||||
"""
|
||||
import time
|
||||
import random
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.GP13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
button = digitalio.DigitalInOut(board.GP14)
|
||||
button.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
led.value = True
|
||||
time.sleep(random.randint(5, 10))
|
||||
led.value = False
|
||||
timer_start = time.monotonic()
|
||||
while True:
|
||||
if button.value:
|
||||
reaction_time = (time.monotonic() - timer_start) * 1000 # Convert to ms
|
||||
print("Your reaction time was", reaction_time, "milliseconds!")
|
||||
break
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
"""
|
||||
Two-player reaction game example for Pico. LED turns on for between 5 and 10 seconds. Once it
|
||||
turns off, try to press the button faster than the other player to see who wins.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* LED on pin GP13.
|
||||
* Button switch on pin GP14.
|
||||
* Button switch on GP16.
|
||||
"""
|
||||
import time
|
||||
import random
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
led = digitalio.DigitalInOut(board.GP13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
button_one = digitalio.DigitalInOut(board.GP14)
|
||||
button_one.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
button_two = digitalio.DigitalInOut(board.GP16)
|
||||
button_two.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
led.value = True
|
||||
time.sleep(random.randint(5, 10))
|
||||
led.value = False
|
||||
while True:
|
||||
if button_one.value:
|
||||
print("Player one wins!")
|
||||
break
|
||||
if button_two.value:
|
||||
print("Player two wins!")
|
||||
break
|
||||
32
Getting_Started_With_Raspberry_Pi_Pico/traffic_light.py
Normal file
32
Getting_Started_With_Raspberry_Pi_Pico/traffic_light.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
"""Traffic light simulator example for Pico. Turns on red, amber and green LEDs in traffic
|
||||
light-like sequence.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* Red LED on pin GP11.
|
||||
* Amber LED on pin GP14.
|
||||
* Green LED on pin GP13.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
red_led = digitalio.DigitalInOut(board.GP11)
|
||||
red_led.direction = digitalio.Direction.OUTPUT
|
||||
amber_led = digitalio.DigitalInOut(board.GP14)
|
||||
amber_led.direction = digitalio.Direction.OUTPUT
|
||||
green_led = digitalio.DigitalInOut(board.GP13)
|
||||
green_led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
while True:
|
||||
red_led.value = True
|
||||
time.sleep(5)
|
||||
amber_led.value = True
|
||||
time.sleep(2)
|
||||
red_led.value = False
|
||||
amber_led.value = False
|
||||
green_led.value = True
|
||||
time.sleep(5)
|
||||
green_led.value = False
|
||||
amber_led.value = True
|
||||
time.sleep(3)
|
||||
amber_led.value = False
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
"""Traffic light with pedestrian crossing simulator example for Pico. Turns on red, amber and green
|
||||
LEDs in traffic light-like sequence. When button is pressed, upon light sequence completion, the
|
||||
red LED turns on and the buzzer beeps to indicate pedestrian crossing is active.
|
||||
|
||||
REQUIRED HARDWARE:
|
||||
* Red LED on pin GP11.
|
||||
* Amber LED on pin GP14.
|
||||
* Green LED on pin GP13.
|
||||
* Button switch on pin GP16.
|
||||
* Piezo buzzer on pin GP13.
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
import pwmio
|
||||
|
||||
red_led = digitalio.DigitalInOut(board.GP11)
|
||||
red_led.direction = digitalio.Direction.OUTPUT
|
||||
amber_led = digitalio.DigitalInOut(board.GP14)
|
||||
amber_led.direction = digitalio.Direction.OUTPUT
|
||||
green_led = digitalio.DigitalInOut(board.GP13)
|
||||
green_led.direction = digitalio.Direction.OUTPUT
|
||||
button = digitalio.DigitalInOut(board.GP16)
|
||||
button.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
buzzer = pwmio.PWMOut(board.GP13, frequency=660, duty_cycle=0, variable_frequency=True)
|
||||
|
||||
button_pressed = False
|
||||
|
||||
|
||||
def waiting_for_button(duration):
|
||||
global button_pressed # pylint: disable=global-statement
|
||||
end = time.monotonic() + duration
|
||||
while time.monotonic() < end:
|
||||
if button.value:
|
||||
button_pressed = True
|
||||
|
||||
|
||||
while True:
|
||||
if button_pressed:
|
||||
red_led.value = True
|
||||
for _ in range(10):
|
||||
buzzer.duty_cycle = 2 ** 15
|
||||
waiting_for_button(0.2)
|
||||
buzzer.duty_cycle = 0
|
||||
waiting_for_button(0.2)
|
||||
button_pressed = False
|
||||
red_led.value = True
|
||||
waiting_for_button(5)
|
||||
amber_led.value = True
|
||||
waiting_for_button(2)
|
||||
red_led.value = False
|
||||
amber_led.value = False
|
||||
green_led.value = True
|
||||
waiting_for_button(5)
|
||||
green_led.value = False
|
||||
amber_led.value = True
|
||||
waiting_for_button(3)
|
||||
amber_led.value = False
|
||||
2
Getting_Started_With_Raspberry_Pi_Pico/variable.py
Normal file
2
Getting_Started_With_Raspberry_Pi_Pico/variable.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
"""Example of assigning a variable."""
|
||||
user_name = input("What is your name? ")
|
||||
Loading…
Reference in a new issue