Regarding the safety aspects, since the projects are based on a very low voltage power supply supplied by the USB port of the PC or by backup batteries or power supplies with a maximum of 9V output, there are no particular risks of an electrical nature. It is however necessary to specify that any short circuits caused during the exercise could cause damage to the PC, to the furniture and in extreme cases even to burns, for this reason every time a circuit is assembled, or modifications are made on it, it will be necessary to do it in power failure and at the end of the exercise it will be necessary to disconnect the circuit by removing both the USB cable for connection to the PC and any batteries from the appropriate compartments or external power connectors. Furthermore, again for safety reasons, it is strongly recommended to carry out the projects on insulating and heat resistant mats that can be purchased in any electronics store or even on specialized websites.

At the end of the exercises it is advisable to wash your hands, as the electronic components could have processing residues that could cause damage if swallowed or if in contact with eyes, mouth, skin, etc. Although the individual projects have been tested and safe, those who decide to follow what is reported in this document assume full responsibility for what could happen in the execution of the exercises provided for in it. For younger children and / or for their first experiences in the field of electronics, it is advisable to perform the exercises with the help and in the presence of an adult.

Roberto Francavilla


In the previous lesson we saw what sound is and how it is generated, a vibrating body produces sound waves, the vibration, or the oscillation of the body over time, establishes what is called frequency. Therefore the Frequency can be defined as the number of complete oscillations of the body in the unit of time (which is the second and is indicated with “s”). The unit of measurement for frequency is Hertz (Hz).

Sounds that have frequencies higher than 20,000 Hz, that is 20 kHz, are called “ultrasounds” and these sounds are used in the electronic field in many applications, for example in detecting the distance of a body from the sound source.

To better understand the principle used in detecting distance using an ultrasound signal, it is necessary to learn a bit of Physics.

Curiosity: what is Speed?

The speed of an object is the distance traveled by that same object in the unit of time. So the speed of a sound wave is the distance (measured in meters) traveled by the same sound wave, in the unit of time (expressed in seconds, “s”). Obviously there are also multiples and submultiples of the meter and the second.

Did you know that a hare runs on average at a speed of about 60 kilometers per hour (km / h), that is, in one hour the hare covers a distance of 60 km.

A sound wave travels through the air at a speed of 343 m per second (m / s), that is, 0.0343 cm per microsecond (cm / ms).

Ultrasound module HC-SR04. Distance Obstacle Detection

The ultrasound module that we will use in our projects is the HC-SR04. The HC-SR04 is a very convenient distance sensing sensor that is mainly used to avoid objects in various robotics projects.

In the first cars with parking sensors it was also used to detect obstacles when parking and even to detect the water level. The HC-SR04 ultrasonic sensor uses an ultrasonic generator to determine the distance to an object like bats do. It is a module that offers excellent non-contact detection with high accuracy and the readings are stable. Operation is not affected by sunlight or black objects as is the case with laser detectors.

It is composed of a base (which can be blue, but I have also seen them in yellow, red or orange), two cylinders, which look like two eyes, an oscillator that seems to give the two eyes a mono-eyelash and four PINs that look like whiskers… in fact this module is often used in a position that appears to be a head…. For example in robot cars …

The two cylinders are used for: the emission of the ultrasonic signal (cylinder indicated with “T”) and receipt of the signal (cylinder indicated with “R”). For the 4 pins: to one you connect the 5V power supply (Vcc) directly from the Arduino, then there is the ground pin (GND) and then the two pins “Trig” and “Echo” which must be connected to two pins analog or even digital of Arduino. One pin is used to send the sound pulse and the other to detect the signal reception time.

To explain how the module works I have to go back to the previous physics lesson, but it is also useful for school …. so read it …

The operating principle of the Ultrasound Module is simple: the module emits an ultrasonic signal and calculates the travel time (in microseconds) between the instant of emission and the instant of reception after the same signal bounces off the obstacle. Once the time has been measured, the distance traveled by the signal is calculated accordingly. In fact, for example, if the travel time of the sound wave in going towards the obstacle and then back once it has bounced off the obstacle itself, was 300 microseconds, making the necessary equivalences, we have that the distance traveled by the outward and return signal is:

distance (in centimeters) = 0.0343 x time (in microseconds)

that is 0.0343 x 300 = 10 cm

Since the time is that of “outward” and “return”, obviously the distance of the obstacle is half, so it is 5 cm.

Let’s see with a graphic representation what explained:

The Tx emitter sends the ultrasonic pulse, the Rx receiver receives the pulse bounced off the obstacle. The module calculates the time elapsed between the outward and return journey, so the distance traveled by the signal is twice the distance between the module and the obstacle, which is why in the calculation to determine the distance value of the obstacle must be divided by two.

Therefore, the “algorithm” to be used to establish the distance is:

Obstacle distance (in centimeters) = [0.0343 x t (in microseconds)]: 2

After the theoretical part, let’s move on to our project.

Project 19 - Detecting the distance of an obstacle.

For this project we need:

The wiring diagram to be made is the following:

The assembly scheme to be made is the following:

The Trig and the Echo are connected respectively to digital PINs 10 and 9. Even if these PINs have the tilde, that is, they can be used to send PWM signals, we will use them as normal digital PINs.

Click on the Arduino icon and after opening a new file we copy the sketch shown below:

Once the sketch has been loaded and the serial plotter activated, the distances of the objects in front of the sensor will be detected

Video-Project 19 - Detecting the distance of an obstacle.

Project sketch analysis 19 - Detecting the distance of an obstacle.

In the distance measurement sketch we find two new functions.

The first:

delayMicroseconds (10);

this instruction introduces a delay which this time is expressed in microseconds (the classic delay, remember, introduces delays of milliseconds in the sketch).

The second:

long duration = pulseIn (PortaEcho, HIGH);

tells Arduino to load in the variable named “duration” defined as a “long” variable, that is a variable that has many decimal digits, the time value calculated from the moment in which the impulse starts from the Tx to the moment in which the signal it is received by the Rx bringing the PIN status to a HIGH status.

CURIOSITY: What are the Degrees of Freedom (DOF) and the Electric Drives

Displacements, limb movements, torso rotation…. they are the movements that allow a robot to carry out those activities that we want it to do to carry out the assigned (programmed) task.

Let’s take an example to better understand the concept of movement by analyzing one of the most used robots in industrial processes for mass production: the Puma Robot.

This robot, which is nothing more than a mechanical arm, is widely used in the automotive sector for welding, painting, moving heavy components, positioning components in precise points in assembly and storage, etc.

From this we understand how the first question we must ask ourselves when designing a Robot is: what should our Robot do?

The complexity in the design of a Robot is given by the number of movements to be given and consequently by the number of “movement joints” to be inserted; in the particular case of the figure, there are six.

But how do we determine how many joints to insert?

The answer is to establish whether our work point must be able to move in space, or on a plane, or a straight line.

The answer is to establish whether our work point must be able to move in space, or on a plane, or a straight line.

In this case, therefore, the movement is characterized by only one degree of freedom.

In the case represented by the figure on the right, however, we have that the body can translate along the abscissas and along the ordinates, that is, it can move on a plane, so the body is characterized by having two degrees of freedom.

In this other case (see figure on the left) the body can move rigidly in space, along three axes, so it is characterized by having three degrees of freedom.

In Physics, but also and in particular in Mechanics, the position in space of a rigid (solid) body is identifiable by six variables (three of translation and three of rotation) they represent the possible movements called degrees of freedom.

Obviously, reproducing the position in space of a point in space using a single device is impossible (… at least with current technological knowledge), because there are “constraints” imposed by the equipment that generate the movement, which are normally electrical drives.

Obviously, reproducing the spatial position of a body using a single device is impossible (… at least with current technological knowledge), because there are “constraints” imposed by the equipment that generate the movement, which are normally electrical drives.

This is why the Puma Robot has six rotation joints (six electric drives), because the movement that allows each rotation joint, as shown in the figure, in turn allows the working point located at the end of the arm to reach “any” point in space and its relative position is always known

Electric drive (or also called Power Drive System: PDS) is defined as a system that converts electrical energy into mechanical energy with the help of electronic power and control equipment.

The motors used for electric drives are represented in the diagram below, obviously I don’t want to teach an Electric Machines course, but I just want to show you the significant number of electric motors we have available to be able to give the mechanical movement we want.

After this first challenging theoretical part, but necessary to understand which and how many motors we need to make particular movements, let’s move on to more practical questions.

For our purposes and our applications with Arduino we will use two types of motors: stepper motors and servo motors.

The Servo Motor - Giving movement to things.

With the Servo Motor it is possible to move objects, for example to a robotic arm, a lever, etc. Also in this case, for its use, it is enough to carefully follow the following lesson. The servomotor is composed of an electro-mechanical and an electronic part, the electro-mechanical one creates the movement, the electronic part controls it. However on the outside the servo motor comes with a plastic shell, a gear that rotates a pin and a three-pole cable.

The servomotor that we will use in the next project is the SG-90, which can be powered directly from Arduino and is used for moving light objects. As shown in the figure below, the colored cable has three poles: one pole is connected to the Vcc 5V voltage of the Arduino, to another the ground (GND) and the third pole is connected to an Arduino digital PIN and is used for control .

For “heavier” applications we can use servomotors with the commercial code MG996R or the TD-8120MG, but in this case, especially if they are more than one servomotor, the power supply must be given by an external power supply.

Project 20 - Controlling an SG-90 Servo Motor

For this project we need:

First we take our Arduino, the servomotor and the cables and connect them as shown in the figure below. The wiring diagram is:

For assembly follow the diagram below:

The Servo Motor has three generally colored cables: orange (control cable), red (5 V) and brown (GND), in other cases, it depends on the manufacturer, the control cable is white.

Click on the Arduino icon and after opening the IDE we create a new file and copy the sketch shown below:

After checking the sketch, let’s load it on Arduino in the usual way and open the serial monitor, we will see the moving part of the motor (called rotor) rotate from 10 ° to 160 ° and then go back and on the monitor the rotation in degrees is indicated.

Project 20 - Controlling an SG-90 Servo Motor

Sketch Analysis: Project 20 - Controlling a Servo Motor.

Analyzing the sketch above we can observe the following instruction:

#include <Servo.h>

That is, it calls the library to drive the servo motor.

This library should already be installed in the Arduino IDE, if this is not the case, you can use the procedure described in the preparatory activities for the Course.

Among the instructions that the library makes available we have the most commonly used ones:

[object name] .attach (PIN): allows you to specify on which pin our servo is connected and bind it to the ServoMotor object;

[object name] .detach (PIN): removes the connection between the ServoMotor object and the pin to which it was linked;

[object name] .read (x): reads the angular position of our servo, returns the last value passed with write ();

[object name] .write (x): indicates to the servo the angle at which to position;

Continuing with the analysis of the sketch … let’s see the individual instructions in the SETUP area:

Servo ServoMotor; //… used to create an object named ServoMotore

ServoMotore.attach (9); // … we specify to the IDE that the ServoMotor is connected to PIN 9

Moving on to the LOOP part:

ServoMotore.write (i); // …. the variable “i” is the one incremented by one by the for loop, so in this way the rotor will turn one degree at a time.

It can be seen from the “for” loops that the first increases from 10 to 160 and the second decreases from 160 to 10.

The rest are instructions already seen in previous projects.

Project 21 - Ultrasound Radar System

For this project we need:

Since we will be using both the HC-SR04 ultrasonic module and the SG-90 servo motor for this project, we recommend attaching the ultrasonic module to the servo motor before proceeding. To do this, “artisanal” methods of any kind can be used, or you can buy the appropriate fixing brackets that can be found on the major market places such as Ebay, Amazon, Aliexpress, or others ..

In the case of an artisanal solution, be careful not to create a metal support that could short-circuit the tracks on the ultrasonic module. Personally I suggest you buy the special brackets for a few euros including shipping costs.

First we take the bracket and fix, with the screws, the HC-SR04 sensor on it and then all we fix it, with other screws, to the servomotor on which we have mounted the foreseen support.

We take the double-sided tape and make sure that the servo motor stands with the bracket (and the sensor) mounted on it. See photos below for assembly:

The wiring diagram is as follows:

We make the connections as shown in the assembly diagram:

Now let’s move on to the sketch, click on the Arduino icon and after opening the IDE we create a new file and copy the sketch below:

As for the analysis of the sketch, as can be seen from the listing above, it is nothing more than a combination of the sketches made for the control of the sensor and the Servo Motor.

At this point all that remains is to load the sketch and activate the Serial Monitor and try to interpose obstacles in front of the radar to see the detection.

Video-Project 21 - Ultrasound Radar System

Project 22 - Ultrasound Radar System with Processing

For the more daring, without modifying, in terms of hardware, what has already been done for the previous project, there is also a version of the same project with more attractive graphics. For this version I had to modify a project already found on the net that uses, in addition to the Arduino IDE, another Open Source software called Processing.

Without going into the technical details of Processing because there will be a Course dedicated to this other programming environment, if you want, you can try your hand at realizing this project (it’s a little more complicated than the usual projects, but it’s worth it!).

Go to the site:

The page appears:

Click on Download Processing and you will access the page:

Choose the version according to your operating system (in my case it is 64-bit Windows):

Processing is downloaded in its latest version (in this case, 3).

Processing is an Open Source program and is therefore free, but if you want you can make a donation, which I recommend, to keep it updated with new features.

The version I downloaded is, unzip it and run the installation program.

Click twice consecutively on the “processing” file and the installation starts:

A message appears after installation saying that files created with processing version 2 may not work, continue and click Get Start. At this point the Processing window opens. You can close it.

Download the files (just click on the name):

The first thing to do is to check that the settings provided on the Radar_Processing_ino file are correct, so connect Arduino to the PC and open the sketch for Arduino named Radar_con_Processing, check which port Arduino is connected to (look at the bottom right), in my Arduino case is connected to COM3 (see below).

At this point, double click on the file: Radar_Processing_ino, obviously after having unpacked it.

The following window opens:

Go to line no. 21 and check that the port number is correct, in case you change the port number (ONLY THE NUMBER!).

Then go to your desktop and right click select screen resolution. In my case it is set to 1280 x 720 (see figure below):

At this point, go back to Processing and check that the resolution shown in line 19 is correct, if you change it, however, do not put resolutions lower than 1280 x 720. If your resolution is lower, adapt it to that shown in Processing

Now load the sketch on your Arduino and after a few seconds start processing by clicking on the “play” button:

A real RADAR will appear !!!

Since I think I’ve stressed you out enough already, I’ll stop here! And see you at the next lesson.

If you found the lesson interesting, make a donation you will help me realize many others.