Help with my school projet (MQ2,MQ7,switch,buzzer)

Hello everyone

I'm very new to programming so I would like to ask you guys if you can help me solve this problem with my code:

For a school project, me and my team decided to build a gas measurement device, that can detect Propane(MQ2) and Carbonmonoxid(MQ7) and give an alarm (buzzer) if a certain threshold is reached. It should also have the function so seek the gases (the buzzer should ring faster the more gas there is).
The idea is to switch between these modes with a 3-way-switch. There should also be a Button to stop the buzzer for a certain amount of time. The PPM of the gases should be displayed on an OLED Display. If none of the switch buttons is pressed, it should silpy show the current PPM value on screen.

I have written the code and tried it out with a build on a breadboard.
The first problem is that the switch doesnt switch between the modes. I've tried alot of diferent things I saw here and on the internet but it doesnt seem to work.
The second problem is the alarm. It just doesnt ring even after the threshold is reached.

The measurement parts and the display parts of the code work well when testing them by themselfs.

Here is the scematic we made for the project.
Please note that some of the comments, names and texts in the scematic and in the code are in german.
grafik
J5 is the switch
J6 is the Button to stop the buzzer
The pins on the diagram and code arent the same as I changed them on the breadboard.

Here's the code:

#include <MQUnifiedsensor.h>

//OLED Einstellungen
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#define I2C_ADDRESS 0x3C
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1
SSD1306AsciiAvrI2c oled;

//Restliche Definitionen
#define placa "Arduino Nano"
#define pin1 A0 //Monoxid
#define type1 "MQ-7" //Monoxid
#define pin2 A1 //Propan
#define type2 "MQ-2" //Propan
#define Voltage_Resolution 5
#define ADC_Bit_Resolution 10
#define RatioMQ2CleanAir 9.83 //Kallibrieren
#define RatioMQ7CleanAir 27.5 //Kallibrieren

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

const int UmschalterCO (4);
const int UmschalterPropan (5);
const int Quittierung (2);
const int Lautsprecher (3);

int MaxPropan = 1000;
int MaxMonoxid = 30;

MQUnifiedsensor MQ2(placa, Voltage_Resolution, ADC_Bit_Resolution, pin2, type2);
MQUnifiedsensor MQ7(placa, Voltage_Resolution, ADC_Bit_Resolution, pin1, type1);
unsigned long oldTime = 0;


void setup() {
    Serial.begin(9600);
    pinMode(UmschalterCO,INPUT);
    pinMode(UmschalterPropan,INPUT);
    pinMode(Quittierung,INPUT);
    pinMode(Lautsprecher,OUTPUT);
    
    
    #if RST_PIN >= 0
    oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
  #else // RST_PIN >= 0
    oled.begin(&Adafruit128x64, I2C_ADDRESS);
  #endif // RST_PIN >= 0
    // Call oled.setI2cClock(frequency) to change from the default frequency.
    oled.setFont(System5x7);
    oled.clear();  
    
    MQ2.setRegressionMethod(1);
    MQ2.setA(658.71); MQ2.setB(-2.168);
    
    MQ2.init();

      Serial.print("Calibrating please wait.");
      float calcR0 = 0;
      for(int i = 1; i<=10; i ++)
      {
      MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
      calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
      Serial.print(".");
      }
      MQ2.setR0(calcR0/10);
      Serial.println("  done!.");
      
      if(isinf(calcR0)) {Serial.println("Warning: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
      if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
      /*****************************  MQ CAlibration ********************************************/ 

      MQ2.serialDebug(true);
    
    MQ7.setRegressionMethod(1);
    MQ7.setA(99.042); MQ7.setB(-1.518);
    
    MQ7.init();
    
      Serial.print("Calibrating please wait.");
      for(int i = 1; i<=10; i ++)
      {
      MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
      calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
      Serial.print(".");
      }
      MQ7.setR0(calcR0/10);
      Serial.println("  done!.");
      
      if(isinf(calcR0)) {Serial.println("Warning: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply"); while(1);}
      if(calcR0 == 0){Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply"); while(1);}
      /*****************************  MQ CAlibration ********************************************/ 
      MQ7.serialDebug(true);
   
}

void loop() {
  
  if (digitalRead(UmschalterCO) == LOW)
  if (digitalRead(UmschalterPropan) == LOW)
{
   oled.clear(); 
   oled.print("Propan:");
   oled.print(MQ2.readSensor());
   oled.println(" PPM");
   oled.print("Monoxid:");
   oled.print(MQ7.readSensor());
   oled.println(" PPM");

  
       int Timer = millis();
       while(millis() - Timer <= (1000))
       
       MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
       MQ2.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
       MQ2.serialDebug(); // Will print the table on the serial port
       
      
      
       analogWrite(5, 255); // 255 is DC 5V output
       MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
       MQ7.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
       MQ7.serialDebug(); // Will print the table on the serial port
      
    if ((digitalRead(MQ7.readSensor())) > MaxMonoxid)
      {
        {
        tone(Lautsprecher,1000,6000);}
      if (digitalRead(Quittierung) == HIGH)
      {
       tone(Lautsprecher,0,6000);}
    if ((digitalRead(MQ2.readSensor())) > MaxPropan)
      {
        {
         tone(Lautsprecher,1000,6000);}
      if (digitalRead(Quittierung) == HIGH)
      {
       tone(Lautsprecher,0,6000);}
      }}}


if (digitalRead(UmschalterCO) == HIGH)
  //Code für Gassuche-Monoxid
  {
     analogWrite(5, 255); // 255 is DC 5V output
     MQ7.update(); // Update data, the arduino will read the voltage from the analog pin
     MQ7.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
     MQ7.serialDebug(); // Will print the table on the serial port

   oled.clear(); 
   oled.println("Suche nach Monoxid-Gas:");
   oled.print("Monoxid:");
   oled.print(MQ7.readSensor());
   oled.println(" PPM");

   
   if ((digitalRead(MQ7.readSensor())) > MaxMonoxid)
   {oled.print("ACHTUNG GEFÄHRLICHE PPM-WERTE ERREICHT:");}
   
   tone(Lautsprecher,(MQ7.readSensor()),6000);
   
   if (digitalRead(Quittierung) == HIGH)
    {
     tone(Lautsprecher,0,6000);}
    }

if (digitalRead(UmschalterPropan) == HIGH)
  //Code für Gassuche-Propan
  {

     int Timer = millis();
     while(millis() - Timer <= (1000))
    
     MQ2.update(); // Update data, the arduino will read the voltage from the analog pin
     MQ2.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
     MQ2.serialDebug(); // Will print the table on the serial port

   oled.clear(); 
   oled.println("Suche nach Propan-Gas:");
   oled.print("Propan:");
   oled.print(MQ2.readSensor());
   oled.println(" PPM");

   
   if ((digitalRead(MQ2.readSensor())) > MaxPropan)
   {oled.print(F("ACHTUNG GEFÄHRLICHE PPM-WERTE ERREICHT:"));}
   
   tone(Lautsprecher,(MQ2.readSensor()),6000);
   
   if (digitalRead(Quittierung) == HIGH)
    {
     tone(Lautsprecher,0,6000);}
    } 
   
delay(100);
}

It would be really nice if someone could help. And if this ist a stupid question I would like to apologize in advance, I really am a noob in programing :sweat_smile:
Thanks

Why are you using the serial pins, 0 and 1, for the switch, when you could be debugging the program using serialPrint() statements to find the errors?

The schematic shows "Mode Umschalter" connected to pins 0 and 1. That won't work as those are for program loading and serial monitor.

It does not agree with these settings, either:

const int UmschalterCO (4);
const int UmschalterPropan (5);

You need a current limiting resistor (e.g. 150 Ohms) in series with the piezo speaker. Leaving it out can lead to destruction of the port pin, which may already have happened.

Hello
I didnt use the pins 0&1. it was a mistake on the scematic. I use the pins 4&5 as shown in the code.
Thanks for the Information on the resistor i will try that out.

Please correct the schematic and add it to your next post (do not replace the original).

Explain what you have done to debug the problem.

The first problem is that the switch doesnt switch between the modes.

Put in a Serial.print() statement to check was was read for the switch value.

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