Changing pinmode on the fly - Nano 33 BLE/nRF52840

Hi,

I am trying to change an analogue pin (#define PIN_HOLD A0) on the fly but running into an issue. The pin is initially set to an output in setup() and held high - no problem. In a function I then change it to an input and perform and A/D read - again no problem and I get the expected value.
I then try to set it back to an output and take it high again but the pin just stays as an input.
If I comment out the analogRead line the pin will happily switch from input to output repeatedly, it just won't play ball after performing an A/D read.
Any ideas very welcome?

pinMode(PIN_HOLD, INPUT);                     //Make the power hold pin an input
delay(20);							// probably not needed
AD_Val = analogRead(PIN_HOLD);              //Perform an A/D read to check for switch activity 
delay(20);
pinMode(PIN_HOLD, OUTPUT);                   //turn the pin back to an output to maintain the power
digitalWrite(PIN_HOLD, HIGH);

I have used the same technique on the Arduino Zero/SAMD21 and it works fine.

Thanks

i believe pinMode () only sets the direction when using digitWrite/Read().

not sure why you would want to set the pin to OUTPUT if it's an input. there's a reason pins are by default set to INPUT because you don't want 2 output drivers fighting one another and possibly damaging one or the other

How are you test that the pin is still input?

i think digitalWrite HIGH enables the pull-up. so it reads HIGH even as a INPUT. setting digitalWrite LOW may clear this (i.e. there is no pull-down)

The port is well protected by a 300k resistor so no harm can be done to it when in output mode.
Everything I am doing relates to the same single port so port to port conflicts can't happen.

looks like you're attempting to read an analog voltage presumably being driven by some external device and want to reconfigure the pins you're reading the voltage from as an OUTPUT.

driving the pin as an OUTPUT may damage the device you're reading from or that device many damage the pin on the Arduino.

don't understand why you would need to configure the pin as an OUTPUT

My oscilloscope shows that the analogue signal is still present via a 300k resistor network and hasn't been forced high by the digitalWrite command following the A/D read.

so what's the problem?

p.s. after setting the pin HIGH, have you tried setting it LOW before reconfiguring it as an input?

My port is used for two functions...

  1. holding the power to my board
  2. monitoring the power button in order to instigate a shutdown

Using a single port for 2 functions is a very useful technique to reduce IO requirements and this has always worked fine on other processors - just not on the nRF52840 SoC.

No, but I will give this a try, it's certainly something quirky like this!

I have tried various permutations of setting the port high/low/input/output before and after doing the A/D read but nothing cures this issue. It's looking like I will need to use an additional port such that one does the A/D read and the other holds the line high in between the A/D reads :pensive:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.