Push button + MQ2 sensor + 16x2 I2C LCD

Hi,
I connected the MQ2 sensor and LCD to arduino and it shows me the MQ2 values.
When the LCD turns on I want it to display my name and when I push the button I want to change it to show the MQ2 values but I don't know how to do it. Is there anyone who could help? Thanks.
(I've already connected the button to arduino)

My code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define MQ2pin (0)

int sensorValue; //variable to store sensor value
void setup()
{
lcd.begin(16,2);
lcd.backlight();
}

void loop()
{
lcd.clear();
sensorValue = analogRead(MQ2pin); // read analog input pin 0

lcd.setCursor(1,0);
lcd.print("VALUE: ");
lcd.print(sensorValue);
lcd.print("ppm");

if(sensorValue > 300)
{
lcd.setCursor(1,1);
lcd.print("SMOKE DETECTED");
}
else
{

lcd.setCursor(3,1);
lcd.print("NO SMOKE");

}

delay(2000); // wait 2s for next reading
}

Welcome to the forums. Please read the sticky post at the top to learn how to properly post your code. It will help people help you.

As for detecting your button, there are many examples in the IDE. I would suggest looking at the stateChangeDetection example (File->examples->02.digital->StateChangeDetection).

You also have to have your button wired up properly. Either with a pull-up or pull-down resistor and declare the pin as INPUT_PULLUP to use the internal pull-up resistor

Here is your code modified. It will display "My Name" at start up and until the button switch is pressed.

I do not use the old LiquidCrystal_I2C libraries. I like the hc44780 library for LCDs. The hd447680 library is available through the library manager.

I do not like to use delay so I use the blink without delay method of timing with millis().

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

const byte buttonPin = 4;  // switch wired to ground and a digital input pin
const byte MQ2pin = A0; // const is preferred over #define

int sensorValue; //variable to store sensor value

void setup()
{
   Serial.begin(115200);
   pinMode(buttonPin, INPUT_PULLUP);  // enable internal pullup
   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.print("  My Name");
   while (digitalRead(buttonPin) == HIGH); // switch input goes low when pressed
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 2000;
   if (millis() - timer >= interval)
   {
      timer = millis();

      sensorValue = analogRead(MQ2pin); // read analog input pin 0

      lcd.setCursor(0, 0);
      lcd.print("VALUE:          "); // extra spaces to overwrite old data
      lcd.setCursor(7, 0);
      lcd.print(sensorValue);
      lcd.print("ppm");

      if (sensorValue > 300)
      {
         lcd.setCursor(0, 1);
         lcd.print(" SMOKE DETECTED ");
      }
      else
      {
         lcd.setCursor(0, 1);
         lcd.print("    NO SMOKE    ");  // extra spaces to overwrite old data
      }
   }
}
  

Non-blocking timing using millis() tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Push button switch wiring

1 Like

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