Pages: [1]   Go Down
Author Topic: can't read LOW ?  (Read 582 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the situation:

Code:
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?

« Last Edit: May 25, 2012, 07:40:45 pm by softuino » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 198
Posts: 12755
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 94
Posts: 4758
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 198
Posts: 12755
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 94
Posts: 4758
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The internal pullup enables a very safe switch sensing between a pin and ground though yes, the logic is "reversed".
Logged

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 94
Posts: 4758
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

Pages: [1]   Go Up
Jump to: