In this tutorial we are going to show you how to use ammonia sensors, arduino and raspberry to measure ammonia concentration and providing alerts if there is a leak or a too high concentration in the air!
This project is our school project, in fact the chemical lab of our school wanted a system to detect if the concentration of ammonia in the air was too high. In the lab there are chemical lab hood, and students needs to turn on those hoods to suck the chemical vapors. But if they forget to turn on the hood, toxic vapors can spread inside the lab. This system will allow the responsible teacher to get an alert if Ammonia (which is one toxic gas) is sensed outside those hoods.
Step 1: Materials
For this project you'll need :
- 2x Ammonia Sensor MQ-137 (or as much as you want)
- 1x Arduino Uno (it has one serial port)
- 1x Genuino Mega 2560 (or other boards with 2 or more serial ports)
- 2x HC-05 Bluetooth modules
- 1x Raspberry Pi model 3B
- 1x Battery 9V
- Wires, cables and resistors
Step 2: Getting Data From the Sensors
Sensors are wired to an arduino Uno.
To realize this application, this sensor must be powered. To do this, the 5V and the mass of the arduino card are used. In addition, the analog input A0 makes it possible to recover the resistance value given by the sensor. Moreover, the Arduino is powered
Unfortunately, those sensors don't provide a linear output proportional to the ammonia concentration. Those sensors are made of an electrochemical cell, changing the resistance related to the concentration. The resistance goes up with the concentration.
The real issue with these, is that they are made to measure different type of gas, and the electrochemical cell react strangely. For example, for the same sample of liquid ammonia, both sensors provide different output. They are also quite slow.
Either way, the resistance provided by the sensor is converted to 0-5V and then to "ppm" (= parts per million, it's a relevant unit to measure gas concentration) by the arduino, using a trend curve and its equation is provided in the documentation of these sensors.
Step 3: Sending Data Via Bluetooth
In order to set the sensors at various places in the laboratory, they are directly connected to an Arduino board powered by a 9V battery. And to communicate the results of ammonia in the air to the Rapsberry card, bluetooth modules are used. The first card connected directly to the sensor board is called slave.
To use the bluetooth modules, they first need to be configured. For that purpose, connect the EN pin of the module to the 5V (you should see the led blinking every 2 sec) and press the button on the module. Telecode an empty code in the arduino and connect the RX pin of the module to the TX pin of the arduino and vice versa. After that, go in the serial monitor, choose the right Baud rate (for us, it was 38400 Br) and write AT.
If the serial monitor show "Ok" then you entered in the AT mode. You can now set the module as slave or Master. You can find below a pdf with all the command for the AT mode.
The following website show the steps the go in AT mode for our bluetooth module: http://www.dsdtech-global.com/2017/08/hc-05-datas...
The bluetooth module use 4 pins of the arduino, 3.3V with a voltage divider, ground, the TX and RX pins. Using the TX and RX pins means that the data are transferred by the serial port of the card.
Don't forget that the pin RX of the bluetooth module is connected to the TX pin of the Arduino and vice versa.
You should see both leds of the bluetooth modules blinking 2 times about every 2 seconds when they are connected to each other.
Both the receipt and the send code are realized on the same card and attached here after.
Step 4: Receiving Data and Transfert to the Raspberry Pi
This part of the project is done by the arduino mega.
This card is wired to a bluetooth module, configured to receive the data, and the raspberry pi. It's called Master.
In this case, the bluetooth module use one serial port, and the data are transferred to the raspberry pi using another serial port. That is why we need a card with 2 or more serial port.
The code is almost the same as before.
Step 5: Logging Data and Alert Feature
The raspberry pi will log the data every 5 seconds (for example, may vary) in a .csv file and save it inside the capacity of the sd card.
At the same time, the raspberry check if the concentration is not too high (over 10ppm for example, may vary) and send an alert e-mail if it is the case.
But before the raspberry can send the email, it needs a little configuration. For this purpose, go in the file "/etc/ssmtp/ssmtp.conf" and change the parameters following your personnal information. You can find an example below (code_raspberry_conf.py).
As far as the main code (blu_arduino_print.py) is concerned, it needs to import some libraries like "serial" to work with the USB communication port or the library "ssmtp" to send the email.
Sometimes, there can be an error when sending the data by Bluetooth. Indeed, the raspberry can only read a line when there is a number terminated with \n. However, the raspberry can sometimes receive something else like "\r\n" or just "\n". So, to avoid the program to shutdown, we used the Try - Except command.
After, it's just a bunch of "if" conditions.
Step 6: Making Cases
- 1 junction box of 220*170*85 mm
- 1 junction box of 153*110*55 mm
- Green ertalon 500*15*15 mm
- 1.5 metres electric cables
- 2 bluetooth modules
- 1 Raspberry
- 1 Arduino Mega
- 1 Genuino
- 9v battery
- 1 Raspberry / Arduino connection cable
- 2 resistors of 2K ohm
- 2 resistors of 1K ohm
- Soldering machine
- Drilling machine
- Drilling bits
- Cutting pliers
We started from two electrical junction boxes in which cuts were made. First, the realization of the sensor/emitter element : two supports to fix the Genuino card where made in green ERTALON. Then, it was necessary to cut the lid to put the ammonia sensor and fix it. The cables were connected from the sensor to the Genuino card. After that we put the bluetooth module on the box, soldered the cables and connected them with the card. Finally, the power supply with a 9V battery was integrated and wired. When the sensor was finished, we were able to start working on the receiver. For this, the same way as before, we started by making the supports for the two electronic cards (the Raspberry and the Arduino mega). Then we cut out the slots for cables and plugs from the Raspberry. The bluetooth module was fixed in the same way as before. Then, the holes were drilled on the top of the box to allow ventilation for the two electronic boards and to avoid any risk of overheating. To finish this step, all the cables were connected and the project only needs to be powered and tested.
Step 7: Improvements
In terms of improvement, several points can be evoked:
- The choice of more performant sensor. Indeed, they do not detect quickly the appearance of ammonia in the air. Add to this that once saturated with ammonia, they need a certain time to get rid of it.
- Used an arduino card directly having a Bluetooth module as specified at the base of our project. Unfortunately, Genuino 101 is no longer available on the European market.
- Integrate a display in the box where the sensor is located to know the concentration in a continuous way
- Ensure the automatic construction of a graph from the data stored on the csv file.