Author Options:

Can a arduino pin mode be changed mid program? Answered

Hi, I'm working on a project that requires a reading to be taken from a pin (digital) and then that pin needs to be supplying power. Can I change the pinmode from input to output mid program, or do I need transistors to switch it between different pins instead?


I'm trying to use the 4 digit 7 segment display ad i cant seem to turn an ground on and off.

void setup() {

// put your setup code here, to run once:


void loop() {

digitalWrite(4, HIGH);

pinMode(11, INPUT);

digitalWrite(11, HIGH);


pinMode(11, OUTPUT);

digitalWrite(11, HIGH);


pinMode(14, INPUT);

pinMode(14, OUTPUT);

Of course it can, look at the example->sensors->ping sketch :)

How fast can this be achieved? would it take 1ms or less, or are we talking 100ms?

Will the output damage the device your reading from? Are you at a point where your out of available pins so one has to run double duties?

No I have a sensor that when a ball bearing falls over certain pins it can tell the direction. However there is a point that it can fall across 2 input lines which means that no reading would be taken. So I thought the easiest way to fix it would be to if there was no power supplied to that pin, it would power it to see what other ones the ball had fallen across.

As long as the external circuits connected to the pins don't interfere with each other, you can change any of the settings at any point in the program. Do you have a circuit for what you are trying to do?