posted: Wed, Jun 14, 2023 |
tagged: projects | dev | electronics | fec
return to project home | resources
Code and reference Fritzing diagrams
Let's explore our board a bit.
Mu
code editorCTRL + C
to interrupt the execution of the codeEnter
to access the REPL (R
ead - E
valuate - P
rint - L
oop)import board
and press enterhelp(board)
and press enterThe Pico doesn't have a bunch of addressable ancillary hardware components, as illustrated by the output. Other boards, such as the Circuit Playground Express from Adafruit
is a prime example of a board with several components on-board such as an accelerometer, microphone, speaker, 10 neopixels, and more.
Before connecting more electronics to the breadboard and the Pico, first disconnect the USB cable. To get you familiarized with the Pico a bit more intimately, we'll have you reference the pinouts to make the connections with slightly more sparse instructions.
A switch is a very simple component, and in a single throw single position switch, you either have a connection or you don't. Think of the power switch for the light in your bedroom. When the light is off, the switch is considered open and the connection between the power source and the light is severed. When you close down / turn on the switch, the connection is established between the source and the bulb.
# Import libraries
import board
import digitalio
# Define LED object
led = digitalio.DigitalInOut(board.GP15)
led.direction = digitalio.Direction.OUTPUT
# Define switch object
switch = digitalio.DigitalInOut(board.GP16)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
# Loop and test for switch state
while True:
if switch.value:
led.value = True
else:
led.value = False
We're going to add a temporary push button into the mix. We're going to explore how the Pico reacts when a button is pressed, then incorporate it into the project above controlling a second LED.
# Import libraries
import board
import digitalio
import time
# Define LED object
led = digitalio.DigitalInOut(board.GP15)
led.direction = digitalio.Direction.OUTPUT
led2 = digitalio.DigitalInOut(board.GP14)
led2.direction = digitalio.Direction.OUTPUT
# Define switch object
switch = digitalio.DigitalInOut(board.GP16)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
# Define button object
button = digitalio.DigitalInOut(board.GP17)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.DOWN
# Loop and test for switch and button state
while True:
if switch.value:
led.value = True
else:
led.value = False
if button.value:
led2.value = False
else:
led2.value = True
time.sleep(0.1)
Ok, let's dig into some Python code. Let's install IDLE (more on IDLE?), Python's Integrated Development and Learning Environment. IDLE is typically installed with Python when installing on Windows, but not Linux.
Execute the command sudo apt install idle3 -y
from within your terminal. This will install IDLE, which you'll find in the Programming menu within the Ubuntu Mate desktop menu system. Open IDLE.
We'll quickly recreate the Hello World program. In IDLE, enter print("Hello World")
and press enter.
Now let's review doing some math in IDLE. You can enter the highlighted commands below directly into IDLE and press enter after each example.
1 + 5
10 - 3
10 / 2
3 * 5
2 ** 3
(2 to the 3rd power)7 % 2
And let's quickly look at a few types. Execute the following highlighted commands in IDLE:
type(2)
type(4.2)
type(True)
type("hello world")
type("4")
String arithmetic? Let's apply some math logic to strings and see what happens in IDLE:
"Hello " + "World"
"Hello" * 3
In support of this section of the learning, please read Chapter 1 of Think Python 2 and practice the examples in IDLE.