Arduinoで作ろう(15) LM35DZの温度計
Multi Function Shieldに温度センサLM35DZを取りつけて,温度計にしてみました。
LM35DZのアナログデータ出力は,A4で読み取ります。
Arduinoの基準電圧は5.0Vではなくて,実測で4.88Vだったので, temp = ((4.88 * analogdata) / 1024)としました。これでセ氏温度になりますが,さらに10倍して小数点以下1桁まで表示できるようにしてみました。
shiftOut(SDI, CLK, LSBFIRST, digit[Buffer[j]]&B11111110); ってとこで,小数点のDpを点灯させています。
スケッチはこちら→ 20180626.ino
/* Multi_function_shield 温度センサ LM35DZ */const int LAT = 4; // Latchピン
const int CLK = 7; // Clockピン
const int SDI = 8; // Serial Data In ピン
float analogdata, temp;
int Buffer[4] = {0}; //1~4桁 数字のバッファ
byte digit[10] = //7seg 点灯パターン
{
B00000011, // 数字 0
B10011111, // 1
B00100101, // 2
B00001101, // 3
B10011001, // 4
B01001001, // 5
B01000001, // 6
B00011011, // 7
B00000001, // 8
B00001001 // 9
};
const byte keta[4] =
{
B00010000, //1桁め
B00100000, //2桁め
B01000000, //3桁め
B10000000 //4桁め
};void setup() {
pinMode(SDI, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(LAT, OUTPUT);
Serial.begin(9600);
}void loop() {
analogdata = analogRead(A4);
temp = ((4.88 * analogdata) / 1024) * 1000;
Serial.print(analogdata);
Serial.println(temp);
Buffer[3] = int(temp) / 1000;
Buffer[2] = (int(temp) % 1000) / 100;
Buffer[1] = (int(temp) % 100) / 10;
Buffer[0] = (int(temp) % 100) % 10;
Disp();
}void Disp() {
for (byte j = 0; j < 4; j++) {
digitalWrite(LAT, LOW);
shiftOut(SDI, CLK, LSBFIRST, digit[Buffer[j]]);
if (j == 1) {
shiftOut(SDI, CLK, LSBFIRST, digit[Buffer[j]]&B11111110); //小数点をつける
}
shiftOut(SDI, CLK, LSBFIRST, keta[j]); // 桁
digitalWrite(LAT, HIGH);
delay(4);
}
}
| 固定リンク | 0
「Arduino」カテゴリの記事
- ATtiny402 + LM35DZ で温度計(2024.09.21)
- aitendo「ESP-32でいいの」っぽい謎ボードでLチカ(2024.04.17)
- ATtiny202 で Hello World!(2024.03.25)
- CH32V003+I2C接続のLCDでHello World !(2023.12.29)
- CH32V003F4P6 開発ボードでLチカ(2023.12.20)