Smoke detector sketch - Digital I/O method

Use the following sketch for detecting smoke, based on the output captured from the MQ2 gas sensor's digital output pin. As always, avoid connecting the devices to the Arduino pins while loading the sketch.

The following code can be freely downloaded from the location mentioned in the Boot Camp section of this book. This is how the code for the smoke detector (digital I/O technique) looks:

//**********************************************************/ 
// Step-1: CONFIGURE VARIABLES
//**********************************************************/
int smokePin = 2; // Digital Pin number 2 for
// connecting the smoke sensor.
int buzzerPin = 8; // Digital Pin number 8 for
// connecting the buzzer.

//**********************************************************/
// Step-2: INITIALIZE I/O PARAMETERS
//**********************************************************/
void setup()
{
pinMode(smokePin, INPUT); // Configure Digital Pin 2
// in input mode,
// for reading signals
// from the smoke sensor.
pinMode(buzzerPin, OUTPUT);// Configure Digital Pin 8
// for output mode,
// for sending signals
// to the buzzer.
delay(5000); // Wait for 5 seconds
// before sending any signals
// to the sensor.
// Although it is not required
// by the smoke sensor's
// datasheet;
// However, it is a good
// practice to let the
// sensor device stabilize
// for a few seconds before
// sending any signals to it.
}

//**********************************************************/
// Step-3: MAIN PROGRAM
//**********************************************************/
void loop()
{
// Read the input at Arduino Uno's Digital Pin 8.
// As soon as smoke is detected:-
// (1) The onboard LED on the MQ2 gas sensor will glow, and
// (2) a LOW signal will be received on Digital Pin 8
if (digitalRead(smokePin) == LOW)
{
// Ring the buzzer 5 times at 30 second intervals.
tone(buzzerPin, 500, 250);
delay(500);
tone(buzzerPin, 500, 250);
delay(500);
tone(buzzerPin, 500, 250);
delay(500);
tone(buzzerPin, 500, 250);
delay(500);
tone(buzzerPin, 500, 250);
delay(500);
}
// Wait for 2 seconds and check again
delay(2000);
}

The preceding sketch is a very simple example of reading the digital output received on Arduino Uno's digital pin number 8. It is important to note the technique used to read the digital input signal from the sensor. Let us try to understand the sketch in detail. The sketch starts with the typical section on declaring variables to be used in the sketch. In this case, we have defined two variables: the smokePin and the buzzerPin.

The setup() function is used to configure the pins used in this setup in the appropriate mode. Digital pin number 2, which is connected to the D0 pin of the MQ2 sensor module, is configured in input mode, so that it can be used to read the input signal from the MQ2 smoke sensor.

Digital pin number 8 is configured in output mode so that it can be used to send digital output signal via digital pin number 8. Then there is a small delay of 5 seconds. This is not mandatory, but it has been done to allow the smoke sensor to stabilize, as shown in the following code:

delay(5000);               // Wait for 5 seconds 

The final step is to use Arduino's in-built function digitalRead(<pin-number>) for reading the input signal level (HIGH/LOW) available on pin 8 (in this example). The digitalRead(<pin-number>) function takes the pin number as an input and returns the signal level available on the digital input pin.

In this example, it checks the signal level on pin number 8 and returns the signal level. A LOW signal level is received as soon as smoke is detected. In conjunction, it would be good to note that there is an onboard LED marked as D0-LED on the reverse side of the MQ2 gas sensor circuit board: this LED starts glowing every time smoke is detected:

  • Receiving digital signals: Use Arduino Uno's digital I/O pins for connecting to a digital I/O pin of a peripheral device. Configure Arduino's digital I/O pin in input mode, for reading input signals from a peripheral device. Use the digitalRead(<pin-number>) function for programmatically reading the input signal, at the specified pin number. The use of this method is depicted in the preceding smoke detector example.
  • Sending digital signals: Configure Arduino's digital I/O pin in OUTPUT mode, for sending output signals to a peripheral device. Use the digitalWrite(<pin-number>, <HIGH/LOW>) function for programmatically sending output signals to a peripheral device, via the specified pin number.

The second part of the preceding sketch focuses upon sounding the Piezo Buzzer five times, at intervals of 30 seconds. Arduino's in-built function is used to emit sounds using the Piezo Buzzer:

tone(buzzerPin, 500, 250); 

In this example, the function call causes the Buzzer to emit a sound at a frequency of 500 Hertz for a period of 250 milliseconds. While the delay(500) function simply delays the program execution for 500 milliseconds (that is, 30 seconds).

Sometimes due to unspecified or unexpected conditions, the electronic peripheral sensor devices might require a few extra seconds to stabilize. Unless specified otherwise in the sensor device's datasheet, it is recommended to halt the program execution for a few seconds in the setup() function, before sending any signals to the peripheral sensor devices. For example, in the preceding sketch, the program execution has been halted for 5 seconds, before any interaction with the MQ2 gas sensor starts.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.78.106