Introduction
Hey, hello everyone. I’m Gary Lu, a geek from China. In this passage I will show you how to design and build an air quality monitor with some simple parts. With different kits, you can also add some other functions you want, like an alarm function or else. I have a hydrogen and a CO monitor, so this sample will be used to monitor these two kinds of common explosive gas. What’s is more fantastic is that apart from displaying the results on a LCD screen, it will also save the results and the time in a SD card you plug in the board. OK, without further ado, let’s start.
Hardware:
Arduino kit:
Sensor:
Others:
Software:
Tutorial: 1. Connect the parts: First you need to plug the IO expansion shield on the Arduino board; and then connect the expansion board, clock module and the LCD screen, then plug the SD module on the expansion board. Attach the buzzer on to the digital pin 4 on the expansion board. Then attach the hydrogen sensor and the CO sensor on the analog pin 0 and pin 1. And it is a necessary knowledge that when attaching them the black wire means the ground, red for the power, green for the digital signal and blue for analog. Be careful when connecting.
Pay attention when connecting the expansion board and the clock module, the pin order on the expansion board is (vcc, GND, SCL, SDA). So it necessary for you to repack your IIC cable. After connecting them all, I power it with a 12V adaptor, but the LCD screen doesn’t work well. After checking the board, I found out it was because of the sensors. The sensors need to be heated while working and only one adaptor is far from enough. So I make a new power with a 5V adaptor and a self-made USB cable. After attaching it on the analog pin 3, everything turns out well.
2. Software Libraries? In this sample we will only use the Arduino IDE as our writing software. You can download it from the official web site of Arduino. And we will use two extra libraries in total, one is for the LCD screen and another is for the clock module. You can download it from the wiki page of these two products. After downloading the libraries. You need to put the library files into your Arduino-1.0.x/libraries folder. After doing this, you can open the Arduino IDE and kick on the Sketch-Import library to check in whether you have setup the library successfully. Function preparing? As we all know, the message an analog pin can read is a round number from 0 to 1023, and it is the same with the analog gas sensor. So how can we change this number into the real content of the gas we want to know? To do this, we have to understand the principle of the gas sensor we use. The sensor we used is the MQ serial sensor. Log on their official website and we can figure out the theory. Let’s take the hydrogen sensor as an example.
Con1 is the signal port, and it send out the information of the electric potential. Con2 is the ground and con3 is VCC. The core of the sensor is a resistance between A and B. Its resistance changes with the content of different gas when heated. The port H (used for heating) is attached between the VCC and GND. You can see that the con1 is connected between the resistance and an adjustable resistor. In this way, we can know the ratio of the resistance between AB and the adjustable resistor. And if we know the resistance of the adjustable, we will also know the resistance of the sensor. The next step is to change the resistance into the content we want.
It shows the relationship between the Rs/Ro and the content of the hydrogen. We can see that all the lines are nearly straight. So we can view it as a linear function. And the X-axis and the Y-axis represent the logarithm of the ratio and the content of the hydrogen. So we can define a function with a point and the slope of the line. Look at the blue line which represents the hydrogen. I choose two point from it, (200, 8.5) and (10000, 0.03). In logarithm term, (2.3, 0.93) and (4,-1.52). With little calculation we can know the slope is -1.44. So we record an array of [2.3, 0.93, -1.44]. It is the same with the CO sensor. In the chart, we know the Ro is the sensor resistance at 1000ppm H¬2 in the clean air. But we can’t make a perfect 1000ppm H2 atmosphere. But we can calculate it with the help red line in the chart. It is a horizontal line and it means that the Rs in the clean air is 70 times of the Ro. We can measure the Rs in the clean air. So the problem is solved, but we need to calibrate the Ro at first. OK, that’s all for the function preparing. Program (main step):/*Show the welcome page and preheat the sensor for 15 minutes*/ lcd.setCursor(0,0); lcd.print("Card initialized."); delay(2000); for(int i=0;i<100;i++) { lcd.setCursor(0,0); lcd.print("Welcome to DFRobot! "); lcd.setCursor(0,1); lcd.print(" Air Quality Test"); lcd.setCursor(0,2); lcd.print(" Please wait."); lcd.setCursor(20,1); lcd.print(" Warming: "); lcd.print(i); lcd.print("%"); delay(50); } lcd.clear(); /*Calibrating the Ro*/ lcd.setCursor(0,0); lcd.print(" Calibrating.."); int i; float val1 = 0; float val2 = 0; for (i=0;i<50;i++) { int a = analogRead(H2_pin); int b = analogRead(CO_pin); val1 += RL_H2*(1023-a)/a;//Turn the number into the resistance of Rs val2 += RL_CO*(1023-b)/b; delay(500); }//Calibrating 50 times in 25 seconds. val1 = val1/50; val1 = val1/H2_air_ratio; val2 = val2/50; val2 = val2/CO_air_ratio; RO_H2 = val1; RO_CO = val2; } /*Read the signal from the sensor and turn it into the content*/ int i; float val1 = 0; float val2 = 0; for (i=0;i<5;i++) { int c = analogRead(H2_pin); int d = analogRead(CO_pin); val1 += RL_H2*(1023-c)/c; val2 += RL_CO*(1023-d)/d; delay(200); }//read 5 times in 1 second val1 = val1/5; val2 = val2/5; float Rs_H2 = val1; float Rs_CO = val2; result_H2 = pow(10,(((log(Rs_H2/RO_H2)-H2Curve[1])/H2Curve[2]) + H2Curve[0])); result_CO = pow(10,(((log(Rs_CO/RO_CO)-COCurve[1])/COCurve[2]) + COCurve[0])); /*Record the time and result with the SD card*/ File dataFile = SD.open(" results.txt", FILE_WRITE); if (dataFile) { int i; for(i=6;i>0;i--) { dataFile.print(rtc[i]); dataFile.print(" "); } dataFile.println(rtc[0]); dataFile.print("H2: "); dataFile.print(result_H2); dataFile.println(" ppm"); dataFile.print("CO: "); dataFile.print(result_CO); dataFile.println(" ppm"); dataFile.close(); }Summary: All right, all above is the steps I made my own air quality monitor. With some other parts you can do anything you want. I have an idea to add a GPS module. In this way I can not only record the time but the location as well. OK, what to achieve is depending on your imagination. I am always believing in one principle: nothing is uninstructable for a geek. This is my first passage, and thank you for reading. Thank the DRFobot company for providing me with the stuff. Related link:
Contact information: Gary Lu [email protected]