Alcohol Sensor with Arduino IoT Cloud platform

 Here we will illustrate how the Arduino IoT cloud platform can be used for monitoring alcohol usages. This can be helpful to detect whether somebody inside workplace or check whether employee have taken alcohol in the office or not. It is also useful for industrial IoT where alcohol is used, for example, in food industry where alcohol level has to be monitored and controlled. Winery and Hard drinks factories would find this IIoT application useful. By using the Arduino IoT cloud based service we can check the level of alcoholat any time from anywhere in the world, record data and perform data analytics for better quality product and overall productivity of the business.

Recommended Tutorials:

This is the 3rd tutorial on IoT projects with Arduino IoT platform. The previous two tutorials are recommended:

1. Simple Arduino IoT cloud tutorial - A simple LED control from the Arduino cloud web interface using IoT cloud platform.

2. Beginner guide to Arduino IoT cloud platform - A tutorial which shows humidity and temperature data on the web using IoT cloud platform.

Hardware Setup

This IoT project requires NodeMCU ESP8266 IoT device and MQ-3 alcohol detector. The circuit diagram of connecting NodeMCU ESP8266 and MQ-3 gas sensor is below.


 

Arduino IoT cloud platform

On the Arduino IoT cloud platform there are three steps we have to take to make the IoT application run.

1. Create and setup of thing(variable, device and network)

2. Writing Sketch

3. Create Dashboard

1. Create and setup of thing(variable, device and network)

The first step is to create a thing. Create a new thing on the Arduino IoT cloud.

Name the new thing as AlcoholSensor and click on the Add Variable.
 

In the variable properties, give the variable a name like mq3Value, set the data type as float, set variable permission to Read Only and set the Variable Update Policy to Periodically.


 After clicking on the ADD VARIABLE button you should see the mq3Value as cloud variable.

The next step is to associate IoT device to connect to the cloud. Click on the Select Device and then click on the device that is automatically detected by the Arduino Cloud IoT agent application or click on the SET UP NEW DEVICE to select your IoT device.


IoT device

After the IoT device is selected the device will be shown in the thing as shown below.

Then we need to add WiFi connectivity credential to connect the IoT device to the internet via the IoT cloud platform. For this click on the Configure button in the network section.

Enter your WiFi SSID name and the password. At the beginning of the Arduino device creation you should have been given secret key for your specific device. Enter that secret key.

Now we have completed setup part and the next step is to write the sketch.

2. Writing Sketch

Click on the Sketch tab. You should see a self generated sketch.


 Now we will have to write code that reads the MQ3 sensor value and print out message.

Open the sketch in full editor mode. 

After the line #include "thingProperties.h" add the following line of code:

const int mq3Pin = 13;

float mq3_value;

This is the alias name for the pin to which MQ3 gas sensor analog output is connected to GPIO13 of NodeMCU ESP8266.

Similarly add the delay(1500); line add the following code in the setup() function. This will allow time for the MQ3 to warm up.

Serial.println("MQ3 Warming Up...");
 delay(5000);
  Serial.println("Warming done...");

This is illustrated in the code picture below.


 

 In the setup function type in the following code.

  mq3_value = analogRead(mq3Pin); // read analog input pin

Serial.print("Alcohol Level: ");
Serial.println(mq3_value);
Serial.print("\n");
if (mq3_value < 200) { // If value read is less than 200, you are sober
Serial.println("Sober");
}
if (mq3_value >= 200 && mq3Value < 280) {
Serial.println("One beer");
}
if (mq3_value >= 280 && mq3Value < 350) {
Serial.println("Two or more beers.");
}
if (mq3_value >= 350 && mq3Value < 450) {
Serial.println("VODKA Level");
}
if (mq3_value > 450) {
Serial.println("Drunk");
}
Serial.print("\n");

 The full code is provided below.


/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/4cee2ad2-ea34-486b-b296-701dd2a440a3 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float mq3Value;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

const int mq3Pin = A0;
float mq3_value;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  Serial.println("MQ3 Warming Up...");
  delay(5000);
  Serial.println("Warming done...");

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
    mq3_value = analogRead(mq3Pin); // read analog input pin

  Serial.print("Alcohol Level: ");
  Serial.println(mq3_value);
  Serial.print("\n");
   if (mq3_value < 200) { // If value read is less than 200, you are sober
      Serial.println("Sober");
      }
      if (mq3_value >= 200 && mq3Value < 280) {
      Serial.println("One beer");
      }
      if (mq3_value >= 280 && mq3Value < 350) {
      Serial.println("Two or more beers.");
      }
      if (mq3_value >= 350 && mq3Value < 450) {
      Serial.println("VODKA Level");
      }
      if (mq3_value > 450) {
      Serial.println("Drunk");
      }
   Serial.print("\n");  
}

The next step is compile and upload the code to the NodeMCU ESP8266 module.

We can verify that we are reading from the NodeMCU and that the MQ3 sensor is producing correct values by using the Monitor. 

We can verify that data is being printed out on the serial monitor on the Arduino cloud IoT platform as shown below.

serial monitor Arduino IoT cloud

Here we have shown how to acquire alcohol sensor data using MQ3 alcohol sensor and NodeMCU as our IoT device and display the alcohol on the cloud web. This is one example of IIoT solution which can be useful in industrial factories that needs to control and monitor alcohol level. Not only alcohol but we can also monitor other gases uses MQ2 gas sensor.

A simpler tutorials on using MQ3alcohol sensor and MQ2 gas sensor are provided below.

- MQ3 Alcohol Analyzer with Arduino

- MQ-2 Gas Sensor with Arduino and LCD

- Gas Sensor MQ-2 with ATmega32 and LCD

Post a Comment

Previous Post Next Post