BASIC COURSE ABOUT ARDUINO - LESSON 9

INDICE

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

The display for Arduino

We dedicate this lesson to the immense world of displays that can be used with Arduino, obviously we will focus on those that actually turn out to be important to know from the point of view of the number of possible applications and we will leave out, at least for the moment, those that have specific uses or those particularly expensive.

First of all, let’s try to understand when we need a display: the answer is simple! all those times when we need immediate feedback from Arduino that would otherwise require: connection with a USB cable, the availability of a PC, etc …

For example, when we are monitoring a quantity, or when we want a PIN status … obviously I am not referring to the debug phase, because in that case I can easily use the Serial Monitor or the Serial Plotter that makes the IDE available to us, but, for example, when constantly monitoring the temperature of an environment, the voltage across a component, etc….

7-segment LED display.

As the name also implies, the display consists of 7 LED segments that are activated individually to dial the digit (i.e. the single number) that we want to show. This type of display is used for the countdown (starting from 9, of course), to signal the status of a PIN, to indicate whether a process has been successful or not, etc.

Since the segments are full-fledged LEDs, they follow the normal rules of LEDs if powered by Arduino, i.e. they must be protected with 220W resistors. Let’s also remember that inserting resistors in series with the LEDs reduces the current supplied by Arduino while preserving it. functionality.

The Display is characterized by the following pinout with the relative identification of the segments:

The display can be of the “common cathode” type, then the GND will go to the common (CC) and the individual PINs must be set to HIGH to make the relative LEDs light up, or, on the contrary, for the common anode version, at the common PIN (CA) the voltage must be brought to 5V and the individual PINs must be placed in the LOW state to make the relative LEDs light up.

Project 30: Let's make a Countdown.

For this project we need:

The wiring diagram is as follows:

The 8 resistors R are 220 W.

The assembly scheme to be made is the following:

Note: The common PIN is connected to the Arduino GND because a “common cathode display” is being used.

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

Once the sketch has started, the display will light up the figure nine and after every second, it will decrease by one until it reaches zero and start over.

Video-Project 30: Let's create a Countdown.

Analysis of the Sketch: Project 30: Let's make a Countdown.

In this sketch there are no particular things to report even if it is very long. In fact, the many lines of written sketches are used only to drive the 7 LED seeds individually for the composition of the numbers and nothing else.

It should be remembered that if we had a common Anode display available, then those that are the PINs with HIGH status would be PINs with LOW status and vice versa.

4-digit 7-segment LED display.

The 4-digit 7-segment LED display works very similar to the single-digit one, the only difference is that in addition to the PINs that turn on the individual LED segments, there are additional 4 PINs to the display which are used to enable the reference digit.

The layout of the display with the various PINs is as follows:

The PINs named A, B, C, D, E, F and G are the ones that turn on the segment LEDs, the PIN named dp is the one that turns on the dot LED and those named D1, D2, D3 and D4, are those which activate the single digit. Obviously, since in this way we could drive only one digit at a time, in case we wanted to represent a number with two or more digits, we will always make the single digits light up one at a time, but at such a speed that in our eyes it will seem that they are the two , or three, or the four digits lit at the same time … cartoon style created by quickly scrolling through the pages of the individual drawings.

Also these displays can be found on the market in the Common Cathode or Common Anode type. In the case of the Common Cathode display, then the D1, D2, D3 and D4 are placed in the LOW state (one at a time) and the PINs A, B, C, D, E, F and G (and dp, if wants to use the dot), are placed in HIGH state.

In the case of a Common Anode display, the opposite occurs.

Project 31: Seconds counter.

With the KY-035 Analog Hall magnetic sensor module it is possible to continuously detect the magnetic field as it is a sensor module that has an analog output.

For this project we need:

The wiring diagram is as follows:

For the assembly it is necessary to 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.

Once the sketch has been started, the seconds elapsed since the Arduino sketch was started will appear on the display.

Video Project 31: Seconds counter.

Project 32: Seconds and tenths of a second counter.

Keep the same wiring and assembly diagram as the previous project.
Install the new SevSeg Library in the IDE:
Go to Sketch, click on #include library and click on Library Management and write in the dialog box the name of the library of interest called SevSeg and then, once found, click on install and the library will be installed.

Once the Library is installed, copy the following code into a new sketch:

Upload the sketch on Arduino you will see that at the end of the upload the display will start counting the time elapsed since the launch of the program on the Arduino (the seconds and tenths of a second will be indicated)

Video-Project 32: Seconds and tenths of a second counter.

Analysis of the sketch: Project 32 - Stopwatch using the Library.

In this sketch there are many interesting things, let’s see the most interesting ones. First of all, you can observe that the use of the SevSeg Library drastically reduces the code instructions to be written as special functions are used to manage the 4-digit seven-segment LED display.

Among these instructions we find:

hardware byteConfig = COMMON_CATHODE;

which tells Arduino what type of display we are using if common cathode or common anode (in this case we would have written COMMON_ANODE).

With instructions:

byte digitPins [] = {4, 3, 2, 1};

byte segmentPins [] = {10, 9, 6, 7, 8, 11, 12.5};

let’s tell Arduino how our display is connected to its pins. In our particular case: Diital pins D1 = 4, D2 = 3, D3 = 2, D4 = 1 Segment pins A = 10, B = 9, C = 6, D = 7, E = 8, F = 11, G = 12 and dp = 5.

With instructions:

bool leadingZeros = false;

we tell Arduino if there must be zeroes in front of the figure we want to show on the display (in this case it was necessary to write “true” or not).

With instructions:

bool disableDecPoint = false;

we tell Arduino that we intend to use the decimal point of the display (otherwise we would have written “true”)

With instructions:

sevseg.setNumber (deciSeconds, 1);

we tell Arduino that the numbers it has to show on the display are numbers with only one decimal.

Let’s see instead the millis () function: when it is called it returns the time elapsed, in milliseconds, since the sketch started on Arduino. Since we want tenths of a second and after ten tenths of a second the second must be triggered, the form to get this is:

if (millis () – timer> = 100) {

timer + = 100;

deciSeconds ++; // 100 milliSeconds is equal to 1 deciSecond

if (deciSeconds == 10000) {// Reset to 0 after counting for 1000 seconds.

deciSeconds = 0;

}

Furthermore, after 999.9 seconds, the counter resets and restarts from zero. Attention, the Arduino Reference Book says that the type of value returned by the millis () function is “unsigned long”, also to make the variable fixed for the call of the millis () function, which is updated from time to time, it must be defined also of the “static” type.

So the correct form is:

static unsigned long timer = millis ();

For more information on the use of the library and instructions in C ++ you can refer to the Readme file that accompanies the library and for the other instructions, to the Arduino reference Book [Refence Book click here], while for the mills () function there is it is an in-depth project, again in this lesson.

Project 33: Potentiometer value display

The applications of a 4-digit display are truly innumerable, in this project it is used to numerically represent the value that the potentiometer assumes at that moment. The diagram to be created for the display part is identical to the previous one so you can use the same wiring:

For assembly, follow the diagram below:

Basically it is the same scheme of the previous project with the addition of the 10K potentiometer. At this point, open the Arduino IDE and write the following sketch:

Video-Project 33: Potentiometer value display

Project 34: Insight into the millis () function

The millis () function, as mentioned in the previous paragraph, once called in the form:

unsigned long timer = millis ();

returns the time elapsed, in milliseconds, from the start of the sketch loaded on Arduino, but this definition does not give it the right importance.

First of all, we can observe that the variable that contains it is of the “unsigned long” type, that is, it can take on a maximum value of 4,294,967,294, so if we divide by 1,000 and then by 60 and again by 60 and then by 24, it comes out that the millisecond counter, before zeroing, it is necessary to spend 49.7 days with Arduino always on and running the same sketch. Only after about 50 days, the variable is reset and starts counting from zero again.

This function has multiple uses, all interesting and all to be discovered by doing different tests.

Among the uses that I find most interesting for this function is the one that gives you the possibility of making actions take place at certain time intervals, so we immediately see an application with a specific project.

For this project we only need our Arduino connected to the PC via its USB cable

The purpose of the project is to make some writings appear on the Serial Monitor at certain intervals of time (but it could be to turn on one LED, turn off another, etc … the sketch can be easily adapted to our purposes).

Copy the code below into a new sketch for Arduino after launching the application:

Video-Project 34: Insight into the millis () function

LED matrix display

As the name implies, the module we will see is made up of 24 LEDs arranged in 8 columns and 8 rows. There are several 8×8 LED displays on the market, the one we will analyze is the classic 16 PIN version. In fact, the number of pins that this module has are 16, 8 pins, or PIN, are used to drive the columns and 8 PINs are used to drive the rows.

Since Arduino has 14 digital PINs, the question arises: how do we drive the 16 PIN digitally if we only have 14 digital PINs with the Arduino UNO? The answer is just as simple: using two of Arduino’s 5 analog PINs, in fact an analog PIN can be used to all intents and purposes as if it were an Arduino digital PIN, as long as you use the same instructions as the digital PIN. But we will see this better in the sketch, now let’s move on to better understand the operation of the 8×8 LED matrix. Even the LED matrices can be with Common Anode or Common Cathode, the latter being the most common.

As you have surely already learned, in the case of the Common Cathode the PIN must be placed in a low LOW state to make the individual PINs light up. LOW state, while the columns must be placed in HIGH state to make the LEDs light up.

So as if we were in the naval battle game, to turn on the LED that is in column 4 and row 2, we have to put column 4 in the state of HIGH and the row 2 in the state of LOW and titte the other rows and columns must be in state of LOW.

Understanding the way in which to make the individual LEDs light up, it is simple at this point to be able to compose both the letters and numbers with the matrix.

Let’s now turn to the project.

Project 35: HELLO WORLD IN MATRIX!.

For this project we need:

The wiring diagram for the connection is:

Pay attention to the wiring diagram connections, it is necessary to consider the following pin-out of the display (in my case), obviously in your case I suggest you to retrieve the datasheet from the internet by entering the serial number of your display in Google, if it coincides with the my then follow the same links, otherwise you will have to adjust them.

Personally, thanks to the datasheet of the LED matrix I purchased, I built this table that helped me:

At this point, the assembly scheme turns out to be much easier to make even if there is a considerable number of dupont cables.

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, the word Hello World appears letter by letter on the LED matrix.

Sketch analysis: Project 35: HELLO WORLD IN MATRIX

As you can see from the sketch, we have introduced a new type of variable. We had already seen what arrays were, that is, variables that could hold multiple values and were identified with an index. In this sketch we see the matrix variable (always called array) which is defined like this:

byte A [8] [8] = {{0,0,0,1,1,0,0,0},… .., {… ..}};

where “byte” indicates the type of variable and how much memory it occupies, “A [8] [8]” indicates the name of the variable and the size of the matrix, ie 8 rows and 8 columns and then after the “=” in brackets staples, the single values are entered, which in our case represent the LEDs that must be turned on to compose the letter “A”, in fact, if you notice, the LED to be kept off is set to zero and the LED to be switched on to 1, so for variable A:

In this way you can compose what you want and assign it a variable name. Alternatively, it is possible to activate the individual LEDs, again in the sketch it is possible to observe the functions “counter1 ()”, “counter2 ()” and “blinking ()” which, thanks to “for” loops, control the individual LEDs of the matrix by acting directly in the vCols and vRows variables.

Returning to the representation on the LED matrix of the phrase “Hello World”, we can observe the writeLetter (…) function that for each letter of the string that we enter, thanks to the switch structure .. case … recalls the variable of the single letter that makes up the word Hello Word and represents it on the LED matrix.

The LCD Display

Even using the LCD display is a good way to immediately represent some values that it is useful to keep under control in an ongoing process in Arduino.

Liquid crystal displays (LCDs) consist of liquid crystals activated by electrical current.

Curiosity: how the LCD display works

The operating process is not easy to explain in detail, so a very simplified description is given here, for any further information on the internet you can find a lot of material.

As far as we are concerned, it is enough to know about the LCD display which is made thanks to the presence of liquid crystals that is a particular substance that at the same temperature can coexist both in the liquid and solid state at the same time, this substance subjected to polarization, that is to a low power electric current. , it has a different behavior that we can manage. Some parts of substance, polarized, orient themselves in a precise configuration, thus forming the figure of our interest. There are several types that differ substantially in the number of lines of alphanumeric information that they can display, normally they are 16 characters and are two, three or four lines (for those used with Arduino).

The liquid crystal display has the distinct advantage of having a low power consumption compared to LED displays. Its other advantages are its low cost and ease of finding on the market. The main disadvantages of LCDs are: a light source must be added to make the information visible and when there is low ambient brightness, a backlight must also be added. Other disadvantages are: it can work in a very limited temperature range (i.e. between 0 ° C and 60 ° C), it also has a short operating life compared to LED displays.

Project 36: HELLO WORLD WITH THE LCD DISPLAY

For this project we need:

The wiring diagram for the connection is:

For better visibility of the PINs, I partially reproduce the photo of the display:

The assembly scheme is as follows:

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

As you can see from the sketch for the functioning of the display we introduce a new library called. This library significantly simplifies the use of the display, in fact a few lines of program code are enough to obtain the desired result.

Video-Project 36: HELLO WORLD WITH THE LCD DISPLAY

Project Analysis 36: HELLO WORLD WITH THE LCD DISPLAY

As you can see from the sketch for the functioning of the display we introduce a new library called. This library significantly simplifies the use of the display, in fact a few lines of program code are enough to obtain the desired result.

Let’s see what instructions help us manage our LCD display.

First of all, you need to call up the dedicated Library with #include <LiquidCrystal.h>, in the void setup part, I can set the title of the information that is shown, it is printed only once on the lcd.print(“hello, world!”);.

In the void loop part, on the other hand, when the information needs to be updated lcd.print (pippo);

The positioning of the cursor from which to start the writing takes place with the instruction: lcd.setCursor (0, 1); at this point have fun making changes and seeing the results.

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