Chapter 2
Input / Output Management
Now that you have gained enough programming knowledge and some notions of electronics from the previous chapter and in this chapter, we will look at the use of the Arduino board. I will tell you about the inputs and outputs of the card. We are going to start simply, so do not be surprised if you go quickly to reading the chapters.
Do not neglect the basics; otherwise, you may not be able to follow the more complex chapters. A piece of advice – make sure you understand everything before you move on – there is no rush; everyone should work at their own pace.
Our First Program
You have finally arrived at the fateful moment when you will have to program. But before that, I will show you what will serve us for this chapter. In this case, learn to use an LED and the reference, present on the Arduino.cc site, which will be very useful when you need to make a program using a concept that is not covered in this book.
The LED / LED Light-Emitting Diode
The question is not to know which abbreviation to choose, but to know what it is.
A LED / LED: Light-Emitting Diode, or "Light Emitting Diode”. It is an electronic component that creates light when an electric current flows through it. I made you buy different colors. You can, for this chapter, use whatever you want. You see, a photo of a red LED. The size is not real, its "head" (in red) is only 5mm in diameter. It is this component that we will try to light with our Arduino board. But before, let's see how it works.
I will call the light-emitting diode, throughout the book, an LED. An LED is a diode that emits light. So I will talk to you about how LEDs work at the same time as LEDs. There is very little difference between the two. The LED is simply a diode that emits light, hence the arrows on its symbol.
Direct Polarization
We speak of polarization when an electronic component is used in an electronic circuit in the "right way." In fact, when it is polarized, it is because we use it in the desired way.
To polarize the diode, we ensure that the current must flow from the anode to the cathode. In other words, the voltage must be higher at the anode than at the cathode.
Reverse Polarization
The reverse bias of a diode is the opposite of forwarding bias. To create this type of assembly, it is simply enough, in our case, to "turn over" the diode finally to connect it "upside down." In this case, the current does not pass.
Note: a reverse-biased diode will not burn out if used in the right conditions. In fact, it works "the same way" for the positive and negative current.
If you do not want to make your first diode go up in smoke, I advise you to read the next lines carefully. In electronics, we must take two parameters into account: current and voltage. For a diode, two voltages are important. These are the maximum voltage in forwarding polarization, and the maximum voltage in reverse polarization. Then, for the LEDs to work properly, the current is also important.
Maximum Direct Voltage
When using a component, we must get into the habit of using the "datasheet" ("technical documentation”), which gives us all the characteristics of the component. In this datasheet, we will find
something called "Forward Voltage” for the diode. This indicator represents the voltage drop across the diode when current flows directly through it. For a conventional diode (type 1N4148), this voltage will be around 1V. For a led, we will rather consider a voltage of 1.2 to 1.6V.
Well, to make our small assemblies, we will not quibble, but it is the approach to do when we design an electrical diagram and that we dimension its components.
Maximum Reverse Voltage
This voltage represents the maximum admissible difference between the anode and the cathode when the latter is connected "upside-down." In fact, if you put too much voltage on these terminals, the junction will not be able to support it and will go up in smoke. We find this voltage under the name of "Reverse Voltage" (or even "Breakdown Voltage"). If we take the diode 1N4148, it will be between 75 and 100V. Beyond this voltage, the junction breaks, and the diode becomes unusable. In this case, the diode becomes either a short circuit or an open circuit. Sometimes this can cause significant damage to our electronic appliances. Anyway, we will never handle 75V.
The Flowing Current
For an LED, the current flowing through it is important. If you connect the led directly to a battery, it will light up, then sooner or later, will eventually go out.
If we do not limit the current flowing through the LED, it will take the maximum current, and that is not good because it is not the maximum current that it can support. To limit the current, a resistor is placed before (or after) the LED. This resistance, expertly calculated, will allow it to ensure optimal operation.
But how do you calculate this resistance?
Simply with the basic formula, ohm's law. Little reminder:
Then, we will take for the example a supply voltage of 5V (at the output of the Arduino, for example) and a voltage at the terminals of the LED of 1.2V in normal operation. We can, therefore, calculate the voltage which will be at the terminals of the resistor:
Finally, we can calculate the resistance value to use:
And voila, you know the value of the resistance to use to be sure not to burn LEDs out of the arm. Is it better to use a higher value or lower value resistor?
Where do we start?
The Goal
The aim of this first program is to light an LED, however, it may feel somewhat tricky here but everything will be in control over time. The plan is to show you two or three things that can help you when you want to leave the nest and take the flight to new skies.
Equipment
To be able to program, you obviously need an Arduino board and an USB cable to connect the board to the PC. But to see the outcome of your program, you will need additional items. In particular, an LED and a resistor.
Production
With the pinout of the Arduino board, you will need to connect the larger tab to the + 5V (5V pin). The smallest tab being connected to the resistor, itself connected to pin number 2 on the card. We could do the opposite, connect the LED to the ground and turn it on by supplying 5V from the signal pin. However, components like microcontrollers do not like to deliver current too much, and they prefer to absorb it. For this, we will, therefore, prefer to power the LED by placing it at + 5V and putting the Arduino pin to ground to pass the current. If we put the pin at 5V, in this case, the potential is the same on each side of the LED, and it does not light up.
Create A New Project
To be able to program our card, we must create a new program. Open your Arduino software. Go to the File menu and choose the Save as... option:
What is it?
Arduino is a project in which the community is very active, offers us on its website a reference. But what is it? Well, it is simply the "user manual" of the Arduino language.
More precisely, an internet page on their site is dedicated to referencing each code that can be used to make a program.
How to use it?
To use it go to the page of their site. What we see when we get to the page is three columns, each with an element that makes up the Arduino languages.
Structure: this column references the elements of the Arduino language structure. It contains conditions, operations, etc.
Variables: As its name suggests, it brings together the different variables that can be used, and certain specified operations
Functions: Here it is everything else, but especially the read / write functions of the pins of the microcontroller (and other very useful functions)
It is very important to know how to use the documentation that Arduino offers us. Because knowing this, you can make programs without having previously learned to use one function or another.
Turn on our LED
1st Step
First of all, we must define the pins of the microcontroller. This step itself makes up two sub-steps. The first being to create a variable defining the pin used, then define if the pin used should be an input
of the microcontroller or an output.
The term const means that we define the variable as being constant. We change the nature of the variable which then becomes constant.
The term int corresponds to a type of variable. By defining a variable of this type, it can store a number ranging from - 2149531648 to +2197438647. That is enough for us.
We are, therefore, in the presence of a variable named led_red, which is a constant which can take a value ranging from -2147483648 to +2147483647. In our case, we assign this variable to 2.
When your code is compiled, the microcontroller will know that on its pin number 2, there is a connecting element.
Well, that is not enough to define the pin used. We must now say whether this pin is an input or an output. Yes, because the microcontroller can use some of its pins for input or output. It is fabulous. It suffices simply to interchange ONE line of code to say that it is necessary to use a pin as input (retrieval of data) or as output (sending of data).
This line of code precisely, let’s talk about it. It must be in the setup () function. In the reference, what we need is in the Functions category, then in Digital I/o. I/o for Input / Output, which means: Input / Output.
The function is pinMode (). To use this function, you must send it two parameters: The name of the variable that you defined on the pin.
2nd Step
This second step is to create the content for our program. Whoever is going to replace the comment in the loop () function, achieving our aim: turn on the LED.
Again, we do not snap our fingers to have the program ready. You have to go back to the Arduino reference to find what you need.
Yes, but now we don't know what we want?
We are looking for a function that will allow us to light this LED, so we have to find it. Let's be a bit logical if you don't mind. We know it's a function we need (I said it a moment ago), so we look in the Functions category of the reference.
If we keep our logical mind, we will take care of lighting an LED, so to say what is the output state of pin number 2 where which is connected to our LED. So it's a safe bet that it's in Digital I/o. Well, there is a suspicious function called digitalWrite (). In French, this means "digital writing." It is, therefore, the writing of a logical state (0 or 1).
What is the first sentence in the description of this function? This one: "Write a HIGH or a LOW value to a digital pin." According to our bilingual level, we can translate by Writing a HIGH value or a LOW value on a digital output. Bingo. This is what we were looking for.
What does "HIGH Value or LOW Value" Mean?
In digital electronics, a high level will correspond to a voltage of + 5V, and a so-called low level will be a voltage of 0V (generally ground). Except that we have connected the LED to the positive pole of the power supply so that it lights up, we must connect it to 0V. Therefore, a low state must be set on the pin of the microcontroller. Thus, the potential difference across the LEDs will allow it to light up.
Let's take a look at how digitalWrite () works by looking at its syntax. It requires two parameters. The name of the spindle that we want to put in a logical state and the value of this logical state.
We will therefore write the following code, using this syntax:
Code: C
digitalWrite (led_red, LOW); // write output (pin 2) of a LOW
state
If we test the whole code:
Code: C
const int led_red = 2; // definition of pin 2 of the card as a variable
void setup ()
// card initialization function
{
pinMode (led_red, OUTPUT); // initialization of pin 2 as an output
}
void loop () // main function, it repeats (runs) endlessly
{
digitalWrite (led_red, LOW); // write output (pin 2) of a LOW state
}
We see the LED light up... It's fantastic.
Now you know how to use the outputs of the microcontroller, so we can get down to business and make our LED flash.
Enter time
It is fine to light an LED, but if it does nothing else, it is not very useful. You might as well plug it directly into a battery (with resistance all the same.). So let's see how to make this LED interesting by making it flash.
For this, we will have to introduce the concept of time. Well, guess what? There is a ready-made function there again. Let's go to practice.
How to do this?
Have a look for yourself; it will help you learn
I let you search a little by yourself, and it will train you.
For those who have attempted to search and have not found the function, here it is: delay ()
A short description of the function will be used to pause the program for a predetermined time.
Use the Command
The function accepts a parameter, which is the time during which we want to pause the program. We must give this time in milliseconds. If you want to stop the program for 1 second, you will have to give the function at the same time, written in milliseconds, or 1000ms.
Practice: Flash an LED
So, if we want to make our LED flash, we will have to use this function.
The LED lights up. Then, we use the delay () function, which will pause the program for a certain time. Then we turn off the LED. We pause the program. Then we return to the start of the program. We start again, and so on. It is this command sum, which forms the process that causes the LED to flash.
Henceforth, get into the habit of making this kind of diagram when you make a program.
Now we need to translate this diagram, bearing the name of the organization chart, into code. You have to replace the sentences in each frame with a line of code.
The Program
Normally, its design should not cause you any problems. It suffices to retrieve the code of the previous program ("light a group of LEDs") and modify it according to our needs.
This code, I give it to you, with the comments that go well:
Code: C
// we keep the same start as the previous program
const int L1 = 2; // pin 2 of the microcontroller is now called: L1
const int L2 = 3; // pin 3 of the microcontroller is now called: L2
const int L3 = 4; //...
const int L4 = 5; const int L5 = 6; const int L6 = 7;
void setup ()
{
pinMode (L1, OUTPUT); // L1 is a pinMode output pin (L2, OUTPUT); // L2 is a pinMode output pin (L3, OUTPUT); //...
pinMode (L4, OUTPUT); pinMode (L5, OUTPUT); pinMode (L6, OUTPUT);
}
// we change the inside of the loop to reach our goal
void loop () // the loop () function executes the following code by repeating it in a loop
{
digitalWrite (L1, LOW); // switch on L1 delay (1000); // wait 1 second digitalWrite (L1, HIGH); // we extinguish L1
digitalWrite (L2, LOW); // we switch on L2 at the same time as we switch off L1
delay (1000); // we wait 1 second digitalWrite (L2, HIGH); // we turn off L2 and digitalWrite (L3, LOW); // we immediately turn on L3 delay (1000); //...
digitalWrite (L3, HIGH); digitalWrite (L4, LOW); delay (1000); digitalWrite (L4, HIGH); digitalWrite (L5, LOW); delay (1000); digitalWrite (L5, HIGH); digitalWrite (L6, LOW); delay (1000); digitalWrite (L6, HIGH);
}
As you can see, this code is very heavy and not practical. We will see later how to make it lighter. But before that, a TP arrives...
Millis () Function
We will end this chapter with a point that can be useful, especially in certain situations where we do not want to stop the program. If you want to flash an LED without stopping the execution of the program,
you cannot use the delay () function, which pauses the program for the defined time.
Limits of the Delay () Function
You have probably noticed that when you use the "delay ()" function, our whole program stops waiting for you to wait. Sometimes, this is not a problem, but in some cases, it can be more troublesome.
Imagine, you are moving a robot forward. You put your motors at medium speed, quiet, until a small button on the front is pressed (it clicks when you touch a wall, for example). During this time, you decide to make signals by flashing your LEDs. To make a nice flash, you turn on a red LED for one second and then turned it off for another second. This is, for example, what we could do as code:
Code: C
void setup ()
{
pinMode (motor, OUTPUT); pinMode (led, OUTPUT); pinMode (button, INPUT);
digitalWrite (motor, HIGH); // we start the engine
digitalWrite (led, LOW); // we turn off the LED
}
void loop ()
{
if (digitalRead (button) == HIGH) // if the button is clicked (we enter a wall)
{
digitalWrite (engine, LOW); // we stop the engine
}
else // otherwise we blink
{
digitalWrite (led, HIGH); delay (1000); digitalWrite (led, LOW); delay (1000);
}
Please note this code is not at all rigorous or even false in its writing; it serves to understand the principle.
Now imagine you drive, test the button that is not pressed, so blink the LEDs (case of else). The time you do, the entire display runs out two long seconds. The robot has been able to take the wall in the middle of this pearl for eternity, and the motors continue to advance head down until smoking. It is not good at all.