
Cosmo Robot
A companion robot that reacts with faces, sounds and moves
What you'll build
Cosmo is a little companion robot with sensor eyes and two arms. It's always paying attention: pat the T1 pad and it cheers, waving both arms; bring your hand close to its eyes and it jumps back in surprise; leave it alone for a while and it gets bored and falls asleep. A companion robot feels alive because it reacts to you in different ways and has something to do when nothing is happening. You'll build all three of Cosmo's moods into one loop.
What you'll learn
- Reacting to two different sensors in one loop
- Counting time with a variable to notice when nothing is happening
- Moving two servos together, even when they face opposite ways
- Giving a robot a personality with faces, sounds and moves
You'll need
- Mieo board
- Two N20 motors and their wheels
- Battery
- 3D-printed mountings
- Caster wheel
- Screws and screwdriver
- Ultrasonic distance sensor and its cable
- Two servo motors, from the MIEO kit
- Cosmo body and arm parts, from the Mieo Pro kit
- A USB cable, or Bluetooth on your computer
- The Robolab app (free)
Stay safe
- Ask an adult to help with the small screws.
- Lift MIEO's wheels off the table before you upload a driving program, so it can't drive off the edge.
- Plug extra parts only into the connector this page names. Wiring a part to the wrong connector can damage it for good.
Build it
- Build Cosmo's body from your Mieo Pro kit, with both N20 motors, their wheels, the caster wheel and the battery, as the Mieo Pro build guide shows.
- With MIEO switched off, plug the left arm servo into S1 and the right arm servo into S2, the right way round. Plug the ultrasonic sensor's Echo pin into D1 and Trig pin into D2, with its power and ground pins on the matching pins.
- Mount the sensor on top as Cosmo's eyes, looking forward.
- Upload the program once before fitting the arms. Then fit both arms hanging straight down.
- Switch Cosmo on and connect it to your computer with a USB cable or Bluetooth.
Code it
- Open Robolab, choose Mieo, connect and set the switch at the top right to Upload. Make two variables, distance and idle.
- Start with “when mieo starts up”, “connect ultrasonic 1 to echo D1 , trig D2”, arms down (Servo 1 to 30 and Servo 2 to 150), “set idle to 0” and “show happy”.
- Add a “forever” loop with “set distance to (get ultrasonic 1 distance (cm))”, then “if … else” with “is T1 touched?”.
- Cheer: in the top part, “set idle to 0”, “show happy animation”, “play sound happy”, arms up (Servo 1 to 150 and Servo 2 to 30), “wait 0.3 seconds”, then arms down again. The servos face opposite ways, so their numbers swap.
- In the else part, add “if … else” with “distance > 0 and distance < 10”. Surprise: “set idle to 0”, “show surprise”, “play sound chirp” and “go backward at 40 % speed for 0.3 seconds”.
- Bored: in its else part, “change idle by 1”, then “if idle = 100 then” with “show sleep” and “play sound sleep”.
- Finish the loop with “wait 0.1 seconds”. Lift the wheels, upload and say hello to Cosmo.
Upload mode: Robolab turns your blocks into MicroPython and sends them to MIEO over USB or Bluetooth. MIEO then runs the program by itself, even after you unplug it.

when mieo starts up
connect ultrasonic [1 v] to echo [D1 v] , trig [D2 v]
set servo on Servo [1 v] to (30) angle
set servo on Servo [2 v] to (150) angle
set [idle v] to (0)
show [happy v]
forever
set [distance v] to (get ultrasonic [1 v] distance (cm))
if <is [T1 v] touched?> then
set [idle v] to (0)
show [happy v] animation
play sound [happy v]
set servo on Servo [1 v] to (150) angle
set servo on Servo [2 v] to (30) angle
wait (0.3) seconds
set servo on Servo [1 v] to (30) angle
set servo on Servo [2 v] to (150) angle
else
if <<(distance) > (0)> and <(distance) < (10)>> then
set [idle v] to (0)
show [surprise v]
play sound [chirp v]
go [backward v] at (40) % speed for (0.3) seconds
else
change [idle v] by (1)
if <(idle) = (100)> then
show [sleep v]
play sound [sleep v]
end
end
end
wait (0.1) seconds
endSee the MicroPython
# generated by Robolab
import mieo
idle = 0
distance = 0
mieo.connectultrasonic(1, 'D1', 'D2')
mieo.setservo('S1', 30)
mieo.setservo('S2', 150)
idle = 0
mieo.showemotion('happy')
while True:
distance = mieo.ultrasonic(1)
if mieo.touched('T1'):
idle = 0
mieo.showanimation('happy')
mieo.playsound('happy')
mieo.setservo('S1', 150)
mieo.setservo('S2', 30)
mieo.sleep(0.3 * 1000)
mieo.setservo('S1', 30)
mieo.setservo('S2', 150)
else:
if distance > 0 and distance < 10:
idle = 0
mieo.showemotion('surprise')
mieo.playsound('chirp')
mieo.run('backward', 40)
mieo.sleep(0.3 * 1000)
mieo.stoprobot()
else:
idle += 1
if idle == 100:
mieo.showemotion('sleep')
mieo.playsound('sleep')
mieo.sleep(0.1 * 1000)
Test it
Cosmo smiles with its arms down. Pat T1 and it plays its happy animation and sound and throws both arms up for a cheer. Move your hand close to its eyes and it looks surprised, chirps and hops backwards. Leave it alone for about ten seconds and it falls asleep, until you pat it or come close again.
If it doesn't work
- One arm goes up while the other goes down? That arm's servo faces the other way to the one this page expects. Swap its two numbers, 30 and 150.
- Cosmo falls asleep too soon, or never? Change 100 in “if idle = 100”: each count is a little over a tenth of a second.
- It keeps jumping back with nothing near? The sensor may see the table edge. Point the eyes slightly up, or change 10 to 6.
Make it your own
- Easy: choose different faces and sounds for each of Cosmo's moods.
- Medium: use button R to play a short tune, so Cosmo can sing for you.
- Hard: give Cosmo a grumpy mood: if it's surprised three times in a row, it shows angry and turns away from you.
What to build next
Remote control
Bluetooth Controlled Jeep
Build a four-wheel jeep and drive it from your keyboard
Remote control
Gamepad Controlled Robot
Drive with W, A, S and D like a gamepad, using your keyboard
Robot arms
Remote Servo Arm
Raise and lower a servo arm with your keyboard