温度センサーLM35DZを使用する。0〜100度まで測定可能
温度センサーの+Vs端子にArduino5V, Voutをアナログ端子(A0〜A5)、GNDにGNDを接続する
アナログ端子は、HIGH/LOW間の1.0V、1.6V、2.0V、2.5V、3.1V…の細かい電圧を読み取れる
読み取った0~5Vの電圧は、0〜1023の値として取得できる。
35だった場合、
35/1023 = 0.034
0.034 * 5 = 0.17V
1V = 100℃
0.17 * 100 = 17℃
#define TERMINAL_A 3 #define TERMINAL_B 4 #define TERMINAL_C 5 #define TERMINAL_D 6 #define TERMINAL_F 7 #define TERMINAL_G 8 #define TERMINAL_H 9 #define TACT_SW 10 // 10番ピンをタクトスイッチ #define DIGIT_1 11 #define DIGIT_10 12 #define DIGIT_100 13 #define DIGIT_1000 14 #define DP 15 // ドット #define THERMO_IN 16 // A2端子のため16を指定 byte counter = 0; // Arduinoで使用できる1byteの型 byte state = 0; byte state_old = 0; byte buf_num = 0; int thermo_buf[10]; void led_output(byte num){ switch(num) { case 0: // 4桁7セグメントではアノードコモンを使用しており、7セグメントLEDの逆 digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, LOW); digitalWrite(TERMINAL_D, HIGH); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, LOW); break; case 1: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, HIGH); digitalWrite(TERMINAL_C, HIGH); digitalWrite(TERMINAL_D, HIGH); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, HIGH); digitalWrite(TERMINAL_H, HIGH); break; case 2: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, HIGH); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, HIGH); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, LOW); break; case 3: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, HIGH); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, HIGH); break; case 4: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, HIGH); digitalWrite(TERMINAL_C, LOW); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, HIGH); digitalWrite(TERMINAL_H, HIGH); break; case 5: digitalWrite(TERMINAL_A, HIGH); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, LOW); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, HIGH); break; case 6: digitalWrite(TERMINAL_A, HIGH); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, LOW); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, LOW); break; case 7: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, HIGH); digitalWrite(TERMINAL_D, HIGH); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, HIGH); digitalWrite(TERMINAL_H, HIGH); break; case 8: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, LOW); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, LOW); break; case 9: digitalWrite(TERMINAL_A, LOW); digitalWrite(TERMINAL_B, LOW); digitalWrite(TERMINAL_C, LOW); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, LOW); digitalWrite(TERMINAL_G, LOW); digitalWrite(TERMINAL_H, HIGH); break; default: digitalWrite(TERMINAL_A, HIGH); digitalWrite(TERMINAL_B, HIGH); digitalWrite(TERMINAL_C, HIGH); digitalWrite(TERMINAL_D, LOW); digitalWrite(TERMINAL_F, HIGH); digitalWrite(TERMINAL_G, HIGH); digitalWrite(TERMINAL_H, HIGH); break; } } void setup() { // 初期化 pinMode(TERMINAL_A, OUTPUT); pinMode(TERMINAL_B, OUTPUT); pinMode(TERMINAL_C, OUTPUT); pinMode(TERMINAL_D, OUTPUT); pinMode(TERMINAL_F, OUTPUT); pinMode(TERMINAL_G, OUTPUT); pinMode(TERMINAL_H, OUTPUT); pinMode(DP, OUTPUT); pinMode(DIGIT_1, OUTPUT); pinMode(DIGIT_10, OUTPUT); pinMode(DIGIT_100, OUTPUT); pinMode(DIGIT_1000, OUTPUT); pinMode(TACT_SW, INPUT); // タクトスイッチに接続した10番ピンをインプットに設定 pinMode(THERMO_IN, INPUT); // 16番ピンをINPUT指定 digitalWrite(DIGIT_1, LOW); digitalWrite(DIGIT_10, LOW); digitalWrite(DIGIT_100, LOW); digitalWrite(DIGIT_1000, LOW); led_output(counter); } void clear_num(void){ digitalWrite(TERMINAL_A, HIGH); digitalWrite(TERMINAL_B, HIGH); digitalWrite(TERMINAL_C, HIGH); digitalWrite(TERMINAL_D, HIGH); digitalWrite(TERMINAL_F, HIGH); digitalWrite(TERMINAL_G, HIGH); digitalWrite(TERMINAL_H, HIGH); digitalWrite(DP, HIGH); } int get_temperature(void){ float thermo_sens; int thermo; int thermo_ave = 0; byte i; thermo_sens = analogRead(THERMO_IN); // センサー値取得 thermo_sens = ((5*thermo_sens)/1024) * 100; // 温度へ変換 thermo = thermo_sens * 10; thermo_buf[buf_num] = thermo; // 初期値は0 buf_num++; if(buf_num > 9){ buf_num = 0; } for(i = 0; i < 10; i++){ thermo_ave = thermo_ave + thermo_buf[i]; } thermo_ave = thermo_ave / 10; return thermo_ave; } void loop() { int num; int thermo; state = digitalRead(TACT_SW); // 電流が流れているとHIGH, 流れていないとLOW if((state == HIGH) && (state_old == LOW)){ counter++; } else { } if(counter >= 10000){ counter = 0; } thermo = get_temperature(); num = thermo/ 1000; clear_num(); digitalWrite(DIGIT_1000, HIGH); delay(1); led_output(num); delay(5); digitalWrite(DIGIT_1000, LOW); num = thermo % 1000; num = thermo/ 100; clear_num(); digitalWrite(DIGIT_100, HIGH); delay(1); led_output(num); delay(5); digitalWrite(DIGIT_100, LOW); num = thermo % 100; num = num/ 10; clear_num(); digitalWrite(DIGIT_10, HIGH); delay(1); led_output(num); delay(5); digitalWrite(DIGIT_10, LOW); num = thermo % 10; clear_num(); digitalWrite(DIGIT_1, HIGH); delay(1); led_output(num); delay(5); digitalWrite(DIGIT_1, LOW); state_old = state; }
配線
あれ、コンパイルして転送したら、突然シリアルポートを読み込まなくなった。
なんだこれ。
一瞬パニクったが、温度センサーの配線が間違っていた模様。表裏をひっくり返して再度実行
出来ました。
Arduinoは中々良い買い物だった。