ultrasound_preparation

Ultrasound preparation

Build your own ultrasonic distance meter that uses sensors connected to the micro:bit to detect objects and measure their distance! Learn the basics of programming, electronics, and working with sensors in a fun way - ideal for teaching physics, computer science, or for home experiments with robotics and smart devices.

Description

The ultrasonic device is a project that serves as a practical demonstration of how ultrasonic distance measurement works. It uses a sensor to transmit and receive sound waves and calculates the distance based on the time it takes for them to reflect from an obstacle - it can display it on the display, start an animation, or light an LED when a certain limit is exceeded.

This device is ideal for school lessons and home experiments and does not require any advanced knowledge. Children and adults can easily learn the principles of measurement, sensors and programming with it.

Connection

The ultrasonic sensor is connected using four wires. On the sensor we find the VCC (power), Trig, Echo, GND (ground) pins. We connect the VCC pin to the white power rail on the board, which supplies a voltage of 3,3V. We connect the Trig pin to pin 8 (P8), Echo to pin 9 (P9) and finally we ground it by connecting the GND to the black ground rail. For more information, see the assembly instructions.

Ultrasonic_device_connection

Recommendation

When connecting, the expansion board must be turned off and the micro:bit disconnected from the power supply.

Program

Basic program

The program works by first setting up the LED strip. Then it repeats a part of the code that constantly compares whether we have, for example, moved our hand too close to the ultrasound. The switching limit is set to 15 cm. If we have exceeded this limit, the code tells the LED strip to light up red.

Linear function

The program is a little more complicated. At the beginning of the code, we declare variables. We will write the distance of the object measured by the ultrasonic sensor into the distance variable. The NUM_LEDS constant is used to store the value of the number of LEDs on our LED strip. In our case, the strip has 6 of them. We initialize the strip object representing our LED strip. Finally, we determine the boundaries of our measurement, i.e. the minimum and maximum distance.

The first loop performs the measurement itself. Using a block from the Sonar library, we write the measured value to the distance variable. To convert the measured distance to the number of lit LEDs, we use the map function from the math library. In principle, this function works as a ternary function. However, we must convert the resulting number to integers using the round down (floor) function. However, in order for the number of lit LEDs to increase with the decreasing distance of the measured object from the sensor, we must subtract the value from the maximum possible (number of LEDs on the strip).

In the second loop, we turn on the LED strip. We go through each LED in turn in the for loop. If the order of the given LED is equal to or less than the leds value obtained from the first loop, we assign a certain color to the LED. If the order is higher, we turn off the LED (“assign it to black”). We must not forget the show function so that the entered values of the individual LEDs are reflected. To slow down the changes and prevent flickering, we add a pause at the end of the loop.

Tasks

Task 1: Linear measurement

Adjust the program so that the LED strip lights up gradually depending on the distance from the obstacle. The closer the object, the more LEDs will light up.

Task 2: Obstacle detection

Modify the program so that, in addition to lighting up the LED strip, a sound is also heard from the micro:bit speaker if an obstacle is detected in the selected vicinity.