Skip to content

Commit 4d1d533

Browse files
fkuhneFelipe Kuhne
andauthored
Added onboard_temperature example. (#80)
Co-authored-by: Felipe Kuhne <[email protected]>
1 parent 2147301 commit 4d1d533

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ App|Description
2020
[hello_adc](adc/hello_adc)|Display the voltage from an ADC input.
2121
[joystick_display](adc/joystick_display)|Display a Joystick X/Y input based on two ADC inputs.
2222
[adc_console](adc/adc_console)|An interactive shell for playing with the ADC. Includes example of free-running capture mode.
23+
[onboard_temperature](adc/onboard_temperature)|Display the value of the onboard temperature sensor.
2324
[microphone_adc](adc/microphone_adc)|Read analog values from a microphone and plot the measured sound amplitude.
2425

2526
### Clocks

adc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ if (NOT PICO_NO_HARDWARE)
33
add_subdirectory(dma_capture)
44
add_subdirectory(hello_adc)
55
add_subdirectory(joystick_display)
6+
add_subdirectory(onboard_temperature)
67
add_subdirectory(microphone_adc)
78
endif ()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(onboard_temperature onboard_temperature.c)
2+
3+
target_link_libraries(onboard_temperature pico_stdlib hardware_adc)
4+
5+
# enable uart output, disable usb output
6+
pico_enable_stdio_uart(onboard_temperature 1)
7+
pico_enable_stdio_usb(onboard_temperature 0)
8+
9+
# create map/bin/hex file etc.
10+
pico_add_extra_outputs(onboard_temperature)
11+
12+
# add url via pico_set_program_url
13+
example_auto_set_url(onboard_temperature)
14+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*
6+
*/
7+
8+
#include <stdio.h>
9+
#include "pico/stdlib.h"
10+
#include "hardware/adc.h"
11+
12+
/* Choose 'C' for Celsius or 'F' for Fahrenheit. */
13+
#define TEMPERATURE_UNITS 'C'
14+
15+
/* References for this implementation:
16+
* raspberry-pi-pico-c-sdk.pdf, Section '4.1.1. hardware_adc'
17+
* pico-examples/adc/adc_console/adc_console.c */
18+
float read_onboard_temperature(const char unit) {
19+
20+
/* 12-bit conversion, assume max value == ADC_VREF == 3.3 V */
21+
const float conversionFactor = 3.3f / (1 << 12);
22+
23+
float adc = adc_read() * conversionFactor;
24+
float tempC = 27.0 - (adc - 0.706) / 0.001721;
25+
26+
if (unit == 'C') {
27+
return tempC;
28+
} else if (unit == 'F') {
29+
return tempC * 9 / 5 + 32;
30+
}
31+
32+
return -1.0;
33+
}
34+
35+
int main() {
36+
stdio_init_all();
37+
gpio_init(PICO_DEFAULT_LED_PIN);
38+
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
39+
40+
/* Initialize hardware AD converter, enable onboard temperature sensor and
41+
* select its channel (do this once for efficiency, but beware that this
42+
* is a global operation). */
43+
adc_init();
44+
adc_set_temp_sensor_enabled(true);
45+
adc_select_input(4);
46+
47+
while (true) {
48+
float temperature = read_onboard_temperature(TEMPERATURE_UNITS);
49+
printf("Onboard temperature = %.02f %c\n", temperature, TEMPERATURE_UNITS);
50+
51+
gpio_put(PICO_DEFAULT_LED_PIN, 1);
52+
sleep_ms(10);
53+
54+
gpio_put(PICO_DEFAULT_LED_PIN, 0);
55+
sleep_ms(990);
56+
}
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)