Simple counter missing objects


I need a small program to count the objects from a conveyor belt, and display the number on a small display. I did the program, it is working, but sometimes it misses an object. The objects are passing by an infrared sensor which i have connected to an optocoupler. The optocoupler is connected to the pin 2 of the arduino. The objects are passing by the sensor in group of 4. Most of the times it reads correctly, but sometimes it counts only 3 objects, after that it read correctly.

This may seem a small ammount if it only looses one object at an interval of several minutes, but if the total number of object is 5882, my arduino counted 5579, so 303 missed objects.

Some help would be greatly appreciated.

Here is the code i used:

int  buttonPin = 2;    
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;    

void setup() {

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin, INPUT);

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      Serial.print("number of objects:  ");
  lastButtonState = buttonState;

void clearScreen(){



Do you have a link to the sensor you are using? Maybe it can't react fast enough.

How fast is the conveyor belt moving?

This is likely a problem with the physical setup; getting things like that to work reliably often requires tweaking of the physical setup to ensure that it doesn't miss counts. Some IR sensors (with the beam of light and all) can be confused by the light bouncing off the surroundings (ie, going around the object that's supposed to be triggering it), so you may need to take steps to address that.

If the time taken to do these two lines of code:-

      Serial.print("number of objects:  ");

Is longer than either the high or low sensor signal this is what you will get. That is it will miss objects.

Hello and thanks for the answers. I don't think it's the sensor fault, becase it is a quite good sensor Wenglor ym22 here is the link: The sensor work with voltages form 10 to 30v. I power both the arduino and the sensor with a 12v power supply. The sensor provide a 12v signal when an object passes by it. Between the sensor and the optocoupler i placed a 1k 5w resistor. The optocoupler is connected to the arduino at pin 2 and GND. In the code i set the pull up resistor to set the pin as high. Whenever the sensor provide the signal, the optocoupler closes the circuit. here is the scheme: |500x294

This sensor also have a small led on that light when an object passes by it, so i watched that and it light every time the object passes. I think is an error in my code, becase the counter seems to have some delay, i mean the group of 4 object allready passed, but the arduino remains behind and post the number 4 after they passed, and here is when sometimes it counts only 3 it looses one. The speed of a group of 4 objects that passes by the sensor is about 2 seconds, after that is a pause of 3 seconds, then another group of 4, and so on.


That is not a schematic it is a physical layout diagram and a very poor one at that. You show no power going to the LCD and none of the wires coming from the components are labeled. Therefore it is impossible to tell if it is wired up correctly.
Their looks to be nothing wrong with your code. However you have still not answered the question about the timing of the sugnal. Groups are not important it is the time between each individual pulse that matters.

Grumpy_Mike has said that the time taken to actually send the lines of text may be longer than the time your sensor indicates an object. If that is the case, you will miss an object as the Arduino is busy doing something else.

Comment out the line outputting text and simply output the value. This will indicate if that is the cause.


Grumpy_Mike, I think the LCD may be representative of a serial display as his code does not indicate LCD.

Sorry for the poor diagram, the display is powered from the 5v pins of arduino. The TX of arduino is connected to the RX of the display. The time between pulses is about 500 miliseconds (it may be even less). If that is the case, than why most of the time it count correctly?

Please answer my question. Great the time between pulses is 500mS, now what is the time of the pulse, these are two different numbers.

Thanks. If you are reffering to the lenght of the signal that i depends of how long the object stays in front of the sensor.That is about 200 miliseconds or less.


“or less”
The very very important thing is the smallest pulse it can be, if it is too short then your code could miss it or it could be too fast for the opto isolator.

We still do not know how that is wired up or what part number it is, along with what wires you connect it to from your sensor.

You seem to think these things are unimportant but they are vital to understanding what is going wrong because on the face of it there is nothing wrong with your code given the information we have so far.

The opto isolator is a sharp pc123. About the exact timing i will go and check it now.

If you are going to only give half answers then I am out of here.

For the very last time, how is it wired up?

The best way to answer this is with a schematic. If you do not post a schematic in your next reply I am not going to read anymore posts in this thread.

I only have Fritzing installed so i done the scheme with it. Instead of the sensor i placed a photoresistor.


This does not show ALL connections.

Draw the complete layout on a sheet of paper and send a photo.


If your customer has already spent $400 on a YM22PA2 sensor, then perhaps you could ask them to spend another $80 to buy a pocket oscilloscope so you can see the waveform timing.

Hi, Have you tried using the other contacts on the sensor, the opto inverts your signal if you are using the NO contacts.

Also try a lower value of pull-up resistor on the output of the opto, the internal arduino ones may be to high and you may need a bit more collector current.

Also try 1K on the LED side of the opto, you may not have enough forward current to guarantee saturation.

Tom.... :)

Hello and thanks. I think i found where the problem is. I disconnected the arduino and connected a modified electronic calculator that i turned into a counter. I soldered 2 wires to the "=" key and then i pressed "1" and "+" keys, that turn any calculator into a counter. I then hooked the wires to the optocoupler and watched, it counted ok, but sometimes it missed one, sometimes it add an extra one. So it is clearly not a problem with arduino or a programming error. The cause of this is the object shape, which confuses the sensor. I think i will make my own sensor with a photoresistor and a laser beam. In this way when the object passes by the beam, it will change the state of the input pin.

ryu1: I soldered 2 wires to the "=" key and then i pressed "1" and "+" keys, that turn any calculator into a counter.

Boy, that's an old trick!