This tutorial will show you how to modify a pet toy to detect shake using the micro:bit.
Step 1: Preparing your supplies
1. Computer (laptop or desktop)
You will need to use a computer with internet access and a USB port to program the micro:bit.
2. Two micro:bits (2) and at least one microUSB cable (1)
You will need two micro:bits (~$12) and at least one USB cable to complete this tutorial. Many retailers sell starter kits such as this one.
3. Battery pack (at least 1)
You will need one battery for the micro:bit that goes inside of the toy. In this tutorial we are using a rechargeable Lithium Ion battery, but you can also use a AA or AAA battery holder.
4. Pet toy
A pet toy of your choosing, I would recommend a stuffed toy since it will be easiest to insert the micro:bit and battery.
5. Scissors and/or utility knife
These will be necessary for making an incision in the pet toy.
6. Glue, tape, sewing kit, etc.
This is for closing the incision on the pet toy.
Step 2: Programming your micro:bits
We will be using two micro:bits in this tutorial. One micro:bit will go inside of the toy and do the actual shake detection, and the other micro:bit will be the one that displays us information and/or does something when a shake is detected. In order to do this, we will be using the micro:bit radio features.
By the end of this tutorial you will have shake sensing code on the micro:bit inside of the toy that sends a message to the other micro:bit that displays "Hi!" on the LED screen when a shake is detected.
Go to https://makecode.microbit.org/ to use the blocks based programming editor for the micro:bit. If you are unfamiliar with micro:bit programming you may want to visit the official Getting Started page.
Step 2.1: micro:bit sending code (for the micro:bit that goes inside the toy)
First let's write the code for the micro:bit going inside of the toy. Let's call this the SENDER micro:bit since it will be sending messages to the other micro:bit. I recommend labeling each of the micro:bits so you remember which code is on which one. Label this one SENDER, and the other micro:bit RECEIVER.
From the Basic block category select the on start block. The on start block is what runs one time when the micro:bit is turned on.
Now let's look inside the Radio block category. Select the radio set group block and place it inside of the on start block. The radio set group chooses what channel the micro:bit's radio will communicate over. Imagine you had two walkie-talkies and you are trying to talk to your friend -- you have to be on the same channel as each other for it to work. This is the same concept. In this example I set my group to 5, but you can choose any number from 1-255 as long as you make sure to set your group to be the same value on both of your micro:bits!
Now, let's open the Input block category. Select the on shake block. This block will be activated when the micro:bit detects that it has been shaken. Let's open the Radio block category again, but this time select the radio send number block and place it inside the on shake block. Now when you shake the micro:bit it will send a number over the radio. For this tutorial the number you send is arbitrary (it is 0 by default), but in more complicated examples you may want to send a specific number.
Now our micro:bit toy code is complete! You can see the program below and download it there if you'd like. Once you've downloaded the code, upload it to the micro:bit we labeled as the SENDER.
Step 2.2: micro:bit receiving code
Now let's program the second micro:bit that will be receiving messages from the micro:bit inside the toy.
Like we did in Step 2.1, go to the Basic block category and select the on start block. Now go to the Radio block category and select the radio set group block and place it inside of the on start block. Make sure you set the group number to be the same number that you used in the code from Step 2.1 otherwise this won't work!
Now, go back to the Radio block category and select the on radio received receivedNumber block. This code will run whenever the micro:bit receives a number over the radio. If you recall, in step 2.1 we wrote a program to send a number whenever a shake is detected; this means that whenever a shake is detected on the micro:bit inside the toy, this on radio received code will be run. Go back to the Basic block category and select the show string "Hello!" block. You can change "Hello!" to whatever you like. I recommend keeping it short since it takes a while for the text to scroll across the micro:bit's screen.
Now our micro:bit receiving code is complete! You can see the program below and download it there if you'd like. Once you've downloaded the code, upload it to the micro:bit we labeled as the RECEIVER.
Step 3: Insert 1 micro:bit into the toy
In this step we'll take the micro:bit labeled "SENDER" and insert it inside the toy. Make an incision either using scissors or a knife on the toy. On mine the best place to do it was on the mouse's belly, on yours it might be different. Once I made the incision, a lot of stuffing started coming out. Instead of pulling it out I cut off the excess with some scissors so that I didn't accidentally remove too much. The toy I used in this tutorial already had a vibrating motor inside of it, so I carefully removed it. Once it looked like there was enough space for the micro:bit and battery I carefully inserted it (make sure you connect the battery first).
You might need to wiggle the micro:bit around to get it to fit nicely. You may want to glue, tape, or sew the toy back together, but I would recommend testing briefly before doing this.
Step 4: Test and play!
Make sure both micro:bits are powered on and then give the toy a good shake, you should see "Hi!" scroll across the LED screen on the other micro:bit.