Project 2: MQ-2 and MQ-9

I teamed up with a student who had previously researched a MQ-7 smoke sensor. Although they are in the same family of smoke sensors, they work slightly different in terms of the internals of them. We had set them up differently to test them, but it did not take much to put them onto the same Arduino bread board in the same format. The aim of this project was to produce a smoke alarm, which would output differently depending on the input. For example, different LED display, sound, and motion.

 

Experiment 1:

The first step was to combine both sets of code, and put them in series in the same circuit. This was so that we could record two sets of data from one circuit.

This was the first attempt:

 

int redLed = 12;

int greenLed = 11;

int buzzer = 10;

int smokeA0 = A5;

int AOUTpin = A4;

int DOUTpin = 8;

 

int sensorThres = 400;

 

void setup() {

  pinMode(redLed, OUTPUT);

  pinMode(greenLed, OUTPUT);

  pinMode(buzzer, OUTPUT);

 

  pinMode(smokeA0, INPUT);

 

  pinMode(DOUTpin, INPUT);

 

  Serial.begin(115200);

 

 

}

 

void loop() {

  int analogSensor = analogRead(smokeA0);

  int value = analogRead(AOUTpin);

  int limit = digitalRead(DOUTpin);

 

  Serial.print(“Pin A0: “);

  Serial.println(analogSensor);

  Serial.print(“CO value: “);

  Serial.println(value);

  Serial.print(“Limit: “);

  Serial.println(limit);

  delay(100);

 

  if (analogSensor > sensorThres)

  {

    digitalWrite(redLed, HIGH);

    digitalWrite(greenLed, LOW);

    tone(buzzer, 2000, 600);

    delay(100);

    tone(buzzer, 1000, 600);

   

  }

  else

  {

    digitalWrite(redLed, LOW);

    digitalWrite(greenLed, HIGH);

    noTone(buzzer);

   

  }

   if (value > 100)

  {

    digitalWrite(redLed, HIGH);

    digitalWrite(greenLed, LOW);

    tone(buzzer, 1000, 600);

    delay(100);

    tone(buzzer, 5000, 600);

   

  }

  else

  {

    digitalWrite(redLed, LOW);

    digitalWrite(greenLed, HIGH);

    noTone(buzzer);

   

  }

}

 

MQ-2, MQ-7, Buzzer

20180224_180249 (1)

 

However, it did not behave as we expected. The readings were not clear, and the gas detection did not seem to have a pattern. Consequently, we decided to rewrite a new code instead of taking syntax from previous ones. This resulted in us achieving more effective and clearer instructions. The following images and video show what we did.

 

Experiment 2:

 

This was our improved code:

int redLed = 12;

int greenLed = 11;

int buzzer = 10;

int smokeA0 = A5;

int AOUTpin = A4;

int DOUTpin = 8;

 

int sensorThres = 400;

 

void setup() {

 

pinMode(redLed, OUTPUT);

pinMode(greenLed, OUTPUT);

pinMode(buzzer, OUTPUT);

pinMode(smokeA0, INPUT);

pinMode(DOUTpin, INPUT);

Serial.begin(115200);

 

}

 

void loop() {

int analogSensor = analogRead(smokeA0);

int value = analogRead(AOUTpin);

int limit = digitalRead(DOUTpin);

 

Serial.print(“Pin A0: “);

Serial.println(analogSensor);

Serial.print(“CO value: “);

Serial.println(value);

Serial.print(“Limit: “);

Serial.println(limit);

delay(100);

if (analogSensor > sensorThres && value > 80) // both triggered

{

digitalWrite(redLed, HIGH);

digitalWrite(greenLed, LOW);

tone(buzzer, 1000, 600);

delay(100);

tone(buzzer, 5000, 600);

 

}

else if (analogSensor < sensorThres && value > 80) //CO trigger4ed

{

}

else if (analogSensor > sensorThres && value < 80) //smoke triggered

{

//outpu goes here

}

else if  (analogSensor < sensorThres && value < 80)

{

digitalWrite(redLed, LOW);

digitalWrite(greenLed, HIGH);

noTone(buzzer);

}

}

 

This code turned the red light and a series of tones, when either of the sensors detected a gas. A green LED was present when nothing was detected. This function does not determine what gas or sensor has detected anything, which would not be very beneficial in actual use.

 

 

Experiment 3:

 

For this reason, we wanted to increase the number of LED’s that would display each detection. Having one colour for neutral, one for each sensor, as well as multiple for both sensor detection. Another improvement from the last configuration, is an increase in resistance over each output component. We reduced the brightness of the LED’s and the amplitude of the buzzer to make it more bearable when testing the circuit. The following image and video show the results.

 

MQ-2, MQ-7, Buzzer, White

20180224_205410

 

This video demonstrates all the components and their functions in use.

 

 

The following code achieves these functions.

 

int redLed = 12;

int greenLed = 11;

int whiteLed = 7;

int buzzer = 10;

int smokeA0 = A5;

int AOUTpin = A4;

int DOUTpin = 8;

 

int sensorThres = 400;

 

void setup() {

// Output setups, LED, buzzer, motor

pinMode(redLed, OUTPUT);

pinMode(greenLed, OUTPUT);

pinMode(whiteLed, OUTPUT);

pinMode(buzzer, OUTPUT);

pinMode(smokeA0, INPUT);

pinMode(DOUTpin, INPUT);

Serial.begin(115200);

}

void loop() {

// Defining the read intergers for the input

int analogSensor = analogRead(smokeA0);

int value = analogRead(AOUTpin);

int limit = digitalRead(DOUTpin);

 

//Output of serial print and LED, buzzer, motor

Serial.print(“Pin A0: “);

Serial.println(analogSensor);

Serial.print(“CO value: “);

Serial.println(value);

Serial.print(“Limit: “);

Serial.println(limit);

delay(100);

if (analogSensor > sensorThres && value > 100) // Both triggered

{

digitalWrite(redLed, HIGH);

digitalWrite(greenLed, HIGH);

digitalWrite(whiteLed, LOW);

tone(buzzer, 1000, 600);

delay(100);

tone(buzzer, 5000, 600);

}

else if (analogSensor < sensorThres && value > 100) // MQ-7 triggered

{

digitalWrite(redLed, HIGH);

digitalWrite(greenLed, LOW);

digitalWrite(whiteLed, LOW);

tone(buzzer, 1000, 600);

delay(100);

tone(buzzer, 5000, 600);

}

else if (analogSensor > sensorThres && value < 100) // MQ-2 triggered

{

digitalWrite(redLed, LOW);

digitalWrite(greenLed, HIGH);

digitalWrite(whiteLed, LOW);

tone(buzzer, 1000, 600);

delay(100);

tone(buzzer, 5000, 600);

digitalWrite(redLed, LOW);

digitalWrite(redLed, HIGH);

}

else if  (analogSensor < sensorThres && value < 100) // Neutral setup

{

digitalWrite(redLed, LOW);

digitalWrite(greenLed, LOW);

digitalWrite(whiteLed, HIGH);

noTone(buzzer);

}

}

 

Using a standard lighter proved to be a challenge to detect specific gases, as they do not always contain just butane. The readings on the Arduino Serial monitor show definite readings of each gas, so we know that they work and behave as we want the code to. However, we think the lighter also contains carbon monoxide, so the red LED also reacts to the lighter. Breathing on the sensor is another method of getting a reading from the MQ-7 sensor, and only the red LED responds.

 

Experiment 4:

The next step of this project is to incorporate an additional output, which will be a motor. We plan on connecting it to a fan, which will blow air through a whistle. This will be covered in the next blog post.

I set up a simple circuit consisting of a DC Motor, Diode and Transistor. The code allowed me to control the RPM of the motor, by typing a number between 150 and 255 into the Arduino Serial monitor.

 

20180224_202008

 

The following code is what I used to control the motors speed:

 

int motorPin = 3;

void setup()

{

pinMode(motorPin, OUTPUT);

Serial.begin(9600);

while (! Serial);

Serial.println(“Speed 0 to 255”);

}

void loop()

{

if (Serial.available())

{

int speed = Serial.parseInt();

if (speed >= 0 && speed <= 255)

{

analogWrite(motorPin, speed);

}

}

}

 

The next stage was to combine the original MQ-2 and MQ-7 breadboard and code with the DC Motor circuit and code. The aim was to be able to set a different motor speed as an output for each different gas readings. A different speed for the MQ-2, MQ-7 and neural set up. This along with the separate LED displays would clearly determine what gas is being detected.

The image shows the Fritzing breadboard of the final set up.

 

MQ-2, MQ-7 and DC Motor

 

The next image shows the real circuit, as well as an additional 3D printed fan to clearly show the rotation of the motor.

 

Fan

The video shows the circuit in action, using all the components.

 

The red LED is shown when the MQ-7 sensor is detecting carbon monoxide, and the green LED lights up when butane is detected. The lighter used contains a combination of flammable gases, which explains why both sensors react to it. But when the fuel is released from the lighter in the direction of each sensor, it is clear that they are responding independently.

The readings on the Arduino Serial monitor also support these observations.

 

MQ-7 (CO value) Detected:

Both

 

MQ-7 (CO value) Detected:

MQ-7

 

Both Detected:

MQ-2

 

As well as the additional motor functions, the following code has also been improved from the last by making the tones more distinguishable between each sensor that is detecting a gas.

 

int redLed = 12;

int greenLed = 11;

int whiteLed = 7;

int buzzer = 10;

int smokeA0 = A5;

int AOUTpin = A4;

int DOUTpin = 8;

int motorPin = 3;

int sensorThres = 100;

void setup() {

// Output setups, LED, buzzer, motor

pinMode(redLed, OUTPUT);

pinMode(greenLed, OUTPUT);

pinMode(whiteLed, OUTPUT);

pinMode(buzzer, OUTPUT);

pinMode(smokeA0, INPUT);

pinMode(DOUTpin, INPUT);

pinMode(motorPin, OUTPUT);

Serial.begin(115200);

}

void loop() {

// Defining the read intergers for the input

int analogSensor = analogRead(smokeA0);

int value = analogRead(AOUTpin);

int limit = digitalRead(DOUTpin);

int speed = Serial.parseInt();

//Output of serial print and LED, buzzer, motor

Serial.print(“Pin A0: “);

Serial.println(analogSensor);

Serial.print(“CO value: “);

Serial.println(value);

Serial.print(“Limit: “);

Serial.println(limit);

Serial.println(“Speed 0 to 255”);

delay(100);

if (analogSensor > sensorThres && value > 100) // Both triggered

{

digitalWrite(redLed, HIGH);

digitalWrite(greenLed, HIGH);

digitalWrite(whiteLed, LOW);

tone(buzzer, 1000, 600);

delay(50);

tone(buzzer, 1000, 600);

delay(50);

tone(buzzer, 1000, 600);

digitalWrite(motorPin, 255);

}

else if (analogSensor < sensorThres && value > 100) // MQ-7 triggered

{

digitalWrite(redLed, HIGH);

digitalWrite(greenLed, LOW);

digitalWrite(whiteLed, LOW);

tone(buzzer, 500, 300);

digitalWrite(motorPin, 150);

}

else if (analogSensor > sensorThres && value < 100) // MQ-2 triggered

{

digitalWrite(redLed, LOW);

digitalWrite(greenLed, HIGH);

digitalWrite(whiteLed, LOW);

tone(buzzer, 2000, 1200);

digitalWrite(motorPin, 150);

}

else if (analogSensor < sensorThres && value < 100) // Neutral setup

{

digitalWrite(redLed, LOW);

digitalWrite(greenLed, LOW);

digitalWrite(whiteLed, HIGH);

noTone(buzzer);

digitalWrite(motorPin, 0);

}

}

 

Now that I have successfully built the circuit with all the necessary components along with the correct code, I now need to incorporate it into a housing with physical components that respond to the outputs.

We have all the code behaving as we want it to, and the circuit responding to it we expected. The inputs and outputs are clearly observable, and moreover easily controlled. Unfortunately, the motor does not provide us with enough torque, and as a result does not turn the fan with a high enough velocity. Therefore, the whistle is not provided with enough directed air pressure to pass through it to make a noise. Despite this, the output of a motor turning could lead to a range of different functions to alert a passer-by that a flammable gas such as butane, or carbon monoxide is present.

Box BaseBox Lid

 

Because of how we have designed and structured the breadboard and Arduino, we had to then distance the sensors, LEDs and motor, so they were easily visible. We did this by taking them off and connecting them with female to male cables. We then designed the box around this arrangement. It featured a lid with six holes, and a base to contain the circuitry.

 

Box open

 

In hindsight, we should of positioned the holes for each smoke sensor on opposing sides of the box. This would of made testing far easier and proving of concept far easier.

 

Final .jpg

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s