Vehicle counter using Arduino and loop detector

We have used a Loop detector (MATRIX-S12-24) as a input with 12V. The output i.e NO and COM pins of the Loop Detector are calibrated along with external 5V and NO pin is given to A1 pin of Arduino. We are receiving an error in the Serial monitor.Please tell us how to calibrate the output of Loop detector.

we wrote this code for the counting system but we are getting random values sometimes.

int count = 100;
int previous = 0;

float voltage;

void setup() 
{
  Serial.begin(115200);
}

void loop()
{
  delay(2000);
 int sensorValue= analogRead(A1);
 voltage= sensorValue*(5.0/1023.0);
  if ( voltage == 0.00)
  {
    Serial.println(voltage);
  }
  else
  {    
    Serial.print("Car hai");
    Serial.println(voltage);
    delay(1000);
    if (previous != 0.00)
    {
      Serial.println("Same car");
    }
    else
    {
      count--;
      Serial.print("count is ");
      Serial.println(count);
    }
  }
  previous =voltage;
}

We have also attached the serial monitor readings when nothing is present on loop

thanks.

Why use analog signal, it jumps around everywhere at best.

Welcome to the forum
You might want to post a schematic. It sounds like you get an output from this detector and need to count how many you receive. As above this is a digital thing ie 1/0 rather than analog. Look at button tutorials and detecting when a button becomes pressed rather than when it is pressed. Then increment your counter

Hi
Do not overcomplicate your code, your task does not require to calculate voltage as float, better use raw integer sensorValue instead.

Get rid all float math from the code and show the results in the forum

Talking of over-complication, as already pointed out there's no need to do an analogRead() anyway. OP said the connections are NO and COM so it's a switch: digitalRead() is the way to go, with state change detection.

edit: or even easier, use one of the button libraries that does state change detection built in. I think (but may be wrong) ezButton does that.

We have used analogRead() because we are using the 5V as base to read the relay output. We have as well tried with digital also and we are still receiving the glitch.We want to receive higher voltage when obj is placed in the loop and decrement the counter indicating the availability, else we want a zero at output

What relay?... but even so, if there's a relay switching 5V on and off into your Arduino pin that's digital.

Not sure I understand. If you are taking a reading from a switch it is digital not analog. Ie the switch is open or closed. In arduino that is the same as any button and there are loads of sample codes for buttons.

This is the schematic made by LarryD of this parish. S3 is most typical
image

Circuit diagram please....


This is the schematic diagram of our project

I did the changes in code according to your suggestion still we are receiving glitch.
For the hardware part , i haven't connected a single wire still i'm getting high as output.

See S3 in post #8.

Throw the 5V supply away.

Put the switch across a digital pin and ground.

Use pinMode() with INPUT_PULLUP.

When the switch is open, pin will be 5V and digitalRead() as high; when closed it will be 0V and read low.

PS.... then put your led with resistor on another digital pin to ground, and digitalWrite() it high and low. (Or use the built in led.)

Seems you want to detect the arrival of a new car, not just the presence of a car: so for that you need to use state change detect as mentioned earlier. Here's the standard example modified for input_pullup, so-called "active low":

We did according to your solution but still receiving glitch.
(we have placed the object at 99 count and is present till the end of the output)
we are receiving error values i.e 0 in between the counting

The part of the code that shows, has pinMode as INPUT not INPUT_PULLUP for a start, and to count you need to use the state change detection as noted.

edit... maybe you are trying to use a form of state change detect, is see a if (previous).... but a screen shot of part of your code is useless.

Thank you so much for your help sir. We got the desired output.

That's great news, glad to help.

Now maybe post the working code and circuit diagram, so that others may benefit from the solution.

I've posted the final code and schematic diagram we referred

int count = 100;
int previous = 0;
int sensorValue = 6;
int voltage;

void setup() 
{
  Serial.begin(115200);
  pinMode(sensorValue,INPUT_PULLUP);
}

void loop()
{
  delay(2000);
  voltage= digitalRead(sensorValue);
  if ( voltage == LOW)
  {
    Serial.println(voltage);  
    }
  else
  {    
    Serial.print("Car hai");
    Serial.println(voltage);

    if (previous != LOW)
    {
      Serial.println("Same car");
    }
    else
    {
      count--;
      Serial.print("count is ");
      Serial.println(count);
    }
  }
  previous =voltage;
}
![circuit diagram (1)|689x372](upload://3HUlXwC7CvkW2GkjhiqeyPdlThc.png)