code to count number of blinks using ldr

we are planning to do a project on iot based energy meter. For that we need to count the num er of blinks of energy meter using ldr. ldr is connected to pin 2 of the arduino board. could anyone please help me to write the code?

Riyaro:
we are planning to do a project on iot based energy meter. For that we need to count the num er of blinks of energy meter using ldr. ldr is connected to pin 2 of the arduino board. could anyone please help me to write the code?

what have you tried so far ?

have you looked at the Example sketch AnalogInput.ino ?

find out what analogRead() values correspond to the LDR reading "off" and "on".

Riyaro:
could anyone please help me to write the code?

This adafruit tutorial shows how to connect and read an ldr. Do you have it connected in a divider like they show? You will need to experiment to find a threshold that reliably defines your meter's light as "on" or "off". Once you've got that, have a look at this State Change Detection tutorial to show how to count inputs.

thank you all. i will refer the suggested links

I wonder if an LDR will respond quickly enough to the blinks. What is the minimum duration of a blink and what is the maximum number of blinks per second?

A photodiode might be a better option.

If an LDR is fast enough you can probably wire it up so that it triggers a digital pin which would make the coding simpler than reading an analog value.

...R

Robin2:
I wonder if an LDR will respond quickly enough to the blinks.

This other thread is also an energy meter, and seems to work ok with an LDR (except for the problem discussed there.)

(I wonder of the forum members are in the same class :wink: )

LDRs are slow (by electronics standard), so you'll need to implement hysterysis or sample sufficiently infrequently that you
don't interpret noise on the waveform as switching.

Naive thresholding on a slowly varying analog input usually causes spurious transitions. Its worth
experimenting to see what waveforms you are getting.

A photodiode is faster, and the light-gathering usually more directional, both of which are probably
useful.

a visual graph might be helpful.

i'd been using a phototransistor for a line-follower setup and found that it was just too "digital" where i needed a more gradual indication of the "light/brightness state-change" and that's where the LDR came in to be more useful.

here's a plot i did to show the differences;