1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Arduinoで温度センサを使うときのポイント

Last updated at Posted at 2024-06-16

精度が悪い時

LM35とかTMP36GZを使うときに温度と電圧は以下に従います。

\begin{eqnarray}
V &=& 0.25 + (T[℃]-25) \times 0.01 \\
&=& T[℃] \times 0.01 \\
&=& T[℃] \times 10[mV/℃]
\end{eqnarray}

次のコードで電圧を測ると誤差が出ます。

void setup() {
  pinMode(A0,INPUT);
  //analogReference(INTERNAL);
  Serial.begin(9600);
}

void loop() {
  int data = analogRead(A0);
  float temp = data * 0.48828;  //5/1024÷0.01 
  Serial.println(temp,3);
  delay(100);
}

室温27℃に対して38℃とかずれた値が計測されます

原因

A/Dコンパレータの基準電圧が5Vからずれていたことです。

5V端子(Vcc)の電圧を計測したところ3.7Vになっていました。結果として1.35倍高い温度が計算されていました。
室温27℃に対して38℃とかずれた値も1.41倍となっており、基準電圧が原因ということを裏付けています。

image.png

基準電圧はAVCCまたは内部基準電圧1.1Vで選択可能です。

対処法

内部基準電圧1.1Vを使います。

analogReference(INTERNAL);

を実行することでA/Dコンバータの基準電圧が1.1Vになります。
これで精度が上がります。

sample_program
void setup() {
  pinMode(A0,INPUT);
  analogReference(INTERNAL);
  Serial.begin(9600);
}

void loop() {
  int sum = 0;
  for(int i=0;i<100;i++){
    sum += analogRead(A0);
    delay(1);
  }
  float data = sum / 100;
  float temp = data * 0.10752688;
  Serial.println(temp,3);
  //delay(100);
}

100回計測して平均化していますが、これをしているのはかなり計測結果がぶらつくためです。

平均なし
image.png

10回平均(計測間隔1ms)
image.png

10回平均(計測間隔0ms、delayなしで連続)
image.png

100回平均
image.png

  • 平均回数は10回ほど必要
  • 計測間隔はあったほうが精度良好、なしで連続にしても効果はあり
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?