Sponsoring
Translate
Powered by Google 翻訳翻訳

How to get accurate voltage values ​​calibrated by ESP32's ADC

Sponsoring

I found the ESP32's ADC to be an easy and accurate way to measure voltage.

Sponsoring

analogReadMilliVolts()

ESP32s manufactured after 2018 now have AD converter calibration data written at the time of manufacture.

By using this calibration data, it is now possible to obtain the voltage value in the program.

The program to get the voltage value of pin 34 is as follows.

# define AD_PIN 34

void  setup () {
   Serial . begin ( 115200 );
   pinMode (AD_PIN, ANALOG);
}

void  loop () {
   Serial .printf( "%d[mV]\n" , analogReadMilliVolts(AD_PIN) );
   delay ( 500 );
}

Get the voltage value with analogReadMilliVolts() and output the result to the serial monitor.

1000mV is input to the AD pin. The result was 995mV.

By using analogReadMilliVolts() like this, you can easily get the voltage value.

Sponsoring

Distortion by attenuator

The ESP32 AD converter itself can only AD convert up to 1.1 V, but by using the attenuator function to reduce the input voltage, it is possible to AD convert up to a higher voltage.

The ESP32 attenuator can be set to the following four settings.

0dB (1.0x)
-2.5dB (0.75x)
-6dB (0.5x)
-11dB (0.28x): Default

Arduino defaults to -11dB (0.28x). Therefore, even the power supply voltage can be AD converted.

However, there is a problem with this default -11dB attenuator.

-11dB attenuation characteristic

This graph plots the relationship between input voltage and AD value (analogRead value) when the attenuator is set to -11dB. Since I wanted to calculate the voltage from the AD value in the past, the horizontal axis is the AD value and the vertical axis is the input voltage.

電圧値とAD値のグラフは直線になるのが理想なのですが、2200mV付近で傾きが変化し、直線ではなくなっています。このため、AD値から実際の電圧値を求めると、誤差が発生してしまいます。

-6dBのアッテネータの特性

こちらは、アッテネータの設定を-6dBに設定したときの、入力電圧とAD値との関係をプロットしたものです。アッテネータによる減衰率が0.28倍から0.5倍になったため、入力電圧の最大が1900mVに下がりました。

先ほどの-11dBのアッテネータのときと違って、グラフが直線になっています。AD値から電圧値を算出する場合、こちらの方が誤差が少なくなります。

これらのことから、測定電圧の範囲は狭くなりますが、アッテネータを-6dBに設定した方が、直線性が良くなり、より正しい電圧値が得られることがわかります。

Sponsoring

アッテネータ-6dBでのanalogReadMilliVolts()

それでは、アッテネータを-6dBに設定した時に、analogReadMilliVolts()がどの程度正しいのか実験してみます。

プログラム

#define AD_PIN 34
#define N 2000
void setup() {
  Serial.begin(115200);
  delay(100);
  analogSetAttenuation(ADC_6db);  //ATT -6dB
  pinMode(AD_PIN, ANALOG);
}

void loop() {
  long adMillivoltTemp = 0;
  for ( int i = 0 ; i < N ; i++ )
  {
      adMillivoltTemp += (long)analogReadMilliVolts(AD_PIN);
  }
  float adMillivolt = (float)((double)adMillivoltTemp / (double)N); //mV Average
  Serial.printf("%d[mV]\n", (int)(adMillivolt+0.5) );
}

analogSetAttenuation()でアッテネータの設定を-6dBにします。

analogReadMilliVolts()で電圧値を2000回取得してその平均を求め、四捨五入した結果をシリアルモニターへ出力しています。

測定結果

上のグラフは、入力電圧を0Vから1.9Vまで変化させたときの、測定結果をプロットしたものです。0V付近と1.9V付近を除けば、完璧に直線になっています。

測定誤差

上のグラフは、入力電圧と測定誤差をプロットしたものです。入力電圧が150mV〜1850mVの範囲では、analogReadMilliVoltsの測定誤差は±7mVであることがわかりました。

Sponsoring

ESP32のADCのATTは-6dBがいいよ

analogReadMilliVolts()を使うことで、簡単に電圧値を取得することができることがわかりました。

さらに、アッテネータを-6dBに設定することで、AD変換の直線性が良くなり測定精度が向上します。

その場合に精度良く測定できる電圧範囲は、150mV〜1850mVでした。

原文
この翻訳を評価してください
いただいたフィードバックは Google 翻訳の改善に役立てさせていただきます