Arduinoで火災報知機をつくってみた
家にある電気ストーブにオフタイマーが付いていなくて、ついついつけっぱなしにしてしまうことがあった。ときには8時間以上つけっぱなしにしていることがあって、その分の電気代もったいないし、火災の危険もあるためなんとかせねばならないと考えた。そこで、ちょうど手元にあったArduinoとGrooveの温度センサとブザーを使って、室温が一定以上になったときに、ブザーが鳴る簡単な報知器システムを構築してみた。

画像のように、Arduino UNOにGrove shieldを取り付けた。それの、A0に温度センサを取り付け、D2にボタンを取り付け、D3にLEDを取り付け、D6にブザーを取り付けた。
そして、以下のプログラムをArduinoに書き込むことによって、システムを稼働させた。温度が一定以上になったときにブザーが鳴り、LEDが赤色に光る。ボタンを押すと音が止む仕組みになっている。
const int PinTemp = A0; // pin of temperature sensor
const int PinButton = 2;
const int PinLed = 3;
const int PinBuzzer = 6;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
float temperature;
int B=3975; // B value of the thermistor
float resistance;
int maxtemp = 30;
int cnt = 0;
int maxcnt = 60*60*3; // 3 hour
int coolingcnt = 0;
int maxcoolingcnt = 60*60; // cooling count 1 hour
void setup()
{
Serial.begin(9600); //Baud rate for the serial communication of Arduino
pinMode(A0,INPUT); //Setting the A0 pin as input pin to take data from the temperature sensor
pinMode(PinLed, OUTPUT); // set led OUTPUT
pinMode(PinButton, INPUT); // initialize the pushbutton pin as an input:
pinMode(PinBuzzer, OUTPUT);
}
void loop()
{
int val = analogRead(PinTemp); // get analog value
resistance=(float)(1023-val)*10000/val; // get resistance
temperature=1/(log(resistance/10000)/B+1/298.15)-273.15; // calc temperature
Serial.println(temperature);
Serial.println(cnt);
if (temperature > maxtemp) {
digitalWrite(PinLed, HIGH);
cnt += 1;
}
else{
digitalWrite(PinLed, LOW);
coolingcnt += 1;
}
// read the state of the pushbutton value:
buttonState = digitalRead(PinButton);
// button clear
if (buttonState == HIGH) {
// turn LED on and off three times:
digitalWrite(PinLed, HIGH);
delay(100);
digitalWrite(PinLed, LOW);
delay(100);
digitalWrite(PinLed, HIGH);
delay(100);
digitalWrite(PinLed, LOW);
cnt = 0;
}
// cnt clear
if (coolingcnt > maxcoolingcnt){
cnt = 0;
coolingcnt = 0;
}
if (cnt > maxcnt) {
digitalWrite(PinBuzzer, HIGH);
}
else {
digitalWrite(PinBuzzer, LOW);
}
delay(1000); // delay 1s
}
Arduinoを用いた電子工作の例をもっと見たい!という方には「作りながら考えるためのArduino実践レシピ」がおすすめです。この本には35本のレシピ(配線図+サンプルコード)が載っているので、作例を探索するだけではなく、Arduinoの教科書としても使える実用的な書籍になっています。






ディスカッション
コメント一覧
サーミスタによる温度測定、サーミスタの抵抗値を読む方法についてあれこれ書いてみました。
ご一読いただければ。
A/Dコンバータでサーミスタの抵抗値を読む サーミスタをつなぐ場所は?
http://igarage.cocolog-nifty.com/blog/2023/03/post-ad45c1.html
Arduino サーミスタを使った温度測定で 【ゼロ除算問題】
http://igarage.cocolog-nifty.com/blog/2023/03/post-4bb96a.html
ミスが広まる 1/1023 vs 1/1024
http://igarage.cocolog-nifty.com/blog/2020/01/post-a02d3f.html