Skip to content

Instantly share code, notes, and snippets.

@Nemo64
Last active February 20, 2021 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nemo64/ed434de06922831722ab2f9581455774 to your computer and use it in GitHub Desktop.
Save Nemo64/ed434de06922831722ab2f9581455774 to your computer and use it in GitHub Desktop.
This is a test sketch to read voltages using an esp32.
// This sketch shows how to read voltages from an esp32
// Your ESP has to be manufactured in 2018 or newer for the values to be burned into eFuse.
// more infos here:
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-calibration
#include <Arduino.h>
#include <esp_adc_cal.h>
esp_adc_cal_characteristics_t chars;
void setup() {
Serial.begin(115200);
auto val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &chars);
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");
}
adc1_config_width(chars.bit_width);
adc1_config_channel_atten(ADC1_CHANNEL_0, chars.atten);
adc1_config_channel_atten(ADC1_CHANNEL_3, chars.atten);
adc1_config_channel_atten(ADC1_CHANNEL_6, chars.atten);
adc1_config_channel_atten(ADC1_CHANNEL_7, chars.atten);
adc1_config_channel_atten(ADC1_CHANNEL_4, chars.atten);
adc1_config_channel_atten(ADC1_CHANNEL_5, chars.atten);
}
void loop() {
auto start = micros();
auto v36 = adc1_get_raw(ADC1_CHANNEL_0); // 36
auto v39 = adc1_get_raw(ADC1_CHANNEL_3); // 39
auto v34 = adc1_get_raw(ADC1_CHANNEL_6); // 34
auto v35 = adc1_get_raw(ADC1_CHANNEL_7); // 35
auto v32 = adc1_get_raw(ADC1_CHANNEL_4); // 32
auto v33 = adc1_get_raw(ADC1_CHANNEL_5); // 33
auto time = micros() - start;
Serial.printf("%umV %umV %umV %umV %umV %umV %luμs\r\n",
esp_adc_cal_raw_to_voltage(v36, &chars),
esp_adc_cal_raw_to_voltage(v39, &chars),
esp_adc_cal_raw_to_voltage(v34, &chars),
esp_adc_cal_raw_to_voltage(v35, &chars),
esp_adc_cal_raw_to_voltage(v32, &chars),
esp_adc_cal_raw_to_voltage(v33, &chars),
time
);
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment