ESP32 MicroPython MQ3 Alcohol Sensor

This tutorial guides you through interfacing the MQ3 alcohol sensor with an ESP32 using MicroPython programming to detect and measure ethanol and alcohol vapor concentrations. In detail, we will learn:

ESP32 MicroPython MQ3 alcohol sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×MQ3 Alcohol Sensor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of MQ3 Alcohol Sensor

The MQ3 alcohol sensor is designed to detect ethanol and alcohol vapor in the surrounding air. It uses Metal Oxide Semiconductor (MOS) technology, where a Tin Dioxide (SnO2) sensing element changes resistance when exposed to alcohol molecules. The sensor features both a digital output pin and an analog output pin for sending signals.

The MQ3 sensor is commonly used in breathalyzer devices, alcohol detection systems, DUI detection equipment, and environmental alcohol vapor monitoring.

Pinout

The MQ3 alcohol sensor module has four pins:

  • VCC pin: Connect this pin to the VCC (5V).
  • GND pin: Connect this pin to the GND (0V).
  • DO pin: This is the digital output pin. It goes LOW when alcohol vapor is detected and HIGH when no alcohol is present. You can adjust the detection threshold using the onboard potentiometer.
  • AO pin: This is the analog output pin, which outputs a voltage that varies based on the alcohol concentration. Higher alcohol levels increase the voltage, while lower alcohol levels decrease it.
MQ3 Alcohol Sensor Pinout

The sensor also has two LED indicators:

  • PWR-LED: Indicates that the sensor is powered on.
  • DO-LED: Lights up when alcohol is detected according to the DO pin value, and turns off when no alcohol is detected.

How It Works

Regarding the DO pin:

  • The module has a potentiometer to adjust its sensitivity to alcohol levels.
  • When the alcohol level exceeds the set threshold, the sensor's output pin goes LOW, and the DO-LED light turns on.
  • When the alcohol level is below the set threshold, the sensor's output pin goes HIGH, and the DO-LED light turns off.

For the AO pin:

  • As the alcohol concentration increases, the voltage rises.
  • As the alcohol concentration decreases, the voltage falls.

The potentiometer does not affect the value on the AO pin.

The MQ3 Sensor Warm-up

The MQ3 alcohol sensor requires a warm-up period before use. If the sensor has not been used for over a month, allow it to warm up for 24-48 hours to ensure accurate readings. If it has been used recently, a warm-up of 5-10 minutes is sufficient. Initially, the readings may be high, but they will stabilize over time.

To warm up the MQ3 sensor, connect its VCC and GND pins to a power source or to the VCC and GND pins on an ESP32, and let it remain connected for the required time.

Wiring Diagram

Since the MQ3 alcohol sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • How to connect ESP32 and MQ3 alcohol sensor using breadboard (powered via USB cable)
The wiring diagram between ESP32 MQ3 alcohol sensor

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and MQ3 alcohol sensor using breadboard (powered via Vin pin)
ESP32 MQ3 alcohol sensor wiring instruction

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and MQ3 alcohol sensor
How to wire ESP32 and MQ3 alcohol sensor
MQ3 Alcohol SensorESP32
VCC3.3V
GNDGND
DOGPIO 16
AOGPIO 36 (ADC0)

ESP32 MicroPython Code - Read value from DO pin

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-mq3-alcohol-sensor """ from machine import Pin import utime # For timing functions DO_PIN = Pin(16, Pin.IN) # The ESP32 pin GPIO16 as a digital input pin of the MQ3 alcohol sensor module while True: alcohol_state = DO_PIN.value() # Read the digital value from the pin if alcohol_state == 1: print("Alcohol is NOT detected") else: print("Alcohol is detected") utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the MQ3 alcohol sensor according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Place alcohol vapor source near the MQ3 sensor (hand sanitizer or rubbing alcohol on cotton works well).
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Alcohol is NOT detected Alcohol is NOT detected Alcohol is NOT detected Alcohol is NOT detected Alcohol is NOT detected Alcohol is detected Alcohol is detected Alcohol is detected Alcohol is detected Alcohol is detected
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

If the LED light remains on always or doesn't turn on, you can adjust the sensor's sensitivity by turning the onboard potentiometer.

ESP32 MicroPython Code - Read value from AO pin

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-mq3-alcohol-sensor """ from machine import ADC, Pin import utime # For timing functions AO_PIN = ADC(Pin(36)) # The ESP32 pin GPIO36 (ADC0) as an analog input pin of the MQ3 alcohol sensor module # Set the ADC width (resolution) to 12 bits AO_PIN.width(ADC.WIDTH_12BIT) # Set the attenuation to 11 dB, allowing input range up to ~3.3V AO_PIN.atten(ADC.ATTN_11DB) while True: alcohol_value = AO_PIN.read() # Read the analog value (0-4095) print("MQ3 sensor AO value:", alcohol_value) # Print the analog value utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 board.
  • Click the green Run button (or press F5) to execute the script.
  • Introduce alcohol vapor to the sensor (hand sanitizer or isopropyl alcohol).
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MQ3 sensor AO value: 120 MQ3 sensor AO value: 125 MQ3 sensor AO value: 128 MQ3 sensor AO value: 450 MQ3 sensor AO value: 620 MQ3 sensor AO value: 850 MQ3 sensor AO value: 920 MQ3 sensor AO value: 980 MQ3 sensor AO value: 950 MQ3 sensor AO value: 680 MQ3 sensor AO value: 420
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

You can use the values from DO or AO to detect alcohol presence, trigger an alarm, or log data for breathalyzer applications.

※ NOTE THAT:

This tutorial demonstrates how to use the adc.read() function to read values from an ADC (Analog-to-Digital Converter) connected to an alcohol sensor. The ESP32's ADC is suitable for projects that do not require high precision. However, if your project needs accurate measurements, keep the following in mind:

  • The ESP32 ADC is not perfectly accurate and may require calibration for precise results. Each ESP32 board may vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you desire.

For projects requiring high precision, consider using an external ADC (e.g., ADS1115) with the ESP32 or opt for an Arduino, which has a more reliable ADC. If you still wish to calibrate the ESP32 ADC, refer to the ESP32 ADC Calibration Driver.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!