temp_sensor_main.c 1.6 KB

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