Exercise 8 - Digital Inputs
Goals
- Enhance understanding of the digtialio library
- Create and use digital inputs
- Work with additional electronic components
Preparation
- We're going to start out with the circuit simulator where wiring will be easier, and then we'll go back to physical circuits
- Go to Wokwi and create a new CircuitPython project by clicking the button, then choosing
CircuitPython on Raspberry Pi Pico
- Find the button and add a pushbutton to your circuit. It should like something like the circuit below
Concepts & Vocabulary
- Digital Input
- Object
- Class
- Pull up resistor
- Input Signal
Wiring
Explanation of the switch
The pushbutton switch has four legs, but that's a little misleading. There's really only a pair of wires, with a gap between them. If you look at the way the legs look like they follow through the board, you can see which ones are actually connected.
One of the nice things about using the simulator is that you can hover over the pins to get a hint of what they are. The wires on the pushbutton are labeled as btn1:1.l
& btn1:1.r
(these are connected) and btn1:2.l
& btn1:2.r
(these are connected). When the button is pushed down, a metal plate connects the two wires to close the circuit. You can also use diagram.json
to change the name of the switch to something other than btn1
if you want (see the image below)
Connecting to the board
You can create wires by clicking on one component, then clicking on the second component to make the connection. Connect one leg of the button to GND 8
on the board, and connect one of the legs not connected to that leg to GP28
on the board.
If you're feeling adventurous, you can also add an LED and connect the anode (says A
when hovered over) to an I/O pin (I used GP0), and connect the cathode to a ground pin.
Demo Code
This assumes you have a button hooked up to Ground and GP28. Please note that the name of the pin is different from what you would use on the physical boards
Code snippets from the presentation
- This is not the full code! There is setup that would have to happen above this. You'd also probably want the if/else section running inside of a loop
- This line will check if the value of
button.value
is True. If it is, the line(s) indented below it will run - If the if statement runs, this is skipped. If it doesn't run, then the line(s) intended below it will run
Try It
Based on what we covered in class, demonstrate that you can use a button to control an LED
Show me your code in action
- 5 points: You successfully control an LED with a button. This should be based on programming, not directly hooking the LED up to the button
- 4 points: You can successfully read values from a button
- Anything less than reading values from a button will not be considered successful completion of this exercise