Read the Temperature With Arduino Uno

Introduction

 
In my first article on the Arduino Uno, we will focus on the use of an analog temperature sensor combined with a Liquid Crystal Display (LCD). The goal is to be able to track and code something. The finished display shows the temperature in degrees Celsius where the sensor is used with Arduino Uno.
 
Prerequisites
 
To do it we need the following components:
  • Arduino Uno
  • Analog sensor TMP36
  • Alphanumeric LCD based on HD44780 controller
  • Basis on which to perform wiring components
  • A resistor 220 ohm
  • Coulometer 65 for electrical connections
  • Potentiometer adjustable resistance value
I remember that all the components listed above are included in the Starter Kit, available at any electronics retail center, also available in the numerous sites selling online.
 
Description of all components
 
 
We will use the Arduino Uno to perform the management of the temperature sensor and the display. This is the basic model features in hardware to a microcontroller Atmega 328 16 MHz, 32 KB flash memory, a memory of SRAM 2K bytes.
 
 
It is an analog temperature sensor that supplies a voltage value of 10mV for degrees directly proportional to the temperature value that is detected. It has a reading range that goes from -40 ° c to 125 ° c.
 
 
Two-line display with 16 characters, backlight. The operation requires an electric voltage of 5v DC and a series of links that we will see in the article.
 
 
This is the basis of where we're going to run the wiring of the electric circuit of all the components necessary to implement our example.
 
 
It is necessary to protect the pin 15 of the display backlight, exactly pin LED +.
 
 
The jumpers are pre-wired, with which we will put in communication all the components that will be the basis, so as to be able to perform all the electrical connections.
 
 
The Potentiometer for adjusting the resistance, required for adjusting the contrast on the display.
 
Electronic scheme and preview the circuit.
 
We now have everything related to the hardware, we just need to start our work of electrical wiring. First, we need to get the wiring diagram. If you also want to devote to the design of the circuit, there are free and paid software that allows the creation of a scheme electrical/electronic, because without it we can't expect to get down to business for the linking phase. One good free software I found is at this link. I leave the outline of connection with the software indicated in the previous link.
 
 
 
For completeness, I also leave the pin for the easy connection of the display.
 
 
Create Sketch code
 
After creating the schema and making the connections of all the components it is time to move from the hardware to the software. The Arduino board to be properly programmed, has a basic IDE development tool that you can find at this link. Currently, the version is 1.6.4 and it was also released by Visual Micro. It is a plug-in for Visual Studio, with which we can easily program any form of the family of Arduino directly from Visual Studio. However, there are constraints on it or are not supported by, versions Express and installed from version 2008 to 2013, not yet available for the current version in 2015. My advice is to download the community version of Visual Studio 2013, the equivalent of a Professional version so be sure to install it. More information about Visual Micro can be found in the link left previously. For programming the sketch of the code, we will use in this article the Arduino IDE. Once installed, double-click on the screen and this and that we will find initially.
 
 
We see that they have two methods, the first called setup() and the second loop(). He said briefly, in the setup() method, we need to provide everything about the initialization required for the operation of the project since it is done only once. All of the remaining code goes in the method loop(), that is executed repeatedly. Another thing is to define the variables globally, in other words, make them visible everywhere and we must declare them to the outside of both methods. First, click with the mouse on the "File", after "Salva con nome", call the project "Temperature Test". After doing that, we paste in the following C++ code.
  1. // Andiamo a includere nel progetto il file LiquidCrystal   
  2. #include < LiquidCrystal.h>  
  3. // Inizializzazione dei pin RS,E,D4,D5,D6,D7 del display  
  4. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  
  5. // la costante analogicInputSensor avrà il valore analogico dell'ingresso A0 sulla scheda Arduino Uno  
  6. const int analogicInputSensor = A0;  
  7. /*In questo metodo viene eseguita l'inizializzazione delle variabili o altro 
  8. che necessita di essere impostato una sola volta, poiché il metodo setup viene  
  9. eseguito una sola volta*/  
  10. void setup()  
  11. {  
  12.    /* add setup code here */  
  13.      // Inizializzazione dei pin GND retroilluminzione e il pin +5V  
  14.      lcd.begin(16, 2);  
  15. }  
  16. /*All'interno di questo metodo va inserita tutta la parte di codice, trattandosi  
  17. di un loop infinito il codice sarà eseguito in maniera sequenziale dalla prima 
  18. all'ultima istruzione*/  
  19. void loop()  
  20. {  
  21.    /* add main program code here */  
  22.     // Questa variabile sarà aggiornata con il valore analogico di analogicInputSensor  
  23.      int sensorInpunaVal = analogRead(analogicInputSensor);  
  24.     // Otteniamo il voltaggio dato dal valore della variabile sensorInpunaVal diviso 1024.0, moltiplicato per 5.0  
  25.      float voltage = (sensorInpunaVal / 1024.0)* 5.0;  
  26.     // Otteniamo il valore di temperatura dal voltaggio -.5 * 100  
  27.      float actualTemperature = (voltage - .5) * 100;  
  28.       
  29.      // Cancelliamo il contenuto attuale sul display  
  30.      lcd.clear();  
  31.     // Scrivo il valore Temp sul display  
  32.      lcd.print("Temp:");  
  33.     // Richiamo il metodo CheckTemperature  
  34.      CheckTemperature(actualTemperature);  
  35. }  
  36. // Metodo CheckTemperature  
  37. void CheckTemperature(float value)  
  38. {  
  39.      // Imposto la posizione del cursore alla posizone 9 e parto con il nuovo valore subuto dopo  
  40.      lcd.setCursor(9, 0);  
  41.     // Scrivo il valore della variabile value, che corrisponde alla temperatura attuale  
  42.      lcd.print(value);  
  43.     // Scrivo il valore c sul display  
  44.      lcd.print("c");  
  45.     // Imposto un ritardo di 2 secondi prima della scansione successiva  
  46.      delay(2000);  
  47. }  
Now we will comment briefly on the preceding code.
 
To make use of the display, you need to include in the sketch of the library CrystalLiquid.h that contains everything you need to use and its management. After it initializess the pins RS, E, D4, D5, D6 and D7 using the method LCD.
 
Next, we define a variable of type int, where we will read the analog value provided by the sensor TMP36 on pin A0 of the Arduino Uno, depending on the temperature reading.
 
We then go to the method loop(), where we find another variable called sensorInputVal and pass the voltage sensor TMP36, all with the method analogRead() that takes as an argument an analog value in the case of the variable analogicInputSensor. After we do the calculation of the voltage and temperature, there is nothing new, it is simply existing formulas.
 
The interesting part comes now, with the method lcd.clear() to eliminate all the content currently visible in the display, whereas the method print(), as opposed to clear(), will ensure the display shows the value "Temp:". The CheckTemperature method is invoked, passing as an argument the value of the variable actualTemperature.
 
Internally, before displaying the temperature value, with the method setCursor(), we set the starting position of the cursor, or 9 characters after the last, in our case it is ":". Again with the method print() we will try to show the value of the parameter value, to show the value of the detected temperature. Even with the method print() to display, it displays the letter c, only to end up with the method delay(), passing as an argument value in milliseconds. This method does nothing but block for two seconds to run the code within the method loop(), then recovering normally from the beginning.
 
Test application
 
We have included all the necessary code, before running the project it must be compiled. From the "Sketch" choose the command "Verifica e compila", as shown in the figure.
 
 
After compiling the Sketch, you can now transfer the code in the Arduino Uno, from the "File" menu, choose the command "Carica". Another way is to mouse-click on the arrow button to the right and then we see the figure.
 
 
After executing the code, it is loaded into the Arduino Uno. If everything was done correctly, that's what we'll show on the display.
 
 

Conclusion

 
In my first article, we have realized a simple circuit for temperature control, starting from the components needed, the electrical diagram of the circuit, to end with the writing of the C++ code and the test application. In the next article, we will expand the project by entering the threshold values of temperatures, showing the actions to be done depending on the temperature detected by the sensor TMP36.