Welcome to my latest project to help demonstrate to students the power of coding and physical computing.
The first video is a quick overview of the project.
The second video is a complete step by step tutorial on how to copy this project and hopefully make it even better.
If you like, then please give the YouTube channel a like and subscribe as well as on here on this site as well!
Step 1: Gather Materials
You will need the following:
1 Micro:bit kit
1 Raspberry Pi
1 Raspberry Pi Camera
1 male/female header cable for GPIO connection
3 Alligator clips
Duct Tape or Packing Tape
Minecraft Steve Head(optional)
Step 2: Test Micro:bit With MakeCode
1. Head to MakeCode.com and choose Micro:bit platform
2. Download a preset image just to make sure the Micro:bit works and you understand how to use the Micro:bit
3. Once you have an image on the Micro:bit and you understand how to download(follow tutorial on website), then we can move to the Raspberry Pi.
Step 3: Setup the Hardware
1. Setup your Raspberry Pi - keyboard, mouse, monitor, and power
2. Plug in the Raspberry Pi camera
3. Maker sure Camera is enabled in Configuration panel
Step 4: Connect Micro:bit and Raspberry Pi
1. Plug Micro:bit to the Raspberry Pi
2. The screen should indicate that it is plugged in and therefore click OK
3. Make sure you see the Micro:bit in the File Manager
4. When plugged in the Micro:bit should be displaying the image from Step 2
Step 5: Update the Pi and Install Mu
1. Open up Terminal on the Raspberry Pi and enter the following to update your Pi
sudo apt-get update
2. We also have to install a programming software called Mu. This is a Micro Python program designed for micro controllers like the Micro:bit. Install in Terminal using the following:
sudo apt-get install mu -y
Step 6: Code Micro:bit in Mu
Open up Mu by going to Menu--> Programming --> Mu
Type in the following code in Mu
from micro:bit import *
display.scroll("Say Cheese in 3,2,1,....!")
Save this code to your Raspberry Pi
Click on Flash to import code to the Micro:bit
Step 7: Wiring the Micro:bit to the Raspberry Pi
Add one alligator clip to Pin 0 on the Micro:bit
Add one alligator clip to Pin 1 on the Micro:bit
Add one alligator clip to GND on the Micro:bit
The alligator clip on Pin 1 needs to be attached to the male/female wire that is clipped to the male lead. The female lead of this wire goes to GPIO pin 4 on the Raspberry Pi
At this point I stick the Pin 0 alligator clip into Play-doh as my button. You can create any type of button you choose, but this is quick and easy.
You can test your Micro:bit if you have flashed the code. Nothing will happen except your message on the Micro:bit. Make sure this works because if your trigger is not working, then the next steps won't work either.
Step 8: Coding in Python
Create a folder to store all the code for this project. You can add the Mu code if you wish to help stay organized.
0. Save this file in your folder. This helps to map pixels to the Minecraft world.
1. Open up Python 3
2. Click New File
3. Save File As whatever name you want.
4. Enter the following code
from picamera import PiCamera
from gpiozero import Button
from mcpi.minecraft import Minecraft
from time import sleep
from skimage import io, color
mc = Minecraft.create()
button = Button(4, pull_up=False)
## Taking a picture with Micro:bit
mc.postToChat("Press the Button!") sleep(2) button.wait_for_press()
with PiCamera() as camera:
camera.resolution = (100,80)
mc.postToChat('fly up in air and look for shadow on the ground')
### load selfie and map
selfie_rgb = io.imread("/home/pi/Desktop/Microbit/selfie.jpg")
map_rgb = io.imread("/home/pi/Desktop/Microbit/colour_map.png")
### Convert to Lab
selfie_lab = color.rgb2lab(selfie_rgb)
map_lab = color.rgb2lab(map_rgb)
### Mapping colours on colour map to Minecraft blocks ### First tuple is coordinates of colour map ### Second tuple is Minecraft block
## Iterate over image and then over map. Find closest colour from map, and then look up that block and place
mc = Minecraft.create()
x, y, z = mc.player.getPos()
for i, selfie_column in enumerate(selfie_lab):
for j, selfie_pixel in enumerate(selfie_column):
distance = 300
for k, map_column in enumerate(map_lab):
for l, map_pixel in enumerate(map_column):
delta = color.deltaE_ciede2000(selfie_pixel,map_pixel)
if delta < distance:
distance = delta
block = colours[(k,l)]
mc.setBlock(x-j, y-i+60, z+5, block, block)
Step 9: Set Everything Up
1. Save all programs.
2. Shut down Pi
3. Wire the Micro:bit and Raspberry Pi inside the Steve head using tape and velcro dots(see video around 19 minute mark)
4. Get everything into position and power up
Step 10: Bringing It All Together
1. Open Up Minecraft
2. Open up Python program
3. Run the Python Program and open up Minecraft World
4. Follow instructions on screen and watch it print your selfie!
Please share your project prints, designs, and setups so I can learn and share with the students I teach.
Stay awesome everyone.