Basic Course about ARDUINO - Lesson 10

TOPICS INDEX

Warnings

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 furnishings 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

Deepening on Light: Infrared

The sun is the source of life for the earth, thanks to it we have the light and heat we need for our survival. Light is produced by a transformation of energy, in fact, thanks to the high temperatures of the mass making up the sun, its incandescence is transferred not only as heat, but also as light.
The same principle applies to candles, to incandescent bulbs which have a tungsten filament which, thanks to the circulation of the current, emits light as well as producing heat.
There are other ways to produce light, for example by passing electrical discharges in a gas (neon lights, or mercury vapor lights), or by exploiting the electroluminescent characteristics of particular materials such as LEDs.

Leaving aside the Quantum Physics of the great scientist Albert Einstein, let’s try to understand a little more than what we learned in Lesson n. 3.
We have already seen that light propagates in waves, but unlike sound, it does not need a “material” medium because they are particular electromagnetic waves (photon waves). Basically, the light also propagates in the void (just think of the light that comes to us from the stars, but also from the sun itself).
The propagation of light takes place on rectilinear trajectories called “rays” and they too, on their path, may encounter obstacles. The speed of propagation of light is much higher than that of sound. The proof of this is when there is a storm … lightning generates both light and noise at the same time, but the lightning strikes us first … and after a few seconds the thunder. Light travels at about 300,000 km per second, that is, light travels about 300,000 kilometers in one second!
When light meets a transparent body, it manages to pass it, only a very small part is reflected. If the body is opaque, then diffuse reflection occurs. If the body is a mirror, then the reflection is said to be specular. Thanks to reflection we are able to see even objects that do not emit their own light.

Just to complete the “light” topic of our interest, I would like to say that infrared is that particular light whose spectrum (ie wavelength) is below the red color (hence “infrared”), ie a frequency whose wavelength is greater than 700 nanometers is of the order of tera-Hertz.

Infrared find innumerable applications in electronics and robotics, we will see some of them.

Infrared Sensor Module KY-032

This sensor module is used as an alternative method to the HC-SR04 sensor module for obstacle detection. The operating principle of the sensor is very similar to the ultrasound module, in fact it is based on the emission of infrared signals which are then reflected by any bodies that are in the trajectory of the beam of incident signals and therefore, once reflected, are picked up by the receiver:

Comparison between Ultrasound Module HC SR04 and Infrared KY032.

For the comparison between the HCSR04 Ultrasonic Module and the KY032 Infrared Module for obstacle detection, we build a small table to highlight the major differences between the two modules for the purpose of obstacle detection:

Main characteristics

Ultrasound module (Sound)

Infrared module (Light)

Spectrum

Frequencies above 20 kHz (20 kilo-hertz, i.e. 20 thousand hetz). Ultrasound is at the high end of the sound spectrum.

Frequency of the THz order (tera-hertz, i.e. one thousand billion hertz). Infrared is at the low end of the light spectrum.

Propagation

Compared to solid bodies, sound in air propagates at a slower speed and does not propagate in a vacuum. In the air the speed of sound is about 340 m / s (340 meters per second)

The maximum propagation speed of light is in vacuum: 300,000 km / s (300,000 kilometers per second).

Ability of the reflected wave to return in the same direction as the generator.

Excellent for solid surfaces that are perpendicular to the direction of the sound wave, as these surfaces incline, with respect to the direction of the wave, the dispersion of the reflected sound increases. For particular inclinations, the sound wave does not return in the direction of the generator and therefore the reflection causes the sound to disperse.

With the same inclination with respect to the sound, with light there is an excellent reflection in the direction of the generator ... even for very inclined surfaces. Light is not reflected by very dark colored bodies (in this case the light is absorbed) and in the case of transparent materials (where the light passes through the material).

Project 37 - The use of Module KY032 with the EN PIN disabled

For this project we need:

The wiring diagram is as follows:

The assembly scheme is as follows:

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 monitor activated, pass your hand in front of the sensor module at different distances and check the detection by switching on the LED on the module and the writing that appears on the serial monitor. The sensor intervention point can be adjusted by means of the trimmers positioned on the module.

Video-Project 37 - The use of Module KY032 with the EN PIN disabled

Sketch analysis: Project 37 - Module KY032.

Since the Module works as a switch, that is, if it finds the obstacle within the distance set by the trimmers, the module puts the signal PIN in the LOW state, it is advisable to proceed with the calibration before using it. This is done with the help of a ruler and placing an obstacle at the desired distance. It will be necessary to adjust the two trimmers until the obstacle, at that precise distance, is detected.

Project 38 - The use of Module KY032 with the EN PIN enabled

For this project we need:

The wiring diagram is as follows:

For the assembly it is necessary to follow the diagram below:

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

<

Once the sketch has been loaded and the serial monitor activated, pass your hand in front of the sensor module at different distances and check the detection by switching on the LED on the module and the writing that appears on the serial monitor. The sensor intervention point can be adjusted by means of the trimmers positioned on the module.

Basically, in this project, as you can see, there is only the particularity that the operation of the module can be enabled or disabled if necessary. To enable it, the EN PIN must be set to HIGH. With the presence of the jumper, the EN is always enabled.

Video-Project 38 - The use of Module KY032 with the EN PIN enabled

Infrared Sensor Module KY-033 - Line Tracking

We have seen, in the previous paragraphs, that when infrared hitting surfaces with particular very dark colors, they cannot be reflected and therefore not picked up by the receiving sensor. So, your fellow Makers have rightly thought of exploiting this phenomenon to create a sensor that could identify a dark-colored line compared to other colors. This module has been called Line Tracking Module and its acronym with which it is sold commercially is KY-033, we will call it sensor or line tracking module.

The operation of this sensor is simple and the module is similar to the previous one:

Basically, when the receiver does not pick up the infrared emitted by the emitter LED, it means that the sensor is positioned on the BLACK color line (obviously it is necessary to use very dark colors to draw the lines, BLACK is the best).

The module has three PINs, one for the ground (indicated with G or GND), one for the power supply (indicated with Vcc or V +) and one for the output signal (indicated with S or OUT). The position is not always the one indicated in the figure, so pay attention to the indications given on the form.

Furthermore, the module has a trimmer which is used to adjust the intensity of the emission signal and therefore, depending on the distance between the emitter and the line to be followed, a small adjustment must be made (generally in the central position, which is the one represented in the figure above, already works fine). It should be noted that the output signal is analog, so it will be necessary to connect the module signal PIN to an Arduino Analog PIN. But let’s see how the module works in the following project.

Project 39 - The use of the KY033 Line Tracking Module

For this project we need:

The scheme to be implemented is the following:

For assembly, follow the diagram below:

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 monitor activated, pass the sensor on a line at least a couple of cm thick and observe the output signal.

Video-Project 39 - The use of the KY033 Line Tracking Module

Sketch analysis: Project 39 - KY033 Line Tracking module.

Analyzing the sketch above we can observe the following instruction:

int ledPin = 13;

With this instruction we say that the integer variable named LED has a value of 13.

Same thing can be said about education:

int sensorPin = A5;

Where A5 is the Arduino Analog PIN number where we connected the sensor “OUT” signal.

The variable named “sensorValue” is assigned a value of 0 initially, with the instruction:

int sensorValue = 0;

Regarding the SETUP section, we can observe the instructions:

pinMode (ledPin, OUTPUT);

which defines as OUTPUT the LED that is on the Arduino (and is connected to PIN 13).

In the LOOP section we have the interesting part: first we read with the analogRead (sensorPIN) instruction; the value present on PIN A5 and assign it to the sensorValue variable. At this point, when the sensor is above a light surface, the module returns a low voltage value (about 83 mV – milliVolt), so remembering that the analog PIN goes from 0 to 1023, we calibrate the sensor to a value of 500, that is: 5 V / 1024 * 500 = 2.44 V. That is when the sensor returns a value greater than 2.44 V (i.e. greater than 500) it is on a dark surface and for us the sensor is in the correct position it is therefore ” ON LINE “.

I carried out several tests, also using the adjustment granted by the trimmer, and I observed that, by positioning the sensor at about 4 – 5 cm from the surface, with the brown color the sensor returns values ​​between 500 and 600, while with the black it goes between the 800 and the 1000.

The best functionality of the sensor, however, is when you put two coupled. They are able to better identify their relative positioning with respect to the line to be followed.

The Infrared Remote Control - IR Remote Controller

In addition to being used for sensors in identifying obstacles or to follow lines, infrared light is also used to transmit information from one device to another. Example of this is our TV remote control.

In this part of the lesson we will focus on learning the operation of an infrared remote control (information transmitter) and its corresponding information receiving device (which in our case is an infrared receiver called KY022). The information from one device to another is transmitted through infrared signals (the abbreviation IR is used to abbreviate) of the coded type.

Normally when you buy the remote control, the manufacturer of the same, in the manual, also indicates the coding with which the remote control transmits the information on the IR signal, but we will do without this information, because we will also see how to obtain this information from our remote control. . So we will carry out two practical projects, one to retrieve the coding information (thus decoding the signal) and the other to use the transmitted coded information to make Arduino perform operations.

Project 40 - Decoding the IR signal of a remote control

For this project we need:

The wiring diagram for the connection is:

For assembly, refer to 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.

Open a new empty window and write the sketch below:

At this point, once the sketch has been uploaded to Arduino, open the Serial Monitor of the IDE, point the remote control at the receiver and press each single button on the remote once. With each press, the green light turns on when the signal is captured and turns off after decoding. The code of the pressed button appears on the Serial Monitor. Now all that remains is to write the codes corresponding to each key on the remote control. In my case:

Off button: 16580863
Volume +: 16613503
Volume -: 16617583
1: 16582903
2: 16615543
3: 16599223
4: 16591063
Etc …

Video-Project 40 - Decode the IR signal of a remote control

Sketch analysis: Project 40 - Decode the IR signal of a remote control

As you can see from the sketch, we have introduced a new library: #include which makes it very easy to use the IR Receiver. In fact, it is enough to read the comment lines in correspondence of the single command lines to immediately understand what function the single instruction performs. Pay attention, it is completely normal that the IR receiver also captures other infrared signals, such as those generated by artificial lighting or even other remote commands.i.

Project 41 - Piloting events with IR remote control

Thanks to the KY-022 module, we can control events with an infrared remote control, in fact, once the codes corresponding to the individual keys of the remote control have been memorized, with the if statement which establishes which key was pressed, we can make a certain event occur. An example is the following project. For this project we need:

The wiring diagram for the connection is:

For assembly, refer to 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.

Open a new empty window and write the sketch below:

At this point, once the sketch has been loaded on Arduino, open the Serial Monitor of the IDE, point the remote control at the receiver and press key “1” to turn on the yellow LED, key “2” to turn on the red LED and the “3” key for the blue one. To turn everything off, press the red off button

Video-Project 41 - Piloting events with IR remote control

Sketch analysis: Project 41 - Piloting events with IR remote control.

Analyzing the sketch above we can observe that to make the IR receiver work in a simple way, the IRremote.h library must be loaded:

#includes <IRremote.h>

As you can see the code, with this library, it is very simple:

To create the “receiver” object

IRrecv xxxxxxx (pinIRremote);

to enable the IR receiver

xxxxxxx.enableIRIn ();

To decode the IR signal

xxxxxxx.decode

Once the signal has been decoded, you enter the value in the results variable

results.value

To prepare to receive another IR signal

xxxxxxxx.resume ();

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