IR PIR sensor code to count on display

Hello!

im doing a Project at school that i need some help with, im new to arduino and need some help with the code. im trying to get the display to Count when a object go past the sensor. im using an 16x2 display with a IR PIR sensor and arduino UNO plate. would love some help on how to do this!

thanks for answer!

Which PIR.sensor? Have u got it working?
Are u able to write to the display?

would love some help on how to do this!

What object is going by the sensor? A PIR sensor measures Passive Infrared. If you object is not emitting Infrared, it won't be detected, regardless of what code you write.

The PIR sensor is infrared sensor that detect motion. the sensor are working since i tried with a simpel code to detect movement using my hand and a shirt. the display works aswell since i tested with "hello world" code, so everything works!

so everything works!

So, you don't have a problem?

loop:

  • wait until sensor 'high'
  • count+=1
  • display it
  • wait until sensor 'low'

This is the code im using right now, so the display react with number one when motion is detected. i need the display to count the number of times my hand go past the sensor. right now the number 1 pile next to eachother and doesnt add like it should, i want the number 1 to dissapear and number 2 should replace it.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup() {

lcd.begin(16, 2);

pinMode(inputPin, INPUT);
Serial.begin(9600);
// Print a message to the LCD.
lcd.print(“Summa”);

lcd.setCursor(0, 1);

}

void loop() {

val = digitalRead(inputPin);
if (val == HIGH) {

if (pirState == LOW) {

lcd.print("+=1")

pirState = HIGH;
}
} else {
if (pirState == HIGH){

lcd.print("");

pirState = LOW;
}
}
}

You need to look at the state change detection example. The PIR sensor tells you that it is, or is not, detecting motion. If you keep track of the answer you got last time you asked, you can tell if motion HAS BEGUN or if motion HAS ENDED.

You decide which event is important, and increment a counter when that even occurs.

Forsök 2:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int inputPin = 2;
int hittils;

void setup() {
  lcd.begin(16, 2);
  pinMode(inputPin, INPUT);
  lcd.setCursor(0, 1);
  lcd.print("Summa");// Print a message to the LCD.
}
void loop()
{
  while (!digitalRead(inputPin)); // vent tills 1
  hittils += 1; // ök telleren
  lcd.setCursor(0, 10);
  lcd.print(hittils);
  while (digitalRead(inputPin)); // vent tills 0
}

thank you so much for the code! but now i got a new problem :stuck_out_tongue: im trying to connect another sensor on the board but dont manage to get the code right, i tried using int inputPin = 3; but it wont work.
thanks for answer!

but it wont work.

If it compiled, you can be certain that it works. That it doesn’t do what you expect means only that your expectations are wrong.

Post the code and a schematic, and describe your expectations, as well as any observations about what the code actually does, if you want us to help you align your expectations with reality.

I will send it as soon as i got the items with me, but is it possible to copy the code and paste it under, sending the code for you to understand what i mean.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int inputPin = 2;
int inputPin2 = 3;
int hittils;

void setup() {
lcd.begin(16, 2);
pinMode(inputPin, INPUT);
pinMode(inputPin2, INPUT);
lcd.setCursor(1, 0);
lcd.print(" Summa");// Print a message to the LCD.

}
void loop()
{
digitalRead(inputPin);
while (!digitalRead(inputPin)); // vent tills 1
hittils += 1; // ök telleren
lcd.setCursor(0, 10);
lcd.print(hittils);
while (digitalRead(inputPin)); // vent tills 0

digitalRead(inputPin2);
while (!digitalRead(inputPin2)); // vent tills 1
hittils += 1; // ök telleren
lcd.setCursor(0, 10);
lcd.print(hittils);
while (digitalRead(inputPin2)); // vent tills 0

}

  digitalRead(inputPin);

Put a blindfold on and read the pin. Why do you bother reading the pin when you don't care what the state of the pin is?

Why do you do the exact same thing for both switches? I'd expect one to increment the counter and one to decrement it.

the project is to have two sensors, one is counting +1 and the other is counting +2, i dont understand what you mean with the answer :stuck_out_tongue: im beginner on arduino so i understand if you get angry :slight_smile:

the project is to have two sensors, one is counting +1 and the other is counting +2

Is that that IS happening, or what you want to happen?

When switch one becomes HIGH, you do this:

  hittils += 1; // ök telleren
  lcd.setCursor(0, 10);
  lcd.print(hittils);

When switch two becomes HIGH, you do this:

  hittils += 1; // ök telleren
  lcd.setCursor(0, 10);
  lcd.print(hittils);

Unless I'm mistaken, that's exactly the same thing in both cases.

I'm not getting angry, damn it! 8)

I want one sensor to count +1 and the other +2, right now sensor number 2 dont count, (the +2 sensor) so i need a code that makes the 2 sensors count with eachother, for exampel, a item go past sensor 1, display count +1. something go past sensor 2, +2, and display show 3. ask me again if you dont understand. thanks

ask me again if you dont understand.

I don't understand how your switches are wired.
I don't understand what the code you posted actually does.
I don't understand why you currently do the same thing when either switch is pressed.

Hi again! i have changed the code but it still wont work! :frowning: so the code is this: but all that is happening is that sensor one is working and the other wont count, please help!

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int inputPin = 2;
int inputPin2 = 3;
int hittils;

void setup() {
lcd.begin(16, 2);
pinMode(inputPin, INPUT);
pinMode(inputPin2, INPUT);
lcd.setCursor(1, 0);
lcd.print(" Summa");// Print a message to the LCD.

}
void loop()
{
digitalRead(inputPin);
digitalRead(inputPin2);
while (!digitalRead(inputPin));
while (!digitalRead(inputPin2)); // vent tills 1
hittils += 1; // ök telleren
lcd.setCursor(0, 10);
lcd.print(hittils);
while (digitalRead(inputPin)); // vent tills 0
while (digitalRead(inputPin2));

}

PaulS:
I don't understand how your switches are wired.
I don't understand what the code you posted actually does.
I don't understand why you currently do the same thing when either switch is pressed.

I will post the curcit in a minute.
the code is used for two sensors, one to count +1 and the other +2.
because im a noob on this and dont understand xD

This is the circuit im using!