7#include <hardware/adc.h>
19static const char *
const TAG =
"internal_temperature";
31#if defined(PICO_RP2350) && !defined(PICO_RP2350A)
32#error "PICO_RP2350A is not defined, so the RP2350 die is unknown and the temperature ADC channel cannot be chosen"
34#if defined(PICO_RP2350) && !PICO_RP2350A
35static constexpr uint8_t TEMPERATURE_ADC_INPUT = 8;
37static constexpr uint8_t TEMPERATURE_ADC_INPUT = 4;
39static constexpr float ADC_VREF = 3.3f;
40static constexpr float ADC_RESOLUTION = 4096.0f;
42static constexpr float TEMPERATURE_AT_REFERENCE = 27.0f;
43static constexpr float REFERENCE_VOLTAGE = 0.706f;
44static constexpr float VOLTS_PER_DEGREE = 0.001721f;
48static constexpr uint32_t SETTLE_TIME_US = 1000;
50static float read_internal_temperature() {
55 static bool adc_ready =
false;
61 adc_set_temp_sensor_enabled(
true);
62 busy_wait_us(SETTLE_TIME_US);
63 adc_select_input(TEMPERATURE_ADC_INPUT);
64 const uint16_t
raw = adc_read();
65 adc_set_temp_sensor_enabled(
false);
67 const float voltage =
raw * (ADC_VREF / ADC_RESOLUTION);
68 return TEMPERATURE_AT_REFERENCE - (voltage - REFERENCE_VOLTAGE) / VOLTS_PER_DEGREE;
81 ESP_LOGD(TAG,
"Ignoring invalid temperature (success=%d, value=%.1f)", success,
temperature);
void publish_state(float state)
Publish a new state to the front-end.