Go Down

Topic: digitalRead always LOW / 0 (Read 722 times) previous topic - next topic

Andy1253

So guys ...
if got an DFRobot Mega with ATMega1280.
This is the code:
Code: [Select]

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 =)

SurferTim

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



SurferTim

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.  :)




IngHooch

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

Code: [Select]

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

The brightest flame burns quickest

Go Up