I made a little doorbell alert system before with a DIY opto-isolator. Basically, if the doorbell is pressed, an LED lights up, which triggers a photoresistor. The Arduino reads that and, if it's above a certain value, prints "ding dong!" to serial (there's more details nickglover.com is available for purchase - Sedo.com if you really want them). I finally purchased a real opto-isolator so I could get more consistent results, but I can't figure out how to set it up. Here's the datasheet: http://www.optekinc.com/datasheets/OPI110-113-1264.PDF. It uses a transistor, so does it hook up to a digital pin? What would the code for reading the transistor look like? Am I going to need to change the resistor I used for the LED?
I couldn't really make sense of the datasheet, so that's why I'm asking all these questions... Thanks for the help.
Emitter (pin 4 on the OPI110 and 113) to ground, collector (pin 3 on the OPI110 and 113) to an arduino digital pin. On the OPI1264 these pin numbers are reversed.
Then in software set the digital pin to be an input, then write a digital HIGH to it to enable the internal pull up resistor.
Then a digitalRead(pin); will read the state of the LED. A logic LOW will indicate the LED is on and a HIGH indicates it is off.
You will need a resistor in the LED side, it's value depending on the voltage you use. If it is 5V then 270R will be fine.
Ok, so here's what I have for the code, but it doesn't seem to be working. The pins are connected just like you said to connect them.
#define SENSOR 1 //pin for the photoresistor
int lightLevel;
int oldLevel;
void setup() {
Serial.begin(9600); //open serial port to receive data
pinMode(SENSOR, INPUT); //sets pin to input
}
void loop() {
digitalWrite(SENSOR, HIGH); //enables pull-up resistor
lightLevel = digitalRead(SENSOR); //read and store value from sensor
if (lightLevel == LOW && oldLevel == HIGH) { //check if button's state has changed
Serial.println("ding dong!");
} //if button is being held, message should only print at first press
oldLevel = lightLevel; //store sensor data to compare state when looped
delay(50); //no need to send too much data
}
One other thing: the doorbell is AC, so in my original circuit I connected a diode before the LED, which worked well enough as a DC converter because the LED can't blink fast enough for the photoresistor I was using as a sensor. Will that be a problem with the new circuit? Also, the circuit will be 10v (though I'm testing with 9v), so is a 470R resistor going to be enough? That's what I was using before, but I can't figure out what the max voltage and current for the LED side of the isolator are.
Put the;-
digitalWrite(SENSOR, HIGH); //enables pull-up resistor
into the setup part.
Change:-
if (lightLevel == LOW && oldLevel == HIGH)
to if ((lightLevel == LOW) && (oldLevel == HIGH))
Apart from that the code look OK.
The LED in the opto can only take 2V reverse voltage so if you have connected it up to AC then you might have blown it. You need a series diode with the opto's LED. Also put a capacitor across it to stop it from pulsing, 50Hz is no problem to any opto.
but I can't figure out what the max voltage and current for the LED side of the isolator are.
which worked well enough as a DC converter because the LED can't blink fast enough for the photoresistor
Hmmm... I wonder if this is true? I had a similar situation, and the "dark" parts of each second were being seen by the phototransistor/ monitoring silicon intelligence.
You may want to try playing with programming a separate simple test, from which you can build a simple "Is opto's LED seeing pulsed light?" routine...
Pseudocode...
initialize answer=no
for x=1 to 100 (make it big enough to span at least 80% of a full AC cycle)
{
is the opto LED on NOW?
if so, make answer=yes
}
Return "answer"
The idea of that: You look at the opto's LED many times, and if it is on even once, assume that a pulsed "on" is being sent to the opto-isolator, even if there are times (the negative phase of the AC cycle) when the LED isn't on.
Well, it worked in the original model because it was a DIY isolator and the photoresistor was running into an analog input. I did get some squiffy readings, but overall it worked pretty well.
Does it matter what size capacitor I'm using? I've had a series diode in there from before. The capacitor would come after the diode and before the resistor (before the LED), right?
Edit: Also, still doesn't work... assuming the LED resistor is the right value, I don't think it would be blown (well, I did have the wires leading from the protoshield to the doorbell/makeshift button test hooked up backwards at one point)... Why does LOW mean the LED is on?
it worked in the original model because it was a DIY isolator and the photoresistor was running into an analog input.
It is the photo resistor, those are slow. Analogue input has nothing to do with it.
Why does LOW mean the LED is on?
The LED produces light, the light injects current into the base thus turning the transistor on. If the transistor is on it conducts current and puts the collector low, which is measured by the ardunio.
The capacitor would come after the diode and before the resistor (before the LED), right?
No the capacitor is across the LED to smooth the voltage. Best experiment for the value, start off with 0.1uF.
I don't think it would be blown
If you wired it up to AC without a diode or without a series resistor or with a diode but the wrong way round I think you have blown it up.
If you need to replace your optocoupler you might get an AC model (which has two LEDs antiparallel) so you don't have to worry about polarity.
What you are really doing with the capacitor is debouncing the input. I've used capacitors both before (across the LED) and after (across the transistor) to filter AC signals. But in both cases the placement was just for convenience - is there a general principle why debouncing should be before or after the optocoupler?
Well, it has a diode and a series resistor, but for a time I had negative voltage going to where positive should've been going and vice versa. So if that would have blown it, then I guess it's blown. Sucks that I don't have another one around, or anywhere convenient to get one...
If you had the diode in series with the LED (and with the correct polarity of course) it should have protected the LED from reverse voltage.
Try switching the coupler on and off with a 9V battery (and appropriate series resistor!). If you have a multimeter, see if the voltage at the collector is what it ought to be (pulled to +5V by the Arduino pullup when LED off; pulled to ground by the phototransistor when LED on).