How to Continuously Read Values From a Sensor in Arduino

The LCD module makes an embedded system completely independent with which can take analog or digital input on its input pins and display the corresponding output in its own screen along with generating other kind of outputs. The LCD modules comes in different sizes varies from single line monochrome display to large graphical color display all of them using almost same method for displaying data.The Arduino can be used as a stand-alone board of which the output or inputs can be taken from the boards or given to the board. The Arduino board even though has various kinds of communication ports they can't match with the effectiveness provided by the LCD module. Moreover the LCD module eliminates the requirement for having a PC or any other kind of devices connected with the Arduino board to display the data send by the board.

Since the basic Arduino board does not have a built–in LCD module to display data one should connect it externally to display data like strings, sensor values etc. The libraries in the Arduino IDE helps in accessing the LCD module very easily. This project demonstrates how to use a 16x2 LCD with the Arduino board for displaying a sensor value continuously. The code has been written using the library functions and the sensor used here is a simple potentiometer which can vary its voltage in its variable pin.


The Arduino board has all the required circuitary to get the built-in AVR microcontroller running. It has also pins which can be configure as digital output only, digital or analog output, digital input only and digital or analog input. Any AVR microcontroller based board which follows the standard Arduino schematic and is flashed with the Arduino bootloader can be called an Arduino board. The Arduino IDE is also very simple that anyone who have basic knowledge of c programming can quickly get started with it. The tutorial on Getting started with the Arduino  explains about the steps required to get start with an Arduino board.

The Arduino board used in this project is the Arduino pro-mini board and the IDE version of the Arduino is 1.0.3 for windows. The image of the Arduino pro-mini board and the Arduino IDE are shown below;

 Typical Arduino Pro-Mini Board

Fig. 2: Typical Arduino Pro-Mini Board

Arduino IDE Software Window

Fig. 3: Arduino IDE Software Window

Since the Arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the Arduino board and also helps in the serial communication with the USB port of the PC.

 External USB To TTL Converter Board For Programming Arduino And Serial Communication

Fig. 4: External USB to TTL converter board for programming Arduino and serial communication

A previous project on how to use analog input and output of the Arduino discusses about how to use analog input /output channels of the Arduino board to read analog values and to write analog values. Another project explains how it is possible to debug the values  which are received by the Arduino board and which the Arduino board write to the external pin so as to generate a voltage. The variable pin of a potentiometer is connected to the analog pin;in this project the pin A0. The other two pins of the potentiometer is connected to the VCC and GND so that as the variable moves it can divide the entire supply voltage and provide it as the analog input voltage for the Arduino board. An LED is connected to the analog output pin through a current limiting resistor; in this project it is connected to the pin 6. The code continuously reads the value from the potentiometer and writes the corresponding value to change the brightness of the LED connected to the pin 6.

The code written for this project make use of the analog read and write functions like analogRead() andanalogWrite() etc. It also uses library functions which help to access the LCD module like lcd.begin(), and lcd.print() . These functions for accessing the LCD are available in the header file <LiquidCrystal.h>. The details of the analog read and write functions are explained in a previous project on how to use analog input and output of the Arduino . The functions for interfacing the LCD are discussed in the previous project on how to interface 16*2 LCD with the Arduino board .

In this particular project one more function from the <LiquidCrystal.h> is used which is not there in the previous project and is discussed below.

lcd.setCursor()

The lcd.setCursor() function is used to set the position of the cursor in the LCD screen in such a way that the subsequent letters will be displayed from that particular position only. The function has two parameters and the first one represents the column number and the second one represents the row number. The row and column number starts with zero and the maximum value depend on the type of LCD used.

The lcd.setCursor() can be used to set the cursor at a particular position of the LCD screen as shown in the following statement;

lcd.setCursor(7,0);

The above statement will set the cursor at the 8th position of the first line of an LCD screen and further display will happen from that position onwards.

THE CODE

The code initializes the 16*2 LCD using the function lcd.begin() after initializing the library using the function LiquidCrystallcd(). The analog value is read from the analog input pin using the function analogRead() and is then mapped from 10 bit mode to 8 bit mode using the function map(). The value is thgen multiplied by 5 and is divided by 255 to get the 5V equivalent of that value.  The lcd.print() function is then used to print the value after setting the cursor using lcd.setCursor() function.

// include the library code:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins

LiquidCrystallcd(12, 11, 5, 4, 3, 2);

constintanalogInPin = A0;  // Analog input pin that the potentiometer is attached to

constintanalogOutPin = 6;  // Analog output pin that the LED is attached to

intpotvalue = 0;

intoutputvalue=0;

void setup()

{

  // set up the LCD's number of columns and rows:

lcd.begin(16, 2);

lcd.print("ENGINEERS GARAGE");

}

void loop()

{

  // read the analog in value:

potvalue = analogRead(analogInPin);

 // map it to the range of the analog out:

outputvalue = map(potvalue, 0, 1023, 0, 255);

lcd.setCursor(7, 2);

lcd.print((outputvalue * 5)/255);

lcd.print("V");

  // change the analog out value:

analogWrite(analogOutPin, outputvalue);

delay(100)  ;

}

The code also glows an LED with the brightness which is equivalent to the value read from the analog input pin as discusses in a previous project on how to use analog input and output of the Arduino .

Once done with the coding one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino and the brightness of the LED can be observed to vary as the variable of the potentiometer moves. The voltage read from the analog input pin will be displayed in the LCD along with varying the brightness of the LED.

Project Source Code

              

###

              

/*============================EG LABS ===================================//

Demonstration on how to use 16*2 LCD with an arduino board

The circuit:

LCD:

* LCD RS pin to digital pin 12

* LCD Enable pin to digital pin 11

* LCD D4 pin to digital pin 5

* LCD D5 pin to digital pin 4

* LCD D6 pin to digital pin 3

* LCD D7 pin to digital pin 2

* LCD R/W pin to ground

* 10K resistor:

* ends to +5V and ground

* wiper to LCD pin 3

* LED anode attached to digital output 6

* LED cathode attached to ground through a 1K resistor

Analog input:

 * Potentiometer attached to analog input A0

 * one side pin (either one) to ground

 * the other side pin to +5V

 * LED anode (long leg) attached to digital output 6

 * LED cathode (short leg) attached to ground through a 1K resistor

//============================ EG LABS ===================================*/

//include the library code:

#include <LiquidCrystal.h> //initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

const int analogOutPin = 6;  // Analog output pin that the LED is attached to

intpotvalue = 0;

intoutputvalue=0;

void setup()

{

  // set up the LCD's number of columns and rows:

  lcd.begin(16, 2);

  lcd.print("ENGINEERS GARAGE");

}

void loop()

{

  // read the analog in value:

  potvalue = analogRead(analogInPin);  // map it to the range of the analog out:

  outputvalue = map(potvalue, 0, 1023, 0, 255);

  lcd.setCursor(7, 2);

  lcd.print((outputvalue * 5)/255);

  lcd.print("V"); // change the analog out value:

  analogWrite(analogOutPin, outputvalue);

  delay(100) ;

}

###


Circuit Diagrams


Project Components

  • Arduino Pro Mini
  • LCD
  • LED
  • Resistor

Project Video


Filed Under: Arduino


lightpues1967.blogspot.com

Source: https://www.engineersgarage.com/how-to-use-arduino-to-display-sensor-values-on-lcd-part-14-49/

0 Response to "How to Continuously Read Values From a Sensor in Arduino"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel