Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Odd Bug - analogRead() sets pin to Low
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Odd Bug - analogRead() sets pin to Low

by climbingNinja on Sun Jul 12, 2020 12:43 am

I am working on creating a debugging tool for the Circuit Playground Express and have come across on odd bug when I try to read the current state of the microcontroller. I have a loop that checks each digital pin and analog pin for it's current value and transmits it to the Serial port (to be processed by another program). But I have found that the Express will permanently set a pin off/low if an analogRead() is performed on a pin that has been set to high. For example:

Code: Select all | TOGGLE FULL SIZE
void setup() {
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
}

void loop() {
  digitalWrite(2,HIGH);
  delay(200);
  analogRead(A5);//A5 shares the same location as D2
  delay(200);
}

Also if I use the analog constants, I get the same bug:

Code: Select all | TOGGLE FULL SIZE
void setup() {
  pinMode(A5, OUTPUT);
  digitalWrite(A5, HIGH);
}

void loop() {
  digitalWrite(A5,HIGH);
  delay(200);
  analogRead(A5);
  delay(200);
}


This code works fine on the Classic but for some reason with the express if I perform an analogRead(), the pin shuts off and will no longer turn on (i.e. this isn't blinking on/off, it just stays off). Also, I've tried this code on two separate Express boards, so I assume this is not a bug specific to my board but a general issue with the Express.

Any suggestions for how to safely read the analog pins? Or anyone know why this is happening?
climbingNinja
 
Posts: 2
Joined: Wed Nov 14, 2012 7:20 pm

Re: Odd Bug - analogRead() sets pin to Low

by westfw on Sun Jul 12, 2020 3:25 am

When you do an analogRead() of a pin, it switches the pin into analog input mode. You probably have to do pinMode(pin, OUTPUT) again to get it back into digital output mode.
The Circuit Playgroup Classic has a differnt CPU chip and core, which perhaps behaves slightly differently, but I don't think either way is actually "wrong." Using the explicit pinMode() should work on all boards (and having digitalWrite() explicitly check and switch modes would slow down a function already famous for being slower than it should be.)

westfw
 
Posts: 1723
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Odd Bug - analogRead() sets pin to Low

by climbingNinja on Sun Jul 12, 2020 2:28 pm

Thanks westfw! You're correct, it is a pinMode issue. If I toggle the pinMode, the LED blinks on/off. I'll have to work on a different method for the Playground to check all the analog pin values.

Code: Select all | TOGGLE FULL SIZE

#include <Adafruit_CircuitPlayground.h>
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
}

void loop() {
  Serial.println("ON");
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
  delay(200);
  Serial.println("OFF");
  analogRead(A5);
  delay(200);
}
climbingNinja
 
Posts: 2
Joined: Wed Nov 14, 2012 7:20 pm

Re: Odd Bug - analogRead() sets pin to Low

by kevinjwalters on Tue Jul 28, 2020 10:50 am

Not exactly the same thing, but there's a previous discussion which eventually heads towards pinMode() on A0 on CPX and Arduino Forum: Arduino Zero DAC (Analog Output) Cutoff Below the Rail Voltage. One's expectations need to be calibrated when it comes to portability of code and api behaviour between boards.

I also find some of the terminology used confusing at times. Digital pwm outputs are sometimes referred to as analog outputs and things like "true analog output" gets used if a DAC is involved.

kevinjwalters
 
Posts: 743
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.