WeMosを使ってみる

LM35DZ(アナログ温度センサー)+PCF8591


前回、MCP3002を使ったアナログ値の読み込みを紹介しました。
今回は、8-Bit ADCのPCF8591を使ってLM35DZのアナログ温度センサーから温度を読み込んでみます。
LM35DZの出力には、ノイズを低減するために100マイクロのバイパスコンデンサーを挟んでいます。
こちらも前回と同様に、キャリブレーションのために、WeMosの3.3Vピンの電圧をテスターで測ります。


WeMosのスケッチは以下の通りです。
RefVにはテスターで測った値を指定しています。
PCF8591は2回読まないと正しい値が取れません。
/*
 * LM35DZ Temperature sensor using PCF8591
 * http://henrysbench.capnfatz.com/henrys-bench/arduino-voltage-measurements/pcf8591-adda-converter-arduino-tutorial-1-a-simple-analog-measurement/
 *
 * PCF8591    LM35ZD      WeMos
 * SCL               ---- D1(GPIO5)
 * SDA               ---- D2(GPIO4)
 * A0                ---- GND
 * A1                ---- GND
 * A2                ---- GND
 * VSS               ---- GND
 * VDD               ---- 3.3V
 * Vref              ---- 3.3V
 * AINT0  --- OUT
 *            VCC    ---- 3.3V
 *            GND    ---- GND
 */
#include "Wire.h"
#define PCF8591 (0x48) // I2C bus address

/*
 pcf8591 The command byte is structured as follows.

 7 6 5 4 3 2 1 0
 0 X X X 0 X X X
   | | |   | | |
   A B B   C D D

 A 0 D/A inactive
   1 D/A active

 B 00 single ended inputs
   01 differential inputs
   10 single ended and differential
   11 two differential inputs

 C 0 no auto inc
   1 auto inc

 D 00 select channel 0
   01 select channel 1
   10 select channel 2
   11 select channel 3
*/

#define AIn0 0x00 // select single ended inputs from channel 0
#define AIn1 0x01 // select single ended inputs from channel 1
#define AIn2 0x02 // select single ended inputs from channel 2
#define AIn3 0x03 // select single ended inputs from channel 3
#define RefV 3.28

int RawValue0 = 0;
float Voltage = 0.0;
float temp_c = 0;  // 摂氏値( ℃ )

void setup()
{
  Wire.begin(4,5); // sda=4, scl=5
  Serial.begin(9600);
}
void loop()
{
  Wire.beginTransmission(PCF8591); // Start your PCF8591
  Wire.write(AIn0); // Tell it to make an Analog Measurement from channel 0
  Wire.endTransmission(); //
  Wire.requestFrom(PCF8591, 2); // Get the Measured Data

  RawValue0=Wire.read(); // read PCF8591 (Dummy)
  RawValue0=Wire.read(); // read PCF8591
//  Serial.print(" RawValue0[PCF8591] = ");
//  Serial.print(RawValue0);
  Voltage = (RawValue0 * RefV )/ 255.0;
  temp_c = Voltage * 100;
  Serial.print(" Voltage[PCF8591] = ");
  Serial.print(Voltage);
  Serial.print(" temp_c[PCF8591] = ");
  Serial.println(temp_c);
  delay(500);
 
}


回路は以下の通りです。

IDEのシリアルモニターに、温度が表示されます。



「高精度温度湿度計 ライフナビD203」と比較すると、なかなかいい結果の値となりました。




続く...