I am using an HCPL-817 optocoupler (Datasheet here) between two Arduino Pro Minis (both 5v version). Please see the attached diagram.
On the Sending Arduino, the following code writes pins 8 (an LED) and 12 (the optocoupler) HIGH for 3 seconds and then LOW for 3 seconds.
int OptoSignal = 12;
int LEDSignal = 8;
void setup()
{
pinMode(OptoSignal, OUTPUT);
pinMode(LEDSignal, OUTPUT);
}
void loop()
{
digitalWrite(LEDSignal, HIGH);
digitalWrite(OptoSignal, HIGH);
delay(3000);
digitalWrite(OptoSignal, LOW);
digitalWrite(LEDSignal, LOW);
delay(3000);
}
On the receiving Arduino, the following code reads the value on pin A1 every 100 ms.
int OptoSignalIn = A1;
int Reading_OptoSignal;
void setup()
{
pinMode(A1, INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
Reading_OptoSignal = analogRead(OptoSignalIn);
Serial.println(Reading_OptoSignal);
delay(100);
}
When pins 8 and 12 on the sending Arduino are written HIGH (ie the led and the infrared-led in the optocoupler are turned on), the Reading_OptoSignal value is approximately 18.
When pins 8 and 12 on the sending Arduino are written LOW (ie the led and the infra-red led in the optocoupler are turned off), the Reading_OptoSignal value is approximately 1020.
Years ago, I cobbled together a home-made optocoupler using an LED and a photoresistor. When the LED was turned on, the reading of the photoresistor increased. I expected the same behavior with the HCPL-817, that is, when pins 8 and 12 are written HIGH, I expected the Reading_OptoSignal value to increase not decrease.
Why does the value decrease when pins 8 and 12 are written HIGH? Is it due to the fact that the HCPL-817 uses a phototransistor, rather than a photoresistor? What exactly is being measured on pin A1 of the Receiving Arduino?