For an upcoming day at work I’m trying to come up with ideas of what students could make with a micro:bit, a button, some lights and a servo motor.
Here we have: the micro:bit love meter!
#microbit love meter using a #button, #neopixels and a #servo. I never win on the 1st go! @microbit_edu pic.twitter.com/FLes6JoR8N
— Lorraine Underwood (@LMcUnderwood) February 21, 2017
Build
I used the following equipment. I’m not paid to advertise any of this.
- 180 degree servo from Kitroniks https://www.kitronik.co.uk/2565-tower-pro-sg90-mini-servo.html
- Adafruit breadboard friendly Neopixels from CPC http://cpc.farnell.com/adafruit-industries/1558/rgb-smart-neopixel-for-breadbrds/dp/MK00267
- Arcade buttons from Pimoroni https://shop.pimoroni.com/products/colourful-arcade-buttons
- Female to female jumper wires x 6
- Male to female jumper wires x 5
- Crocodile clips x 2
Servo
The servo has three wires which end in a female connection.
- Plug the male end of the male to female jumper wires into each one.
- Plug the female end of the wires into the micro:bit edge connector pins like so:
- Ground (Brown) to 0V
- Power (Red) to 3V
- Data (Orange) to Pin 1
(PICTURE Coming Soon!)
Button
The arcade button has two connections.
- Clip a crocodile clip onto each connection
- Clip the male end of a male to female jumper wire to the end of each crocodile clip
- Plug the female end of the wires into the micro:bit edge connector pins
- 0V
- Pin 12



Neopixels
The neopixels have three pins on each side. On the side with IN written on it:
- Connect a female to female jumper wire to each pin
- Plug the female end of the wires into the micro:bit edge connector pins like so:
- + to 3V
- G to 0V
- In to Pin 0
- Chain another neopixels to the first using female to female jumper wires
- + to +
- G to G
- O to In


Code
Before you start you need to add the Package Neopixels by selecting “Add Package” at the bottom of the menu.
- Create a new variable and call it myLights (Variables > Make a Variable)
- Set myLights to RGB Neopixels at P0 using the block below:
You also need to set the button up using the block “set pull pin P12 to up” which you find under: Advanced > Pins
The game starts with the lights on blue.
The program is waiting for the user to press the button. Once the button is pressed a random number between 0 and 179 is chosen. The servo is then sent to that angle.
If the angle is less than 90, the user is successful and the lights go red. If it’s greater than 90 the user is not successful in love and the lights go yellow.
After 2 seconds, the servo goes back to 180 and the lights go blue again.
We do this in a forever loop as we are forever waiting for the user to press the button.