Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO with MQ-2 Gas Sensor and LED Indicator

Image of Arduino UNO with MQ-2 Gas Sensor and LED Indicator

Circuit Documentation

Summary of the Circuit

This circuit is designed to read analog data from an MQ-2 gas sensor and display the sensor's reading through the brightness of an LED. The MQ-2 sensor is connected to an Arduino UNO, which processes the sensor's signal and controls the LED brightness proportionally to the gas concentration detected. The circuit uses a resistor to limit the current through the LED, ensuring safe operation. The Arduino UNO also sends the sensor's analog value to the serial monitor every 500 milliseconds.

Component List

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Purpose: Acts as the central processing unit for the circuit, reading sensor data, controlling the LED, and communicating with the serial monitor.

LED: Two Pin (red)

  • Description: A basic red light-emitting diode.
  • Purpose: Indicates the level of gas detected by the MQ-2 sensor through its brightness.

Resistor

  • Description: A passive two-terminal electrical component with a resistance of 220 Ohms.
  • Purpose: Limits the current flowing through the LED to prevent damage.

MQ-2 Gas Sensor

  • Description: A gas sensor used for detecting a wide range of gases, including LPG, smoke, and alcohol vapor.
  • Purpose: Senses the concentration of gases in the air and provides an analog output proportional to the detected concentration.

Wiring Details

Arduino UNO

  • 5V: Powers the MQ-2 sensor.
  • GND: Common ground for the circuit.
  • A0: Receives the analog output from the MQ-2 sensor.
  • D6: Controls the brightness of the LED through PWM.

LED: Two Pin (red)

  • Anode: Connected to pin D6 on the Arduino UNO.
  • Cathode: Connected to one end of the resistor.

Resistor

  • Pin1: Connected to the GND on the Arduino UNO.
  • Pin2: Connected to the cathode of the LED.

MQ-2 Gas Sensor

  • Vcc: Connected to the 5V output on the Arduino UNO.
  • GND: Shares a common ground with the Arduino UNO.
  • AO: Sends the analog signal to pin A0 on the Arduino UNO.

Documented Code

/*
  MQ-2 Sensor Reading with LED Indicator
  This sketch reads analog data from an MQ-2 gas sensor connected to pin A0.
  It maps the sensor's 10-bit analog output to an 8-bit range and uses this
  value to control the brightness of an LED connected to pin D6 using PWM.
  The LED's brightness is directly proportional to the sensor's reading.
  The serial monitor outputs the sensor's analog value every 500 milliseconds.
*/

// Sensor pins: D6 for LED output, A0 for analog input from MQ-2 sensor
#define ledPin 6
#define sensorPin A0

void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 baud rate
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
  digitalWrite(ledPin, LOW); // Ensure the LED is off when starting
}

void loop() {
  int sensorValue = readSensor(); // Read the sensor value
  Serial.print("Analog output: "); // Print a message to the serial monitor
  Serial.println(sensorValue); // Print the sensor value to the serial monitor
  delay(500); // Wait for half a second before reading the sensor again
}

// This function reads the analog value from the sensor and maps it to a range suitable for PWM
int readSensor() {
  unsigned int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
  unsigned int outputValue = map(sensorValue, 0, 1023, 0, 255); // Map the 10-bit data to 8-bit data
  
  // Use the mapped value to set the brightness of the LED
  analogWrite(ledPin, outputValue);
  
  return sensorValue; // Return the original analog value
}

This code is designed to run on the Arduino UNO microcontroller. It initializes the serial communication, configures the LED pin as an output, and continuously reads the analog value from the MQ-2 sensor. The read value is then mapped to an 8-bit range suitable for PWM control of the LED's brightness. The sensor value is also sent to the serial monitor for observation.