Alcohol sensor on A0, on an Arduino Uno
An MQ-3 alcohol sensor on A0, printed twice a second: breathe some alcohol vapor on it and the reading climbs. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $29. On Mokxi they're free.
What you'll learn
- Reading an analog sensor with analogRead
- How a gas sensor's resistance changes with vapor
- Printing readings to the Serial Monitor
- Powering a sensor module from 5V and GND
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 2
How it works
The MQ-3 module has a sensing element whose resistance drops when alcohol vapor is near. The module turns that into a voltage on its AO pin, which goes to A0 on the Uno. More alcohol means a higher voltage.
The sketch reads A0 with analogRead, which gives a number from 0 to 1023, and prints it every half second. Drag the module's alcohol level up in the simulator and the numbers climb.
Parts list
2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Alcohol sensor, MQ-3 pin VCC | Arduino Uno R3 pin 5V |
| Alcohol sensor, MQ-3 pin GND | Arduino Uno R3 pin GND |
| Alcohol sensor, MQ-3 pin AO | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Alcohol sensor on A0.
//
// module AO -> A0 VCC -> 5V GND -> GND
//
// Like the gas sensor, the element's resistance falls in alcohol vapor, and
// the reading on A0 rises with it.
//
// Drag the module's alcohol level up.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: Alcohol sensor on A0");
}
void loop() {
int reading = analogRead(A0);
Serial.println(reading);
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. Serial.begin(9600) opens the serial connection at 9600 baud so you can read values in the Serial Monitor. It then prints a short title line.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: Alcohol sensor on A0");
}loop sketch.ino
Reads A0 with analogRead, which turns 0 to 5 V into 0 to 1023. Serial.println(reading) prints the number, and delay(500) waits half a second, giving two readings a second.
void loop() {
int reading = analogRead(A0);
Serial.println(reading);
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The number never changes This project
Check that AO (not a digital pin) goes to A0, and that VCC and GND are connected. - The reading is high and drifts after power up. Alcohol sensor
The heater needs a warm-up time. Wait several minutes, and preheat a new sensor for 24 hours, before trusting any value. - The sensor works poorly or the board browns out. Alcohol sensor
The heater needs about 150 mA at 5 V. Power it from a solid 5 V supply and join ground to the board. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.