3

I have a device that has one blinking LED.

I want to count the number of blinks. I planned to do it using Arduino. I will connect two terminals of LED input to Arduino digital input pins, and I will count the number of positive voltage pulses

I just want to know if it is possible? and if yes then can you please hint the code for the same?

3
  • 2
    be aware that an LED can be turned on by applying Vcc to the LED, but it can also be turned on by applying 0 V ... it all depends how the LED is connected in the circuit
    – jsotola
    Commented Dec 24, 2019 at 20:30
  • Look in the Arduino IDE. There is an example called "State Change Example" That's what you need.
    – Delta_G
    Commented Sep 23, 2020 at 1:29
  • Trying to use the voltage over the LED might not work, because it only changes from 0V to the forward voltage of about 2V (+/- some tenth). Use jsotola's comment and use the side that gets switched, this could as well be at the current limiting resistor. Commented Sep 23, 2020 at 16:35

2 Answers 2

1

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;
    }
}
4
  • 2
    There is no "light diode resistor". I think you mean a light dependent resistor. Though I wouldn't suggest an LDR for blinking measurement, they are rather slow. Depending on the blinking frequency this could very well be too slow. A photo transistor is a better choice (a phototransistor is basically a diode in reverse, which get's conductable by light), it can work very fast.
    – chrisl
    Commented Dec 24, 2019 at 23:29
  • Sir, I am planning to solder. But I think I have to solder both the wires from the led. I thin voltage difference is relative, so how do I compare the difference between voltage(when LED glows) using digital pins.
    – SSR
    Commented Dec 27, 2019 at 21:56
  • @chrisl Thanks for the notification (I mean indeed light dependent resistor). I never tried a photo resistor (added your name, upvoted your comment). Commented Dec 27, 2019 at 23:46
  • @SSR At least both wires should be connected to something otherwise you don't have a (closed) circuit, at least not with the LED in it. For comparing you might use an opamp but that's probably worth a different question. Commented Dec 27, 2019 at 23:47
1

Detecting the LED Pulses

You want to detect each LED pulse. For this you will need edge detection code. There is an Arduino example built into the IDE and information describing the function here: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection

To add to the Arduino explanation, you can either detect the rising edge (ie. the transition from a LOW to a HIGH) or the falling edge (ie. the transition from a HIGH to a LOW). I personally prefer the following code to detect a rising edge. In simple terms the code can be translated to this:

if ( (lastLedState == LOW) && (ledState == HIGH) )

where ledState would be assigned by a digitalRead and lastLedState would record the value of ledState at the end of the loop function (ie. just before it reads in the led states again).

Full Code

Now if we optimise the above statement and put it into context:

unsigned long int pulseCounter = 0;
const int LedInput = 3;    // Digital input 3
bool ledState = 0;
bool lastLedState = 0;

void setup() {
    
}

void loop() {
    ledState = digitalRead(LedInput);
    
    // If the LED turns ON
    if (ledState && !lastLedState)
    {
        // Increment your counter
        pulseCount++;
    }
    
    // Update LED flag
    lastLedState = ledState;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.