Go Down

Topic: How to read the state of an output pin ? (Read 7462 times) previous topic - next topic

hary

Apr 23, 2012, 10:45 pm Last Edit: Apr 23, 2012, 10:48 pm by hary Reason: 1
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: [Select]
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 ?

AndyCC

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

Code: [Select]
digitalWrite(10, HIGH);
statepin10 = HIGH;




hary

Thank you.

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

nickgammon

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

Code: [Select]
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: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

sebflippers

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

graynomad

#5
Apr 24, 2012, 01:45 am Last Edit: Apr 24, 2012, 01:52 am by Graynomad Reason: 1
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
Rob Gray aka the GRAYnomad www.robgray.com

liuzengqiang


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: [Select]
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.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

nickgammon


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

I've got something like tihs :

Code: [Select]
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: [Select]
void setup ()
  {
  Serial.begin (115200);
  digitalWrite (10, HIGH);
  }
 
void loop ()
  {
  Serial.println (digitalRead (10), DEC);
  delay (500);
  digitalWrite (10, !digitalRead (10));
  }


I get:

Code: [Select]

1
1
1
1
1


So the writing LOW is not having an effect.

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

liuzengqiang

If you touch the pin it may get LOW though.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Sembazuru

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.  8)
http://www.catb.org/jargon/html/I/I-didn-t-change-anything-.html

MarkT

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).
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up