phototransistor. It can detect infrared light with a wavelength ranging from 700nm to 1000nm and its detection angle is about 60°.

We will be building a fire alarm system by using this photodiode with some resistors and a buzzer all connected to the arduino uno. The circuit connection is also easy; the shorter terminal of the photodiode is negative while the longer lead is positive. Connect positive lead to 5V and connect a 10K resistor to the negative lead and to GND. Connect a cable from A0 to the negative lead also.
Then conncet a buzzer to pin 9
(Source:keyestudio arduino kit)
The code;
const int flameSens = A0;//set flame sensor to pin A0
const int speaker = 9;//set speaker to pin 9
int flameVal;//set up the data variable for the sensor
const int speaker = 9;//set speaker to pin 9
int flameVal;//set up the data variable for the sensor
void setup() {
Serial.begin(9600);//begin the serial monitor
pinMode(speaker, OUTPUT);//define the speaker as an output
pinMode(flameSens, INPUT);//define flame sensor as an input
}
void loop() {
int flameVal = analogRead(flameSens);//read the sensor value of flame sensor
if (flameVal > 0) //if the data is greater than 0 then set off an alarm
{
tone(speaker, 800, 800);
delay(200);
tone(speaker, 600, 800);
delay(200);
}
else{
digitalWrite(speaker, LOW);// if else don't set off alarm
tone(speaker, 800, 800);
delay(200);
tone(speaker, 600, 800);
delay(200);
}
else{
digitalWrite(speaker, LOW);// if else don't set off alarm
}
Serial.println(flameVal);//print the data to the serial monitor
}
}
The first part of the code, we declare all relevant variables. In void setup(), we set our pinModes and tell the arduino to start the serial monitor. Then in void loop(), we read the value from our flame sensor and if it is greater than 0, we use the tone function to sound the buzzer, otherwise if there is no flame detected, we do not set off the alarm and sound the buzzer.
Down below is a video which illustrates this system; although in the video the wiring is a bit different as well as the code. However, the results are the same and I am certain you will understand. Ciao
No comments:
Post a Comment