Smoke detector sketch (analog I/O method)

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

The following code can be downloaded from the location mentioned in Chapter 1, Boot Camp. Here is the code for the smoke detector (analog I/O technique):

//**********************************************************/ 
// Step-1: CONFIGURE VARIABLES
//**********************************************************/
int smokePin = A5; // Analog Pin number A5
// 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 Analog Pin A5
// in input mode,
// for reading signals
// from the smoke sensor's
// Analog output pin.

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;
// it is a good practice
// to let a 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 Analog Pin A5.
// As soon as smoke is detected
// the reading should be above 325
if (analogRead(smokePin) > 325)
{
// 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 analog output received on Arduino Uno's analog pin number A0. It is important to note the technique used to read the analog input signal from the sensor.

The first step is to connect the sensor's analog output pin to Arduino Uno's analog I/O pin number A0 (in this example). The next step is to configure (via the C sketch) Arduino's pin A0 in INPUT mode. The final step is to use Arduino's in-built function - analogRead(<pin-number>) - for reading the input signal available on pin A0 (in this example).

The integer number 325 used for detecting the smoke threshold might need some calibration based on the type of smoke being detected. Hence it is advisable to adjust the number in order to detect a particular type of smoke.

The analogRead(<pin-number>) function takes the pin number as an input and returns an analog value corresponding to the signal available on the analog input pin. In this example, it checks the analog signal available on pin number A0 and returns an integer value corresponding to the signal on the A0 pin. This integer number is usually greater than 325, as soon as smoke is detected:

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

Now that we have seen how a basic compound device is built, the next step would be to introduce another useful dimension while prototyping compound devices. In the real world, most devices have the need to store data. For example, an internet-enabled device has the capability to store your Wi-Fi credentials. Another practical example would be a conference room light controller that has the ability to be configured with user preferences such as switching off the conference room light if there is no movement in the room for an hour. Similarly, there are a myriad of uses for storing data locally in a device.

In the next section, we will introduce the usage of local storage, using the SD card module. We will learn how to use the SD card module by adding the module to the smoke detector device that we built earlier in this chapter. Usually, large amounts of data get logged by sensors and therefore SD cards provide an inexpensive temporary local storage, until the data is flushed to IoT Cloud channels. Chapter 12, Day 10 – The Internet of Things Project will explain what IoT cloud channels are.

It is worthwhile to note that the ATmega328 microcontroller on the Arduino board comes in-built with a local data storage of 1024 bytes, for storing very small amounts of data. This memory area is known as EEPROM (electrically erasable programmable read-only memory). The data stored in the EEPROM is persisted even after switching off the power supply to the board. You can refer to the following Arduino Foundation website URL for a tutorial of writing data to the EEPROM area:

https://www.arduino.cc/en/Tutorial/EEPROMWrite.

Upon rebooting the Arduino board we can easily retrieve the stored information. You can refer to the following Arduino foundation website URL for a tutorial of retrieving data from the EEPROM area:

https://www.arduino.cc/en/Tutorial/EEPROMRead.

..................Content has been hidden....................

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