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.
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()
{
stdio_init_all();
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);
adc_init();
adc_gpio_init(26);
gpio_init(2);
gpio_init(3);
gpio_set_dir(2, true);
gpio_set_dir(3, true);
adc_select_input(0);
const float conversion_factor = 3.3f / (1 << 12);
while(1)
{
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)
{
continue;
}
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);
sleep_ms(500);
}
}
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!