Introduction
Connect a pushbutton to a Bit Board and control an LED with it.
We'll explore how we can use a pushbutton as an input for the micro:bit
Video Overview
Featured Document
-
-
We'll use Maker Tape to connect the Button and LED to the Bit Board on a LEGO baseplate.
-
We've already connected an LED in Chapter 1 so our new addition this time is the Button.
-
We'll connect our LED to Pin 1 and our Button to Pin 4.
-
And of course we'll connect the Ground (GND) sides of each component to Ground on the Bit Board.
-
You'll notice the Ground on the Bit Board (as well as the LED and other Crazy Circuits components we'll use) is color coded White.
-
While the LED has Polarity our Button does not. If you connect it the other way around it will still work fine. Not all components have Polarity.
-
-
-
If you've never used a micro:bit before you'll want to check out this guide: Bit Board V2 Setup and Use
-
We're going to load the following code for our Button Press program: https://makecode.microbit.org/_ghw7KwC7J...
-
This code will turn on the LED when the button is pressed, and then the LED will stay on for 3 seconds before it turns off.
-
For this code we are using two variables, buttonValue to hold the result of digital read pin, and onTime to hold the number of milliseconds the LED will stay on.
-
We've also got two separate forever blocks, one which reads the value of Pin 4, and one which acts upon the that value by looking at the buttonValue variable.
-
-
-
Once the code is loaded it should start running immediately.
-
You can power the micro:bit via the USB cable you used to load the code or you can use a battery pack plugged into the Bit Board.
-
The LED should turn on when the button is pressed, stay on for 3 seconds, and then turn off.
-
-
-
Follow along with our recorded Live Stream!
-
You can watch the full video of us walking through this project, along with explaining and exploring the code: https://www.youtube.com/live/sWfe8znn6C0
-
