Introduction: Obstacle Avoiding LEGO Robot

About: I used to teach middle school science, but now I run my own online educational science website. I spend my days designing new projects for students and Makers to put together.

We love LEGO and we also love Crazy Circuits so we wanted to combine the two into a simple and fun robot that can avoid running into walls and other objects. We'll show you how we built ours, and outline the basics needed so you can build your own. Your version might not match ours exactly, and that's okay.

Below is a list of the Electronic Parts we used, and a list of the LEGO Parts we used. Your parts may vary, so don't be afraid to do your own thing.

If you like our projects and want to see more of what we get up to each week please follow us on Instagram, Twitter, Facebook, and YouTube.


Brown Dog Gadgets does in fact sell kits and supplies, but you don't need to buy anything from us to make this project. Though if you do it does help support us in creating new projects and teacher resources.

Electronic Parts:

1 x Crazy Circuits Robotics Board

2 x LEGO Compatible Continuous Rotation 360 Degree Servo

1 x HC-SR04 Ultrasonic Distance Sensor

4 x Dupont Female to Female Wires

1 x USB Power Bank

(We found a small USB Power Bank that fit into our robot nicely. You may need to design your robot to fit the USB Power Bank you have on hand, or you can also use a your own battery pack.)

LEGO Parts:

We used a variety of parts but you should feel free to build yours however you see fit, using whatever LEGO parts you have on-hand. The important things you’ll need to do is have a way to mount the servos on the bottom, the ultrasonic sensor so it can point to the front, and some way to hold the Robotics Board and power source in place. In a pinch you can use some tape or rubber bands to mount things where needed. We have provided links to each part on BrickOwl but you can find them anywhere LEGO or LEGO-compatible parts are sold.

2 x LEGO Wedge Belt Wheel (4185 / 49750)

1 x LEGO EV3 Technic Ball Pivots Set 5003245

1 x LEGO Technic Cross Block Beam 3 with Four Pins (48989 / 65489)

1 x LEGO Technic Brick 1 x 6 with Holes (3894)

2 x LEGO Axle 4 with End Stop (87083)

4 x LEGO Half Bushing (32123 / 42136)

4 x LEGO Brick 2 x 2 Round (3941 / 6143)

1 x LEGO Plate 6 x 12 (3028)

Step 1: Build Your LEGO Base

We started with a 6 x 12 LEGO Base, which was the smallest we were able to build with. You can go larger if desired but smaller may be a challenge.

The width of our robot was determined by the USB Power Bank we had, since we needed to be able to slide it into place. A larger battery might require a larger robot.

Make your base tall enough to accommodate the battery and leave room above it to fit the Robotics Board.

Step 2: Add Wheels

Each servo motor will need to get mounted to the bottom of your robot base.

We ended up using these parts to do so:

  • LEGO Axle 4 with End Stop (87083)
  • LEGO Half Bushing (32123 / 42136)
  • LEGO Brick 2 x 2 Round (3941 / 6143)

You'll need 4 of each part to mount the 2 servos.

Once mounted you can add the wheel, which is LEGO Wedge Belt Wheel (4185 / 49750).

Like other LEGO builds, there are many options! The above servo/wheel mount is what worked for us, but you can try something different.

Step 3: Add Caster Wheel

Our caster wheel allows our robot to roll, powered by the two wheels attached to the servos, with the caster acting as the "third wheel" so our robot can pivot and move easily.

These are the parts we used for our caster wheel attachment:

  • LEGO EV3 Technic Ball Pivots Set 5003245
  • LEGO Technic Cross Block Beam 3 with Four Pins (48989 / 65489)
  • LEGO Technic Brick 1 x 6 with Holes (3894)

In an earlier version of our robot we just used a few round LEGO pieces as a "leg" and those work fine on a smooth surface like a table, but do not work well on carpeting or a non-smooth floor. If you don't have a caster wheel handy, consider the "leg" option.

Step 4: Add Distance Sensor

We'll want to mount the ultrasonic distance sensor on the front of the robot so it can "see" where it is going, and know when to stop before hitting an obstacle.

We 3D printed a LEGO-compatible holder for the ultrasonic sensor. You can find the file on Thingiverse if you want to use it:

If you do not have access to a 3D printer, you can fashion a way of holding the sensor in place using some LEGO pieces, tape, rubber bands, zip ties, or some other method. The important thing is that it should point towards where the robot is going when it is moving forward.

Step 5: Add Robotics Board

The Robotics Board is the brains of this operation. It's meant to sit on top of LEGO bricks so mounting it is simple.

Typically the Robotics Board is use with conductive tape to build circuits directly on top of LEGOs, but since we're just using two servos and a distance sensor, we can plug those directly into the header pins on the board.

We'll want to orient the board so that you can easily plug in the USB cable for power. (We were lucky to find a very short USB cable in our "Giant Bin of Random Cables")

You can now plug in the sensor and the servos!

For the sensor you'll need to connect the echo pin to pin 3 on the Robotics board, then connect the trigger pin to pin 5, then VCC to 5V and Gnd to GND. This will power the sensor and allow it to talk to the Robotics Board.

Next you'll need to attach each servo connector. They are easy to plug in, just make sure that the brown wires connect to GND, the red wires connect to 5V, and the orange wires connect to pin D6 for for left servo, and D9 for the right servo.

Step 6: Program the Robotics Board

Before our robot will work you'll need to upload code to the Robotics Board. If you've not done so already, make sure you have the latest version of the free Arduino IDE software installed on your computer.

Our code is found at our GitHub repo, which you can find here:

The code is simple, and had been commented heavily to help explain what everything does.

You will also need the NewPing library, which can be found here:

Step 7: Let Your Robot Roam

Once you've got your robot built, and the code has been uploaded to the Robotics Board, you can test it out!

The simplest way is by plugging in the USB Power Bank and letting your robot start to roll forward. If you put your hand in front of it, it should back up, turn, and then move forward again. (Don't let it roll off of a table!)

We built a simple hexagonal cardboard "arena" for our robot to roll around in using an old cardboard box. Feel free to get creative with what you have on hand.

Step 8: Go Further

Below are some questions and an additional activity if you want to go a bit further with this project.


What did you learn when building your robot?

What determined your choices in LEGO parts used?

Would your robot roll faster if it had larger wheels?

Additional Activity

There are two variables in the code (shown below) you can adjust that will change the amount of time the robot runs when it backs up and then turns to avoid a wall. Feel free to change the goBackwardTime and turnRightTime and see how the affect the actions of the robot. Remember, when you make changes to your code you will need to re-upload it to your robot.

// set how many milliseconds your robot will move backwards for

int goBackwardTime = 1000;

// set how many milliseconds your robot will turn for

int turnRightTime = 1000;

(Note: 1000 milliseconds is equal to 1 second.)

We hope you enjoyed our Crazy Circuits Avoidance Robot, and that you got to build your own. We had fun building ours and sharing it with you!

Arduino Contest 2020

Participated in the
Arduino Contest 2020