杏吧原创

Electronics projects for beginners: How to make a roaming robot

Using a BBC micro:bit, motors and some sensors, here's how to make an autonomous robot that can roam around and avoid objects in its path

homemade robot

New stuff you need

Second continuous rotation servo

2 x wheels to fit servos

2 x long-arm limit switches

Battery box (to hold 4 x AA batteries)

micro:bit edge connector (optional)

Robot chassis or CD case

WHAT is twice as fun as a motor? Two motors. Last week, we learned how to meet their power-hungry needs. Now, we can double up 鈥 and because the micro:bit can control them, we can make a robot that roams.

First, give your robot a body. I used a pre-made chassis, but you can use cardboard, a CD case or even a flip-flop. Add the wheels to the servos and attach them to your body. Then, take the breadboard and wire the servos like last week 鈥 red to power (the red rail), brown to ground (the blue rail).

The yellow wires go to micro:bit pins 2 and 3. I attached the left servo to pin 2 and the right to 3. Pin 3 is the skinny gold strip to the left of 0. You can attach a wire to pin 3 with some conductive paint, solder or an edge connector, which lets the micro:bit slot into the breadboard with each pin to a row.

Your new battery case will connect to the power and ground rails on the breadboard. Don鈥檛 forget to wire the breadboard ground to the micro:bit ground.

Next, limit switches. The long arms act like whiskers, letting your robot sense objects. The pressure of a collision clicks the switch. They usually have three 鈥渓egs鈥. Connect the one called C to ground and NO to micro:bit pins 0 and 1, then stick them either side of the front of your robot, so the arms protrude.

In the micro:bit code editor, clip two 鈥渟ervo write pin鈥 blocks into 鈥渇orever鈥. Make sure they are set to pins 2 and 3 (P2 and P3), then add values to make them go forward. To remind you: 0 is full speed ahead, 180 is reverse and 90 is still.

To add some autonomy, grab an 鈥渋f \<true\>鈥 block from 鈥淟ogic鈥 and clip it into 鈥渇orever鈥, too. Then, from 鈥淚nput鈥, take 鈥減in P0 is pressed鈥 and put it in 鈥淺<true\>鈥.

Assuming P0 is the left switch, when it is pressed 鈥 if the bot hits an obstacle to its left 鈥 first the bot should reverse. Inside your 鈥渋f 鈥 block, stick two more 鈥渟ervo write pin鈥 blocks for P2 and P3 and set them to go backwards. From 鈥淏asic鈥, add a 鈥減ause (ms)鈥 below them. This sets the duration of the movement, which in this case is how long to go backwards.

We then want the robot to turn right, avoiding the obstacle on its left. To do this, it first needs to stop the left wheel and drive the right backwards. Below the previous 鈥減ause鈥, add two more servo blocks and another pause to do so.

Underneath, adapt another 鈥渋f \<true\>鈥 block for the right switch and copy the contents from above, but change P0 to P1 and turn left instead of right. Upload your code, attach the micro:bit battery and tweak the durations and speeds to optimise. If the coding is tricky, go online .

I stuck a paintbrush to my robot and let it sweep the floor. You can build mazes for it or keep it as a roaming electronic pet. Whatever you decide, let us know.


For next week

2 x line sensors

Next in the series

1 Introduction

2 Electric candle

3 Toast notifier

4 Desktop traffic light

5 Propeller car

6 Magic 8 Ball

7 Theremin

8 Sound-sensitive disco ball

9 Rubbish sweeper

10 Biscuit bot
A line-following, cookie-carrying robot

To download a printable version of this page click here

Thanks to Imperial College Advanced Hackspace for use of their facilities

Projects so far and a full list of kit required are at
Email: maker@newscientist.com

Topics: Electronics