Go Down

Topic: Running out on pins on my Uno ...... !! (Read 857 times) previous topic - next topic

Remittub

Jan 27, 2013, 07:56 pm Last Edit: Jan 27, 2013, 07:58 pm by Remittub Reason: 1
I suspect that this topic has arisen before !!  (Apologises in advance !!)

I'd like to multi-purpose the pins in my current Arduino project.  In essence, can I define a pin TWICE and safely use that definition within it's own "enviroment"

e.g.

int pinPP_nAddrTag = 12;        // Parallel port nAddrTag
int pinLED = 12;                    // The output LED pin (Also pinPP_nAddrTag)

In Setup()
 pinMode(pinPP_nAddrTag, INPUT);
 pinMode(pinLED, OUTPUT);

Question 1 :  
    Is the attribute of INPUT specified in pinMode associated with the variable pinPP_nAddrTag or with pin 12 ??

Another way of looking at this is as follows ....

In function 1:
      digitalWrite(pinLED, HIGH);

In function  2:
      int iDataRead;
      iDataRead = digitalRead(pinPP_nAddrTag);

Question 2:
      Should I use pinMode(....) in the above functions to establish whether INPUT or OUTPUT) ?


Hopefully, you will be understand what I DON'T understand (or what I need reassurance on !!!)

Thanks in advance !


tack


I suspect that this topic has arisen before !!  (Apologises in advance !!)

I'd like to multi-purpose the pins in my current Arduino project.  In essence, can I define a pin TWICE and safely use that definition within it's own "enviroment"

e.g.

int pinPP_nAddrTag = 12;        // Parallel port nAddrTag
int pinLED = 12;                    // The output LED pin (Also pinPP_nAddrTag)

In Setup()
 pinMode(pinPP_nAddrTag, INPUT);
 pinMode(pinLED, OUTPUT);

Question 1 :  
    Is the attribute of INPUT specified in pinMode associated with the variable pinPP_nAddrTag or with pin 12 ??


As it says it is 'pinMode'; it acts on the pin. In your example code you set Pin 12 to INPUT and then you set it to OUTPUT. Therefore it is no longer an input. You need to be careful what you are doing here as you could damage the pin. Let's say you thought it was an input and set it high; that would turn the pullup resistor on, and you'd be expecting something to pull that pin low, with perhaps a direct short to Ground, against the pullup. If you did that when actually your pin was an output then you could short the pin to ground, exceeding it's current rating and destroy it.


Another way of looking at this is as follows ....

In function 1:
      digitalWrite(pinLED, HIGH);

In function  2:
      int iDataRead;
      iDataRead = digitalRead(pinPP_nAddrTag);

Question 2:
      Should I use pinMode(....) in the above functions to establish whether INPUT or OUTPUT) ?


It doesn't matter, you can't use it in both modes simultaneously. If you use it as an output first in Function1 then as soon as you set it to an INPUT it will go high impedance and your LED will light only dimly.

OUTPUT is a Low Impedance state
INPUT is a High Impedance state


Hopefully, you will be understand what I DON'T understand (or what I need reassurance on !!!)

Thanks in advance !


If you were just using it to send a signal then you could manage it's state and use it for both input and output consecutively. That's exactly how something like the I2C bus works but, you can't keep an output high and driven if you're going to change to input. If you had some way of latching the signal out so it only needed a momentary high then you could do so and then re-use the pin.

It's safer just to use one pin for one thing unless you fully understand the limitations.

Remittub



I suspect that this topic has arisen before !!  (Apologises in advance !!)

I'd like to multi-purpose the pins in my current Arduino project.  In essence, can I define a pin TWICE and safely use that definition within it's own "enviroment"

e.g.

int pinPP_nAddrTag = 12;        // Parallel port nAddrTag
int pinLED = 12;                    // The output LED pin (Also pinPP_nAddrTag)

In Setup()
 pinMode(pinPP_nAddrTag, INPUT);
 pinMode(pinLED, OUTPUT);

Question 1 :  
    Is the attribute of INPUT specified in pinMode associated with the variable pinPP_nAddrTag or with pin 12 ??


As it says it is 'pinMode'; it acts on the pin. In your example code you set Pin 12 to INPUT and then you set it to OUTPUT. Therefore it is no longer an input. You need to be careful what you are doing here as you could damage the pin. Let's say you thought it was an input and set it high; that would turn the pullup resistor on, and you'd be expecting something to pull that pin low, with perhaps a direct short to Ground, against the pullup. If you did that when actually your pin was an output then you could short the pin to ground, exceeding it's current rating and destroy it.


Another way of looking at this is as follows ....

In function 1:
      digitalWrite(pinLED, HIGH);

In function  2:
      int iDataRead;
      iDataRead = digitalRead(pinPP_nAddrTag);

Question 2:
      Should I use pinMode(....) in the above functions to establish whether INPUT or OUTPUT) ?


It doesn't matter, you can't use it in both modes simultaneously. If you use it as an output first in Function1 then as soon as you set it to an INPUT it will go high impedance and your LED will light only dimly.

OUTPUT is a Low Impedance state
INPUT is a High Impedance state


Hopefully, you will be understand what I DON'T understand (or what I need reassurance on !!!)

Thanks in advance !


If you were just using it to send a signal then you could manage it's state and use it for both input and output consecutively. That's exactly how something like the I2C bus works but, you can't keep an output high and driven if you're going to change to input. If you had some way of latching the signal out so it only needed a momentary high then you could do so and then re-use the pin.

It's safer just to use one pin for one thing unless you fully understand the limitations.


Hi,
   I think that you have confirmed my suspicions.   (It may SEEM obvious, but I wanted to be CERTAIN that pinMode set the attributes of the PIN and not the VARIABLE associated with the pin)

   Can you confirm that multiple variables can be defined for a pin (so that the name is relevant to the environment).  I do appreciate that NOTHING can be assumed (now) about whether the pin is inbound or outbound, and so you would have to use pinMode on entry to that environment.   I realise that care would need to be taken, but when you run out of pins, you have to start exploring these things !

    Thanks for your help !!

dc42

Tell us what I/O devices you want to connect your Arduino to, and we can then advise on how you can share pins between devices.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Nick_Pyner

I found it really helps to make up a spreadsheet of your pin assignments and devices.

jraskell

Shift Registers may provide the IO expansion you need.  3 Arduino pins gets you 8/16/... additional outputs or inputs.

I second jraskell, use shift registers to add more ports.  I always have a stock of the following:

74LS164, 74HC595N in-shift registers
CD4021 out-shift registers
TLC5940NT LED controller

They're all quite easy to implement.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Go Up