How to use Arduino sensor in IoT project

Follow this guide to know how to use Arduino and IFTTT to build an IoT system. Learn how to monitor the using a MQ-4 sensor and send an alert using IFTTT

This post describes how to create an IoT system integrating Arduino and IFTTT. In more details,  this IoT tutorial covers how to connect Arduino to IFTTT so that we can monitor the gas sensor and send a notification when there is an anomaly. You will learn how easy is creating an IoT system that monitors the gas in a room using Arduino sensors and sends a notification when the gas concentration is over the threshold using IFTTT. First of all, we assume you are already familiar with IoT projects and what IoT means and how it will impact our future.

What do we need to create an IoT system that uses Arduino and IFTTT that monitors the sensor?

  • IFTTT account
  • MQ-4 sensor
  • Arduino Uno + internet shield (or MKR1000 as shown in the project)
  • Jump wirings

Step 1: Implement the notification integrating Arduino and IFTTT

In this step, we will implement a notification system, so that when the gas concentration is over the threshold we will get an email. We will see later how to monitor the sensor. To implement this IoT system we will integrate Arduino and IFTTT. IFTTT stands for If This Then That. It is a powerful system that helps us to connect to countless services like social services., email, SMS and so on. The first step is creating an account if this is the first time you use this service. The services provided by IFTTT are free.

Once you have your account on IFTTT, then click on the search on the top and looks for maker service. This service is made for makers that want to integrate external services with Arduino or similar boards:

arduino ifttt maker

Now go to Applet and create a new Applet. Click on the plus sign and add the service. In this case, you should add Maker service the one we created before. Click on Receive a web request and configure your trigger. This is the event that triggers the notification process, in other words, as soon as IFTTT receive a web request it sends an email:

ifttt web request

Create the trigger. Now we have to configure the other step: the service that should be executed when the trigger is fired. Click on the plus sign again (in the then part) and add the email service. You can add other services too:

ifttt send email

Add email details and confirm. At the end you have your service configured in IFTTT:

ifttt service configured

That’s all. Now to trigger this event we have to call the URL as shown in the service configuration. The Arduino system will call this URL when the sensor detects an anomaly in the gas concentration.

That’s all. In this first step, we have connected together Arduino and IFTTT. Now Arduino can use IFTTT to send notifications.

Step 2: Arduino sensor: How to connect MQ-4 gas sensor

To trigger the event Arduino will read the sensor values. To make things simple, this IoT system uses a MQ-4 sensor. You can use other sensors too like PIR, temperature, humidity and so on. The same steps can be applied to other sensors too. The process that stands behind this project doesn’t depend on the sensor you will use.

MQ-4 is a fast and reliable sensor very sensitive to natural gas and CH4 (Methane). It has a long life and is commonly used in the gas leakage detection. What we want to do is creating a simple project that shows the gas concentration using two LEDs:

  • Green Led: The gas concentration is under the threshold
  • Red Led: Alert the gas concentration is above the threshold

MQ-4 Arduino sensor has four pins:

  • Vcc (+5V)
  • Ground
  • Digital output
  • Analog output

We will use the analog output to check the gas concentration. The circuit below shows how to connect the gas sensor to Arduino:

Please notice that the connections to the gas sensor in the schematic above are not real, while the Arduino pin used to get data from the sensor is the real one (A5). In the picture above, the resistors are not showed.

When you turn on the gas sensor at the beginning it could smell a bit. This is normal do not worry. In the schematic, there are two resistors that connect the Arduino digital pin and the LEDs. This resistor limits the current flowing through the LEDs. They are 220 Ohm. That’s all. You are ready now to develop the sketch to monitor the Arduino sensor.

Step 3: Programming Arduino to read Gas sensor data and trigger the IFTTT

The sketch below describes how to implement the gas monitoring system:

int pinRedLed = 11;
int pinGreenLed = 8;
int pinSensor = A5;
int THRESHOLD = 250;

void setup() {
  pinMode(pinRedLed, OUTPUT);
  pinMode(pinGreenLed, OUTPUT);
  pinMode(pinSensor, INPUT);
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(pinSensor);
  Serial.println("Val: " + analogValue);
  digitalWrite(pinGreenLed, HIGH);

  if (analogValue >= THRESHOLD) {
    digitalWrite(pinGreenLed, LOW);
    digitalWrite(pinRedLed, HIGH);
  }
  else {
   digitalWrite(pinRedLed, LOW);
  }
  delay(5000);
}

The code is very simple. In the beginning, it declares the pins we will use to connect to the sensor and LEDs. The green LED is always on while the red LED turns on only when the gas concentration is over the threshold.

 

Conclusion

In the end, you have implemented a simple IoT project in just three steps. Integrating online services and Arduino you can expand your Arduino and create interesting and useful IoT project with a few lines of code. In this tutorial, you learned how to create an IoT project that monitors  sensor. The best part is the integratation between Arduino and IFTTT so that this integration can be applied to other scenarios too.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here