UNO R3 with multiple MQ-2 Gas sensors getting unstable readings

Gretting!

I'm a beginner at Arduino.

I'm going to assemble a UNO board with four MQ-2 sensors for my project. We are having difficulty wiring the MQ-2 (smoke).

Currently, the UNO is a PC USB port, and the sensor is supplying external 5V power. The sensors are using ports A0, A1, A2, and A3. If only one sensor is left, the analog readings will be normal. On the other hand, multiple sensors make analog readings very irregular.

Reading is unstable when power is supplied even with a resistor.

Do you have any ideas?

Thanks in advance.


#include <LedControl.h> 
// #include <DFPlayer_Mini_Mp3.h>
// #include <SoftwareSerial.h>

LedControl lc1 = LedControl(13, 12, 11, 1); 
LedControl lc2 = LedControl(10, 9, 8, 1);
LedControl lc3 = LedControl(7, 6, 5, 1);
LedControl lc4 = LedControl(4, 3, 2, 1); 

int gas1 = A0; 
int gas2 = A1;
int gas3 = A2;
int gas4 = A3;

int gasValue1; 
int gasValue2;
int gasValue3;
int gasValue4;

byte Arrow_up[] = 
{
  B00011000,
  B00111100,
  B01111110,
  B11111111,
  B00111100,
  B00111100,
  B00111100,
  B00111100
};

byte Arrow_down[] = 
{
  B00111100,
  B00111100,
  B00111100,
  B00111100,
  B11111111,
  B01111110,
  B00111100,
  B00011000
};

byte prohibition[] = 
{
  B10000001,
  B01000010,
  B00100100,
  B00011000,
  B00011000,
  B00100100,
  B01000010,
  B10000001
};


void display_Arrow_up1(); 
void display_Arrow_down1();
void display_prohibition1();
void display_Arrow_up2();
void display_Arrow_down2();
void display_prohibition2();
void display_Arrow_up3();
void display_Arrow_down3();
void display_prohibition3();
void display_Arrow_up4();
void display_Arrow_down4();
void display_prohibition4();

void setup()
{
  lc1.shutdown(0, false); 
  lc1.setIntensity(0, 5); 
  lc1.clearDisplay(0); 

  lc2.shutdown(0, false);
  lc2.setIntensity(0, 5);
  lc2.clearDisplay(0);
  
  lc3.shutdown(0, false);
  lc3.setIntensity(0, 5);
  lc3.clearDisplay(0);
  
  lc4.shutdown(0, false);
  lc4.setIntensity(0, 5);
  lc4.clearDisplay(0);
  
  Serial.begin (9600);

  /*mp3_set_serial (Serial);
  mp3_set_volume (30);*/

  pinMode(gas1, INPUT); 
  pinMode(gas2, INPUT);
  pinMode(gas3, INPUT);
  pinMode(gas4, INPUT);
}

void loop()
{
  float gasprint1 = analogRead(gas1);
  float gasprint2 = analogRead(gas2);
  float gasprint3 = analogRead(gas3);
  float gasprint4 = analogRead(gas4);

  gasValue1 = gasprint1;
  gasValue2 = gasprint2;
  gasValue3 = gasprint3;
  gasValue4 = gasprint4;

  Serial.print("sensor1 : ");
  Serial.println(gasValue1);
  Serial.print("sensor2 : ");
  Serial.println(gasValue2);
  Serial.print("sensor3 : ");
  Serial.println(gasValue3);
  Serial.print("sensor4 : ");
  Serial.println(gasValue4);
  delay(3000);
  
  if(gasValue1 < 900 && gasValue2 < 900 && gasValue3 < 900 && gasValue4 >= 900) 
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_down3(0);
    display_Arrow_down4(0);
    delay(1000); 
  } 

  else if(gasValue1 < 900 && gasValue2 < 900 && gasValue3 >= 900 && gasValue4 < 900) 
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_down3(0);
    display_Arrow_up4(0);
    delay(1000); 
  }

  else if(gasValue1 < 900 && gasValue2 >= 900 && gasValue3 < 900 && gasValue4 < 900) 
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000); 
  }

  else if(gasValue1 >= 900 && gasValue2 < 900 && gasValue3 < 900 && gasValue4 < 900)
  {
    display_Arrow_down1(0);
    display_Arrow_up2(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000);
  }


  else if(gasValue1 < 900 && gasValue2 < 900 && gasValue3 >= 900 && gasValue4 >= 900)
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_prohibition3(0);
    display_Arrow_up4(0);
    delay(1000); 
  }
/*
  else if(gasValue1 < 900 && gasValue2 >= 900 && gasValue3 < 900 && gasValue4 >= 900) 
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_down3(0);
    display_Arrow_up4(0);
    delay(1000); 
  }

  else if(gasValue1 >= 900 && gasValue2 < 900 && gasValue3 < 900 && gasValue4 >= 900)
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_down3(0);
    display_Arrow_up4(0);
    delay(1000); 
  }
*/
  else if(gasValue1 < 900 && gasValue2 >= 900 && gasValue3 >= 900 && gasValue4 < 900)
  {
    display_Arrow_down1(0);
    display_prohibition3(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000); 
  } 
/*
  else if(gasValue1 >= 900 && gasValue2 < 900 && gasValue3 >= 900 && gasValue4 < 900) 
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000);
  }
*/
  else if(gasValue1 >= 900 && gasValue2 >= 900 && gasValue3 < 900 && gasValue4 < 900) 
  {
    display_prohibition1(0);
    display_Arrow_up2(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000);
  }

  else if(gasValue1 < 900 && gasValue2 >= 900 && gasValue3 >= 900 && gasValue4 >= 900) 
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_prohibition3(0);
    display_Arrow_up4(0);
    delay(1000);
  }

  else if(gasValue1 >= 900 && gasValue2 >= 900 && gasValue3 >= 900 && gasValue4 < 900)
  {
    display_Arrow_down1(0);
    display_prohibition2(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000); 
  }

  else if(gasValue1 >= 900 && gasValue2 >= 900 && gasValue3 >= 900 && gasValue4 >= 900)
  {
    display_Arrow_down1(0);
    display_Arrow_down2(0);
    display_Arrow_up3(0);
    display_Arrow_up4(0);
    delay(1000);
  }

  else 
  {
    display_prohibition1(1);
    display_prohibition2(1);
    display_prohibition3(1);
    display_prohibition4(1);
  } 
}

void display_Arrow_up1(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc1.setRow(0, i, Arrow_up[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc1.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_up2(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc2.setRow(0, i, Arrow_up[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc2.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_up3(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc3.setRow(0, i, Arrow_up[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc3.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_up4(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc4.setRow(0, i, Arrow_up[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc4.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_down1(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc1.setRow(0, i, Arrow_down[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc1.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_down2(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc2.setRow(0, i, Arrow_down[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc2.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_down3(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc3.setRow(0, i, Arrow_down[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc3.setRow(0, i, B00000000);
    }
 }
}

void display_Arrow_down4(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc4.setRow(0, i, Arrow_down[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc4.setRow(0, i, B00000000);
    }
 }
}

void display_prohibition1(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc1.setRow(0, i, prohibition[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc1.setRow(0, i, B00000000);
    }
 }
}

void display_prohibition2(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc2.setRow(0, i, prohibition[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc2.setRow(0, i, B00000000);
    }
 }
}

void display_prohibition3(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc3.setRow(0, i, prohibition[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc3.setRow(0, i, B00000000);
    }
 }
}

void display_prohibition4(int a)
{
 if(a == 0)
 {
  for (int i = 0; i < 8; i++)
  {
    lc4.setRow(0, i, prohibition[i]);
  }
 }
 else
 {
  for (int i = 0; i < 8; i++)
    {
      lc4.setRow(0, i, B00000000);
    }
 }
}

Much of your program makes no sense, for instance in the above code you read an analog value, which is an int, and immediately convert it to a float. Then you convert the float back to an int and display it. Why?

Four times 160mA + 50 mA for the Uno = 690mA.
Absolute max current draw through USB is 500mA (a 500mA fuse).
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.