computer scince and engineeringInternet of Things

IOT LIST OF PRACTICALS IN HINDI

42views

Installation of Arduino IDE.

ARDUINO IDE

आप लोग यह सोच रहे होंगे की ARDUINO की कोडिंग कैसी होती होगी. मै आपको बताना चाहता हूँ कि ARDUINO की कोई अलग प्रोग्रामिंग लैंग्वेज नहीं है बल्कि ARDUINO को हम C प्रोग्रामिंग से कोड करते है और उसे ARDUINO C के नाम से जाना जाता है. ARDUINO C में लिखे गए कोड को SKETCH कहा जाता है.

हम लोग सबसे पहले बात करते है, ARDUINO IDE की. ARDUINO IDE मतलब ARDUINO Integrated Development Environment. IDE के उपयोग से हम लोग ARDUINO को code करते है और IDE की मदद से ही ARDUINO में कोड dump(अपलोड) करते है.

– Popular Around the World –

आप ARDUINO IDE को www.arduino.cc से डाउनलोड कर सकते है.

ARDUINO IDE डाउनलोड step by step:

  • सबसे पहले आप arduino.cc पे जाइये.

2019-01-08_21h25_06.png

  • फिर आप menu bar में software>downloads पर क्लिक करे. क्लिक करने पर आपको कुछ एसी स्क्रीन मिलेगी.

2019-01-08_21h28_17.png

  • ARDUINO community अब आपको ARDUINO को कोड करने के लिए दो विकल्प दे रही है.
    1. ARDUINO web editor: इस से आप बिना कुछ डाउनलोड किये अपने ARDUINO को इन्टरनेट से कोड कर पाएंगे.
    2. ARDUINO IDE: हम लोग इस IDE के माध्यम से कोड करेंगे. IDE के बारे में ज्यादा जानकारी आगे इस article में दी गई है.
  • Downloads के पेज में नीचे जाने पर आपको डाउनलोड करने का ऑप्शन मिलेगा. यहाँ पर आपको Windows, Mac और Linux तीनो OS के लिए डाउनलोड का ऑप्शन मिलेगा.
  • किसी एक ऑप्शन पर क्लिक करने के बाद अगले पेज पर आप डाउनलोड कर पाएंगे. हमे यह तो पता है की ARDUINO open source कम्युनिटी है इसलिए आप उनको DONATE करके उनको प्रोत्साहित कर सकते है. अगर आपकी इच्छा है तो.

2019-01-08_21h31_45.png

  • डाउनलोड होने के बाद आपको कुछ एसी स्क्रीन दिखाई देगी.

2019-01-08_21h32_33.png

अगर आपने यह सारे steps सही से फॉलो किये होंगे, तो आपने ARDUINO IDE सही से डाउनलोड कर लिया है. चलिए तो अब इसको गौर से देखते और समझते है. इसके मेनू बार में हमे files, edit, sketch, tools और  help जेसे TABS मिलेगे.

2019-01-08_21h38_04.png

ARDUINO project को save, rename, open फाइल से कर पाएंगे. फाइल के अन्दर आपको examples का ऑप्शन मिलेगा. जिसमे आपको ARDUINO की तरफ से ही ढेर सारे SKETCH पहले से ही मिलेंगे. जिनको आप use करके आसानी से कुछ ARDUINO project कर पाएंगे. एडिट ऑप्शन में आपको कॉपी पेस्ट के ऑप्शन मिल जाएँगे. आपने जो कोड लिखा है उसे आप compile और upload sketch टैब से कर पाएंगे.

अब ARDUINO का सबसे मुख्य टैब, tools, की बात करते है. इसमें हम सबसे पहले BOARDS के बारे में बात करते है. ARDUINO IDE से आप सिर्फ ARDUINO को ही नहीं बल्कि nodeMCU, adaFruit के BOARDS और दूसरे कोड कर सकते है. आप अपने Tools > Boardsमें जाइये और अपना बोर्ड select कर लीजिए. सही बोर्ड select करना एक important STEP है.

अब आप Tools > Board में गए लेकिन आपको आपका डेवलपमेंट बोर्ड नहीं मिला. तो फिर आप Tools >Board Board Manager में जाइये और आपका बोर्ड search करके उसकी फाइल्स डाउनलोड कर ले.

2019-01-08_21h49_05.png2019-01-08_21h52_45.png

दूसरी बात जिसका ध्यान रखना है वो है Port. सही Port select करना महत्वपूर्ण है क्युकी आपके PC/ LAPTOP के साथ सिर्फ ARDUINO ही नहीं बल्कि और कई सारी DEVICES जुडी होंगी. आप इस image में देख सकते है की मेरा UNO COM 8 पे connect किया हुआ है.

2019-01-08_21h53_47.png

अभी के लिए ARDUINO IDE की इतनी जानकारी काफी है. आगे जेसे-जेसे हम नए-नए प्रोजेक्ट्स करेंगे वैसे-वैसे और बाते जानेंगे.


– Popular Around the World –

Interfacing Light Emitting Diode (LED)- Blinking LED

LEDs are small, powerful lights that are used in many different applications. To start, we will work on blinking an LED, the Hello World of microcontrollers. It is as simple as turning a light on and off. Establishing this important baseline will give you a solid foundation as we work towards experiments that are more complex.

Components Required

You will need the following components −

  • 1 × Breadboard
  • 1 × Arduino Uno R3
  • 1 × LED
  • 1 × 330Ω Resistor
  • 2 × Jumper

Procedure

Follow the circuit diagram and hook up the components on the breadboard as shown in the image given below.

Breadboard

Note − To find out the polarity of an LED, look at it closely. The shorter of the two legs, towards the flat edge of the bulb indicates the negative terminal.

LED

Components like resistors need to have their terminals bent into 90° angles in order to fit the breadboard sockets properly. You can also cut the terminals shorter.

Resistors

Sketch

Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open the new sketch File by clicking New.

Sketch

Arduino Code

/*
   Blink
   Turns on an LED on for one second, then off for one second, repeatedly.
*/

// the setup function runs once when you press reset or power the board

void setup() {  // initialize digital pin 13 as an output.
   pinMode(2, OUTPUT);
}

// the loop function runs over and over again forever

void loop() {
   digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
   delay(1000); // wait for a second
   digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
   delay(1000); // wait for a second
}

Code to Note

pinMode(2, OUTPUT) − Before you can use one of Arduino’s pins, you need to tell Arduino Uno R3 whether it is an INPUT or OUTPUT. We use a built-in “function” called pinMode() to do this.

digitalWrite(2, HIGH) − When you are using a pin as an OUTPUT, you can command it to be HIGH (output 5 volts), or LOW (output 0 volts).

Result

You should see your LED turn on and off. If the required output is not seen, make sure you have assembled the circuit correctly, and verified and uploaded the code to your board.

Interfacing Button and LED – LED blinking when button is pressed.

In my last tutorial, I explained why it is easy to work on electronic projects with Arduino. In this tutorial I will be using an Arduino board to make a simple circuit, one that can turn on a LED light. Using some more basic code I will make the same LED light blink. Finally, I will add a push-button and use it to speed up the blinking.

Some of the diagrams in this article were developed using the Fritzing program.

 

Supplies

Arduino components
Arduino components

In this tutorial I will be using the following components:

  • Arduino Uno R3 x 1
  • Breadboard x 1
  • Standard Type B USB cable x 1
  • Push Button x 1
  • 10K resistor x 1
  • Short jumper wires x 3

You can buy these items either through a local electronics store, such as Fry’s (if you live in the US), or via the Internet from the Arduino WebsiteAmazonLittle Bird Electronics or even eBay.

Alternatively, you may choose any of these other Arduino boards to follow this tutorial:

Other Arduino boards or third party compatible boards may work as well. They, however, may require a different USB connector. Ensure that your Arduino board can connect to your computer using a suitable USB cable.

Furthermore, ensure that you have downloaded and installed the latest version of the Arduino IDE on your computer. Installing the Arduino IDE may require a few more steps on Windows computers because you will need to install some drivers. For further instructions please follow the installation guides on the Arduino website.

You may be wondering how we are going to turn a light on when we have not included one in the list of components? That’s easy! Arduino boards are usually equipped with an onboard LED, which we can use in our projects. To keep our circuit as simple as possible we are going to use this LED.

Arduino board
Our code is going to work with the light marked with letter L next to it.

1. Set Up a New Project

To begin, connect your Arduino board to your computer using the appropriate USB cable. You will notice that one or multiple lights may momentarily or permanently turn on. Arduino Uno has four onboard LED lights. If your board has more than one LED, blinking lights indicate that it is connected to a power source and is booting up. It takes around five seconds for Arduino to become ready for you to interact with it.

Launch the Arduino IDE on your computer. You will be presented with an empty working space where you will write the necessary code in order to program your Arduino board.

Next, you must tell the Arduino IDE which board your are going to connect to. From the menu, select Tools > Board, and then select your Arduino board from the list. If you have chosen to use an official Arduino board, then its name should be listed for you to select. Third party boards are usually equivalent to another official Arduino board. If you know which board that is, then go ahead and select that one from the list. Otherwise, refer to its manual to work out which model from the list should work with your specific board.

Finally, you must select the right port for communication with your Arduino board. Once again from the menu, go to Tools > Serial Port, and select the right Serial port. On Macs, the correct serial port is often listed as /dev/tty.usbmodem1421 or similar. On Windows, the connection should be listed as a COM port.

2. Turning the Light On

Arduino is equipped with many different input and output connectors, which we will refer to as IO Pins. Right now, we want to use a digital IO pin to instruct the LED light to come on. Because we are also using the onboard LED light, the appropriate IO pin has already been decided for us by the Arduino makers. It is pin 13, which, by design, has been attached to the onboard LED light.

Copy the following code and paste it into your Arduino IDE:

In the code, I have made use of two Arduino functions: pinMode(pin_number, mode) and digitalRead(pin_number, value).

I will call the pinMode() function inside setup() to instruct Arduino to treat its pin-13 as an output. Then by calling the digitalWrite() function inside loop() I can activate HIGH signal on pin-13 which turns on the LED light.

Click on the Upload button to push the above code into your Arduino board. Provided the correct Arduino board and Serial port is selected, you should see a progress bar followed by a Done Uploading message.

Whilst the code is being uploaded you may see different lights flashing, which indicates successful communication between your PC and the Arduino board. At this point, the onboard LED light should be permanently turned on.

3. Make It Blink

You have completed the Arduino equivalent of “Hello World”. Now you are going to make that light blink by introducing the delay() function into the above code. The delay() function accepts an integer value, equal to a length of time in milliseconds. 1000 milliseconds is equal to one second.

01
02
03
04
05
06
07
08
09
10
11
12
int delay_value = 1000; int led_pin = 13;
void setup() {
  pinMode(led_pin, OUTPUT);
}
void loop() {
  digitalWrite(led_pin, HIGH);
  delay(delay_value); digitalWrite(led_pin, LOW);
  delay(delay_value);
}

The delay() function keeps the LED light on and also turns it off for one second during every iteration of the loop() function. As the loop() function is perpetually repeated, this code will turn the LED light on and off repeatedly.

Digital Wave diagram
Digital Wave diagram

Using a digital wave diagram, I can loosely explain how the original behavior has changed. Before introducing the delay function in step three, I was producing a digital wave that looked like the diagram, above. Every cycle of every loop() function iteration was spent on keeping the LED light turned on.

Using the delay() function, our code divides each cycle into two parts, making every iteration last for two seconds. During the first second the LED is turned on, and, during the next second it is turned off.

Digital Wave diagram showing the delay() function
This figure shows how the the delay() function is used to maintain the current state.
Advertisement

4. Change The Delay Time Using a Button

Up to this point, the behavior of this Arduino project has been nicely driven by the code we have written. However, once it is uploaded and running, we don’t have any way of interacting with this electronic circuit. This is very static and I am going to change it by adding a pushbutton which will let me change the blinking speed. In this step, I will need to use a breadboard. This is a good time to take a look at what a breadboard is and how it can be used.

Breadboard

A breadboard is a solderless prototyping board used for making temporary electronic circuits, mainly for experimenting with different circuit board designs.

Modern breadboards are made up of a solid piece of perforated plastic, with many copper clips under its surface for making electrical connections. The numerous holes on the surface of these boards allow for inserting different electronic components without the need to solder any in place.

Common breadboards have two columns, each with their own rows of five holes. Any hole is connected to all other holes in the same row but not to any of the holes in the adjacent column. In other words, if two wires are not placed in the same row of the same column, then the two are not connected to each other. This setup allows us to share a single connection from a component through four other connection points in the same row.

The reason for having two or more columns on the same breadboard is to allow multiple Integrated Circuits (ICs) to be connected. ICs are usually connected into both columns of a breadboard because they have more than two pins on both sides as illustrated in the following diagram.

40-pin IC attached to a breadboard.
40-pin IC attached to a breadboard.

On either sides of most boards, there are long strips that are used for sharing power. These strips are often referred to as the bus strips, or power rails, as they run along the entire length of the board. The power rails on some boards will let you connect both positive and ground connections. Unlike normal rows, all of the holes along the length of the board are connected to each other.

Power rails on breadboard
Power rails are usually marked with red and blue lines running along the entire length of the board.

When buying a breadboard, it’s a good idea to choose the type with grooves on either sides of the board. These grooves can be used to attach multiple breadboards together to make a larger working space. A good quality breadboard is marked with numbers and letters making it easy to identify each row and column.

Step 1

Begin by placing the push button and connecting it with the power jumpers from the Arduino board. Arduino Uno can output two levels of power, 3-Volts and 5-Volts. For this circuit, we will need to use the 5V rail. The reason why you’d use one over the other depends on the components you’re going to connect. Some components may need lower power voltage to operate, hence the 3V output.

Circuit with Arduino and breadboard
Circuit with Arduino and breadboard

In the diagram, above, we have a complete circuit. We have connected the top pins of the push button to both the 5V-pin, on the Arduino, and to the 10K resistor. This then connects to the ground (GND) pin on our Arduino. Our third wire (in yellow) connects to the digital pin–2 and will carry the ON signal to the Arduino board.

Advertisement

Resistor

The purpose of a resistor is to slow down the electrical current, as current passes through it, thereby limiting the amount of current flowing through the circuit. This is achieved by making resistors from materials with a low conductive property. Resistance is measured in ohms andcan be determined from the follow equation:

resistance (in ohms) = power or voltage (in volts) / current (in amps)
resistance (in ohms) = power or voltage (in volts) / current (in amps)

Suppose I want to connect a LED light to a 9V power source, but the LED can tolerate only 30 milliamps of current. Based on the above equation we will need to use a 300 Ohms resistor in order to limit the current flowing through the LED light.

9 volts / 0.03 milli-amps = 300 Ohms
9 volts / 0.03 milli-amps = 300 Ohms

There are three main categories of resistors:

  1. Fixed resistors, such as the one we are using here,
  2. Variable resistors, commonly known as Potentiometers, and
  3. Variable resistors that are dependent on physical qualities, such as temperature (thermistors) or light (photovoltaic cells)

Although resistors serve to limit current flow, there are different types of resistors in each of those three categories for different applications.

Most fixed resistors are marked with colored bands to help us work out their resistance. From the left, the first two bands give the first and second digits of the resistance value. The third band gives the multiplication factor. Finally, the fourth band gives the tolerance of the resistor.

Resistor color codes
Resistor color codes

From the colors on the above resistor, we can work out the following:

  • Brown (first digit) = 1
  • Black (second digit) = 0
  • Orange (multiplier) = 10^3
  • Gold (tolerance) = +/- 5%
  • 10 * 10^3 = 10,000 ohms or 10 killo ohms or 10K resistance

Here is the complete table of colors. For more information, please refer to Wikipedia on resistor color codes.

Color Digit Multiplier Tolerance
Black 0 x10^0 ±1%
Brown 1 x10^1 ±2%
Red 2 x10^2
Orange 3 x10^3 (±5%)
Yellow 4 x10^4 ±5%
Green 5 x10^5 ±0.25%
Blue 6 x10^6 ±10%
Violet 7 x10^7 ±1%
Gray 8 x10^8 ±0.05% (±10%)
White 9 x10^9
Gold x10^–1 ±5%
Silver x10^–2 ±10%
None ±20%

The Purpose of the Resistor

I connected the yellow signal wire from digital pin 2 to one leg of the pushbutton. That same leg of the button, on its other side, connects through the 10K resistor to the ground to form a complete circuit. When the button is not pushed, the traveling current gets read by Arduino as a LOW.

Once the button is pushed down, a connection between pin 2 and positive 5V will get established through the push button legs. Since electricity will always travel through the path of least resistance, it will avoid going through the resistor and will flow through to pin 2 which results in a HIGH reading by the Arduino board.

Step 2

Now, let’s finish Step 4 and make the LED light blink faster when we press down the pushbutton.

This time, the code is instructing Arduino to treat its pin–2 as an input source by calling the pinMode(button_pin, INPUT) inside the setup() function. This allows us to read the state of the pushbutton later inside the loop() function by calling digitalRead(button_pin). Getting the state of the pushbutton lets us determine whether the delay function should be called with a smaller value.

Now go ahead and upload the above code to your Arduino, and then press the pushbutton to see the LED light blink faster.

Troubleshooting

If you’ve gotten this far and the above code isn’t working for you, there could be a few reasons for this:

  • This may sound obvious, but ensure your Arduino is connected to a power source and the ON LED is turned on.
  • Make sure all the pins and wires, the resistor and the push button are firmly connected to your Arduino board and to the breadboard. If you’re unsure about continuity of your connections, use a multimeter to measure the continuity.
  • Ensure all connections to the Arduino board are connected to the right digital inputs.
  • If your problem persists, then consult the Arduino troubleshooting guide.

Conclusion

The finished project
The finished project

In this tutorial, you learned some basic techniques in using an Arduino board, a breadboard, resistors and pushbuttons along with the Arduino IDE. You also learned how the delay() function can be used to maintain a state for any given length of time.

If you have any questions about this tutorial, please leave them in the comments section, below.

Interfacing Light Dependent Resistor (LDR) and LED, displaying automatic night lamp
Interfacing Temperature Sensor (LM35) and/or humidity sensor (e.g. DHT11)

Circuit Diagram

The following circuit diagram shows all the necessary connections required to implement this project.

DHT11 Humidity Sensor on Arduino Circuit Diagram

Components Required

  • Arduino UNO  [Buy Here]
  • DHT11 Temperature and Humidity Sensor
  • Breadboard (or perfboard)
  • Power supply
  • 16 x 2 LCD Display  [Buy Here]
  • 10K Ohm Potentiometer
  • 5K Ohm Resistor (1/4 W)
  • Connecting wires

Circuit Description

We will see the circuit design of DHT11 interfacing with Arduino. The DHT11 Humidity and Temperature sensor comes in two variants: just the sensor or a module.

The main difference is that the module consists of the pull – up resistor and may also include a power on LED. We have used a module in this project and if you wish to use the sensor itself, you need to connect a 5K Ω pull – up resistor additionally.

Coming to the design, the data pin of the DHT11 Sensor is connected to the Pin 11 of Arduino. A 16 x 2 LCD display is used to display the results. The control pins of LCD i.e. RS and E (Pins 4 and 6 on LCD) are connected to pins 4 and 5 of Arduino. The data pins of LCD i.e. D4 to D7 (pins 11 to 14 on LCD) are connected to pins 0 to 3 on LCD.

NOTE: For ease of connection, we have connected the DHT11 Sensor Module at the ICSP pins of the Arduino as it provides adjacent VCC, DATA and GND pins. This type of connection is not necessary and you can connect the data pin of sensor to normal Digital I/O pins.

Component Description

DHT11 Temperature and Humidity Sensor

DHT11 is a part of DHTXX series of Humidity sensors. The other sensor in this series is DHT22. Both these sensors are Relative Humidity (RH) Sensor. As a result, they will measure both the humidity and temperature. Although DHT11 Humidity Sensors are cheap and slow, they are very popular among hobbyists and beginners.

DHT11 Sensor

The DHT11 Humidity and Temperature Sensor consists of 3 main components. A resistive type humidity sensor, an NTC (negative temperature coefficient) thermistor (to measure the temperature) and an 8-bit microcontroller, which converts the analog signals from both the sensors and sends out single digital signal.

This digital signal can be read by any microcontroller or microprocessor for further analysis.

DHT11 Pinout

DHT11 Humidity Sensor consists of 4 pins: VCC, Data Out, Not Connected (NC) and GND. The range of voltage for VCC pin is 3.5V to 5.5V. A 5V supply would do fine. The data from the Data Out pin is a serial digital data.

The following image shows a typical application circuit for DHT11 Humidity and Temperature Sensor. DHT11 Sensor can measure a humidity value in the range of 20 – 90% of Relative Humidity (RH) and a temperature in the range of 0 – 500C. The sampling period of the sensor is 1 second i.e.
DHT11 Application CircuitAll the DHT11 Sensors are accurately calibrated in the laboratory and the results are stored in the memory. A single wire communication can be established between any microcontroller like Arduino and the DHT11 Sensor.

Also, the length of the cable can be as long as 20 meters. The data from the sensor consists of integral and decimal parts for both Relative Humidity (RH) and temperature.

The data from the DHT11 sensor consists of 40 – bits and the format is as follows:

8 – Bit data for integral RH value, 8 – Bit data for decimal RH value, 8 – Bit data for integral Temperature value, 8 – Bit data for integral Temperature value and 8 – Bit data for checksum.

Example

Consider the data received from the DHT11 Sensor is

00100101 00000000 00011001 00000000 00111110.

This data can be separated based on the above mentioned structure as followsDHT11 Sensor Data

In order to check whether the received data is correct or not, we need to perform a small calculation. Add all the integral and decimals values of RH and Temperature and check whether the sum is equal to the checksum value i.e. the last 8 – bit data.

00100101 + 00000000 + 00011001 + 00000000 = 00111110

This value is same as checksum and hence the received data is valid. Now to get the RH and Temperature values, just convert the binary data to decimal data.

RH = Decimal of 00100101 = 37%

Temperature = Decimal of 00011001 = 250C

Working of the Project

A simple project is built using Arduino UNO and DHT11 Humidity and Temperature Sensor, where the Humidity and Temperature of the surroundings are displayed on an LCD display.

After making the connections, we need not do anything as the program will take care of everything. Although there is a special library for the DHT11 module called “DHT”, we didn’t use it. If you want to use this library, you need to download this library separately and add it to the existing libraries of Arduino.

The program written is based on the data timing diagrams provided in the datasheet. The program will make the Arduino to automatically read the data from the sensor and display it as Humidity and Temperature on the LCD Display.

CODE

#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 0, 1, 2, 3);
byte degree_symbol[8] =
{
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
int gate=11;
volatile unsigned long duration=0;
unsigned char i[5];
unsigned int j[40];
unsigned char value=0;
unsigned answer=0;
int z=0;
int b=1;
void setup()
{
lcd.begin(16, 2);
lcd.print(Temp = );
lcd.setCursor(0,1);
lcd.print(Humidity = );
lcd.createChar(1, degree_symbol);
lcd.setCursor(9,0);
lcd.write(1);
lcd.print(C);
lcd.setCursor(13,1);
lcd.print(%);
}
void loop()
{
delay(1000);
while(1)
{
delay(1000);
pinMode(gate,OUTPUT);
digitalWrite(gate,LOW);
delay(20);
digitalWrite(gate,HIGH);
pinMode(gate,INPUT_PULLUP);//by default it will become high due to internal pull up
// delayMicroseconds(40);
duration=pulseIn(gate, LOW);
if(duration <= 84 && duration >= 72)
{
while(1)
{
duration=pulseIn(gate, HIGH);
if(duration <= 26 && duration >= 20){
value=0;}
else if(duration <= 74 && duration >= 65){
value=1;}
else if(z==40){
break;}
i[z/8]|=value<<(7– (z%8));
j[z]=value;
z++;
}
}
answer=i[0]+i[1]+i[2]+i[3];
if(answer==i[4] && answer!=0)
{
lcd.setCursor(7,0);
lcd.print(i[2]);
lcd.setCursor(11,1);
lcd.print(i[0]);
}
z=0;
i[0]=i[1]=i[2]=i[3]=i[4]=0;
}
}

Applications

  • DHT11 Relative Humidity and Temperature Sensor can be used in many applications like:
  • HVAC (Heating, Ventilation and Air Conditioning) Systems
  • Weather Stations
  • Medical Equipment for measuring humidity
  • Home Automation Systems
  • Automotive and other weather control applications

Interfacing Liquid Crystal Display (LCD) – display data generated by sensor on LCD

What are the components required for interfacing an Arduino Uno with an LCD?

Hardware components required for interfacing LCD with Arduino

Understanding the 16×2 LCD

What is a Liquid crystal display (LCD)?

LCDs are devices that display data. Liquid crystal displays show data using the principle of ‘light blocking’. They produce an image using a backlight. An LCD is made up of a large number of pixels. The resolution depends on the number of pixels. It is available in various backlight colors like green, yellow, blue. Mostly we use the standard 16×2 Liquid crystal display. That 16×2 indicates that it has 16 rows and two columns of cells made up of a 5×7 or 5×8 dot matrix. Each dot matrix has a 5×8 dot resolution. Sounds a bit confusing? Don’t worry. The image below will clear up your doubts.

16x2 LCD
This is a 16×2 LCD display with its two lines. It has 16 characters (cells) spaces per line. Each of these cells is individually made up of a 5×7 or 5×8 dot matrix.

How does an LCD screen work?

LCD screen working internal structure

What are the specifications and features of the 16×2 LCD screen?

An LCD module has 16 pins with backlight and contrast adjustment functions.

Features

  • Operating Voltage of an LCD is 4.7V to 5.3V.
  • The current consumption is 1mA without backlight.
  • Alphanumeric LCD module. It can display alphabets and numbers and some characters.
  • Consists of two rows and each row can print 16 characters.
  • Each character is built by a 5×8 (or 5×7) pixel box.
  • Can work on both 8-bit and 4-bit mode. This feature is handy when you don’t feel like using a lot of pins for one module. More on this later.
  • It can also display any custom generated characters.
  • Most commonly available in Green and Blue Backlight.
Item Symbol Standard Value
Typ.
Unit
Input Voltage VDD 3/5 V
Recommended LCD Driving
Voltage for Normal Temp. Version module @25°C
VDD-VO 3.70 V

What are the physical dimensions of the LCD that we are going to connect to the Arduino Uno?

Item Standard Value Unit
Module Dimension 80.0 × 36.0 mm
Viewing Area 66.0 × 16.0 mm
Mounting Hole 75.0 × 31.0 mm
Character Size 2.95 × 5.55 mm

What are the functions of the pins present on the LCD screen?

LCD-Display-interface-with-arduino

In JHD162A LCD which has 16 pins,

Pin No. Function  Name
1 Ground (0V) Ground
2 Supply voltage; 5V (4.7V – 5.3V)  Vcc
3 Contrast adjustment; the best way is to use a variable resistor such as a potentiometer. The output of the potentiometer is connected to this pin. Rotate the potentiometer knob forward and backward to adjust the LCD contrast.  Vo / VEE
4 Selects command register when low, and data register when high RS (Register Select )
5 Read when low and write when high. 

The Read/Write (RW) pin (pin 5) of an HD44780 LCD is the pin which either enables a user to write to the LCD so that it displays characters (write mode) or to read the characters from the LCD, so that is on it

The dominant majority of the time, the RW pin will be set to Write mode. In some rare cases, we may need to read from the LCD what it is displaying. In such cases, the RW pin will be set high. However, the vast majority of the time, the RW pin will be set low, since we are concerned about writing characters to the LCD.

Modes of RW Pin
Read Mode- RW set high (or to Vcc)
Write Mode- RW set low (or to the ground)

Read/write
6 Sends an enable HIGH for a few milliseconds when we need to execute an instruction. Then back to LOW. Enable
7 8-bit data pins DB0
8 DB1
9 DB2
10 DB3
11 DB4
12 DB5
13 DB6
14 DB7
15 Backlight VCC (5V) Led+
16 Backlight Ground (0V) Led- 

 

How to send commands and data to an LCD module?

16×2 LCDs have two built-in registers. Namely,

  1. Command register
  2. Data register

Command register:

We use this register to insert a command into the LCD. These commands can be things like telling the LCD to clear the screen, set the cursor, move to line 1, move to character 1, etc. Here’s the table of all the commands that you can send to the LCD.

Sr.No. Hex Code Command to LCD instruction Register
1 01 Clear display screen
2 02 Return home
3 04 Decrement cursor (shift cursor to the left)
4 06 Increment cursor (shift cursor to the right)
5 05 Shift display right
6 07 Shift display left
7 08 Display off, cursor off
8 0A Display off, cursor on
9 0C Display on, cursor off
10 0E Display on, cursor blinking
11 0F Display on, cursor blinking
12 10 Shift cursor position to left
13 14 Shift cursor position to the right
14 18 Shift the entire display to the left
15 1C Shift the entire display to the right
16 80 Force cursor to the beginning (1st line)
17 C0 Force cursor to the beginning (2nd line)
18 38 Two lines and 5×7 matrix (8-bit mode)
18 30 1 line and 5×7 matrix (8-bit mode)
18 20 1 line and 5×7 matrix (4-bit mode)
18 28 Two lines and 5×7 matrix (4-bit mode)

Here’s a slightly detailed version of the above table. Don’t get too caught up in reading these. You’ll most probably not even need a bunch of these codes. Also, the ones you need, you’ll pick up as you code. Don’t sweat trying to remember these. Just glance at them and move on. It’s just a bunch of hex codes that control stuff on the LCD.

16x2 LCD commands register control words table
This is a slightly more detailed table consisting of the various commands that can be sent to an LCD. For the most part, the chart above this one will suffice. You can just ignore this one. But if you need the additional details (you daredevil), you can get them off this table. Save the image or bookmark this page. 

Data register

You can send the ASCII codes of the characters you would like to display into this register. Here’s the ASCII table for all the characters that you can view on a 16×2 LCD screen.

Ascii Table to display characters on LCD screen - Arduino tutorial

Difference and use case of 4-bit and 8-bit functions feature of the LCD.

8-bit mode working of the 16×2 LCD

In our case for the 8-bit mode, the 8 data pins (D0-D7) are the data and address bus while the three control pins(RS, R/W, and E) are the control bus. Thus using these, we can control the LCD peripheral that we are interfacing.

We are greedy, so we want to interface as many peripherals as possible with the same microcontroller. This requires a large number of ports. Or we need to be smart and utilize what we have to the fullest. Throughout this free Arduino course, you’ll notice us stressing about the importance of pin real estate.

Thus the first thing we try to do is reduce the number of pins required for controlling the peripheral. Here comes the need for 4-bit mode. Using this mode, we end up cutting the port pins needed from 11 to 7. It might not seem much, but for a small microcontroller with limited port pins, this is a significant amount.

4-bit mode working of the 16×2 LCD

In the 4 bit mode, we send the data nibble (4 bits/half a byte) by nibble. First upper nibble and then lower nibble. For those of you who don’t know what a nibble is: a nibble is a group of four bits, so the lower four bits (B0-B3) of a byte from the lower nibble while the upper four bits (B4-B7) of a byte from the higher nibble. This enables us to send 8-bit data or ASCII code or the command code by using four pins instead of 8. The connections remain identical. The only change is that the lower nibble pins of LCD are unused (D0-D3).

How to connect the LCD with the Arduino Uno?

The Arduino Uno has a microcontroller that can read data from various inputs like sensors and also can compute it according to the program coded. Here we use Arduino Uno, which has 14 digital input/output pins and six analog pins.

Check out the schematic below to hook up your LCD module with an Arduino Uno.

arduino connection with LCD circuit

Circuit for connecting an Arduino with an LCD. (Source)

  1. LCD pin 1 (GND) is connected to the ground pin of the Arduino and to one end of the 10k potentiometer
  2. Pin 2 (Vcc) connects with the 5V Vcc pin of Arduino.
  3. Connect the third pin of the LCD (Vee) with the middle pin of the 10 k potentiometer.
  4. You can connect the fourth pin (RS) with any of the digital pins on the Arduino. However, make sure that you include this connection in the code accurately. The Register select pin controls whether commands or data is being sent to the LCD. When the pin is given a 0 (LOW), it accepts commands as input. When it is given a 1 (HIGH), it takes data as input. We will see this implementation in the code.
  5. Connect pin 5 (R/W) with the ground pin of the Arduino. Since we are writing to the LCD, we will select the Write mode by connecting it to a LOW value.
  6. LCD pin 6(EN) connects with any of the digital pins on Arduino. This is the enable pin, and it enables the next seven pins of the LCD (D0-D7) to send 8-bit data.
  7. LCD pin 7- pin 14(DB0-DB7) connects with any of the digital pins of the Arduino. However, remember to declare these as data pins in the code.
  8. Next, connect Pin 15(LED+) with the 5V Vcc pin of Arduino. The function of this pin is to control the backlight of the LCD screen.
  9. LCD pin 16(LED-) should be connected with the ground pin of the Arduino.

Coding for connecting the LCD screen with an Arduino Uno

The Arduino program for displaying data on LCD is done in the Arduino IDE. First, we need to connect the Arduino environment with our Arduino Uno board by selecting a suitable port.

The Arduino code should contain two functions as void setup() and void loop() all pin mode declarations are made in void setup() and the function to be performed, and the computing process is coded in void loop(). This part will run infinitely.

Programming in Arduino is usually done with embedded C. For displaying data on an LCD, we’ll require a header file (library) – called LiquidCrystal.h. This file is available in the Arduino IDE. Here’s how you can include a library in your program.

In void setup() function, we need to begin the function as object.begin(16,2), which indicates the 16 rows and two columns. And simply print “Hello world” by writing a function called object.print(“Hello world”). Use delay() function to keep the data stay on display for some seconds. The argument of this delay() function should be in milliseconds.

In the void loop() function, we need to set the cursor using the object set cursor(0,0) function. The arguments are column and row at which the data starts to be printed and print the information whatever you want. It is necessary to pass arguments for the object created in LiquidCrystal library which has six arguments as digital pin of Arduino to which the Read/write pin of LCD is connected, digital pin of Arduino to which the enable pin of LCD is connected, and followed by digital pins of Arduino to which the six data pins of LCD is connected.

Some of the built-in functions of the LiquidCrystal library are mentioned here:

  • LiquidCrystal(R/W, EN, D4, D5, D6, D7) – to initialize the digital pin modes of Arduino to wich the Read/Write, enable, and data pins of LCD are connected
  • clear()- to clear the display.
  • home()- to set the cursor at the top left of the LCD screen.
  • setCursor(col, row)- to set the cursor at position wherever we want. It has two arguments as column and row, which are the coordinates of the point where we want to set the cursor.
  • write(data) – to write a character on the LCD screen.
  • print(data, base)- to print the data on the LCD screen. The base argument is optional, which determines the base in which the data should be printed as binary(base 2), decimal(base 10), octal(base 8), hexadecimal(base 16).
  • cursor()- to show the cursor at which the next character will be written.
  • no cursor()- to hide the cursor()
  • blink()- it makes the cursor blinks.
  • blink()- it should turn off the blinking cursor
  • right to left()- it causes the text flow from right to left.
  • left to right()- it causes the text flow from left to right.
  • auto-scroll()- it should move all the text one space to the left.
  • scroll()- it should stop the scrolling of text.

Program to connect the LCD screen with the Arduino Uno

Code to display hello world on 16×2 LCD

#include <LiquidCrystal.h>

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

void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
}

void loop() {

}

Example code for displaying sensor readings on liquid crystal display using Arduino Uno

// include the library code:
#include <LiquidCrystal.h>

// initialize an object with the numbers of the interface pins
LiquidCrystal lcd(R/W,EN,D4,D5,D6,D7);

int data1 = A1;//analog pins
int data2 = A2;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();

pinMode(data1, INPUT);
pinMode(data2, INPUT);
}

void loop()
{
lcd.setCursor(0,0); // Sets the cursor at col 0 and row 0
lcd.print("Sensor1: "); // Prints Sensor1: on LCD
lcd.print(analogRead(data1)); // Prints data1 on LCD
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("Sensor2: "); // Prints Sensor2: on LCD
lcd.print(analogRead(data2)); // Prints data2 on LCD
}

Common errors with the interfacing of the Arduino and the LCD screen and their troubleshooting

Some of the common problems arising while interfacing Arduino Uno with LCD screen are listed below.

There is no problem with data pins of LCD. It could be connected with any of the Arduino’s digital pins, which should be declared in the program.

It is necessary to connect a potentiometer with Vout to control the backlight. If you didn’t connect the potentiometer, the screen would appear shady.

A resistor of 1kΩ or 3.9kΩ should be connected with the LED+ Anode pin of the LCD to control the contrast. Otherwise will fall into some issues like not showing the data clearly.

Liquid crystal display requires an input voltage Vcc in the range between 4.7V to 5.3V. Fluctuations in the voltage given by 5V Vcc out pin of Arduino may cause problems in getting an output even if the connections are correct. To avoid this issue, use a stable power supply for Arduino and check whether there is fluctuation in 5V Vcc out the pin. In this case, use resistors with the corresponding ohm.

Many users prefer to connect 10kΩ with anode pin of a backlight in LCD, but it works well with a 1kΩ resistor too.

Applications of the 16×2 LCD

Alternatives to using a 16×2 LCD

You can use a seven-segment display instead of an LCD. Those look way sharper and brighter. Check out our post on connecting a seven-segment display with an Arduino Uno.

Were you able to successfully build this project? If you have any queries or doubts, let us know in the comments section below, and we would love to help you out.

Interfacing Air Quality Sensor-pollution (e.g. MQ135) – display data on LCD, switch on LED when data
sensed is higher than specified value.
Interfacing Bluetooth module (e.g. HC05)- receiving data from mobile phone on Arduino and display on
LCD
Interfacing Relay module to demonstrate Bluetooth based home automation application. (using
Bluetooth and relay).

Leave a Response