I'm trying to measure the voltage of a 11.1V LIPO battery, which is substituted by a power supply for testing, using the raspberry pi pico on a breadboard, and then it changes the voltages of an RGB Led to show the battery voltage level. The higher the voltage is, the greener the led. The lower it is, the redder it is. I have a voltage divider which divides the battery voltage by 3.5 so the pico can read it. In the code I just multiply the voltage value by 3.5 and get the actual voltage. Firstly, when I read the analog signal using the built-in ADC, the value fluctuates a lot but 90% of the time it shows it as the maximum value, 4096. I wondered if the ADC is broken, so I tested it with a potentiometer, and it was in-fact able to read it's value. During that I also noticed the PWM is not working properly on the RGB LED, the red and green are always both on, but the main issue here is the ADC. I have checked with the multimeter that the voltage coming from the voltage divider is correct. What might be wrong here? Breadboard circuit and code below.

breadboard circuit

Here the red and blue wires are the positive and negative of the power supply. The white wire coming from between the resistors is the analog value which is being read incorrectly. The green wire is the green of the RGB LED. The white wire coming from the +3.3V of the Pico going to the RGB LED is the anode. Last wire of the RGB LED is the red. Blue is not connected to anything because it is not needed.

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/pwm.h"

long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){
 return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow;

int main()

    gpio_set_function(2, GPIO_FUNC_PWM);
    gpio_set_function(3, GPIO_FUNC_PWM);

    uint slice_num_red = pwm_gpio_to_slice_num(2);
    uint slice_num_green = pwm_gpio_to_slice_num(3);

    pwm_set_wrap(slice_num_red, 255);
    pwm_set_wrap(slice_num_green, 255);

    pwm_set_chan_level(slice_num_red, 0, 0);
    pwm_set_chan_level(slice_num_green, 0, 0);

    pwm_set_enabled(slice_num_red, true);
    pwm_set_enabled(slice_num_green, true);

    gpio_set_dir(2, true);
    gpio_set_dir(3, true);

    const float conversion_factor = 3.3f / (1 << 12);

        uint16_t result = adc_read();
        printf("Result: %d, Voltage by Conversion factor: %f\n", result, result * conversion_factor);
        float voltage = (float)result / 4096 * 3.3f;
        if(voltage < 2)
        printf("LIPO Voltage: %f, Divided LIPO Voltage: %f\n", voltage*3.5f, voltage);
        float green_voltage = map(voltage, 2.7, 3.25, 0, 3.3);
        float red_voltage = 3.3 - green_voltage;
        printf("Green Voltage: %f, Red Voltage: %f\n", green_voltage, red_voltage);

        float high_value = red_voltage / 3.3f * 4096;
        float value_red = map(high_value, 0, 4095, 0, 255);

        pwm_set_chan_level(slice_num_red, 0, value_red);

        high_value = green_voltage / 3.3f * 4096;
        float value_green = map(high_value, 0, 4095, 0, 255);

        pwm_set_chan_level(slice_num_green, 0, value_green);

I have seen on the serial that Green voltage and red voltage are correct, if only was the reading correct.

This is my first project on the Pico, so I am very inexperienced. PLEASE give me tips on how to improve. Thank you!

  • 1
    The blue wire doesn't seem to be connected to the Pico, so the white wire from the voltage divider is floating, unless you somehow have a connection via the power supplies. Probably the blue wire or the row of holes next to the blue line should be connected to the Pico's GND. For easier debugging I suggest to split the program into two minimalized versions, one that concentrates on the voltage measurement problem, the other on the PWM problem. BTW: You should think about protective circuitry for the inputs. If you do something wrong with the battery or power supply you could fry your Pico.
    – Bodo
    Mar 23 at 18:31
  • @Bodo the blue wire is connected to the power supply. It is the negative and the red is the positive. Thanks, I will try connecting the pico's GND there too. Also the protective circuitry is a good idea. I will put some fuses in the finalised version.
    – epicMan123
    Mar 23 at 19:56
  • You initialise 2/3 for PWM and later cancel that by initialising them as ordinary GPIO.
    – joan
    Mar 23 at 20:21
  • @epicMan123 I doubt that fuses will work fast enough. A series resistor to limit the current in case of voltage outside the operating range might be a good idea. I don't know if the Pico has internal input protection diodes. if not you might have to add them.
    – Bodo
    Mar 23 at 22:22

1 Answer 1


You DON'T have a voltage divider because the Gnd is NOT CONNECTED!

All electrical circuits NEED to have a complete circuit; in particular all Gnd should be connected to a common point.

What you have is effectively a long antenna connected to the Pico by the white wire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.