keyestudio
www.keyestudio.com
Experiment Principle
When it's approaching a fire, the voltage value read from the analog port will differ. If you use a
multimeter, you can see that when there is no fire approaching, the voltage it reads is around 0.3V;
when there is fire approaching, the voltage it reads is around 1.0V. The nearer the fire is, the
higher the voltage is.
So in the beginning of the program, you can initialize voltage value i (no fire value); Then,
continuously read the analog voltage value j and obtain difference value k=j-i; compare k with
0.6V (123 in binary) to determine whether there is a fire approaching or not; if yes, the buzzer will
buzz.
Sample Code
//////////////////////////////////////////////////////////
int flame=0;// select analog pin 0 for the sensor
int Beep=9;// select digital pin 9 for the buzzer
int val=0;// initialize variable
void setup()
{
pinMode(Beep,OUTPUT);// set LED pin as “output”
pinMode(flame,INPUT);// set buzzer pin as “input”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(flame);// read the analog value of the sensor
Serial.println(val);// output and display the analog value
if(val>=600)// when the analog value is larger than 600, the buzzer will buzz
{
digitalWrite(Beep,HIGH);
}else
{
digitalWrite(Beep,LOW);
}
delay(500);
}
//////////////////////////////////////////////////////////
Result
This program can simulate an alarm when there is a fire. Everything is normal when there is no
fire; when there is fire, the alarm will be set off immediately.