Loading...
Pages: [1]   Go Down
Author Topic: How to read the state of an output pin ?  (Read 524 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

I'm trying to read the output state of an output pin but it seams not working.

I've got something like tihs :

Code:
digitalWrite(10, HIGH);
statepin10 = digitalRead(10);
if (statepin10 == HIGH)
{
dutyCycle = map (Ubat100, 1340, 1445, 100, 220);    
dutyCycle = constrain(dutyCycle, 100, 220);      
analogWrite(8, dutyCycle);  
}

But it doesn't analogWrite anything on my pin N°8 (only 0).

If I' m not doing right, how can I do to know the state of an output pin ?
« Last Edit: April 23, 2012, 03:48:18 pm by hary » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure you can digitalRead() an output pin. You might have to keep track of the state manually:

Code:
digitalWrite(10, HIGH);
statepin10 = HIGH;


Logged

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

Thank you.

At least, that's a way to make it working !
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 219
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think your code conclusively proves your point. Take this for example:

Code:
void setup ()
  {
  Serial.begin (115200);
  pinMode (10, OUTPUT);
  digitalWrite (10, HIGH);
  }
 
void loop ()
  {
  Serial.println (digitalRead (10), DEC);
  delay (500);
  digitalWrite (10, !digitalRead (10));
  }

That outputs:

Code:
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1

In that I am reading pin 10 and using its value to toggle it.

So I suggest your problem lies elsewhere.
Logged


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

Pinmode isn't required.  It just changes something really technical (not sure what).  You can safely drive an led without calling pinmode(output), but It isn't recommended for something like a relay).
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6830
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking at the code the digitalRead() function just does a read of the appropriate bit in the appropriate PINx register without modifying anything, so it should work.

One catch, if it's a PWM pin the PWM will be stopped.

Quote
Pinmode isn't required.  It just changes something really technical (not sure what).
It writes to the DDR (data direction) register. To drive a LED you will have to set the pin to an output first.

______
Rob
« Last Edit: April 23, 2012, 06:52:00 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

Central MN, USA
Offline Offline
Faraday Member
**
Karma: 35
Posts: 5935
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

I'm trying to read the output state of an output pin but it seams not working.

I've got something like tihs :

Code:
digitalWrite(10, HIGH);
statepin10 = digitalRead(10);
if (statepin10 == HIGH)
{
dutyCycle = map (Ubat100, 1340, 1445, 100, 220);    
dutyCycle = constrain(dutyCycle, 100, 220);      
analogWrite(8, dutyCycle);  
}

But it doesn't analogWrite anything on my pin N°8 (only 0).

If I' m not doing right, how can I do to know the state of an output pin ?

1) Can you put a delay between writing and reading?
2) What device is connected to pin 10?
3) Why do you want to know the pin 10 logic state and why trying to read it?

In case you want to know if the pin fails to drive the device connected to it (needs too much current) and is not HIGH enough, you still can't find out. This is supposed to be an advantage in Write after Read situation. You can always connect another pin to pin 10 and read that pin.
Logged


Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 219
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to read the output state of an output pin but it seams not working.

I've got something like tihs :

Code:
digitalWrite(10, HIGH);
statepin10 = digitalRead(10);


As I implied earlier you haven't shown that it is an output pin. If it was not, you could write HIGH to it and read LOW, because it was not set to output. Or you might write LOW and read back HIGH.

With this test (and nothing connected):

Code:
void setup ()
  {
  Serial.begin (115200);
  digitalWrite (10, HIGH);
  }
 
void loop ()
  {
  Serial.println (digitalRead (10), DEC);
  delay (500);
  digitalWrite (10, !digitalRead (10));
  }

I get:

Code:
1
1
1
1
1

So the writing LOW is not having an effect.

Logged


Central MN, USA
Offline Offline
Faraday Member
**
Karma: 35
Posts: 5935
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you touch the pin it may get LOW though.
Logged


Mid-Atlantic, USA
Offline Offline
Full Member
***
Karma: 12
Posts: 215
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, sorry I'm resurrecting an old thread... But this thread was referenced in a new thread so I thought it best to put my explanations here instead of there. My apologies to the forum gods if I offend.

Long bits snipped for brevity.
Quote
As I implied earlier you haven't shown that it is an output pin. If it was not, you could write HIGH to it and read LOW, because it was not set to output. Or you might write LOW and read back HIGH.

With this test (and nothing connected):
Long bits snipped for brevity.
Quote
So the writing LOW is not having an effect.

On initial start, the pins are all inputs. Doing a digitalWrite() on an input pin just enables (HIGH) or disables (LOW) the internal pullup.

On the example you demonstrate (yes, Nick, you probably know this, but I'm explaining for others here), because you never initialize the pinMode of pin 10, it is an input. Then setting it HIGH with digitalWrite() enables the internal pullup resistor enforcing the HIGH state because you explicitly stated nothing is connected to the pin (it's floating). Then in the first iteration of the loop you send a !digitalRead() which would evaluate to LOW, effectively turning off the internal pullup. Now the pin is truly floating. Floating pins on digital chips usually have a tendency to drift towards HIGH as it picks up stray EMF either directly on the die or from the physical pin. Because the pin is HIGH already, it probably just hangs out around the HIGH status, never drifting low enough to trigger a LOW state. Thus, the next iteration of the loop again evaluates to LOW, attempting to turn off the already off internal pull up. This keeps happening ad infinitum. (This is why liudr made the assumption that the pin may go LOW if you touch it, but then that would then be connecting something to the pin.)

Just a bit of hardware electrical theory for your Monday.  smiley-cool
Logged


0
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6625
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course the real answer to the (now year-old) original question in this thread might well be:
 "pin 8 isn't a PWM pin so analogWrite() won't be doing what you expect"...
 (Although that depends which version of the Arduino was involved).
Logged

Pages: [1]   Go Up
Print
 
Jump to: