I've been working on a project that need two sensors(mq2 and mpu6050), I got each sketches from the internet and I managed to work the mq2 but for the mpu6050, I'm still not sure if its working. The thing is I want the two sensors to work on their own but in one sketch. When the mq2 senses some gas, I want the LED and BUZZER turned on and the only way to turn it off is by resetting the arduino. For the MPU6050, it's the same with the mq2, when the MP6050 is triggered, I want the other LED and the same buzzer turn on, again the only way to turn it off is by resetting the arduino. I want to combine the se two skectches in one. But when I combined it, the circuit won't work. Could someone help me ?

Here is the code that I modified and combined:

#include <Wire.h>
#include <MPU6050.h>

#define minval -5
#define maxval 3

MPU6050 mpu;
//////////////////////////////////////////////////////////////////

//for mq2

int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
// Your threshold value
int sensorThres = 100;

//////////////////////////////////////////////////////////////////

void setup(){
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  mpu.setThreshold(3); 
}

//////////////////////////////////////////////////////////////////

void loop(){
  // for mq2

  int analogSensor = analogRead(smokeA0);

  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    tone(buzzer, 1000);
  }
  else{
  digitalWrite(greenLed, HIGH);
  }
  // for MPU6050

  Vector rawGyro = mpu.readRawGyro();
  Vector normGyro = mpu.readNormalizeGyro();
  if (normGyro.XAxis > maxval ||
      normGyro.XAxis < minval &&
      normGyro.YAxis > maxval ||
      normGyro.YAxis  < minval &&
      normGyro.ZAxis > maxval || 
      normGyro.ZAxis  < minval)
  {
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
  }
  else
  {
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }

}

Here is the code for the mq2:

int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
// Your threshold value
int sensorThres = 150;

void setup() {
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
}

void loop() {
  int analogSensor = analogRead(smokeA0);

  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    tone(buzzer, 1000);
  }
  else{
  digitalWrite(greenLed, HIGH);
  delay(100);
  }
}

Here is the code for the MPU6050(I removed some lines when I put it at the combined sketch because I dont need the LCD code, dunno if I did it correct):

#include <Wire.h>
#include <MPU6050.h>

#define minval -5
#define maxval 3

MPU6050 mpu;


void setup()
{       
  Serial.begin(115200);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);

  delay (2000);

  // Initialize MPU6050
  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  { 
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }

  mpu.setThreshold(3); 
  // Check settings
  checkSettings();
}



void checkSettings()
{
  Serial.println();
  Serial.print(" * Sleep Mode:        ");
  Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
  Serial.print(" * Clock Source:      ");
  switch(mpu.getClockSource())
  {
    case MPU6050_CLOCK_KEEP_RESET:
      Serial.println("Stops the clock and keeps the timing generator in reset"); 
      break;
    case MPU6050_CLOCK_EXTERNAL_19MHZ:
      Serial.println("PLL with external 19.2MHz reference"); 
      break;
    case MPU6050_CLOCK_EXTERNAL_32KHZ:
      Serial.println("PLL with external 32.768kHz reference");
      break;
    case MPU6050_CLOCK_PLL_ZGYRO:
      Serial.println("PLL with Z axis gyroscope reference");
      break;
    case MPU6050_CLOCK_PLL_YGYRO:
      Serial.println("PLL with Y axis gyroscope reference");
      break;
    case MPU6050_CLOCK_PLL_XGYRO:
      Serial.println("PLL with X axis gyroscope reference");
      break;
    case MPU6050_CLOCK_INTERNAL_8MHZ:
      Serial.println("Internal 8MHz oscillator"); 
      break;
  }

  Serial.print(" * Gyroscope:         ");
  switch(mpu.getScale())
  {
    case MPU6050_SCALE_2000DPS:
      Serial.println("2000 dps");
      break;
    case MPU6050_SCALE_1000DPS:
      Serial.println("1000 dps");
      break;
    case MPU6050_SCALE_500DPS:
       Serial.println("500 dps");
       break;
    case MPU6050_SCALE_250DPS:
       Serial.println("250 dps");
       break;
  }

  Serial.print(" * Gyroscope offsets: ");
  Serial.print(mpu.getGyroOffsetX());
  Serial.print(" / ");
  Serial.print(mpu.getGyroOffsetY());
  Serial.print(" / ");
  Serial.println(mpu.getGyroOffsetZ());
  Serial.println();
}

void loop()
{
  Vector rawGyro = mpu.readRawGyro();
  Vector normGyro = mpu.readNormalizeGyro();
  Serial.print(" Xraw = ");
  Serial.print(rawGyro.XAxis);
  Serial.print(" Yraw = ");
  Serial.print(rawGyro.YAxis);
  Serial.print(" Zraw = ");
  Serial.println(rawGyro.ZAxis); 

  if (normGyro.XAxis > maxval ||
      normGyro.XAxis < minval &&
      normGyro.YAxis > maxval ||
      normGyro.YAxis  < minval &&
      normGyro.ZAxis > maxval || 
      normGyro.ZAxis  < minval)
  {
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    delay(300);
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    delay(300);
    delay (1000);
  }
  else
  {
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }
  Serial.print(" Xnorm = ");
  Serial.print(normGyro.XAxis);
  Serial.print(" Ynorm = ");
  Serial.print(normGyro.YAxis);
  Serial.print(" Znorm = ");
  Serial.println(normGyro.ZAxis);
  delay(10);
}
  • The i2c bus uses a4 and a5 for sda and scl. You can still use a0 up to a3 for the mq2 sensor. With some luck, nothing is broken yet, but keep in mind that the mpu6050 is a 3.3v sensor. – Jot Dec 1 at 15:52
  • only you can decide if your sketch is working ..... if it does something that you do not want it to do, then it is not working ....... not working is a meaningless description of the program behavior ...... if you want real help then you should describe exactly what the program does and what you want it to do – jsotola Dec 1 at 20:03
  • Ok, I'll edit my post – Hamdanie Impa Dec 2 at 2:44
  • @jsotola could you help me ? – Hamdanie Impa Dec 2 at 2:54
  • @HamdanieImpa, did you understand my answer? You can read about the used pin numbers also on this page: arduino.cc/en/Reference/Wire – Jot Dec 2 at 11:34
up vote 0 down vote accepted

The Arduino Uno has the special functions on fixed pins.
The I2C bus, for example, uses pin A4 and A5.

In the Arduino Wire reference page is a table of the pin numbers for the Arduino boards.

The pins called "SDA" and "SCL" near the USB connector are connected to the pins "A4" and "A5" (for an Arduino Uno).
That means that if you use the I2C bus, you may not use pin "A4" and "A5" for something else.

Connect the MQ2 gas sensor to pin A0, A1, A2 or A3, and it should work.

The MPU-6050 is an old sensor, there are newer and better sensors with less noise.
The MPU-6050 is a 3.3V sensor, that means its power should be 3.3V and the I2C bus should have 3.3V levels.
The Arduino Uno has a microcontroller that runs at 5V, and therefor it has a I2C bus with 5V levels.
A I2C level shifter can be used to connect the Arduino Uno with the MPU-6050. You can also use 4k7 pullup resistors from SDA to 3.3V and from SCL to 3.3V. Those pullup resistors are often already on the MPU-6050 module.

The MPU-6050 will work (for some time) with a I2C bus that has 5V levels, it might even work when the MPU-6050 is powered with 5V directly to the chip. That will influence the data, and the sensor might get damaged. Newer 3.3V sensor chips are often faster damaged with 5V, but don't push your luck with the MPU-6050.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.