Arduino and Photoresistor - trigger on frequency

Hi everyone!

I’m currently working on a project that requires me to have some sort of authentication mechanism in place.
The idea is this - we have a photoresistor wired to an analog INput. I need to be able to detect whether a certain frequency is present or not (5-10% error scope).

Here is a maybe more understandable example - I oscillate an LED at some frequency /maybe using a 555 timer/ next to the phototransistor and if it’s the right frequency we, for example, digitalWrite another LED HIGH.

I’ve searched the Internet high and low, trying to find something similar done and documented before, but maybe I’m not looking in the right place…

Thanks in advance to anyone who might be able to help with this :wink:

Some people claimed that this function might be used to get the frequency (PulseIn) but as to exactly how to implement it (code-wise) and whether the photoresistor could get the values quickly enough - I have no idea…

I’m thinking about doing something like this - setting a threshold and measuring last and current reading - if ((current-last)>0&&(current-last)>threshold) - set HIGH… else if ((current-last)<0&&(last-current)>threshold) - set LOW…
Hopefully you get the idea…

Thanks again to anyone who takes the time to try and help… :slight_smile:

There is a frequency counter library that has been mentioned on the forum. Might be a starting point, at least.

First, thanks for the quick reply... :D

Are you referring to this?

As far as I could tell this library needs a digital input... How would this tie in with the photoresistor?

From the description... "The Frequency input is fixed to digital pin 5."

If you could average the input from the photoresistor over a period longer than the period of your desired frequency (which has to be quite low because photoresistors are quite slow) then any signal which is x above the average becomes a "HIGH" and any signal which is x below the average becomes a "LOW". Then you could count how long the HIGHS and LOWS are and if they are within the correct time for a few cycles then you can accept the input as valid. This way it could work with different ambient light levels. I would figure out "x" by sending the value to the serial monitor and watching to see what kind of numbers you get. If you used a IR remote pickup IC, it does most of that for you and you could use a digital input instead.

At one two pointa you refer to a photoresistor, and in another to a phototransistor.

Which is it?

You also don't mention what frequency you are looking to detect. Basically a photo resistor will be too slow to respond to anything much over a few hundred Hz. So you probably need a photo diode or photo transistor.

Thanks to everyone for the replies!

Sorry, I couldn't respond more actively, but I fell asleep on the computer ;D

1) I currently have a photoResistor wired to analog IN. Sorry for the mistake. It's not a photoTransistor... My bad.

2) The photoresistor is being used to drive a servo, which opens a door, when you shine a light trough a small hole somewhere on the door /e.g. value of photoresistor > threshold/. This is my setup simplified /I have a lot of other components connected, but they don't relate directly to the task at hand/. :) The problem with this is that I'd like to authenticate this opening somehow /so that even if someone knows where the hole is and how to open it, he doesn't know that he has to use something other than just 'any' light to do it/ and one of the ways I thought of doing it was to look for some frequency /it doesn't make any difference to me what that frequency is as long as I don't have to stand in the hallway blinking at the door for more than 1,2 seconds :D / I'm currently using a regular pocket LED light. I hope this clears some of the confusion I caused :)

3) Ideally, if it's not absolutely necessary I'd like the photoresistor to remain as an analog Input, and doing the authentication in software, rather than using an remote control IC and putting it in as a digital Input.

Thanks to everyone, who tried to help! 8-)

p.s. On a side note: I might consider moving the whole thing to the IR realm, just to make it cooler ;D just a change of the LED on the transmitter part and a change of the receiver could do the trick after we firm off /hopefully with the help of the all-mighty Arduino community/ the authentication part...

OK final question. The pocket LED you have, are you planning to flash it from a push button or do you want to make the LED controlled with something like a 555 timer so the flashing is a regular controlled time?

I was thinking of possibly using a 555 /I think I mentioned it in the original post/ just because this is my dorm room and I don't think I'll always be in the right state of mind to successfully put out 10100100101001 in rapid succession ;D /just to not confuse anyone the '10100100101001' part was an attempted joke (rofl)/.

But seriously a 555 would be a neater solution both for ease of use and would minimize false positives.

Thanks for the reply!

Fine, if you use the NE555 to output a frequency of say 10 Hz then it looks flashing and it is slow enough to measure the pulse time with a simple recording of millis(). Then you need only open the door if the pulse time you measure is between two limits. That is not too short and not too long. I would give it about 40% either side of the nominal.

Thanks for the reply!

I think I'm getting an idea of what you're saying - maybe when the threshold is over some value /e.g. there is light shined on the photoresistor/ i start function authenticate();

Then the function during a period of say 2 seconds measures the number of blinks and then subtracts the millis(); Were you referring to something like this? If you could possibly throw in 2-3 lines of the authentication code your help would be invaluable!

Thanks again! I owe you one ;)

I was thinking more of looking at the input until it reaches a threshold. Then copying the value of miils() into a long int variable. Carry on looking at the input until it drops below the threshold and then subtracting the variable from the current value of millis() to give you the time for one on cycle. Then use this time to see if it is in the range to open the door.

Okay, thanks again!

I'll try that!