Skip to content

Instantly share code, notes, and snippets.

@nm17
Last active May 1, 2022 13:54
Show Gist options
  • Save nm17/c03a79dd1d92811b5cb3a0f42234d3b8 to your computer and use it in GitHub Desktop.
Save nm17/c03a79dd1d92811b5cb3a0f42234d3b8 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <string>
#include <Arduino.h>
#include <esp_adc_cal.h>
#include "driver/adc.h"
#if CONFIG_IDF_TARGET_ESP32
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
#elif CONFIG_IDF_TARGET_ESP32S2
static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
#endif
esp_adc_cal_characteristics_t *adc_chars;
void setup() {
Serial.begin(115200);
adc_chars = new esp_adc_cal_characteristics_t();
//pinMode(8, INPUT);
adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_0);
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_0, width, 5000, adc_chars);
delay(5000);
//Check type of calibration value used to characterize ADC
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
Serial.println("eFuse Vref");
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
Serial.println("Two Point");
} else {
Serial.println("Default");
}
}
void loop() {
uint32_t reading = adc1_get_raw(ADC1_CHANNEL_4);
auto output_voltage = esp_adc_cal_raw_to_voltage(reading, adc_chars);
Serial.print(output_voltage);
Serial.println("mV");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment