Skip to content

Commit a9b3118

Browse files
Add an example to read VBUS and VSYS (#331)
The process is different on Pico and Pico W so demonstrate how to do it. Fixes #324
1 parent 0da9d45 commit a9b3118

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ App|Description
2323
[onboard_temperature](adc/onboard_temperature) | Display the value of the onboard temperature sensor.
2424
[microphone_adc](adc/microphone_adc) | Read analog values from a microphone and plot the measured sound amplitude.
2525
[dma_capture](adc/dma_capture) | Use the DMA to capture many samples from the ADC.
26+
[read_vsys](adc/read_vsys) | Demonstrates how to read VSYS to get the voltage of the power supply.
2627

2728
### Clocks
2829

adc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ if (NOT PICO_NO_HARDWARE)
55
add_subdirectory(joystick_display)
66
add_subdirectory(onboard_temperature)
77
add_subdirectory(microphone_adc)
8+
add_subdirectory(read_vsys)
89
endif ()

adc/read_vsys/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
add_library(power_status_adc INTERFACE)
2+
target_sources(power_status_adc INTERFACE
3+
${CMAKE_CURRENT_LIST_DIR}/power_status.c
4+
)
5+
target_include_directories(power_status_adc INTERFACE
6+
${CMAKE_CURRENT_LIST_DIR}
7+
)
8+
target_link_libraries(power_status_adc INTERFACE
9+
hardware_adc
10+
hardware_gpio
11+
)
12+
13+
add_executable(read_vsys
14+
read_vsys.c
15+
)
16+
target_include_directories(read_vsys PRIVATE
17+
${CMAKE_CURRENT_LIST_DIR}
18+
)
19+
target_link_libraries(read_vsys
20+
pico_stdlib
21+
power_status_adc
22+
)
23+
if (PICO_CYW43_SUPPORTED)
24+
target_link_libraries(read_vsys
25+
pico_cyw43_arch_none
26+
)
27+
endif()
28+
29+
pico_add_extra_outputs(read_vsys)
30+
example_auto_set_url(read_vsys)

adc/read_vsys/power_status.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "stdbool.h"
8+
#include "hardware/adc.h"
9+
#include "power_status.h"
10+
11+
#if CYW43_USES_VSYS_PIN
12+
#include "pico/cyw43_arch.h"
13+
#endif
14+
15+
#ifndef PICO_POWER_SAMPLE_COUNT
16+
#define PICO_POWER_SAMPLE_COUNT 3
17+
#endif
18+
19+
// Pin used for ADC 0
20+
#define PICO_FIRST_ADC_PIN 26
21+
22+
int power_source(bool *battery_powered) {
23+
#if defined CYW43_WL_GPIO_VBUS_PIN
24+
*battery_powered = !cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
25+
return PICO_OK;
26+
#elif defined PICO_VBUS_GPIO_PIN
27+
gpio_set_function(PICO_VBUS_GPIO_PIN, GPIO_FUNC_SIO);
28+
*battery_powered = !gpio_get(PICO_VBUS_GPIO_PIN);
29+
return PICO_OK;
30+
#else
31+
return PICO_ERROR_NO_DATA;
32+
#endif
33+
}
34+
35+
int power_voltage(float *voltage_result) {
36+
#ifndef PICO_VSYS_PIN
37+
return PICO_ERROR_NO_DATA;
38+
#endif
39+
#if CYW43_USES_VSYS_PIN
40+
cyw43_thread_enter();
41+
// Make sure cyw43 is awake
42+
cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
43+
#endif
44+
45+
// setup adc
46+
adc_gpio_init(PICO_VSYS_PIN);
47+
adc_select_input(PICO_VSYS_PIN - PICO_FIRST_ADC_PIN);
48+
49+
adc_fifo_setup(true, false, 0, false, false);
50+
adc_run(true);
51+
52+
#if CYW43_USES_VSYS_PIN
53+
// We seem to read low values from cyw43 sometimes - this seems to fix it
54+
int ignore_count = PICO_POWER_SAMPLE_COUNT;
55+
while (!adc_fifo_is_empty() || ignore_count-- > 0) {
56+
(void)adc_fifo_get_blocking();
57+
}
58+
#endif
59+
60+
// read vsys
61+
uint32_t vsys = 0;
62+
for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) {
63+
uint16_t val = adc_fifo_get_blocking();
64+
vsys += val;
65+
}
66+
67+
adc_run(false);
68+
adc_fifo_drain();
69+
70+
vsys /= PICO_POWER_SAMPLE_COUNT;
71+
#if CYW43_USES_VSYS_PIN
72+
cyw43_thread_exit();
73+
#endif
74+
// Generate voltage
75+
const float conversion_factor = 3.3f / (1 << 12);
76+
*voltage_result = vsys * 3 * conversion_factor;
77+
return PICO_OK;
78+
}

adc/read_vsys/power_status.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef POWER_STATUS_H
8+
#define POWER_STATUS_H
9+
10+
/*!
11+
* \brief Get power source
12+
*
13+
* Returns whether battery powered
14+
* \note On Pico W must have called cyw43_arch_init
15+
*
16+
* \param battery_powered True if powered by battery, False if powered by USB or another means
17+
* \return Zero if the battery status can be determined, an error code otherwise \see pico_error_codes
18+
*/
19+
20+
int power_source(bool *battery_powered);
21+
22+
/*!
23+
* \brief Get system voltage
24+
*
25+
* Returns the system voltage
26+
* \note Must have called adc_init
27+
*
28+
* \param voltage Calculated voltage
29+
* \return Zero if the voltage can be determined, an error code otherwise \see pico_error_codes
30+
*/
31+
int power_voltage(float *voltage);
32+
33+
#endif

adc/read_vsys/read_vsys.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include <pico/stdlib.h>
9+
#include <power_status.h>
10+
#include "hardware/adc.h"
11+
#include "pico/float.h"
12+
13+
#if CYW43_USES_VSYS_PIN
14+
#include "pico/cyw43_arch.h"
15+
#endif
16+
17+
int main() {
18+
stdio_init_all();
19+
20+
adc_init();
21+
adc_set_temp_sensor_enabled(true);
22+
23+
// Pico W uses a CYW43 pin to get VBUS so we need to initialise it
24+
#if CYW43_USES_VSYS_PIN
25+
if (cyw43_arch_init()) {
26+
printf("failed to initialise\n");
27+
return 1;
28+
}
29+
#endif
30+
31+
bool old_battery_status;
32+
float old_voltage;
33+
bool battery_status = true;
34+
char *power_str = "UNKNOWN";
35+
36+
while(true) {
37+
// Get battery status
38+
if (power_source(&battery_status) == PICO_OK) {
39+
power_str = battery_status ? "BATTERY" : "POWERED";
40+
}
41+
42+
// Get voltage
43+
float voltage = 0;
44+
int voltage_return = power_voltage(&voltage);
45+
voltage = floorf(voltage * 100) / 100;
46+
47+
// Display power if it's changed
48+
if (old_battery_status != battery_status || old_voltage != voltage) {
49+
char percent_buf[10] = {0};
50+
if (battery_status && voltage_return == PICO_OK) {
51+
const float min_battery_volts = 3.0f;
52+
const float max_battery_volts = 4.2f;
53+
int percent_val = ((voltage - min_battery_volts) / (max_battery_volts - min_battery_volts)) * 100;
54+
snprintf(percent_buf, sizeof(percent_buf), " (%d%%)", percent_val);
55+
}
56+
57+
// Also get the temperature
58+
adc_select_input(4);
59+
const float conversionFactor = 3.3f / (1 << 12);
60+
float adc = (float)adc_read() * conversionFactor;
61+
float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
62+
63+
// Display power and remember old vales
64+
printf("Power %s, %.2fV%s, temp %.1f DegC\n", power_str, voltage, percent_buf, tempC);
65+
old_battery_status = battery_status;
66+
old_voltage = voltage;
67+
}
68+
sleep_ms(1000);
69+
}
70+
71+
#if CYW43_USES_VSYS_PIN
72+
cyw43_arch_deinit();
73+
#endif
74+
return 0;
75+
}

0 commit comments

Comments
 (0)