As mentioned earlier, sometimes the two pushbuttons on the micro:bit just aren't enough! If this ever happens with your inventions and prototypes, it helps to be able to add more to a breadboard.
In this activity, you will:
These are the parts you will add to the setup you started with in Setup from Previous Activities.
(1) Resistor - 220 Ω (red-red-brown-gold)
(1) Resistor - 10 kΩ (brown-black-orange-gold)
(1) Jumper wires - red
(1) Pushbutton - New part, see Pushbutton diagram
A part drawing with numbers by each pin is called a pin map. Pin maps are normally published in part datasheets. Engineers and technicians use schematics and pin maps when designing, troubleshooting, and maintaining circuits. The schematic shows how the parts are interconnected, and the pin map shows how the pin numbers in the schematic relate to the physical part. This pushbutton can be rotated 180°, and the pin map still applies. That’s not common! Most parts have a reference mark by pin 1 and cannot be rotated, for example the two black chips, an 8-pin op amp and a 3-pin transistor from your kit.
The pushbutton_test script displays whether or not the pushbutton is pressed as values of 1 and 0. These values are often called states. With this pushbutton circuit and script, a state of 1 indicates pressed, and 0 indicates not pressed.
# pushbutton_test from microbit import * state = 0 display.off() pin6.set_pull(pin6.NO_PULL) while True: state = pin6.read_digital() print("state = ", state) sleep(250)
(Answers are in the Teacher's Guide.))
This script is short, but it contains some important instructions that are unique to the micro:bit module hardware.
To begin, the state = 0 statement just creates a variable named state that will be used inside the while True loop.
state = 0
The micro:bit module’s P6 pin is part of the module's 5x5 LED display grid. The display.off() call prevents the LED circuits built into the micro:bit from interfering with the pushbutton circuit you just built on the breadboard.
display.off()
The 10 kΩ resistor connected to the pushbutton is called a pull-down resistor. More about that later! P6 also has an internal pull-down resistor enabled by default, and this statement disables it since a 10 kΩ pulldown resistor has been added to the circuit on your breadboard. Many microcontrollers used in
pin6.set_pull(pin6.NO_PULL)
Inside the main loop, when the pushbutton is pressed, the circuit applies 3.3 V to the P6 pin. The pin6.read_digital() call returns 1 when it detects 3.3 V. If the button is released, the circuit applies 0 V to the pin, and pin6.read_digital() returns 0 instead. In state = pin6.read_digital(), the state variable stores that 1 or 0 result.
while True: state = pin6.read_digital()
Next, the print statement displays "state = " followed by the 1 or 0 that just was stored in the state variable.
print("state = ", state)
Last in the loop, the sleep(250) call just slows down the display to make it easier to observe the transitions from 0 to 1 and 1 to 0.
sleep(250)
The binary (1 or 0) value returned by the pin6.read_digital() method can be used as the argument to turn the LED light on and off in pin13.write_digital(state). When state is 1 because the button is pressed, pin13.write_digital(state) becomes pin13.write_digital(1). This sets the P13 LED light to on. When the state variable is 0 because the button is not pressed, pin13.write_digital(state) becomes pin13.write_digital(0). This sets the LED light to off.
A light that a microcontroller turns on and off can be considered a binary (2-state) output circuit. Likewise, a pushbutton circuit can be considered a binary input circuit. The microcontroller monitors the pushbutton circuit to find out which of two states the button is in: pressed or not pressed. The microcontroller does this by monitoring the voltage that changes at a certain point in the circuit in response to the pushbutton being pressed.
A pin.read_digital() call sets a micro:bit I/O pin to input. As an input, an I/O pin monitors the voltage of a circuit that’s connected to it without affecting the circuit. The circuit behaves the same regardless of whether the I/O pin is connected or not.
Inside the micro:bit’s microcontroller, there is memory—like a variable—called an input register that stores the voltages measured by I/O pins as 1s and 0s. Each memory element that stores a 1 or 0 is called a bit. Likewise, a register bit refers to each memory element that stores a 1 or 0 to indicate the voltage applied to an I/O pin set to input.
When an I/O pin input register bit stores a 1, it means the pin detected a voltage that was above 2.3 V. If it instead stores a 0, it means the voltage was below 1 V. If the voltage is in the 1 to 2.3V range, the register just keeps the most recent measurement that was above 2.3 V or below 1 V. The pin6.digital_read() method returns that binary value (1 or 0) that the P6 input register bit stores.
The binary (1 or 0) value returend by the pin6.read_digital() method returns can be used in decision making with if statements. When state is 1 because the button is pressed, code in this example script’s if statement will make the light turn on for 0.25 seconds. After that, the light is turned off for another 0.25 seconds. When state is 0, the statements that turn the light on are skipped, and the while True loop just repeatedly turns the P13 light off. The end result is that the light blinks while the button is pressed and held, and stays off when it is not pressed.
Write a script to make the light blink when the button is pressed but without using an if...statement. Hint: the 1/0 value from the pushbutton state can be used to turn the light on or off, and then sleep. After that, the script just needs to make sure it turns the light off and sleeps again before repeating the while True() loop.
Solution: The trick is to use the 1/0 value of state.
from microbit import * state = 0 display.off() while True: state = pin6.read_digital() pin13.write_digital(state) sleep(100) pin13.write_digital(0) sleep(100)
Links
[1] https://python.microbit.org/v/2
[2] https://learn.parallax.com/sites/default/files/content/Python/button/pb-top-button-digital-read.mp4