Arduino

Arduino Tutorial #21 How to make counter using PIR sensor with LCD display

PIR sensor counter with LCD display

In this tutorial we will see how to make counter using motion detection sensor (PIR) and output will be shown on LCD display. It will count how many times object passed over it. So let’s get started. For this you will need

  1. Arduino,
  2. LCD display 16X2,
  3. PIR sensor,
  4. Potentiometer (For adjusting contrast of LCD),
  5. LED,
  6. Resistors,
  7. Breadboard,
  8. Jumper wire.

 

Back side of LCD display
Back side of LCD display

 

At the back side of LCD you can see, which pin number of LCD is for VCC, VSS etc.

circuit diagram for PIR sensor counter with LCD
circuit diagram for PIR sensor counter with LCD

 

Do connection as shown in diagram. I have used 2.2K ohms resistor for LCD backlight. Depending upon your need of brightness of display you can increase or decrease the resistance. If LED is not working after doing connection as per shown in diagram, try to add separate ground for LED.

 

Sketch for PIR counter with LCD:
// 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);
int ledPin = 13;
int pirPin = 10; 
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare sensor as input
 lcd.begin(16, 2);
 lcd.setCursor(4, 0);
 lcd.print("counter");
}
void loop(){
val = digitalRead(pirPin); // read PIR sensor input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
currentState = 1;
// We only want to print on the output change, not state
pirState = HIGH;
delay(1000);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
currentState = 0;
// We only want to print on the output change, not state
pirState = LOW;
}
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.setCursor(4,1);
lcd.print(counter);
delay(200);
}
}
}

 

I have made this tutorial already but in this video we have added LCD so that we will get output on LCD not on serial monitor. This code is already explain in tutorial motion detection sensor as a counter. I will cover this code briefly here. Include Liquid crystal library which is preinstalled in Arduino IDE. Initialize library with the numbers of interface pins. You can see in circuit diagram 2,3,4,5 and 11, 12 pins are connected to LCD. Define LED and PIR pin numbers. Here we are declaring parameter such as current state , previous state, val which will tell Arduino motion detected. In setup function declare pinmode of LED and PIR. Lcd.begin will define dimension of LCD in this case it is 16 columns and 2 rows. I am Setting cursor at little bit right side. That’s why I took 5th column but its index number is 4. Similarly for first row its index number is 0. In loop function Lcd.print will print the text counter. If PIR sensor get motion detection it will turn on LED. Otherwise LED will be off. We want to count when there is motion detection therefore if currentsate is equal to 1 it will count 1.  Initially counter value is set to zero. We have set cursor for counter value in second row. Lcd.print will print the counter value. Make sure it will be not in double quote otherwise you will get counter text as an output.

You can see when I move my hand over sensor it will count how many times my hand pass over it.

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2vqnDHc   (PIR sensor)

http://amzn.to/2vmSK8l  (Resistor)

http://amzn.to/2vSpUON   (LED)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

KEEP LEARNING KEEP MAKING 🙂

2 thoughts on “Arduino Tutorial #21 How to make counter using PIR sensor with LCD display

Leave a Reply

Your email address will not be published. Required fields are marked *