adc1_example_main.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* ADC1 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 "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/queue.h"
  12. #include "driver/gpio.h"
  13. #include "driver/adc.h"
  14. #include "esp_system.h"
  15. #include "esp_adc_cal.h"
  16. /*Note: Different ESP32 modules may have different reference voltages varying from
  17. * 1000mV to 1200mV. Use #define GET_VREF to route v_ref to a GPIO
  18. */
  19. #define V_REF 1100
  20. #define ADC1_TEST_CHANNEL (ADC1_CHANNEL_6) //GPIO 34
  21. //#define V_REF_TO_GPIO //Remove comment on define to route v_ref to GPIO
  22. void app_main(void)
  23. {
  24. #ifndef V_REF_TO_GPIO
  25. //Init ADC and Characteristics
  26. esp_adc_cal_characteristics_t characteristics;
  27. adc1_config_width(ADC_WIDTH_BIT_12);
  28. adc1_config_channel_atten(ADC1_TEST_CHANNEL, ADC_ATTEN_DB_0);
  29. esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_0, ADC_WIDTH_BIT_12, &characteristics);
  30. uint32_t voltage;
  31. while(1){
  32. voltage = adc1_to_voltage(ADC1_TEST_CHANNEL, &characteristics);
  33. printf("%d mV\n",voltage);
  34. vTaskDelay(pdMS_TO_TICKS(1000));
  35. }
  36. #else
  37. //Get v_ref
  38. esp_err_t status;
  39. status = adc2_vref_to_gpio(GPIO_NUM_25);
  40. if (status == ESP_OK){
  41. printf("v_ref routed to GPIO\n");
  42. }else{
  43. printf("failed to route v_ref\n");
  44. }
  45. fflush(stdout);
  46. #endif
  47. }