Adding a Keypad to a smoke sensor project with LCD

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everyone!

 

I'm new to Arduino and having trouble adding 4X4 keypad to the project. Basically, I do not have enough pins in the Arduino to connect the Keypad. Also, I'm having trouble editing the code to met the required task. The keypad is to turn on the project or turning it off. Attached is the schematic and the code for the project. Can someone help me editing it? 

 

Thanks!

#include <LiquidCrystal.h>

// connected to the output pin of MQ-X
int mqx_analogPin = A0;
int redLed = 7;
int greenLed = 6;
int buzzer = 9;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
   pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
   pinMode(buzzer, OUTPUT);
  // Print a message to the LCD.
  lcd.print("Smoke Sensor");
}
void loop()
{
  //initiallize float variable to store value from sensor (analog value from A0)
  float mqx_value = analogRead(mqx_analogPin);

  //set the curser starting address of LCD where we want to print sensor value
  lcd.setCursor(0, 1);

  //print sensor value on lcd
  lcd.print(mqx_value);

  // buzer thereshhold you can set it according to the sensitivity
  if(mqx_value > 400)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    //buzzer on
    tone(buzzer, 1000);
  }
  else
  {
     digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    //buzzer off
     noTone(buzzer);
  }
  //Just here to slow down the output.
  delay(1000);
}

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is no use editing the code you supplied - you need to write code to perform the tasks you want. If you want someone to write the code for you, then this is the wrong place.

You need to resolve your keypad problem first - you need four diodes and four port pins. The other four port pins can be shared with the lcd. You either write to the lcd or scan the keypad.this is a common problem, so a bit of googling should find the answer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The keypad is to turn on the project or turning it off.

If that is truly all that is needed, then just one pin, for one switch, would be sufficient.

 

If it is an on/off switch, the micro can read the pin's input state, high or low.

 

If it is a push button switch, then the micro can toggle the value of a flag to represent on  or off each time the push button is pressed.

 

More info is needed.

 

Welcome to the Forum.

 

JC 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The project runs automatically. I need it to run by the user using keypad. When the project detects gas and turn on the red LED, the keypad will allow the user to turn the buzzer off and set the project to normal which is the green LED.

 

Thanks JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kh7 wrote:

The project runs automatically. I need it to run by the user using keypad. When the project detects gas and turn on the red LED, the keypad will allow the user to turn the buzzer off and set the project to normal which is the green LED.

 

That functionality still only needs one switch.

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Responder valuesoft is correct.  You don't need a 4x4 keypad to do what you describe.  Basically you need a restore switch.  When the power is applied, the gas sensor turns on the green LED and then checks continuously for the presence of gas.  If gas is detected, the system turns off the green LED, turns on the red LED, and sounds the alarm buzzer.  The user presses one button to turn off the buzzer and red LED, and turn on the green LED.  The code need not test for the button being pressed in normal (green LED) mode. 

 

 Since you are using Arduino, I suggest that you download one of the popular libraries for push-button switches.   The one that I use compares the current button state with the previous button state each time that the button.process() function is called. When the button is released, a callback function executes the button's code in the background.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kh7 wrote:

I'm new to Arduino and having trouble adding 4X4 keypad to the project.

Basically, I do not have enough pins in the Arduino to connect the Keypad.

 

Take a look at an I2C 8-bit I/O Expander such as the PCF8574A by TI.

This chip will provide the 4 inputs and 4 outputs needed for scanning the keypad.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

guys can someone tell me what the following code would do? I'm trying to make the program detect the gas level. When it's higher than (400), it will turn on the red LED and the buzzer, but it will not go back to normal (green LED) until key (1) in the keypad is pressed. Unfortunately, I'm getting different result. Please guide me through this. 

// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>
// connected to the output pin of MQ-X
int mqx_analogPin = A0; 
int redLed = 7;
int greenLed = 6;
int buzzer = 7;

int row3=A1;
int row2=A2;
int row1=A3;
int row0=A4;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Keypad init
const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= { {'1', '2', '3', 'A'},
                                 {'4', '5', '6', 'B'},
                                 {'7', '8', '9', 'C'},
                                 {'*', '0', '#', 'D'} };
                                 
byte rowPins[numRows] = {row0,row1,row2,row3};      //Rows    0 to 3
byte colPins[numCols]= {13,10,9,8};                //Columns 0 to 3


//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup(){
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);

   
  // Print a message to the LCD.
  lcd.print("Smoke Sensor");

  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, HIGH);
  //buzzer off
  noTone(buzzer);
  
}
void loop()
{
  //initiallize float variable to store value from sensor (analog value from A0)
  float mqx_value = analogRead(mqx_analogPin);
//  mqx_value= mqx_value*9.775;       // i.e 10000ppm/1023 adc bits
  //set the curser starting address of LCD where we want to print sensor value 
  lcd.setCursor(0, 1);

  //print sensor value on lcd
  lcd.print(mqx_value);

  // buzer thereshhold you can set it according to the sensitivity
  if(mqx_value > 400) 
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    //buzzer on
    tone(buzzer, 1000);
  }

char keypressed = myKeypad.getKey();
if ( keypressed)

{


//Serial.print(keypressed);
 
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("Pressed ");
 // lcd.setCursor(1, 1);
 lcd.print(keypressed);

 if(keypressed=='1'){
     digitalWrite(redLed, LOW);
     digitalWrite(greenLed, HIGH);
    //buzzer off
    noTone(buzzer);
    }

 delay(1000);
 
} 
//  Just here to slow down the output.
//  delay(1000);  
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char keypressed = myKeypad.getKey();
if ( keypressed)

{


//Serial.print(keypressed);
 
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("Pressed ");
 // lcd.setCursor(1, 1);
 lcd.print(keypressed);

 if(keypressed=='1'){
     digitalWrite(redLed, LOW);
     digitalWrite(greenLed, HIGH);
    //buzzer off
    noTone(buzzer);
    }

 delay(1000);
 
} 
//  Just here to slow down the output.
//  delay(1000);  
}

 

 

 

You will need only a single pin (i.e. pinMode(switchPin, INPUT) in setup())... Whenever your buzzer will go all out shouting... and so your program will reach char keyPressed line...

you will need to poll that pin (i.e. while(digitalRead(switchPin) != HIGH);   )...

Connecting a pull up resistor to VCC to that pin will exit while statement...

The rest would look like..

digitalWrite(redLED, LOW);

digitalWrite(greenLED, HIGH);

noTone(buzzer); //buzzer off

delay(1000);   //i don't know why u have added it but ok

//hope that would do..

Show's ON.... Failure come and go... Life goes on... SHOW's back...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your help, but I don't know which pin should I assign to the input. I attached the schematic diagram so you may have a better look. Please explain to me what should I edit in the code so it runs the way I wanted it to.

 

// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>

// connected to the output pin of MQ-X
int mqx_analogPin = A0;
int redLed = 7;
int greenLed = 6;
int buzzer = 7;

int row3=A1;
int row2=A2;
int row1=A3;
int row0=A4;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Keypad init
const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= { {'1', '2', '3', 'A'},
                                 {'4', '5', '6', 'B'},
                                 {'7', '8', '9', 'C'},
                                 {'*', '0', '#', 'D'} };

byte rowPins[numRows] = {row0,row1,row2,row3};      //Rows    0 to 3
byte colPins[numCols]= {13,10,9,8};                //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup(){
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
   pinMode(redLed, OUTPUT);
   pinMode(greenLed, OUTPUT);
   pinMode(buzzer, OUTPUT);

  // Print a message to the LCD.
  lcd.print("Smoke Sensor");

  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, HIGH);
  //buzzer off
  noTone(buzzer);

}
void loop()
{
  //initiallize float variable to store value from sensor (analog value from A0)
  float mqx_value = analogRead(mqx_analogPin);
//  mqx_value= mqx_value*9.775;       // i.e 10000ppm/1023 adc bits
  //set the curser starting address of LCD where we want to print sensor value
  lcd.setCursor(0, 1);

  //print sensor value on lcd
  lcd.print(mqx_value);

  // buzer thereshhold you can set it according to the sensitivity
  if(mqx_value > 400)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    //buzzer on
    tone(buzzer, 1000);
  }

char keypressed = myKeypad.getKey();

if ( keypressed)

{

//Serial.print(keypressed);

 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("Pressed ");
// lcd.setCursor(1, 1);
 lcd.print(keypressed);
 delay(1000);

  if(keypressed=='1'){
     digitalWrite(redLed, LOW);
     digitalWrite(greenLed, HIGH);
    //buzzer off
    noTone(buzzer);
    }

}
//  Just here to slow down the output.
//  delay(1000);
}

 

Attachment(s): 

Last Edited: Sun. Dec 10, 2017 - 12:43 PM