MQ-2 Reading '0' on LCD

Hey there,

So I am having difficulty with displaying my sensor readings on the LCD using this code:

#define redLED 9 //define the LED pins
#define greenLED 12
int buzzer = 22;
int smokeA0 = A6;
int sensorThreshold = 600;
int analogSensor=analogRead(smokeA0);
 char gas ;


char* password ="1160"; //create a password
int place = 0; //keypad position

const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;

char keyMap [rows] [cols] = { //define the symbols on the buttons of the keypad

  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins [rows] = {28, 29, 30, 31}; //pins of the keypad
byte colPins [cols] = {32, 33, 34,35};

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);

U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/17);

void setup(){

  Serial.begin(9600);
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.setFont(u8g2_font_helvB10_tf);
  u8g2.setColorIndex(1);
  pinMode(redLED, OUTPUT);  //set the LED as an output
  pinMode(greenLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  setLocked (true); //state of the password
}

void secondscreen(){
    u8g2.setCursor(15, 20);
    u8g2.print("Home Security");

       delay(6000);
}
void draw(){

  u8g2.drawFrame(0,0,128,31);         
  u8g2.drawFrame(0,33,128,31);           

 
  u8g2.setCursor(32, 25);
  u8g2.print("Welcome");
  u8g2.setCursor(10, 50);
  u8g2.print(" Enter Password");
}

void thirdscreen(){
      u8g2.drawFrame(0,0,128,31);         
  u8g2.drawFrame(0,33,128,31);           
  u8g2.drawStr( 15, 13, "Gas/Smoke:");   
  u8g2.setFont(u8g2_font_helvB10_tf);
  u8g2.setCursor(15, 28);
  u8g2.print(analogSensor);
  u8g2.drawUTF8(76, 28, "PPM");
}

void setLocked(int locked){
  if(locked){
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
    }
    else{
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
    }
  }

void loop(){

    u8g2.firstPage();
  do {   
    draw();
  } while( u8g2.nextPage() );

  char whichKey = myKeypad.getKey(); //define which key is pressed with getKey


  if(whichKey == '*' || whichKey == '#' || whichKey == 'A' ||       //define invalid keys
  whichKey == 'B' || whichKey == 'C' || whichKey == 'D'){

    place=0;
    setLocked (true);
   
    u8g2.firstPage();
  do {   
       u8g2.setCursor(20, 30);
    u8g2.print("  Invalid Key!");
   
  } while( u8g2.nextPage() );

    delay(1000);
     u8g2.clear();
  }
  if(whichKey == password [place]){

    place ++;
  }
  if(place == 4){
    setLocked (false);
    u8g2.clear();
   
      u8g2.firstPage(); 
  do {
   
  u8g2.setCursor(20, 35);
    u8g2.print("*** Verified ***");
   
  } while( u8g2.nextPage() );
 
    delay(3000);
   
     u8g2.clear();

         u8g2.firstPage(); 
  do {
   
secondscreen();
    } while( u8g2.nextPage() );


             u8g2.firstPage(); 
  do {
thirdscreen();
  } while( u8g2.nextPage() );



if (analogSensor < sensorThreshold)
     {
        digitalWrite(redLED, HIGH);
        digitalWrite(greenLED, LOW);
        tone(buzzer, 1000, 200);
       }
else
      {
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
      noTone(buzzer);
      }

delay(10000);
       
  }}

I’m basically trying to enter a password on a keypad then display a message if the password is correct then display the gas sensor readings on the LCD. The readings I get on the LCD is “0”.

However when I test the gas sensor, it is fine and does display readings normally using this code:

#include <U8g2lib.h>
#include <U8x8lib.h>

int LED1 = 9;
int LED2 = 12;
int buzzer = 22;
int smokeA0 = A6;
int sensorThreshold = 630;
 int analogSensor;




U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/17);

void setup() {


  Serial.begin(9600);
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.setFont(u8g2_font_helvB10_tf);
  u8g2.setColorIndex(1);
  pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
}

void draw(){

  u8g2.drawFrame(0,0,128,31);         
  u8g2.drawFrame(0,33,128,31);           
 
  u8g2.drawStr( 20, 13, "Gas/Smoke:");   
 

u8g2.setCursor(35, 28);
u8g2.print(analogSensor);

  u8g2.drawUTF8(65, 28, "PPM");

}



void loop() {
  u8g2.firstPage();
  do {   
    draw();
  } while( u8g2.nextPage() );

analogSensor = analogRead(smokeA0);

if (analogSensor > sensorThreshold)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
noTone(buzzer);
}
delay(500);
}

I’m not sure where I went wrong.

Another problem I face is that the LCD takes a lot of time to display the “Home Security” message even though “Verified” is instantly displayed.

Note: I’m using a 128x64 LCD

Your description of the problem is not helpful towards guessing where the error(s) might be.

I suggest to put in some Serial.print()s to track the program flow at several stages, and see if the password is accepted, the sensor is being read properly, and that other values make sense. That way you can narrow down where the problem might be.

What do you think this program line does, outside of any function?

int analogSensor=analogRead(smokeA0);

jremington:
Your description of the problem is not helpful towards guessing where the error(s) might be.

Whenever I use the first code, the LCD displays '0 PPM' and not the actual reading of the gas sensor. I dont know where the problem might be.

jremington:
What do you think this program line does, outside of any function?

int analogSensor=analogRead(smokeA0);

I tried putting it in the function, still does not work. I tried putting it in the main loop, and still did not fix it.

That line of code should be placed where it makes sense to read the sensor, and then print the result.

Nowhere in the loop function of your first program do you read the sensor, and send the result to the display.

jremington:
That line of code should be placed where it makes sense to read the sensor, and then print the result.

Nowhere in the loop function of your first program do you read the sensor, and send the result to the display.

Yes!! Thank you so much it worked!!

But it gives one reading only... I want it to show a reading every second for the first 10 seconds after the screen appears. Do you have any idea how this could be done?

To learn how to program timed, repeated actions without using delay(), carefully study the Blink Without Delay example.