Basic Course about ARDUINO - Lesson 4



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


We have seen in lesson n. 1 that the LED is made up of a particular semiconductor material which, when passed through by current, emits light. It is called a semiconductor because it conducts and therefore emits light only if the voltage is applied correctly, that is, the anode (the longest terminal) must be connected to the positive voltage, while the cathode (the shortest terminal) is connected to ground.

The semiconductor part takes up less than a millimeter, so what we see is the external part of the lens that diffuses the light. We have seen that the LEDs can be of different colors, the RGB LED is essentially composed of three LEDs, so it emits three different wavelengths of light, one red, one green and one blue (hence the abbreviation RGB: Red, Green and Blue).

The emission of light with a different wave frequency depends on the type of substance used as a semiconductor in the LED. The table above shows, depending on the color of the LED, the substances used, the wavelength and also the threshold voltage which also represents the maximum voltage applicable to the LED, otherwise it will be damaged or may even burn.

For this reason, as mentioned in the Be Maker 01 module, the LED must be protected and thanks to the above table we can also calculate which is the most appropriate resistance value to use. In fact, it can be observed that the threshold voltage is generally, for the various LEDs, about 2 V. Considering that the maximum current for an LED is about 15 mA, i.e. 0.015 A, applying Ohm’s Law:

Once the value has been calculated, take the “commercial” resistance value closest to the calculated value. The commercial values are the standard resistance values that we can find on the market which are summarized (the main ones) in the table below:

The RGB LED can have different appearances: as a normal 5 mm LED, with four terminals, or in strips of the SMD type (with four cables), or mounted on a base (RGB module) with four PINs.

The three main colors mixed, in different intensities, produce practically infinite combinations of colors:

There are two types of RGB LEDs, one is with COMMON CATODE (common negative which is connected to GND) and the other is with COMMON ANODE (common positive and therefore connects to 5V).

In the common anode type (+), there will be three negative terminals each representing the single color of the emitted light, in the common cathode type (-), there will be three positive terminals and each of them represents the single color of light. In series with each LED (and therefore at each terminal with the exception of the common anode or the common cathode) a resistor is inserted that will allow you to adjust the current circulating in the diode, for which you will need three 220 Ohm resistors (red circles, red, brown).

The internal circuit of an RGB LED can be represented as below.

The wiring diagram with the three protection resistors is instead:

Project 12 - RGB LED with Arduino

For this project we need:

The wiring diagram is:

For assembly, follow the diagram below:

Once the circuit has been assembled, the sketch to be written and uploaded to Arduino is:

Once the code has been written, launch the verification precompilation (check mark), it will ask you to save the sketch (you can change its name) and then click on the arrow for loading and on the lens for the serial monitor:

Once the sketch has been loaded on Arduino and opened the serial monitor, the RGB LED shows the R-G-B color combinations according to the values represented on the monitor.

Video-Project 12 - RGB LED with Arduino

CURIOSITY: The stars shine and change color

The stars “shine” and change color because it is a phenomenon that depends on the fact that the light of the stars, before reaching your eyes, must pass through the atmosphere.

The atmosphere is made up of many air cells. Each cell moves with a random motion, rising or falling due to thermal convection: it is called atmospheric turbulence.

When the star’s light passes through these cells of air, the path of its rays is randomly deflected and this causes you to see the star apparently flash and sometimes change color.

The lower the star is on the horizon or the brighter it is, the more intense the phenomenon.

Obviously, if we observed the starry sky from the Moon, since there is no atmosphere on the Moon, this optical phenomenon would not exist.

Project 13 - The light of the stars

For this project we need:

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 code has been written, launch the verification precompilation (click on the check mark), it will ask you to save the sketch (you can change its name) and then click on the arrow for loading and on the lens for the serial monitor:

Once you have started the sketch and opened the serial monitor, you can see on the screen which color of the RGB LED is active and the relative intensity value, at this point by acting on the potentiometer you will change the color of the RGB LED because you are changing the intensity of the color. active.

To change the active color and therefore the color to be modified, you can press the button, the new active color will also be shown on the screen and so on.

In this way you can mix the three RGB colors at will and obtain the desired color by simulating the change in the color of the stars.

Video-Project 13 - The light of the stars

Analysis of the sketch: The light of the stars

In this sketch there are many new and interesting things to learn, let’s start with the simplest one:

Use of “INPUT_PULLUP“; as you can see in the void setup the digital input pin has been set which controls whether or not the button has been pressed, such as INPUT_PULLUP. This declaration allows the Arduino to prepare itself to receive an unstable signal such as that coming from pressing a button. Then Arduino will try to stabilize the input signal.

Use of the control structure switch (..)… .. case…; in one of the previous projects we had seen the use of the “nested” if structure, but I also said that this way of making decisions by the computer was not very recommended especially when there are many cases to be examined. The use of the switch … case … simplifies the structure, in fact it is enough to define for each individual case what to do and then the selection of the case is done directly to the switch function .. The general syntax of the control structure is:

switch (var) {

case label1:

// statements


case label2:

// statements



// statements



where var is a variable of type int or char and indicates the number of the “case” you want to process, while label1 is the label, also a variable of type int or char. In the syntax example there are only three cases, one of which is default. The last case, the default one is selected when there is none of the previous cases. The structure may also have a higher number of cases than those represented.

Use of the call of an external “procedure”; in the sketch you can see that in the void seup, in the final part, there is: “intensity_color (color); “In this way we are going to call a procedure external to the void setup that executes a particular process.

As you can see the external procedure has been defined with the “void” also since in the procedure there is the control structure switch … case … to the same procedure, through its call, we are also passing it a parameter (put in round brackets) that it will use in its process.

In fact, the procedure is called putting name_procedure (eventual_parameter); , the definition of the procedure is:

void name_procedure (int any_parameter) {process}

Project 14 - The light of the stars 2

This project is the natural continuation of the previous one, in fact with this project I want to visually highlight, instead of reporting it on the serial monitor, what is the color on which we are working with the adjustment of the potentiometer. And this can be done by integrating three LEDs with the relative protection resistors to the previous project. For this project, therefore, we need:

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 is started, in addition to the RGB LED turning on, the LED will also turn on and its intensity can be changed using the potentiometer. By pressing the button it is possible to change the active color that can be modified and this will be represented by the relative colored LED.

Video-Project 14 - The light of the stars 2

The Tilt Sensor or also called the Ball Tilt Sensor

This sensor can be found on the market as a simple sensor or even as a pre-assembled module. The operating principle is the same and is basically a digital signal that tells us if the sensor has intervened or not.

The principle of operation is represented by the animated GIF image that I made:

inside a casing there are two terminal plates and a metal sphere, when the casing, which has the cylindrical shape, is moved, the sphere touches the two plates and closes the electrical contact between the terminals.

Basically it behaves like a button, except that the contact closes because the ball rolls inside the cylinder.

By appropriately positioning the terminals and sizing the casing, it is also possible to choose with what degree of minimum inclination the sphere puts the two terminals in contact. In the following project we see an application and how to connect it to Arduino.

Project 15 - Making an electronic hourglass.

For this project we need:

The wiring diagram is as follows:

For assembly follow the diagram below:

Be careful when positioning the tilt sensor, it must be positioned in such a way that in the initial position it causes contact and by rotating it by 180 ° it does not, otherwise the desired effect is not obtained.

At this point, click twice on the Arduino icon on the Desktop and the IDE opens and copy the following sketch, please follow each step indicated:

Once the sketch has been loaded, Arduino will check the status of the Tilt Sensor, and will appropriately orient the “sand in the hourglass”, that is, it will turn on the appropriate LEDs, obviously once this phase is completed, that is, that the LEDs are all lit on one side, then for restart the hourglass, it will be necessary to flip the tilt sensor to start the hourglass in reverse.

Video-Project 15 - Making an electronic hourglass

Analysis of the Sketch: The Electronic Hourglass

As you can see from the sketch, in the setup part a “for” loop is used to set the PINs to which the LEDs are connected, obviously they are all set in OUTPU because they generate a signal that can be high (LED on) or low state (LED off).
The PIN to which the Tilt Sensor is connected is set as INPUT_PULLUP to prepare the Arduino to best stabilize the signal coming to it on that PIN.
The delay instruction which in its classic syntax is: delay (milliseconds); it is used to mark the time and therefore to simulate the sand coming down from the hourglass.
In fact, as you can see from the sketch, the project is set for a time equal to one second (i.e. 1,000 milliseconds), the time that passes to switch off an LED in the upper part of the hourglass and switch it on in the lower part (transfer of sand from above to below).

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