Basic Course about ARDUINO - Lesson 2
TOPICS INDEX
- Warnings
- Copyright Notice
- Insights into electrical resistance
- How to read the value of a resistance
- 4-circle colored resistors
- 5-circle colored resistors
- Curiosity: What is a physical quantity and the uncertainty in the measurements
- Project 5 – The Traffic Light with Arduino
- Introduction to Coding
- The sketch
- Project 6 – Lo Stelliere
- Analysis of the sketch: Lo Stelliere
- Analog PINs of Arduino
- Project 7 – Ohmeter. Use Arduino to measure a resistance
- Analysis of the Sketch: Ohmeter with Arduino

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.
Copyright Notice
All trademarks shown belong to their legitimate owners; third party trademarks, product names, trade names, corporate names and companies mentioned may be trademarks of their respective owners or registered trademarks of other companies and have been used for explanatory purposes only and for the benefit of the owner, without any purpose of infringement. of the Copyright rights in force. What is reported in this document is the property of Roberto Francavilla, Italian and European copyright laws are applicable to it – any texts taken from other sources are also protected by copyright and property of their respective owners . All information and contents (texts, graphics and images, etc.) shown are, to the best of my knowledge, in the public domain. If, unintentionally, material subject to copyright or in violation of the law has been published, please notify us by email to info@bemaker.org and I will promptly remove it.
Roberto Francavilla
Insights into electrical resistance

We saw in the previous lesson, referring to a watercourse, that the electrical resistance can be defined as the “obstacle” that the water, that is the electric current, encounters during its natural path in the riverbed, or electrical cables.
Obviously, it is easy to understand how this difficulty of the water current (but also of the electric current) depends on various factors, i.e. the length of the path (length of the electric cables), the width of the river bed (section of the cables) and the type of land that makes up the river bed (from the material used for the electrical conductors, whether in copper, aluminum, or other material).

In fact, the further along the river’s path, the higher the resistance; the larger the section of the riverbed (of the electrical cables), the more easily the water flows inside it, so the resistance is lower; if the riverbed consists of fine and compact sand or a well-smoothed surface, it will offer less resistance to water than a riverbed composed of irregular rocks, in the same way there are materials that have such properties as to be crossed by the electric current better than others.
For example, gold is an excellent electrical conductor, as are silver, copper, aluminum … (all metals are good conductors), while there are materials that hinder the passage of electric current to such an extent that be called “insulators” such as plastic, rubber, …
Another way to visualize the electrical resistance in order to fully understand the physical principle is the one represented in the figure below:


The amount of water contained in the two tanks is initially the same and the water level represents the electric potential, that is, the force with which the water is pushed out of the pipe. The narrower the outlet pipe is, the more difficult the water will have to exit.
Taking this argument to the extreme, if we were to have such a small tube (imagine a hair wide) it is obvious that the water will have a lot of difficulty getting out. The tube that represents the electrical conductor, if it were a hair, will obviously almost totally prevent the water from escaping, in this case the material constituting the conductor is called insulator and therefore the current that flows through this conductor is practically zero.

The electrical resistance, whose symbol is a zig-zag line, is indicated with the capital letter “R”, then a subscript “1”, “2”, … especially when there are several electrical resistances of different values, the electric current that runs through it is indicated with the “I”, while the electric voltage at the ends of the electric resistance is indicated with a “V” (remember the waterfall seen in Lesson 1 …. the water falls from the highest point “+ “At the lowest point” – “).
Observe that the voltage V is conventionally indicated with an arrow contrary to the direction of the current and the tip of the arrow of the voltage corresponds to the “+”. The current, always conventionally, goes from the positive “+” pole to the negative “-” pole.
On the market, electrical resistors are easily available components and are of different types depending also on the currents they must be able to withstand. In reality, the value of the resistance you want and the electrical power must be indicated to the seller (the current is not indicated). Electric power is another important electrical quantity in the field of electrical engineering and in the case of DC powered circuits, as in our case, it is indicated with P and is measured in Watts (electrical symbol “Ω”). The electrical power is calculated by making the product between the value of the voltage applied to the resistance and that of the current that passes through it, but it is also equal to the product of the electrical resistance and the value of the current that crosses it squared, that is:

For our projects we will generally refer to electrical resistance values ranging from a few hundred Ohms up to a few tens of miles of Ohm and we will use the resistance to adequately distribute the electrical voltage generated by Arduino, i.e. 5 V, in in order to make our components work at their best in the various electronic circuits that we will develop. Also for Ohms, as for other units of measurement of physical quantities, there are multiples and submultiples. However, we will always stay in the field of multiples, that is, we will often use the “kOhm” (it reads kiloOhm) [it is a multiple of Ohm, that is, 1 kOhm corresponds to 1000 Ohm]. So when I talk about tens of miles of Ohms, I mean tens of kOhms. The current values that circulate in the circuits that we normally make are very low, in the order of milli-Ampere (mA). From this you understand very well that the electrical powers involved are very low in the order of 0.25 W or at most 0.5 W (on the market, electrical resistors capable of withstanding powers of this type are indicated with ¼ of Watt or with ½ of Watt).
The figure below shows how the resistor we use in our circuits is made.

There is a ceramic support on which a wire of a particular conducting material is wound that has its own electrical resistance, the number of revolutions represents its length and therefore the total resistance. The conductor wire ends at the ends on two plates with two metal terminals which are the output connectors. The whole is then covered with another ceramic material. The ceramic material is insulating and therefore, in addition to mechanically protecting the resistance, it prevents the creation of short circuits.
How to read the value of a resistance
The resistance value is indicated on the outer casing by means of colored circles or colored bands.

Let’s say immediately that there are different methods used to indicate the resistance value, among the most common ones, of which standards have been created, are the indication of the value with 4 circles in total and the one with 5 colored circles in total, the one with 6 circles is less common, but still used above all for laboratory components.
The table below shows the correlation between circle colors and resistance values. I would like immediately and in any case to point out that one of the circles is always further apart from the others (in the case of a 6-circle resistor, the most widely spaced circles are 2).
The farthest circle indicates the tolerance, to read the colors we start from the first circle on the opposite side to the spaced one.

4-circle colored resistors
The first circle indicates the value of the first digit, the second circle indicates the value of the second digit and the third circle indicates the multiplier, and finally the fourth circle indicates the tolerance.
Let’s take an example to understand how to apply the resistance enhancement criterion.
Suppose we have a resistance like the one in the figure below:

The first thing we must identify is the most spaced circle which is “gold” in color, this circle represents the tolerance on the resistance value of the component, in this case the tolerance, ie the uncertainty on the value, is +/- 5 %.
Then let’s see the color of the first circle on the opposite side to the more distant one, the color is “brown”, that is, the first digit has a value of “1”, the second circle is “black”, so the second digit has a value of “0” and the third circle is “red”, that is a multiplication factor “x 100”.
At this point we know the value of the resistance, in fact:
R = 1, 0, x 100, +/- 5% i.e. 10 x 100 +/- 5% and therefore R = 1.000 Ohm +/- 5%
Our resistance is one thousand Ohms, that is, one kilo Ohm with a tolerance of 5%.
Since we Makers like mathematics, let’s also calculate, in terms of values, what it means to have a tolerance of 5%:
We calculate 5% of 1,000, i.e. 5 x 1,000 and then divide by 100, so we get 50, so:
R = 1.000 +/- 5% Ohm means that the value of R is definitely a value between (1.000-50) 950 and (1.000 + 50) 1.050 Ohm.
5-circle colored resistors
The first circle indicates the value of the first digit, the second circle indicates the value of the second digit, the third circle indicates the value of the third digit and the fourth circle indicates the multiplier, and finally the fifth circle indicates the tolerance.
Also in this case we make an example to understand how to apply the resistance enhancement criterion.
Suppose we have a resistance like the one in the figure below:

Also in this case the first thing we must identify is the most spaced circle which is “brown” in color, this circle represents the tolerance on the resistance value of the component, in this case the tolerance, that is the uncertainty on the value, is of +/- 1%.
Then let’s see the color of the first circle on the opposite side to the more distant one, the color is “red”, that is, the first digit has a value of “2”, the second circle is “purple”, so the second digit has a value of “7”, the third circle is “black”, so the third digit is “0” and finally the fourth circle is “orange”, that is a multiplication factor “1 K” that is ” x 1000 “.
At this point we know the value of the resistance, in fact:
R = “2”, “7”, “0”, x 1.000 +/- 1%, that is R = 270.000 Ohm or also R = 270 kOhm (kilo Ohm), with a tolerance of 1%, that is: 1 x 270,000: 100 = 2,700 Ohms. So the true value of the resistance is a number between 267,300 Ohm and 272,700 Ohm.
Since we are not building Space Shuttle, for our projects having tolerances of 1%, 5%, but even 10% does not involve making significant errors, so from now on we will consider tolerance only a qualifying element of the component ( i.e. we will say that a component with a tighter tolerance is better than one with a wider tolerance), but we will not take this into account in our calculations.
One last example:

The first circle is red, so 2, the second circle is still red and then another 2, the third circle is black, so the third digit is 0 and finally the fourth circle is black, so the multiplier is by 1. So the R = 220 x 1 that is 220 Ohm.
Curiosity: What is a physical quantity and the uncertainty in the measurements
A physical quantity is a property of a body (or phenomenon) that can be measured. To specify the physical quantity you need a number (which indicates its value) and a unit of measurement. The unit of measurement is a reference quantity of a quantity, to which the value of 1 is assigned.
Let’s take an example to better understand what I wrote:
One of the fundamental quantities is the length to which the meter [m] has been assigned as the unit of measurement.
But what is the meter?


In France in Sèvres there is a shaft that has a length of 1 in 10 million of the length of the arc of the terrestrial meridian which starts from the North Pole and passes through Paris, stops at the Equator. This rod, thus determined, represents the length of 1 m. Obviously, for comparison, the “meters” used when we want to take measurements of an object have been reproduced.
In fact, to measure a length, we just see how many times the meter is contained in the length to be measured.
Obviously when measurements of quantities are made, there are also errors, which can be instrumental errors due to the imperfect calibration of the instruments, reading errors of the instrument (the so-called parallax error) … we are obviously talking about small errors that are related to the measure of size. These errors therefore introduce a small uncertainty in the measurement and therefore there is the so-called tolerance in the measurement.
Project 5 - The Traffic Light with Arduino

Let’s continue with the use of Arduino digital PINs and create a simple project that will entertain us a lot. We create a traffic light with Arduino.
For this project we need:
The wiring diagram is:

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.

Since when you open the IDE it usually loads the last written sketch, we start with a clean sketch so as not to create confusion. To do this, click on “File” and then on “New”:

After creating a new file, let’s recopy the sketch below:
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 to upload:

The result will be: the red LED turns on and stays on for 6 seconds, then the red one turns off and the green LED turns on. After another 4 sec. the yellow LED also lights up and stays on for 2 sec. Then the yellow and green LEDs turn off and the red LED lights up again.
If you place the cardboard with the traffic light, previously printed and cut out, it will surely give you a nice effect.

Video-Project 5 - The Traffic Light with Arduino
Introduction to Coding

The time has come to enter the world of Coding and the first thing I want to introduce is the sketch. But first let’s take a step back to understand what we mean by Coding.
The term Coding was born a few years ago and as the name suggests it derives from “codify” or generate codes, which in turn, in IT terms, refers to Computer Programming (or more generally: electronic devices) using a particular programming language.
So, in summary, the term “coding” refers to computer programming and therefore to the design and development of software.
Said so … you will tell me, bravo! you copied the definition from the first year high school computer book….

…. but really: what does it mean to Program a Computer or an electronic device ????
So I try to explain better … and to explain it, I don’t want to start from the birth of Computers, but from a concrete example.
Suppose we have an industrial process that we want to automate, for example the production of corks for wine bottles …


The first thing we do is to identify the entire production process in detail: take the cork sheet, transfer it to the press, etc. ….
then we define a very precise ordered sequence of actions …

The various actions, although in a well-defined sequence, are linked together and the link is called an algorithm.
For example: before operating the press check that the cork sheet is positioned correctly, if yes, then proceed with pressing, otherwise stop….
The one described above is a logic type algorithm…. But there are also mathematical ones …
then we define a very precise ordered sequence of actions …

Once the individual steps of the process and the algorithms that regulate them have been determined, then we move on to translate everything into the programming language and then we have the production of the software that installed on the control machines and on the PCs, create the desired automated process. .
Coding… means this!
Good! After this introduction, let’s move on to our Arduino development platform and let’s see the sketch …
The sketch
You have certainly clear, at least from the latest projects carried out, that Arduino, since it is nothing more than a minicomputer, needs to be programmed to execute what we want, and this happens in its development environment called IDE.

In fact, it is precisely in the sketch that we will write, in a particular language, step-by-step, what Arduino will have to do for us.
The programming language we will use for the sketches is basically C ++
The sketch consists of two parts, one starting with “void setup () {…….}” and the other with “void loop () {…….}” both parts enclose the instructions that Arduino will execute in two curly brackets . And in particular, in the part enclosed by “void setup () …” as the name “setup” also says, the “variables” are defined, the Arduino PINs are “set”, libraries are recalled, etc …, however they are written all those instructions and / or all those operations that need to be performed at least once. While in the “void loop ()…” part all the instructions that Arduino will execute in an infinite loop are inserted.
Project 6 - Starmaker

With this project we learn how to activate an event on Arduino by simply pressing a button. The event, in this particular case, is the lighting of an LED, but it could also be an entire starry sky!
For this project we need:
The wiring diagram is:

For assembly, follow the diagram below:

After the connections, let’s move on to writing the sketch. Connect the Arduino to the PC via the USB cable and launch the Arduino IDE application and write the following code …
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 to load.

Once the sketch has been started, each time you press the button, Arduino commands the LED to turn on. Position the printed card so that the yellow LED comes out of the star and you have become a Starmaker!
From this project onwards, in order to learn the programming language for the realization of the skatches, we also begin to analyze the sketches of the projects that we will gradually realize.

Video-Project 6 - Starmaker
Analysis of the sketch: Starmaker
Analyzing the sketch, we can observe several interesting things, first of all the usefulness of inserting comments. Comments (which will be ignored by Arduino) can be entered in two different ways. The first is with the use of the divided “/” and the asterisk “*”, in particular they are placed at the beginning and at the end of everything that Arduino must ignore as a comment:
/* [comment] */ . This method is used when the comment spans multiple lines.
The second method is to put the double divided “//”, in this way everything to the right of the // and for the entire line, is ignored by Arduino and not executed because viewed as a comment. This mode is used when the comment is on the same line as an instruction to describe what the specific instruction does.
Another interesting thing that I want to highlight about the sketch is the definition of the digital PINs in the void setup (), we have said that the digital PINs can take on two values: HIGH and LOW (or even 0 and 1) and we have said that the digital PINs of Arduino ranging from n. 0 to no. 13 can be INPUT and OUTPUT PINs. In this project, the digital PIN 1 is INPUT, in fact when the button is pressed the voltage is brought to the PIN, so it receives a signal that we read with the digitalRead instruction. The digital PIN 3, on the other hand, provides a LOW or HIGH signal with the digitalWrite instruction, so it is a PIN in OUTPUT. This functionality of the digital PINs in the sketch, whether of INPUT or OUTPUT, must be declared in the void setup ().
Analog PINs of Arduino

Arduino has 6 Analog PINs named A0, A1, A2,… A5. Analog PINs, like Digital PINs, can be configured as inputs or outputs according to their use.
A clarification must be made: an analog quantity can actually assume infinite values, since Arduino is essentially a minicomputer and is therefore characterized by digital quantities, it cannot reproduce the infinite values required by an analog quantity, but the quantity is “discretized ”And reduced to a value between 0 and 1023, ie in a range of 1024 possible values. Since the quantities read or reproduced by the Analog PINs are actually electrical voltages and knowing that Arduino has a maximum voltage of 5V at the output of its PINs (both digital and analog), it is immediately clear that the value 0 is associated with 0 V and the value of 1023 to 5V.

At this point, by applying a simple proportion, we can obtain the value of the quantity (if the value of the analog PIN is known), or the value of the analog PIN if what is known is the value of the quantity. Let’s see the application of this concept in a Project.
Project 7 - Ohmeter. Use Arduino to measure a resistance

Suppose we have a resistor whose bands are not readable or have canceled, the question is: how do we determine the resistance value of an unknown resistor?
This project shows the use of Arduino when you want to know the value of a resistance of which for any reason the colored bands above the component are not shown. We need to:
The wiring diagram is the following. We use a 220 Ohm resistor as unknown resistance indicated with Rx:

For assembly, follow the diagram below:

Note: 220 Ohm resistor was used as unknown resistor.
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 code has been written, launch the verification precompilation (check mark), it will ask you to save the sketch (you can change the name) and then click on the arrow to load and then on the lens to access the serial monitor.

Started the serial monitor, the result is this:

If you remember the tolerance…. so the measured value (214.71 Ohm) will always be slightly different from the theoretical one of 220 Ohm…. in fact, if you apply the percentages formula [(220-214.72) / 220] * 100 = 2.4, then you get an error of about 2.4%, i.e. within the allowed tolerance which is the one declared by the manufacturer of the resistance (+/- 5%), otherwise you could have asked for a refund …
Suggestion ! : When you use Arduino to make measurements, always check the correct functionality of the components, otherwise you risk going crazy … for example, I happened to use a 1 kOhm resistor which was actually 10 kOhm! So I came up with values ten times smaller than expected. I solved the problem by checking the actual value of the resistance with a tester and then I realized that a lot of resistors bought and sold for 1 kOhm, were actually 10 kOhm.
Video-Project 7 - Ohmeter. Use Arduino to measure a resistance
Analysis of the Sketch: Ohmeter with Arduino
Analyzing the sketch of the Ohm meter, we observe the following:
In the “void setup () {…….}” enclosed by the two curly brackets there is the following instruction:
Serial.begin (9600);
this instruction sets the communication speed between the Arduino and the PC at 9600 bits per second, i.e. the maximum communication speed on the COM port between the PC and Arduino. This instruction is inserted when you want to view the values of the variables on the serial monitor in the IDE. The variables are empty containers that are filled during the execution of the sketch (we will see this topic better in a subsequent lesson).
The “analogRead (…)” instruction is one of the most important instructions in the Arduino language, in fact through this instruction it is possible to read the values of the analog PINs present on the Arduino. The correct syntax is the following (“read” is an arbitrary name assigned to a variable):
int reading = analogRead (A0);
At the end of each instruction there is the “;” , the semicolon indicates that the instruction is finished and the Arduino will move on to the next one. After this instruction, the sketch running on Arduino reads the value on the PIN and assigns it a number from 0 to 1023 (ie 1024 values), where “0” if the voltage value read at PIN A0 is 0 V and “1023” , if the voltage read is 5 V (i.e. the maximum voltage that can be produced on the PIN by the Arduino). For intermediate numbers at these two extremes, and therefore to have the intermediate voltage values read on the PIN, it is necessary to carry out the proportions… .. Holy Mathematics !!!
In fact, for example, suppose that the value read on the PIN is “184”, at this point we apply the proportion:
1023: 5 = 184: x
(we read 1023 stands for 5 Volts as 184 stands for X Volt, where X is the unknown)
The formula for solving this proportion tells us that to derive the unknown value X, we need to multiply the inner members of the proportion and divide by the outer (known) member, so:
X = 5 * 184/1023 = 0.899 V
Therefore the voltage measured by Arduino on PIN A0 is about 0.9 V. Note the voltage, applying Ohm’s Law, seen in the previous Lesson, the value of the unknown resistance is obtained. I’ll stop here with the explanation of the formulas used, because I don’t want to bore you with further calculations that we will see in other projects anyway.
At this point we see the last two instructions:
Serial.print (“Output voltage:”);
Serial.println (Vout);
The Serial.print Instruction (“….”) prints on the screen, on the serial monitor, the content between the “” (quotation marks) and the printing takes place on the same line. To access the serial monitor, as previously mentioned, click on the lens. The Serial.println (…) instruction prints and then goes to the line break for the next print.
The Serial.println (Vout) instruction; prints the value contained in the Vout variable (and then wraps to the next line).
If you found the lesson interesting, make a donation you will help me realize many others.
