Gas Sensor System Using 8051 Microcontroller
- Sanskruti Ashtikar
- Nov 15, 2024
- 4 min read
Updated: Jan 23
Introduction
Gas sensors are crucial for detecting hazardous gases in the environment and ensuring safety in various settings such as homes, industries, and laboratories. In this project, we will design a gas sensor system using the 8051 microcontroller. The system will use an MQ-2 gas sensor to detect the presence of gases such as LPG, propane, methane, and smoke. When gas is detected, an alarm will be triggered, and the gas concentration will be displayed on an LCD.
Components Required
Circuit Diagram
The MQ-2 gas sensor provides an analog output that needs to be converted to a digital signal using an ADC. The 8051 microcontroller reads this digital value to determine the gas concentration. If the concentration exceeds a predefined threshold, a buzzer is activated, and the concentration is displayed on the LCD.
+5V ----- +5V | | MQ-2 +---+ +5V --|VCC| | | |OUT |------- Vin+ (ADC0804) | | GND --|GND | +---+ADC0804Vin+ to MQ-2 outputVCC to +5VGND to GroundCS to GroundRD to P2.0 of 8051WR to P2.1 of 8051INTR to P2.2 of 8051D0-D7 to P1.0-P1.7 of 8051Vref/2 to 2.5V (using a voltage divider)BuzzerAnode to P3.0 of 8051 through 1kΩ resistorCathode to GroundLCDRS to P3.6 of 8051RW to GroundE to P3.7 of 8051D4-D7 to P2.4-P2.7 of 8051Pin Connections
MQ-2 Gas Sensor:
VCC to +5V
GND to Ground
OUT to Vin+ of ADC0804
ADC0804:
Vin+ to MQ-2 output
VCC to +5V
GND to Ground
CS to Ground
RD to P2.0 of 8051
WR to P2.1 of 8051
INTR to P2.2 of 8051
D0-D7 to P1.0-P1.7 of 8051
Vref/2 to 2.5V (using a voltage divider)
Buzzer:
Anode to P3.0 of 8051 through 1kΩ resistor
Cathode to Ground
LCD Display:
RS to P3.6
RW to Ground
E to P3.7
D4-D7 to P2.4-P2.7
Software Implementation
The code is written in C using Keil uVision IDE. It involves initializing the ADC, reading the analog value from the gas sensor, converting it to a digital value using the ADC, and then displaying it on the LCD. If the gas concentration exceeds a threshold, the buzzer is activated.
#include <reg51.h>#include "lcd.h"sbit RD = P2^0; // Read signal for ADCsbit WR = P2^1; // Write signal for ADCsbit INTR = P2^2; // Interrupt signal from ADCsbit BUZZER = P3^0; // Buzzer pinvoid delay(unsigned int count) { int i, j; for(i=0; i<count; i++) for(j=0; j<1275; j++);}void adc_init(void) { WR = 0; // Start conversion delay(1); WR = 1; // End conversion}unsigned char adc_read(void) { unsigned char value; while(INTR); // Wait for conversion to complete RD = 0; // Read the value value = P1; // Get the digital value delay(1); RD = 1; return value;}void main() { unsigned char gas_value; float gas_concentration; unsigned char threshold = 50; // Example threshold value lcd_init(); // Initialize LCD adc_init(); // Initialize ADC BUZZER = 0; // Turn off buzzer initially while(1) { gas_value = adc_read(); // Read digital value from ADC gas_concentration = (gas_value * 5.0 / 255.0) * 100; // Convert to gas concentration lcd_cmd(0x80); // Move cursor to first line lcd_write_string("Gas: "); lcd_cmd(0xC0); // Move cursor to second line lcd_write_number(gas_concentration); // Display gas concentration if(gas_concentration > threshold) { BUZZER = 1; // Turn on buzzer } else { BUZZER = 0; // Turn off buzzer } delay(1000); // Update every second }}Explanation
Initialization:
LCD Initialization: The lcd_init() function sets up the LCD.
ADC Initialization: The adc_init() function prepares the ADC for conversion.
Buzzer Initialization: The buzzer is initially turned off.
Reading Gas Concentration:
The adc_read() function reads the analog voltage from the MQ-2 sensor, converts it to a digital value using the ADC0804, and returns it.
Gas Concentration Conversion:
The digital value is converted to gas concentration using the formula: gas_concentration = (value 5.0 / 255.0) 100, assuming a 5V reference voltage for the ADC.
Displaying Gas Concentration:
The gas concentration is displayed on the LCD using lcd_write_string() and lcd_write_number() functions.
Buzzer Control:
If the gas concentration exceeds the predefined threshold, the buzzer is activated. Otherwise, the buzzer is turned off.
Conclusion
This project demonstrates the use of the 8051 microcontroller to create a gas sensor system. The system is capable of reading gas concentration data from the MQ-2 sensor, converting it to a digital format using the ADC0804, and displaying it on an LCD. Additionally, an alarm is triggered if the gas concentration exceeds a predefined threshold. This project is a great way to learn about sensor interfacing, ADCs, and using microcontrollers for safety and monitoring applications.
Want us to guide through your project or make the project for you ?
Create Various Projects
Check out our Free Arduino Projects Playlist - Arduino Projects
Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects
Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects
Check out our Free IoT Projects Playlist - IoT Projects
Check out our Free Home Automation Projects Playlist - Home Automation Projects
Check out our Free NodeMCu Projects Playlist - NodeMCu Projects






Comments