Introduction: Intel® Edison Hands-on Day 2: FlameFire Alarm
Install a fire alarm in the kitchen will be of use to us. A small flame can trigger it and the range of it can reach 20CM. This mini fire alarm may avoid some accidents so why not?
Components required
Step 1: Connection
Digital Buzzer Module → Digital Pin 8
Flame sensor → Analog Pin 0
Step 2: Coding
float sinVal;
int toneVal;
void setup(){
pinMode(8, OUTPUT); //Set the buzzer pin as output
Serial.begin(9600); //Set the baud to 9600
}
void loop(){
int sensorValue = analogRead(0);//Read the Analog value from Flame sensor
Serial.println(sensorValue);
delay(1);
if(sensorValue < 1023){ // If the value is less than 1023, the fire exists and let the buzzer run.
for(int x=0; x<180; x++){
//Change from degree to radian using sin() function
sinVal = (sin(x*(3.1412/180)));
//Create the frequency for the buzzer.
toneVal = 2000+(int(sinVal*1000));
//Run the buzzer.
tone(8, toneVal);
delay(2);
}
} else { // If the value is more than 1023, the fire doesn’t exists and let the buzzer stop.
noTone(8); //Turn off the buzzer
}
}
Try to move the lighter close to the flame sensor, and hear whether the buzzer will work.
Step 3: Principle(Analog Input—Digital Output)
Flame sensor is an analog input device to detect the fire,buzzer is parentally an output device,the Edison is the controller.
Step 4: Code Review
Note the two variable:
float sinVal;
int toneVal;
Float variable sinVal stores the sine changing corresponding to the degree. Sine wave describes a smooth repetitive oscillation, so that we use it to create the frequency of the sound. Thus we convert the sinVal to toneVal, which makes it suitable to the buzzer output.
Flame sensor is the input device, so we need to read the analog value from the specific pin. The Syntax looks like this:
analogRead(pin)
Reads the value from the specified analog pin. The Edison Arduino kit contains a 6 channel 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. For example, the analog value 512 stands for 2.5V.
The sin() function calculates the sine of an angle (in radians). The result will be between -1 and 1. To avoid the negative, we should constrain the degree changing between 0 and 180. We realize it by using for statement.
for(int x=0; x<180; x++){}
The sin() function uses radian as input, so conversion should be done at first. Multiplied by 3.1415/180 can change from degree to radian:
sinVal = (sin(x*(3.1412/180)));
After that convert the sinVal to toneVal, which makes it suitable to the frequency of the buzzer:
toneVal = 2000+(int(sinVal*1000));
There is one point here, which is how to convert the float to integer.
sinVal is a float type, just use int() to do the convertion:
int(sinVal*1000)
sinVal multiplied by 1000 and plus 2000 results in toneVal. Then toneVal can be used as the frequency for buzzer.
After that, use tone to run the buzzer.
tone(8, toneVal);
Let’s talking about the tone:
(1)tone(pin,frequency)
It is used to generate a square wave of the specified frequency on a pin
pin: the pin on which to generate the tone
frequency: the frequency of the tone in hertz - unsigned int
(2)tone(pin,frequency,duration)
pin: the pin on which to generate the tone
frequency: the frequency of the tone in hertz - unsigned int
duration: the duration of the tone in milliseconds (optional) - unsigned long
(3)noTone(pin)
It stops the generation of a square wave triggered by tone()