Cannot Get Pi Pico to Read Analog Voltage

Hello,

I'm thinking it might be a hardware problem with my Pico, but I wanted to run it by here just in case I'm missing something, or more likely ... there is something I just don't know that I need to know ...

I've got a Raspberry Pi Pico with a very simple sketch, and a POT connected to it so that I can vary the POT and get different readings from one of the analog pins.

Here is the sketch:

#include <Arduino.h>

int P1 = A0; //GP26 or Pin 31
int P2 = A1; //GP27 or Pin 32
int P3 = A2; //GP28 or Pin 34

unsigned long startTime = millis();

void setup() {
    Serial.begin(115200);
    pinMode(P1,INPUT);
    pinMode(P2,INPUT);
    pinMode(P3,INPUT);
    delay(8000);
    Serial.println("\nPi Pico Ready!");
}

void loop() {
    if((millis() - startTime) > 800) {
        String a1 = String(analogRead(P1));
        String a2 = String(analogRead(P2));
        String a3 = String(analogRead(P3));
        Serial.println(a1 + " " + a2 + " " + a3);
        startTime = millis();
    }
}

This is the output from the sketch:

Pi Pico Ready!
4095 4095 4095
4095 4095 4095
4095 4095 4095
4095 4095 4095
4095 4095 4095
4095 4095 4095
4095 4095 4095
...

And here are a couple of pics of the actual setup ... when I pull the green wire off of the pico and read it with my meter, I get exactly what I expect. I get anywhere from 0 to roughly 3.25 volts as I turn the POT ... but I get what you see above from the sketch.


I've tried assigning 26, 27, and 28 to variables P1, P2, and P3, and that did not work either.

Is there something here I'm not getting? Cause it seems like this should be stupid easy to do... or could the pico just be not working properly?

EDIT: I just ran the sketch with my Fluke meter connected at the same time that everything is connected to the Pico, and what is interesting about that is that with the pot cranked all the way up, where I should be reading 3.25 volts on the meter, I'm only reading 2.1 volts, but as soon as I pull that wire off of the Pico, the voltage kicks back up to 3.25 ... so it's like the Pico is sinking current somehow or its acting like a voltage divider ... like there is a resister in series with the circuit when connected... which might actually not be a bad thing though I would not expect it to pull down a supply voltage when it's merely reading it and doing nothing else.

Mike

In my experience, which does not extend to the Pi Pico I have to admit, this sort of thing usually goes wrong in the pin assignments. I.e. you expect P1, A0, GP26 and Pin 31 to be one and the same, but are they really? Yes, P1 = A0 because you say it is, but is that really GP26 and is that really pin 31 on the board? So start by double checking all that. Check in particular of 'A0' is indeed defined the way you think it is - I bet it isn't....

These are available just about anywhere you can spit.

Also, When I tried different pin numbers other than what the pinout sheets say is assigned to the ADC, I got back a value of ZERO ... so I know it's reading the ADC when I tell it to read those pin numbers... so like .... A0 and 26 both work and give me a value of 4095 ... I just cant get that number to change by varying the voltage .

I will, however, set one of the pins to output and see what happens ... didn't think to try that.

Koraks,

I just ran this sketch on the Pico and confirmed that all three pins worked perfectly as output pins, so I know I got the numbers right.

#include <Arduino.h>

int P1 = A0; //GP26 or Pin 31
int P2 = A1; //GP27 or Pin 32
int P3 = A2; //GP28 or Pin 34

unsigned long startTime = millis();

void setup() {
    Serial.begin(115200);
    pinMode(P1,OUTPUT);
    pinMode(P2,OUTPUT);
    pinMode(P3,OUTPUT);
    delay(8000);
    Serial.println("\nPi Pico Ready!");
}

void loop() {
    static int V = 50;
    if((millis() - startTime) > 2000) {
        analogWrite(P1, V);
        analogWrite(P2, V);
        analogWrite(P3, V);
        V +=25;
        if (V >= 255) V = 50;
        startTime = millis();
    }
}

I figured out what the problem was. It's the Pico itself. I bought a few of these some time ago but I couldn't remember where I put the other ones, well I found one still in the package and busted it out, soldered headers on it, and plugged it into the same spot on the breadboard, and uploaded the same sketch only this time, it works perfectly.

Good you've found it; apparently the initial one got fried in an earlier experiment. Or perhaps you've had the one in a million DOA.

1 Like

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