Arduino/C2/Display-counter-using-Arduino/English
Title of script: Display counter using Arduino
Author: Manivel and Nirmala Venkat
Keywords: Arduino UNO board, Arduino IDE, Compile and upload, LCD, Push button, counter using Arduino, Video tutorials
Visual Cue | Narration |
Slide 1: | Welcome to the Spoken Tutorial on Display counter using Arduino. |
Slide 2:
Learning objectives |
In this tutorial, we will learn to:
|
Slide 3:
Pre-requisites |
To follow this tutorial, you should have:
|
Slide 4:
System Requirement |
To record this tutorial, I am using:
|
Show the circuit diagram | In an earlier tutorial in this series, we had created a circuit using Arduino and LCD.
We will use the same circuit in this tutorial. |
Here, we will add a pushbutton and make a simple counter.
We have already learnt about the working of pushbutton in an earlier tutorial. | |
Now let us learn the connection circuit details. | |
A pushbutton is connected to a 100 ohm resistor.
The pushbutton is connected to pin number 7 and a 100 ohm resistor is connected to the ground. All other connections are exactly the same as in our previous experiment. | |
This is the live setup of the connection, as shown in the circuit diagram. | |
Switch to Arduino IDE | Now we will write a program in the Arduino IDE.
So let’s switch to the Arduino IDE. |
In the menu bar, click Sketch and Include Library.
Then select LiquidCrystal. |
First, we need to include the Liquid crystal library. |
Type LiquidCrystal lcd(12,11,5,4,3,2) | Type the code as shown.
I have initialized a variable lcd of type LiquidCrystal. Here, pin number 12 is initialized as Register Select and pin number 11 as Enable. The next 4 parameters represent data lines of LCD. |
Type,
lcd.begin(16,2); |
In the void setup function, type lcd.begin 16 comma 2 as shown.
This command initializes the LCD with rows and columns. |
Type,
pinMode(pbutton, INPUT); Type, int pbutton=7; |
Next we will setup the pin number 7 as INPUT.
Type the code as shown. In another way, we can store the pin number in a variable pbutton. Let us define the pbutton variable as shown. |
Point to Void loop
|
Now we will write the code for void loop.
Whenever the pushbutton is pressed, one count is increased on the LCD. |
Highlight,
if (digitalRead(pbutton)==HIGH) |
We will write a simple if statement to check whether the pushbutton is pressed or not. |
Type,
if (digitalRead(pbutton)==HIGH){ lcd.setcursor(0,0); lcd.print (“button pressed”); } |
Before displaying the count, let us check the status of the button.
This command will set the cursor position in the LCD. lcd.print will print the message. |
Click on compile and upload button on the tool bar | We will now compile and upload the program.
Now, I will press the pushbutton. |
Point to the output | Here we see the message “button pressed” in the LCD.
It shows that the pushbutton is working successfully. <pause> |
Next we will modify the program to set a counter. | |
Type
int count=0; |
We need a variable for counter.
Now, we will initialize the variable count to zero. |
Modify the print statement as,
lcd.print(count++); |
Modify the print statement as shown here.
count++ will increase the count by one, each time the button is pressed. |
Click on compile and upload button on the tool bar | Let us now compile and upload the program. |
Press the push button. | Now, I will press the pushbutton. |
It didn't work as expected.
We see a different count displayed here. Why is this so? | |
Point to the output | This is because we specified the status for button pressed.
But we didn't mention the status for button released. The output shows the incremented number based on the button pressed time. |
Type,
while(digitalRead(pbutton)==HIGH); |
So, we will write a while statement, as shown here.
This will display the count when the pushbutton is in pressed state. That means the pin 7 is in HIGH mode. When you release the button, the state is LOW and it will come out of the while loop. |
Highlight the program line by line | Let me explain the program.
The program starts with initialization of the LCD. We have the variable pbutton and count. Inside the void setup function, we have initialized the LCD with 16 columns and 2 rows. Then the pinMode is input for the pin number 7. In the void loop function, we are checking the condition whether the pushbutton is HIGH or not. When the pushbutton is pressed, the cursor is set to position zero comma zero. The lcd.print statement will print the count value. Initially count is zero. Count plus plus would be 1. When the button is released, it will break the while loop and come out of the loop. Again, if you press the button, the next iteration begins and it increases the count. |
Click on compile and upload button on the tool bar | Let us compile and upload the program. |
Show the output. | Now I'll press the button once and release it. |
Again I'll press and release the button.
You can see that the count is increased whenever the button is pressed. | |
This brings us to the end of this tutorial. Let us summarize. | |
Slide 11:
Summary |
In this tutorial, we learnt to:
|
Slide 12:
Assignment |
Do the following assignment.
|
Slide 13:
About Spoken Tutorial project |
The video at the following link summarizes the Spoken Tutorial project.
Please download and watch it. |
Slide 14:
Spoken Tutorial workshops |
The Spoken Tutorial Project Team:
For more details, please write to us. |
Slide 15:
Forum for specific questions |
Please post your timed queries in this forum. |
Slide 16:
Acknowledgement |
Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link. |
This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
Thanks for watching. |