Go Down

Topic: can't read LOW ? (Read 629 times) previous topic - next topic

softuino

May 26, 2012, 02:34 am Last Edit: May 26, 2012, 02:40 am by softuino Reason: 1
Here's the situation:

Code: [Select]

int trigger = 12;

(...etc)

if(digitalRead(trigger)==HIGH){
  digitalWrite(8, HIGH);
}
if(digitalRead(trigger) == LOW){
  digitalWrite(8, LOW);
}


I have pin_8 connected to voltage supplier with a wire.
when I plug the wire, it normally goes to HIGH
meaning that it read HIGH correctly

but when I unplug the wire, it keeps HIGH, why?
I know that when an INPUT is not connected to circuit, it reports random changes in pin state
but how can I put the pin state at LOW when it senses no current, and after it been previously set to HIGH?


Coding Badly


Add a (10K) pulldown resistor to the pin: pin connected to resistor; other side of resistor connected to ground (GND).

GoForSmoke


but when I unplug the wire, it keeps HIGH, why?


An unconnected INPUT pin is said to be floating. I can't give the best explanation on just how that happens but you can't trust an unconnected pin for input unless you turn the internal pullup on, and then it will read HIGH.

To turn the internal pullup on, set the pin mode as INPUT and then digitalWrite the same pin HIGH. To turn that off, digitalWrite it LOW. The internal pullup supplies voltage through about 20k ohms when it's on.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

dkl65

Quote
To turn that off, digitalWrite it LOW.
That will pull the pin LOW through 20k ohms (pull-down resistor as Coding Badly described). However, using pull-up is recommended for digital inputs (reverse logic). You don't need to supply your own resistors; use the internal ones provided to you!

Coding Badly

Quote
To turn that off, digitalWrite it LOW.
That will pull the pin LOW through 20k ohms (pull-down resistor as Coding Badly described).


AVR processors do not have internal pulldown resistors.  What I described was an external pulldown resistor.  Writing LOW to a pin configured as an INPUT turns off the internal pullup resistor.

GoForSmoke

The internal pullup enables a very safe switch sensing between a pin and ground though yes, the logic is "reversed".
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Grumpy_Mike

Quote
but when I unplug the wire, it keeps HIGH, why?

It is called a floating input, read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

dkl65

Oops! So writing LOW to an input pin puts it into high impendance (completely unconnected, floating) mode. So you need to write HIGH to the input pin.

GoForSmoke

You need to have it hooked up like the examples on Mike's page there. Depending on how you hook it up you should use digitalWrite() to engage the internal pullup or not. If the pullup is used then grounding the pin through your switch will cause the pin to -read- LOW and when the switch is open it should read HIGH.
The pullup is just a 20k resistor between power and the pin that you can switch on or off using digitalWrite().

How is your understanding of basic electricity? Do you know Ohm's Law?
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up