How to use the Ultrasonic Sensor with the Catapult built from EV3/NXT (Part 5)

This video lesson shows a very interesting specific example on how to use the LEGO Mindstorms EV3 Ultrasonic Sensor. The sensor detects if the catapult is loaded or if we have just fired an element. At the end of the lesson I also offer an interesting challenge that you could use in your classroom or at home.

  • #69
  • 01 Feb 2015
  • 7:52

Ultrasonic Sensor

The sensor detects distance in centimeters or in inches. When the catapult is loaded the distance between the sensor and the base of the robot is about 7-9 centimeters (~3 inches). 

It is more interesting however that after adding the sensor we should balance the nest holding the brick. Many novice robot builders make the mistake of adding parts, sensors and motors only on one side of the robot without thinking about how to balance it.

The challenge

The challenge from this video is.

How to calculate the speed at which the nest falls? (Using those calculations in some of the next video lessons we would calculate the speed of the parts fired from the catapult). Use the knowledge from:

How to measure the speed of the robot

Previous episodes from the series:

Catapult build from LEGO Mindstorms EV3/NXT (Part 4 - EV3 clutch and loading)

Catapult build from LEGO Mindstorms EV3/NXT (Part 3 - Automatic loading)

Catapult build from LEGO Mindstorms EV3/NXT (Part 2 - Base)

Catapult build from LEGO Mindstorms EV3/NXT (Part 1)

Download the program developed in the video lesson

Building instructions for EV3 Mindstorms Catapult Robot with Ultrasonic Sensor are given below

Catapult Frame - LEGO Mindstorms construction for a frame of a catapult

This frame holds the motor and ev3 brick used in the catapult. The frame is placed on top of the rotating base and thus could be easily rotated. The brick could be both NXT and EV3

Catapult Rotating Base - LEGO Mindstorms construction for a base for a catapult

This is the initial construction of a catapult. The base on which we will place the rest of the robot. The goal of this construction is to give the catapult the ability to rotate.

Catapult Full - Full catapult from LEGO Mindstorms with a Motor and a Touch Sensor

These are building instruction for a working automatic catapult with a motor and a touch sensor.

EV3-G programs form running the catapult with ultrasonic sensor

Courses with this episode

The episode is used in the following courses where you can find additional tasks for it to use in class

A robot a "day" keeps the questions awake. Part 1

Use the ultrasonic sensor to measure the speed of falling objects. Use the task in your classroom, for your team or at home to engage students in learning. 

There is 1 task from a middle complexity giving 2 points.

Complexity 2 (2+ points)

  • Using the Ultrasonic Sensor measure the speed at which the nest falls? (Using those calculations in some of the next video lessons we would calculate the speed of the parts fired from the catapult)

Use the knowledge from: 

How to measure the speed of the robot


Prepare a video of the achieved result. You can capture the video with your phone or even with your webcam. 

Send your files for evaluation to with subject "069 Tasks"


In the previous episode of the series on how you can construct your own catapult we finished with a robot that can load and fire small Lego parts completely automatically. A very interesting gear system with a clutch transfers the power for loading the catapult from the EV3 motor to the axle. The main challenge was to find out when the motor should stop. In the previous episode we used a touch sensor to detect this. In this episode I would like to introduce you to the ultrasonic sensor and to how we can stop loading the robot using this sensor.

The ultrasonic sensor detects distance. We can use this sensor to detect whether we have just loaded the catapult and we have just fired or we should load it. For example, if we attach the sensor on this side of the nest of the catapult, and if we detect a distance of 4 - 5 cm or about 1 - 1.5 inch, we can say that we have just fired an element. Now, I will load the catapult. And during loading, we can see that if we detect a distance of, let's say,10 cm (we would check this with the software), we can say that the catapult is loaded and we are ready to fire. Now, what you should pay attention to is that without the sensor the nest is balanced. But when we attach the sensor to one side of the nest,

the center of mass changes and the nest is not balanced anymore. So, we should find a way to balance the nest. A solution would be to move the brick a little to the right. Now, I will remove the brick from the nest and move it one step to the right. Now the construction is much more balanced. Let's check the numbers with the software. Now, let's check the exact numbers using the EV3 software. As you can see, the ultrasonic sensor is attached to the nest and currently, it returns a value of about 4 cm. Here. If I now load the catapult, the value will increase. The catapult is loaded and we can see that the value of the ultrasonic sensor is about 7.8 cm. We can use these values to program our robot, so we'll start the motor and we'll turn the motor until we reach a value of more than 7 cm. This will be our threshold for knowing that we have just loaded the catapult. Now, the program for the robot goes like this. First, we need to start the medium motor. And we start with with a power of 50 but for an unlimited amount of time. It's connected to port D. The second step is to wait until the catapult is loaded. So, we are going to wait. And we are waiting for the ultrasonic sensor to detect a distance. We need distance in cm and we need distance that's more than 7 cm.

Let me just check. Yes, it's connected to port 4.

Then we stop loading the catapult. The medium motor just stops. And the catapult is loaded. Now, the question is how we know when we have just fired an element and we should load it again. We know that we have fired an element, when the sensor detects a distance of less than 5 cm. This means that we've just fired, the nest is down and we can now load again. Let me just zoom this.

And again we wait -

wait for the ultrasonic sensor to detect a distance of less than, let's say, 4 cm. Or even 5 cm.

This means that we have just fired an element and after firing we'll wait for a second so that we have a smoother video and we'll put all this in a loop. We select all the elements and add them in a loop. Now, let's download the program and see how it goes. That's the program that we have just developed. I've downloaded it onto the robot and it's currently running. So, I'll fire. We detect a distance of less than 4 cm, we load and fire. We again load. What you can see is that we are not loading the catapult to a maximum capacity. For example, the lever should be something like this. So we should probably change the value not to 7 cm but to 8 cm, 9 cm - let's experiment with a different value. Now, let's change the value for loading the catapult not to more than 7 cm but to more than, let's try, 9 cm. And let's start the robot again. The robot is currently running the new program where we are waiting for a distance of more than 9 cm. As you can see, we have loaded so that the lever is as low as possible and if I now release, we fire, we load again; we fire, we load again. So, this is how you can use an ultrasonic sensor to detect whether you should load the catapult and when exactly you have fired a small Lego part.

A challenge that I would like to leave to you is to try to find the speed at which the nest goes down. When we release the catapult, you can see that the nest goes down and we fire. Try to find the speed by using the ultrasonic sensor right here and post your answers in the comment section below. We will look at some of the answers and then we'll show you our solutions to this problem.