Author Topic: Really basic digital I/O question  (Read 4930 times)

dbell

  • Newbie
  • *
  • Posts: 44
Really basic digital I/O question
« on: December 05, 2013, 05:19:32 pm »
I thought I could use a digitalRead() to check the current state of an output pin, but it always returns 0/LOW.
This is a simple example:

int pin = 13;

void setup() {
  Serial.begin(9600);
  pinMode(pin, OUTPUT);
}

void loop() {
  digitalWrite(pin, LOW);
  Serial.print("Pin low: ");
  Serial.println(digitalRead(pin));
  delay(500);
 
  digitalWrite(pin, HIGH);
  Serial.print("Pin high: ");
  Serial.println(digitalRead(pin));
  delay(500);
}

The LED flashes as expected, but the response is 0 in both states.

Dave, puzzled...

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Really basic digital I/O question
« Reply #1 on: December 06, 2013, 12:00:48 pm »
Try this:
  http://stackoverflow.com/questions/15100517/can-not-get-current-output-pin-state-on-arduino-due

This says that if you set the state to INPUT at some point, even it you change it to OUTPUT later, you will be able to read the state.

If that doesn't work, then maybe this:
  http://stackoverflow.com/questions/6160963/how-can-i-digitalread-a-pin-that-is-in-pinmode-output

This one shows how to directly read the bits of the the port corresponding to the pins.


dbell

  • Newbie
  • *
  • Posts: 44
Re: Really basic digital I/O question
« Reply #2 on: December 06, 2013, 01:42:16 pm »
Meh... 
Previously setting pinMode(pin, INPUT) didn't help; digitalRead(pin) still returns 0, regardless of state.

The second approach, reading the port directly sounds good, but:
"Bazzz I updated the answer above. You want to use bitRead( PORTD,3). To poll the status of pin 3. I have confirmed this works on an Arduino – MikeRags May 31 '11 at 13:59
   
Brilliant, this works! :) thanks for your answer. – Bazzz Jun 1 '11 at 9:52
   
This only works for those Arduino variants where pin 3 is actually mapped to bit 3 of Port D. – Udo Klein"

Where can I find a port map for the DigiX?

Dave

dbell

  • Newbie
  • *
  • Posts: 44
Re: Really basic digital I/O question
« Reply #3 on: December 06, 2013, 03:13:20 pm »
Never mind that last - bitRead(port, pin) will never work!
bitRead() is a bit manipulation function, and simply returns the state of a bit in the variable "port".

So, what IS the method to read the low level port contents?

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Really basic digital I/O question
« Reply #4 on: December 09, 2013, 06:19:44 am »
I believe on ATTiny85, you want to look at PORTB. I'm at work at the moment, so don't have any of my Arduino resources handy, but I'm sure there's a way to examine the state of the PORTB bits.