
How to create a classic magic trick using two micro:bits and some cardboard.
Author
Derek Graham
Derek is the Principal Developer at Sage UK and STEM ambassador specialising in the micro:bit. @deejaygraham on twitter.
Recently I gave a talk about the BBC micro:bit and needed to come up a good demonstration without being too technical: something that was short, memorable and showed off the full range of what it could do. Finally, I hit on the idea of a magic trick.
The Plan
I had in mind a card trick, a bit like you would see on tv, where the magician will ask a volunteer to pick a card, perhaps show it to the rest of the audience but keep it secret from the magician himself. The magician then concentrates for a minute for dramatic effect before guessing the correct card to everyone’s amazement. That’s the version of the trick we are most familiar with. I wanted to create a digital equivalent using some micro:bits.
The Trick
In my version, I introduced my magic card-guessing robot which would speak aloud the name of a card someone would pick at random. I then asked for a volunteer from the audience and handed them a micro:bit, explaining that they could press the ‘A’ button to select an electronic card at random and the name of the card would scroll across the display. They could change their mind as many times as they wanted and when they were satisfied, they could lock in their selection using the ‘B’ button. It’s then over to the magic card-guessing robot to finish the trick by announcing which card they had selected.
The Robot

I built the robot out of some thick cardboard recycled from a box. I added legs and feet and drew a face on the front. Inside I used a hot glue gun and Velcro to mount a micro:bit in an edge connector, a battery pack and a mini rechargeable speaker. I used Velcro on the edges of the box to allow me to open it easier to switch on the power to the speaker and the micro:bit and to allow me to modify wiring or re-flash the micro:bit with new code.

The robot uses the radio to listen for messages coming from the other micro:bit and uses the speech synthesiser to announce the card using the rechargeable speaker. I added introduction and announcement functions with some randomisation of the text to keep things interesting for the audience. Because this is a magic trick, I couldn’t resist calling the robot Robot Houdin in honour of the great magician Jean Robert-Houdin who was the inspiration for Harry Houdini. Please note that some of the text was modified to make sure that the words from the voice synthesiser were clearer for the audience.
from microbit import *
import radio
import speech
import random
def say(sentence):
speech.say(sentence, speed=120, pitch=100, throat=190, mouth=190)
def introduce_trick():
introduction = [
'Hello',
'I am Robot Who Dan, the Magic Robot.',
'I am now ready to magic'
]
for sentence in introduction:
say(sentence)
sleep(1000)
def guess_card(card_name):
beginning = [
'I think your card was the, ',
'Was your card the, ',
'The card you picked was the, ',
'abra cad abra'
]
ending = [
'was that correct ?',
'did I get it right ?',
'thank you!'
]
sentence = [random.choice(beginning), card_name, random.choice(ending)]
for part in sentence:
say(part)
sleep(250)
introduce_trick()
radio.on()
card_name = ''
while True:
sleep(250)
message = radio.receive()
if message:
if message == 'announce':
if card_name:
guess_card(card_name)
else:
say('I do not have a guess yet')
else:
card_name = message
The Card Selector
The second micro:bit is used by the audience volunteer to select a card at random. Each time the A button is pressed, a different random selection is made from a list of all of the card names and the four suits to form a single card selection e.g. the four of clubs.
The Secret
Here’s the secret bit I only explain after the demonstration. After the card is selected using the A button we silently broadcast the card name to the robot using the radio. Each time a new card is selected, that selection is sent to the robot. When the volunteer is asked to ‘lock-in’ their selection by pressing the B button, that actually sends another message over the radio to cause the robot to announce the most recent card selected.
from microbit import *
import random
import radio
cards = [
["A", "Ace"], ["1", "One"],
["2", "Two"], ["3", "Three"],
["4", "Four"], ["5", "Five"],
["6", "Six"], ["7", "Seven"],
["8", "Eight"], ["9", "Nine"],
["10", "Ten"], ["J", "Jack"],
["Q", "Queen"], ["K", "King"]
]
suits = [
["H", "Harts"], ["C", "Clubs"],
["S", "Spades"], ["D", "Die a monds"]
]
def show_startup_screen():
display.show(Image.RABBIT)
sleep(1000)
display.clear()
sleep(1000)
def broadcast_selected_card(card, suit):
# this message will be read aloud, so make sure it is english.
message = ' '.join([card[1], 'of', suit[1]])
radio.send(message)
show_startup_screen()
radio.on()
display.scroll('pick a card, any card')
selected_card = ''
while True:
if selected_card:
display.scroll(selected_card)
# pick a card at random
if button_a.was_pressed():
card = random.choice(cards)
suit = random.choice(suits)
broadcast_selected_card(card, suit)
selected_card = card[0] + ' of ' + suit[0]
# prompt robot to announce the "guess"
if button_b.was_pressed():
radio.send('announce')
Again, notice that some of the card suits are spelt strangely, this is because we are sending the words to the robot to speak aloud and some of the words didn’t sound correct over the speaker when they were spelt correctly.
Further improvements
The demonstration was well received and we went on to discuss the parts that made up the trick and how the micro:bits worked together to make the trick possible. There are a few improvements I would make to the robot to make the trick more polished. The robot definitely needs a new, high-quality set of batteries to work correctly using an ordinary AAA battery pack. I found that the power needed for the speech synth to drive a speaker and having the radio on all the time meant that any slightly worn batteries would allow the robot to start off driving the speaker but would the voltage would dip after a few seconds and reset the device so it only got half of the trick done. With new batteries (or with a beefier battery pack) it worked flawlessly. I did some wiring for the robot to have LED eyes that would light up when it spoke but again I found that the power requirements were a bit too much when practicing the trick. A better power supply would allow me to incorporate that as another extra in the trick.