Basic Course about ARDUINO - Lesson 5



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


This Lesson we dedicate it to the study of Sound, or rather to the generation and propagation of sound waves and to the related Arduino sensors and modules that exploit this phenomenon to manage events.

CURIOSITY: What is sound?

Suppose you drop a stone into the water of a pond, you will see that waves expand from the point where the stone falls, these waves move away from the point of fall and propagate in concentric circles.

If we make a body vibrate, these vibrations of the body are transferred to what surrounds the body itself (for example: air, water, etc.) and this phenomenon occurs thanks to the expansion / compression action of the air or water molecules in which is immersed in the body that is vibrating. In this way the sound is generated.

The sound produced by the vibration of a body propagates through waves and in this regard, to give visibility to this phenomenon, we have given the example of a stone dropped into the water of a pond.

In the animated image above, instead, the compression / expansion phenomenon is shown with the example of a sheet vibrating in the air; the light blue arrows indicate the air sucked into the depression area created by the movement of the foil. The blue arrows instead indicate the overpressure created by the same movement in the opposite direction. The vibration, therefore, compresses and expands the air that surrounds the body, this compression and expansion is transmitted to the surrounding air thus producing waves called “sound waves” (sound: because at certain frequencies, to simplify … at a certain speed of vibration, these waves can also be heard). In fact, these vibrations are then picked up by the ear which, thanks to the presence of the “eardrum” and thanks to the presence of “ossicles”, transfer and transform the vibrations captured into electrical impulses that our brain translates into a sensation called sound .

So sound is a sensation that we receive and comes from a vibration of a body. If this sensation is pleasant, then we call it “sound“, otherwise it is classified as “noise“.

Very important in the propagation of sound is the medium in which it propagates which also determines its speed.

In the water the speed is 1480 m / s. That is, in one second the sound wave travels about 1.5 km.

In the air, since the molecules are more rarefied (they are more distant), the speed of sound propagation is about 340 m / s, that is, in one second the sound travels 340 m.

In iron the propagation speed is 5130 m / s, i.e. in one second the sound travels about 5 km. This is because the crystallines are much closer to each other and therefore the transmission of the vibration induced by the compression / expansion action is more immediate.

Is it in a vacuum? Obviously in the vacuum, since there is no compression / expansion of molecules, no sound is propagated.

The sound wave on its way can encounter an obstacle, usually a more “dense” body. For example, a sound wave that is propagating in the air, when it encounters a rocky wall, is reflected on it and a “reflected wave” is generated and part of the sound is also reproduced in the rock


When the reflected wave returns in the same direction as the source, there is the phenomenon we call “echo“.

The frequency, which is measured in Hz (pronounced erz), that is the proximity between the sound waves produced (or better: the number of sound waves transmitted in a second), classifies the sound in: low sounds (what we call “bass ”) Or high-pitched sounds (which we call “treble”).

Only certain frequencies of sound are audible to the human ear, those that have a frequency between 20 Hz and 20,000 Hz (or even written 20 kHz). Elephants can perceive sounds even below 20 Hz, while bats can perceive sounds as high as 120 kHz.

After the theoretical part, which gave us the opportunity to get to know the sound better, we move on to the practical part with a nice project, very funny.

Project 16 - The Teresa wasp and the active buzzer.

For this project we need:

Before moving on to assembling the circuit it is good to know that the active buzzer has two PINs, one of which is longer. The longer one is the positive PIN “+”, the shorter one is the negative “-“. Generally the passive Buzzer also has a sticker where the positive PIN is indicated.

The electrical symbol of the active buzzer is:

The wiring diagram to be made is the following:

The assembly scheme to be made is the following:

Note: The positive PIN of the active buzzer is connected to the Arduino digital PIN 8, while the negative PIN to GND.

Once the circuit has been assembled (and the Vespa drawing positioned on the buzzer to make the project more choreographic), the sketch to be written and uploaded to Arduino is:

Once the sketch has been loaded, the buzzer will sound intermittently, you can change the on / off time of the buzzer, changing the time indicated in the delay of the sketch.

Video-Project 16 - The Active Buzzer

The Passive Buzzer

The Buzzer is a piezoelectric device. Piezoelectricity is the property of some crystalline materials to polarize, generating a potential difference when they are subjected to mechanical deformation and at the same time to deform in an elastic way when subjected to an electrical voltage.

The GIF above shows how a deformation of the piezoelectric material generates a voltage

The GIF above shows how a Voltage applied to the piezoelectric generates a Deformation

The Buzzers can be active if they have an oscillator inside them (a quartz with a predetermined and fixed frequency) which, once powered with electric voltage, stresses the piezoelectric material at a certain frequency and therefore, this vibration of the piezoelectric material (usually a membrane), produces sound at a fixed frequency (this is the so-called Monotone Sound).

Passive Buzzers, not having an oscillator, in order to produce a sound, need a supply voltage that varies over time. Therefore the variable voltage with a certain frequency is given from the outside. To do this we can exploit the capabilities of our Arduino to produce a variable voltage over time thanks to PWM (Pulse Width Modulation), that is a voltage with modulated pulse width. But at this point the question arises, what if we changed the frequency? You will get different sounds and at certain frequencies, you can play musical notes as well. So, the conclusion is that with an Arduino and a passive Buzzer we can play melodies.

The buzzer is used in alarms, in countdowns, for the confirmation of keyboard input, etc …

The passive Buzzer is the one shown in the photo below:

The table shows the correspondence between the main notes and the frequency.

Project 17 - The passive buzzer and the Piano-Man.

For this project we need:

Before moving on to the assembly of the circuit it is good to know that the passive buzzer has a low resistance, which is why we use a 220 Ohm resistor in series with the passive buzzer, moreover, since we will feed the buzzer with a “square half wave”, to have a cleaner emitted sound, it would be useful to couple a “filter” (basically a capacitor) to the buzzer, but these aspects, concerning the applied electronics, we will see them in a dedicated course, for the moment, let’s just insert a resistor and our didactic purposes.

The electrical symbol of the passive buzzer is:

The wiring diagram is as follows:

For assembly follow the diagram below:

After the connections we move on to write the sketch.

Connect the Arduino to the PC via the USB cable and launch the Arduino IDE application by double clicking on its icon.

An empty window opens, or you need to open a new empty one and write the sketch below:

Once the sketch has been loaded, the buzzer will play a melody formed by the note scale

Video-Project 17 - The passive buzzer and the Piano-Man

Analysis of the Project 17 sketch - The passive buzzer and the Piano-Man.

In the sketch it is possible to observe that to activate the passive Buzzer, the instruction to be given is “tone” with the following syntax:

tone (num_pin, known_frequency, known_life);

With this instruction, a PWM signal is generated with a specified frequency and maintaining the same frequency equal to the value also indicated.

While for the activation of the active Buzzer it was sufficient to set the Buzzer PIN to high status with a simple instruction

digitalWrite (buzzerPin, HIGH);

and to turn it off, of course:

digitalWrite (buzzerPin, LOW);

Another thing that I would like to point out is the definition of the vector, that is, that particular variable that we have called “melody“, which, as you can see, contains multiple values. The individual values ​​are placed in an orderly manner from position 0 to position “n-1”. They can be recalled by writing the name of the variable in the sketch and inside the square brackets, the position of the value we want to read:

melody [thisNote]

It is possible to experiment with new melodies and practice with the Tone instruction, using the examples in the IDE. To open them:

You can also try this melody I found on the internet (just copy and paste):

CURIOSITY: The Microphone and the Loudspeaker or Acoustic Box

At this point, with the knowledge acquired on Sound and Piezoelectric materials, we are able to understand quite well the operation of the microphone.

The microphone is nothing more than a “transducer” of the pressure that sound waves exert on a membrane, in electrical impulses. This “transduction” (or more simply, this translation of a vibration into a sequence of electrical impulses) takes place thanks to the aid of a particular material called piezoelectric. In fact, the piezoelectric material as we have seen is able to produce a voltage when it is stressed to a compression or expansion, that is, when it undergoes a deformation.

In the speaker there is essentially the reverse process of the microphone, that is the electrical impulse, suitably amplified, activates an electromagnet which attracts a permanent magnet fixed to a stretched membrane. In this way the movement of the magnet generates the vibrations of the membrane and therefore reproduces the sound waves with the same frequency indicated by the movement of the magnets.

So with the use of microphones and speakers we are able to equip our Robot with hearing and speech.

Project 18 - What happens in that house? We use the microphone.

For this project we need:

Before moving on to the assembly of the circuit it is good to know better the sound sensor module or microphone that we will use for educational purposes, in fact, on the market there are different types of sound sensor modules, from the most sophisticated ones that also have an amplification circuit and very microphones. sensitive, and those that have a pure didactic purpose, less sensitive but equally functional. However, the operation is essentially the same.

We will use the sensor modules that are on the market with the initials KY-037 and with the initials KY-038 which is the older brother (slightly more sensitive), these in turn can have 4 PINs because they make available both an output signal digital and analog, those with 3 PINs make available only the digital signal.

They can also have one or two LEDs, at least one LED is used to check the correct sensitivity adjustment, while the second, which is not always present, indicates the power supply of the module. Finally, these modules always have a trimmer (i.e. a small potentiometer) that by adjusting it using a screwdriver it is possible to establish the threshold sensitivity that the microphone must have to make the digital output signal available in the LOW or HIGH state at the relative PIN indicated with D0 or with OUT (in the case of a 3-PIN module).

At this point we move on to the wiring diagram:

(Note: Use the digital PIN of the D0 Sound Sensor Module or also indicated as OUT for the 3 PIN Module). The assembly scheme is instead:

After the connections we move on to write the sketch.

Connect the Arduino to the PC via the USB cable and launch the Arduino IDE application by double clicking on its icon.

Warning: once the sketch has been uploaded to Arduino, the sensor module must be calibrated according to the background noise present in your environment. So if the LED connected to PIN 3 remains on, it means that the background noise is exceeding the calibration threshold, turn the screw on the trimmer of the module, using a flat head screwdriver, until the LED does not turn off. Conversely, if the LED is turned off even with loud noises, set it until it does not turn on. Position the screw right at the limit so that the LED is off and turns on at the minimum noise slightly higher than that present in the environment.

Video-Project 18 - Use of the microphone sensor with Arduino

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