Overview
This project reads analog values from a gas sensor and displays the readings on a 20x4 LCD screen with an I2C backpack. Gas sensors typically measure the concentration of the gas and provide an analog reading which is converted into a percentage for better readablity.
There is a wide range of gas sensors that detect various types of gas, referred to as the MQ Series. Any one of those sensors can be used in this project.
Components
Component | Purpose |
---|---|
Arduino Nano | This will be the microcontroller |
MQ sensor | This will be the gas sensor |
I2C LCD 20X4 | This will be the LCD display |
Circuit Diagram
Connections
MQ gas sensor
Nano Pin | MQ sensor Pin |
---|---|
A0 | AO |
5V | VCC |
Gnd | Gnd |
I2C LCD Pin
Nano Pin | I2C LCD Pin |
---|---|
A4 | SDA |
A5 | SCL |
5V | VCC |
GND | GND |
Code
/*
Project: Gas sensor input with output on LCD screen.
Project Description:
This sketch writes the readings read from Gas sensor onto a LCD screen.
This sketch is for a 20x4 screen.
Author: STEMVentor Educonsulting
This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs.
This code is for educational purposes only and is not for production use.
*/
// LIBRARIES
//used for communicating with I2C devices
#include <Wire.h>
//https://www.arduino.cc/reference/en/libraries/liquidcrystal-i2c/
// Install the LiquidCrystal I2C by Frank de Brabander from the IDE library manager.
#include <LiquidCrystal_I2C.h>
// PIN DEFINITIONS
// define the pin to which the gas sensor is connected
// for Arduino Nano , comment if using ESP32
#define gasSensorPin A0
// for ESP32 , uncomment if using ESP32
// #define gasSensorPin 13
// GLOBAL VARIABLES
// Define the display size
const byte rows = 4;
const byte cols = 20;
// used for reading sensor value
int percentValue;
int gasValue;
/* INITIALIZE OBJECTS
* Libraries usually follow an object-oriented approach that requires
* an instance of the class to call its methods.
*/
/*
* All I2C components have an address, the default is usually 0x27
* If that doesn't work, see this:https://playground.arduino.cc/Main/I2cScanner/
* The init statement accepts the address and the number of columns and rows.
*/
LiquidCrystal_I2C lcd(0x27, cols, rows);
/* LOCAL FUNCTIONS */
// Read Sensor value
void readGasValue() //analog read gas sensor value
{
gasValue = analogRead(gasSensorPin);
// for ESP32, max analog read value is 4098
// percentValue = map(gasValue, 0, 4098, 0, 100); //the analog readings from the gas sensor is converted to a value between 0-100 like a percentage.
// for Arduino Nano, max analog read value is 1024
percentValue = map(gasValue, 0, 1024, 0, 100); //the analog readings from the gas sensor is converted to a value between 0-100 like a percentage.
}
/*
Function to print to LCD on a single row.
Takes the row number and the text to display on that row (max 20 chars, rest will be truncated).
The entire display is cleared if the clearAll flag is true, else only the row is cleared (the default).
*/
void printToLcdByRow(int row, String text, bool clearAll = false)
{
const char* twentySpaces = " ";
if (clearAll) {
lcd.clear(); //clears the entire display
}
lcd.setCursor(0, row - 1);
lcd.print(twentySpaces); //clears the row
lcd.setCursor(0, row - 1); //cursor has to be set again after printing spaces
lcd.print(text);
}
/*
Function to print to LCD across rows with each row having 20 chars.
Messages can be a maximum of 20x4 chars, rest will be truncated.
The entire display is cleared before printing the text.
Test string of 20 char:
12345678901234567890
Test strings of 80 chars:
12345678901234567890123456789012345678901234567890123456789012345678901234567890
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec varius est donec.
*/
void printToLcd(String text)
{
lcd.clear(); //clears the entire display
byte charsRemaining = text.length();
byte charFrom = 0;
byte charTo = charsRemaining < cols ? text.length() : charFrom + cols;
byte row = 1;
while (charsRemaining > 0) {
String line = text.substring(charFrom, charTo);
printToLcdByRow(row, line);
charsRemaining = text.length() - charTo;
charFrom = charTo;
charTo = charsRemaining < cols ? text.length() : charFrom + cols;
row++;
}
Serial.println("Data string has been displayed on LCD");
}
void lcdSetup() // function to LCD setup
{
// Initializing wire
Wire.begin();
// Initialize the LCD.
lcd.init();
// Turn on the blacklight and print a message.
lcd.backlight();
// Use the function to display text on each line individually.
printToLcdByRow(1, "I2C LCD Ready!", true);
printToLcdByRow(2, "Enter some text.", false);
printToLcdByRow(3, "Max 80 characters.", false);
printToLcdByRow(4, "For a 20x4 LCD.", false);
delay(1000);
}
void gasSensorSetup() //gas sensor setup
{
//Define the pin mode
pinMode(gasSensorPin, INPUT);
}
/* SETUP CODE: runs once when the board starts up or resets */
void setup()
{
// Start the serial communication with baud rate suitable for your components.
Serial.begin(9600);
lcdSetup();
gasSensorSetup();
Serial.println("The board is ready!");
printToLcdByRow(1, "Analog value :", true);
printToLcdByRow(3, "Gas Percentage :", false);
}
/* MAIN LOOP: runs repeatedly at a very high frequency (1000s of times a second) */
void loop() {
delay(1000);
readGasValue();
printToLcdByRow(2, String(gasValue), false);
printToLcdByRow(4, String(percentValue) + "%", false);
}