Skip to main content
2 of 2
added 4 characters in body
Michel Keijzers
  • 13k
  • 7
  • 40
  • 58

You should not count the number of pulses, but the number of transition from 0 to VCC volts or VCC to 0 volts (called rising resp. falling edge).

If you don't want to solder, you can also use a light dependent resistor (LDR) very close by the LED to measure the difference in brightness. See also chrisl's remark below.

The counting you can do manually (in a loop, check as often as possible or needed), or by setting up a timer to check for either rising or falling edges.

I will give the pseudo code so you can figure out the actual code:

bool _ldrPin;
unsigned long int _counter;


setup()
{
   // Set Serial
   Serial.begin(9600);

   // Set LDR
   setAnalogPin(X, INPUT);
   _ldrPin = readAnalog(X);
}

loop()
{
    if (!_ldrPin && readAnalog(X) == HIGH)
    {
        // Rising edge, increase counter
        _counter++;
        Serial.Write(_counter);
        _ldrPin = true;
    }
    else if (ldrPin && readAnalog(X) == LOW)
    {
       // Falling edge, reset status
        _ldrPin = false;
    }
}
Michel Keijzers
  • 13k
  • 7
  • 40
  • 58