Pages: [1]   Go Down
Author Topic: digitalRead always LOW / 0  (Read 611 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So guys ...
if got an DFRobot Mega with ATMega1280.
This is the code:
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 =)
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5976
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no i don't
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5976
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i'll try it
Logged

Firenze, Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Dislessia portami via
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

The brightest flame burns quickest

Pages: [1]   Go Up
Jump to: