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.
/*
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.