Robotic Tank – Ultrasound

Expand your Robot Tank with the Ultrasound module! Connect it to your existing building set and learn how to use the ultrasonic sensor to recognize obstacles and automatically avoid them - an ideal addition for developing skills in robotics, programming and algorithmic thinking.

Description

The Ultrasound module is a practical extension for a robotic tank that allows the tank to sense obstacles and react to its surroundings. Using an ultrasonic sensor, the vehicle can measure the distance in front of it and stop, slow down, or avoid obstacles according to programmed behavior. The sensor connects directly to the expansion board and is easily integrated into the existing tank design.

This expansion significantly expands the capabilities of the tank and opens the door to more advanced projects such as autonomous driving, security systems or interactive educational scenarios. The module is fully compatible with the MakeCode environment and can also be controlled in Python or JavaScript. It is ideal for students who already have the basics of programming and want to move on to real-world robot control algorithms and working with sensors.

US-016 wiring

MB1

The ultrasonic sensor is powered from the expansion board via pins 3V3 a GNDNext, you need to connect the output from the ultrasonic sensor, i.e. pin Oct to the micro:bit analog input pin. In this case we chose the pin P2.

ultrasound2_mb1

MB2

MB3

ultrasound2_mb3

Connection (TRIG and ECHO)

The ultrasonic sensor is powered from the expansion board via pins 3V3 a GND. Next, you need to connect the pin for the transmitter (Trig) and the receiver (Echo). In the following programs, we use the pins P14 – Echo a P15 – Trig.

MB1

ultrasound_mb1

MB2

ultrasound_mb2

MB3

ultrasound_mb3

Programmes

The programs again differ according to the type of ultrasonic sensor. They are further divided according to the expansion board used. The MB1 board is equipped with a built-in LED strip, which is great for indicating the distance that the sensor measures.

However, in principle, the programs are the same, they only differ in the use of the Neopixel library for the LED strip.

Sensor US-016

Expansion board MB1

Basic program

This program uses the LED strip that our MB1 expansion board has. If you use this program on another board, it will work too, but it will unnecessarily send signals to the empty pin P16. Therefore, we recommend uploading the code below.

First, we initialize the LED strip using the Neopixel library. The built-in strip has 8 LEDs and is connected to pin P16 (that's why this pin is not brought out on the MB1 board).

The distance is then read in the loop. If the distance is less than the limit we have chosen, 10 cm, the tank stops. We set the color of the LED strip to red. If this condition is false, the tank starts moving at the selected speed and we set the LED strip to green. Finally, we light up the strip.

Advanced program

This program already works with variable speed. Based on the measured distance, it sets the speed of the tank to the variable “speed”. It also sets the number of lit LEDs.

Expansion board MB2 and MB3

If you have a tank with an MB2 or MB3 expansion board, the previous programs will work exactly the same. The difference is that it will send a signal to the empty pin P16, which is connected to the LED strip on the MB1 board. The following codes are therefore exactly the same, the only difference is that they do not contain the control of the LED strip using the Neopixel library.

Basic program

The program in the loop measures the distance of the object from the ultrasonic sensor. If this distance is greater than 10 cm, the tank moves forward. Otherwise, it stops.

Advanced program

This program already works with variable speed. Based on the measured distance, it sets the speed of the tank to the variable “speed”. By changing the value of the constant DIST_MIN or DIST_MAX You can adjust the range of measured distances.

Common sensors (TRIG and ECHO)

Expansion board MB1

Basic program

This program uses the LED strip that our MB1 expansion board has. If you use this program on another board, it will work too, but it will unnecessarily send signals to the empty pin P16. Therefore, we recommend uploading the code below.

First, we initialize the LED strip using the Neopixel library. The built-in strip has 8 LEDs and is connected to pin P16 (that's why this pin is not brought out on the MB1 board).

The distance is then read in the loop. If the distance is less than the limit we have chosen, 10 cm, the tank stops. We set the color of the LED strip to red. If this condition is false, the tank starts moving at the selected speed and we set the LED strip to green. Finally, we light up the strip.

Advanced program

This program already works with variable speed. Based on the measured distance, it sets the speed of the tank to the variable “speed”. It also sets the number of lit LEDs.

Expansion board MB2 and MB3

If you have a tank with an MB2 or MB3 expansion board, the previous programs will work exactly the same. The difference is that it will send a signal to the empty pin P16, which is connected to the LED strip on the MB1 board. The following codes are therefore exactly the same, the only difference is that they do not contain the control of the LED strip using the Neopixel library.

Basic program

Same function as the program for the MB1 board, only without LED.

Advanced program

Same function as the program for the MB1 board, only without LED.

Tasks

Task 1: Obedient tank

Make a robot tank into a playful vehicle. Reverse the stop and go logic so that if it detects an obstacle in front of it, it will move directly behind it. You can then use this to control the tank with your hands. Once it detects your hand in front of it, it will follow it on command. When you raise your hand, the tank will obediently stop.

Task 2: Safe tank

Create a program that helps the tank avoid an obstacle. When it gets close to one, it turns to the side and starts moving again. Let the tank turn long enough to actually avoid the obstacle.

Task 3: Robotic vacuum cleaner

You've probably seen how a robot vacuum cleaner works. When it encounters an obstacle, it stops, turns back a little, and then chooses a new direction until it encounters another obstacle. Try out this exact feature with your tank.

Try using the library Mathematics, specifically a random number block that will determine how much the tank will turn when choosing a new direction. This will give your robotic tank its own mind!

Gallery