1 /* Temperature Sensor Example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "esp_log.h"
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/task.h"
14 
15 /* Note: ESP32 don't support temperature sensor */
16 
17 #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
18 #include "driver/temp_sensor.h"
19 
20 static const char *TAG = "TempSensor";
21 
tempsensor_example(void * arg)22 void tempsensor_example(void *arg)
23 {
24     // Initialize touch pad peripheral, it will start a timer to run a filter
25     ESP_LOGI(TAG, "Initializing Temperature sensor");
26     float tsens_out;
27     temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
28     temp_sensor_get_config(&temp_sensor);
29     ESP_LOGI(TAG, "default dac %d, clk_div %d", temp_sensor.dac_offset, temp_sensor.clk_div);
30     temp_sensor.dac_offset = TSENS_DAC_DEFAULT; // DEFAULT: range:-10℃ ~  80℃, error < 1℃.
31     temp_sensor_set_config(temp_sensor);
32     temp_sensor_start();
33     ESP_LOGI(TAG, "Temperature sensor started");
34     while (1) {
35         vTaskDelay(1000 / portTICK_RATE_MS);
36         temp_sensor_read_celsius(&tsens_out);
37         ESP_LOGI(TAG, "Temperature out celsius %f°C", tsens_out);
38     }
39     vTaskDelete(NULL);
40 }
41 
app_main(void)42 void app_main(void)
43 {
44     xTaskCreate(tempsensor_example, "temp", 2048, NULL, 5, NULL);
45 }
46 
47 #elif CONFIG_IDF_TARGET_ESP32
48 
app_main(void)49 void app_main(void)
50 {
51     printf("ESP32 don't support temperature sensor\n");
52 }
53 
54 #endif
55