Skip to content

Commit 2d3cb99

Browse files
committed
Add an example to read VBUS and VSYS
The process is different on Pico and Pico W so demonstrate how to do it. Fixes #324
1 parent 0df7abb commit 2d3cb99

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
int power_battery(bool *battery_powered) {
20+
#if defined CYW43_WL_GPIO_VBUS_PIN
21+
*battery_powered = !cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
22+
return PICO_OK;
23+
#elif defined PICO_VBUS_PIN
24+
gpio_set_function(PICO_VBUS_GPIO_PIN, GPIO_FUNC_SIO);
25+
*battery_powered = !gpio_get(PICO_VBUS_GPIO_PIN);
26+
return PICO_OK;
27+
#else
28+
return PICO_ERROR_NO_DATA;
29+
#endif
30+
}
31+
32+
int power_voltage(float *voltage_result) {
33+
#ifndef PICO_VSYS_PIN
34+
return PICO_ERROR_NO_DATA;
35+
#endif
36+
#ifndef PICO_VSYS_ADC_INPUT
37+
#error If PICO_VSYS_PIN is defined PICO_VSYS_ADC_INPUT must also be defined
38+
#endif
39+
#if CYW43_USES_VSYS_PIN
40+
cyw43_thread_enter();
41+
#endif
42+
// setup adc
43+
adc_gpio_init(PICO_VSYS_PIN);
44+
adc_select_input(PICO_VSYS_ADC_INPUT);
45+
// read vsys
46+
uint32_t vsys = 0;
47+
for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) {
48+
vsys += adc_read();
49+
}
50+
vsys /= PICO_POWER_SAMPLE_COUNT;
51+
#if CYW43_USES_VSYS_PIN
52+
cyw43_thread_exit();
53+
#endif
54+
// Generate voltage
55+
const float conversion_factor = 3.3f / (1 << 12);
56+
*voltage_result = vsys * 3 * conversion_factor;
57+
return PICO_OK;
58+
}

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 batttery status
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_battery(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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
while(true) {
34+
// Get battery status
35+
bool battery_status = false;
36+
char *power_str = "UNKNOWN";
37+
if (power_battery(&battery_status) == PICO_OK) {
38+
power_str = battery_status ? "BATTERY" : "POWERED";
39+
}
40+
// Get voltage
41+
float voltage = 0;
42+
int voltage_return = power_voltage(&voltage);
43+
voltage = floorf(voltage * 100) / 100;
44+
45+
// Display power if it's changed
46+
if (old_battery_status != battery_status || old_voltage != voltage) {
47+
char percent_buf[10] = {0};
48+
if (battery_status && voltage_return == PICO_OK) {
49+
const float min_battery_volts = 3.0f;
50+
const float max_battery_volts = 4.2f;
51+
int percent_val = ((voltage - min_battery_volts) / (max_battery_volts - min_battery_volts)) * 100;
52+
snprintf(percent_buf, sizeof(percent_buf), " (%d%%)", percent_val);
53+
}
54+
55+
// Also get the temperature
56+
adc_select_input(4);
57+
const float conversionFactor = 3.3f / (1 << 12);
58+
float adc = (float)adc_read() * conversionFactor;
59+
float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
60+
61+
// Display power and remember old vales
62+
printf("Power %s, %.2fV%s, temp %.1f DegC\n", power_str, voltage, percent_buf, tempC);
63+
old_battery_status = battery_status;
64+
old_voltage = voltage;
65+
}
66+
sleep_ms(3000);
67+
}
68+
69+
#if CYW43_USES_VSYS_PIN
70+
cyw43_arch_deinit();
71+
#endif
72+
return 0;
73+
}

0 commit comments

Comments
 (0)