Use micro:bit and cardboard to create a Jedi knight that sounds the alarm when evil approaches!
What will you need for this project?
- A micro:bit
- Micro USB lead to connect micro:bit to computer
- A Kitronik breakout board
- A Servo
- Neopixels (we used an 8 pixel strip)
- Screw Terminals (optional but handy)
- Lots of cardboard
- Piezo Buzzer
- Arcade button or push button
- Lots of male to female jumper wires
- 2 x Female to female jumper wires
- 4 x AA battery box and batteries
Cardboard, it is the most plentiful, easiest to work with, and adaptable material available to anyone. Your local grocery store will give you their cardboard, but what can we do with it? Well Mythbuster Adam Savage made a suit of armour when he was a child, and while our project is not as advanced as that, it does involve a knight…a Jedi Knight!
By creating this project we will learn:
- How to connect components to the micro:bit
- How a servo
- How to generate sound
- How a loop works
- How a conditional statement works
- How neopixels work
Getting started: building the hardware
Building the project requires a little electronics, and plenty of cardboard. For this stage of the project we shall concentrate on the electronics as we need to know that they are working before we install them into the cardboard frame.
To make these connections we need to use the Kitronik breakout board which will break out all of the GPIO pins present on the micro:bit ready for use with a breadboard. Insert the micro:bit into the breakout board so that it is firmly in place.
Now let’s use some female to male jumper wire to connect the Ground (GND) of the micro:bit to the GND (-) rail of the breadboard. Now connect the battery back GND (Black) wire to the same rail. Then connect the Red 5V wire to the (+) rail. We have now established a common ground which means our devices will all exist in the same circuit. The next component to connect is the piezo buzzer. The red wire of the buzzer should be inserted into a spare place in the breadboard, and then a male to female jumper wire used to connect the red wire to pin 0 of the micro:bit breakout board. The black wire of the buzzer can be connected to the (-) rail of the breadboard.
The next connection is made to the neopixels, and they have three pins. Vcc is connected to the (+) rail of the breadboard via two male to female jumper wires connected together. The GND pins connects to the (-) rail using two more male to female jumper wires. The DIN pin, which means Data In, connects to pin 1 on the breakout board using one male to female wire, and one female to female wire.
Our servo connects to the breadboard (+) and (-) but the colour of their wires is a little different. In this case red connects to (+) and brown to (-). The yellow pin connects to pin2 of the breakout board using one male to female wire, and one female to female wire.
The final connection to make is the button used to trigger the device to life, and this is an arcade push button but you can also use a standard push button on the breadboard. The button connects to pin 13 of the breakout board and to the 3V pin. The button does not have a polarity so either pin can be used.
Writing the code
We start the code for this project by creating a link in the code to our neopixels. For this we need to go to Variables and create a new variable called “pixels” then we need to drag “set item to” and place it inside the “on start” loop. Change the “item” option to “pixels”, this is something that we need to keep an eye on. Next we need to tell the micro:bit where the neopixels are connected, and for that we need the neopixel blocks. These are not installed as standard and require us to go to the “Add Package” option and install the Neopixels library.
Now we need the “Neopixel at pin… with …leds as” block from Neopixel. Attach this to the previous block and configure the block to say that our neopixels are on pin 1 and that we have eight of them (change this to match the number that you have)
Now let’s move on and start coding the functionality that will be triggered at the press of the button. Inside the “forever” loop we need to place a conditional test called “if”. This test will check to see if our button has been pressed. The if block can be found in the “Logic” menu and it is the top option. Drag it inside the forever loop and then from the Logic menu drag the “0 = 0” block and connect it to the “If” block. This will form our test. The test will be to see if the button has been pressed. When it is pressed the button connects the 3V pin of the micro:bit to pin 13, and it essentially turns that pin on (otherwise known as high) and returns a value of “1” telling us the pin in on. To check this we need to go to the Pins menu and drag “digital read pin P0” in to the first “0” of the “0 = 0” block, next manually change the second 0 to 1.
So we have the button press ready, next we need to tell the micro:bit that we would like the following code to runs three times. So we use the “repeat 3 times” block from the Loops menu, place this inside the “if” condition test. In order to use the servo, we need to tell the micro:bit how far it needs to turn. The Servo blocks are located in the Pins menu and we need the “servo write pin P0 to 180” block. Drag this and place it inside the repeat 3 times loop. Change the P0 to P2 as our servo is on Pin 2. Now go to the Basic menu and drag the “pause (ms) 100” block and place it under the servo block and still inside the if condition. Change the value to 1000 which means 1 second. Your code should look like this.
So now our servo will move 180 degrees and wait for 1 second, but in the next step we shall ignite Luke’s lightsaber ready to battle the Sith! For this we need to go to the Neopixel menu and select “item show color red” and place this under the previous blocks and still in the if conditional. Change the “item” option to our variable called “pixels” and change red to another colour (Sith lightsabers are red, but Jedi have blue, green, purple and even a white lightsaber!) Still inside the Neopixel menu drag the “item show” block and place it under the previous neopixel block, remember to change “item” to “pixels” Why do we do this? Well our neopixels need to be explicitly told to show a colour change otherwise they won’t do it.
Luke is ready for battle, but he needs help. So we sound the alarm, which in this case is out piezo buzzer. From the music blocks drag the “play tone” block and place it under the previous blocks but still inside the repeat 3 times loop. Change the tone to any note that you like, we chose a Low C as is sounded like an alarm. We also changed the temp of the note so that it plays for half a beat, making it quicker. Carrying on we use another “servo write pin” block from Pins to instruct our servo on Pin 2 to rotate to 0 degrees, causing Luke’s head to spin round, we then pause for a further 1 second using the “pause (ms)” block from Basic.
The last two blocks inside the repeat 3 times loop are from the Neopixel menu and they are “item clear” and “Item show”, drag them both into the code and remember to change “item” to “pixels” These blocks will clear the neopixels and turn off Luke’s lightsaber.
We are now outside the “repeat 3 times” loop, but still inside the “if conditional” and here we shall use another “servo write pin” to tell the servo connected to Pin 2 that we would like it to return to 90 degrees, effectively returning Luke’s head to its normal position. Lastly we use another “pause (ms)” from the Basic menu and change the value to 1000 to give us a one second pause.
With the code completed, name the file “Sith-Alarm” in the box at the bottom centre of the screen. Then click on Download to download the code to your computer. Insert you micro:bit via the USB port and it will appear as a USB flash drive. Drag and drop the code that we have downloaded onto the micro:bit and it will write the code to the micro:bit and after a few seconds be ready for use. Make sure your battery pack is switched on and there are batteries in it. Now press the button and watch as Luke jumps to our rescue!
Congratulations you have learnt how to interface your micro:bit with a servo, button and neopixels, and defended us all from the evil Galactic Empire.
How can we take this further?
In this project we used a button as a trigger, but if we wanted to automate the alarm we could easily use an infrared sensor which would detect movement in the vicinity. There are also doppler radar sensors that work like infrared but use radio waves to detect movement…even through walls!
Types of Sensors
Also In The June 2018 Issue
Stuck in a maze? Not anymore! Here are tips and tricks for beating the most convoluted labyrinths.
Code up your digital turtle mascot and watch him dash around the screen in this simple Python coding activity.
A phone and tablet app exposes the invisible waves that connect our computers.
How AI technology is helping fans keep the magic alive for one more chapter.
Use Scratch to become the architect of your very own digital metropolis.
It’s free, open source, and comes with a snazzy graphics and physics engine for both 2D and 3D games.
Use SketchUp to create dizzying patterns and shapes, Escher-style.
Whiz around your computer’s folders and modify files at lightning speed like a pro.
Why use three languages to make a stunning web page when you can use just one?
Use micro:bit and cardboard to create a Jedi knight that sounds the alarm when evil approaches!
Learn about the infamous Enigma machine and how its “unbreakable” code was finally defeated.
Take your 3D-printed gizmos to the next level with harder, sleeker, and stronger material.
How 3D printers are helping us learn more about prehistoric reptiles.
How daily coding puzzles with constant feedback can be a useful tool to help students master text-based languages.
Scientists draw inspiration from nature to create remarkable specialized robots.
Find it, mine it, and use it to automate machines.
Links from the bottom of all the June 2018 articles, collected in one place for you to print, share, or bookmark.
Interesting stories about computer science, software programming, and technology for June 2018.