digitalRead always LOW / 0

So guys ...
if got an DFRobot Mega with ATMega1280.
This is the code:

void setup(){
  pinMode(39, OUTPUT); 
}

void loop(){
  digitalWrite(39, HIGH);
  Serial.println(digitalRead(39));
  delay(100);

  digitalWrite(39, LOW);
  Serial.println(digitalRead(39));
  delay(100);
}

To pin 39 is an Opto-isolator connected. So in Serial i always get 0. But if i change the pin to 41 (just an led connected) the Output is right. So it go 1 0 1 0 1 0 ....
Why it doesn't work with an Opto-isolator?

Thank you =)

Do you have a current limiting resistor in series with the opto-isolator input?

no i don't

That is probably why. The input of the opto-isolator is the anode of a led. The cathode is connected to ground. You must set the current when the state of the pin is HIGH. May I suggest a 470-500 ohm resistor between the Arduino pin and the opto-isolator input. Hopefully, you have not torched the output on that pin.

You write to the control register, and read from the pin. At least that is the way I remember it. :slight_smile:

ok i'll try it

Maybe is also necessary a little delay between the digitalWrite and the digitlaRead, something like this:

  digitalWrite(39, HIGH);
  delay(1);
  Serial.println(digitalRead(39));
  delay(100);