MQ-3 Alcohol Sensor (datasheet, circuit plan, code)

Dear Community,

this is my first post, and first of all I want to say Hello to the other users of the forum.

I started to work with a arduino duemilanove which was a gift from a friend of mine.

My first steps were a knowledge collection for very basic electronic understanding.

What helps me is the simulation tool:
https://www.falstad.com/circuit/circuitjs.html

Here I simulated my circuit plan, the sensor was just exchanged with a lamp, with the same Power Consumption as the sensor. Maybe not 100% accurate, but it worked fine to check the parameters and If my circuit with work.

I connected the 5V connector from arduino with the 10 Ohm resistor, and ground connector with the 25 Ohm resistor. Then I connected the 25 Ohm resistor with ground connector from the sensor, and the 10 ohm resistor with the VCC connector of the sensor.

Better would be 33 Ohm resistor in line, but I don't have enough 1 Ohm Resistors here, in the datasheet of the sensor, it is written, 33 Ohm, at 5V, which is simple when you know the 750mW power consumption and the 5V operating voltage. It is 0,75W/ 5V=0,15A. The resistors calculation is just 5V / 0,15A = 33,33Ohm.

My sensor is the MH MQ-3, it has 4 connector, one digital out, one analog out, ground and VCC.

First I was a bit frustrated, I bought the sensor in a box of many sensors, at amazon, and the dealer didn't submitted the datasheets, but hey they were cheap :slight_smile:

So I was lucky and found them via google.

The other MH Sensor datasheets you can find here:
MQ-3 Alcohol Sensor Module Pinout, Datasheet & Other Gas Sensors (components101.com)

I'm also new to programming, I had a small course 10 years ago at university, but when you don't code for a couple of years, you will forget.

So my code is very basic, but it works fine.

Resistance value of MQ-3 is difference to various kinds and various concentration gases. So,When using
this components, sensitivity adjustment is very necessary. we recommend that you calibrate the detector for
0.4mg/L ( approximately 200ppm ) of Alcohol concentration in air and use value of Load resistancethat( RL) about
200 KΩ(100KΩ to 470 KΩ)

So what I did here, I calculated Promille=(0,4/200) * value of the analog output *2.

Die Umrechnung mg/l in Promille: Einfache Rechnung

Um von den angegeben mg/l auf den ‰-Wert zu gelangen, gilt ein gesetzlich vorgeschriebener Umrechnungsfaktor von 2,00. Demnach muss der mg/l-Wert mit zwei multipliziert werden, so entsprechen zum Beispiel 0,4 mg/l Atemalkoholgehalt 0,8 ‰ Blutalkoholgehalt.

My code:

/* MQ-3 Alcohol Sensor Circuit with Arduino */
int AnalogPin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
int value1 = 0; 
int value2 = 0;
float promille=0;
void setup() {
Serial.begin(115200);//sets the baud rate
pinMode(AnalogPin, INPUT);//sets the pin as an input to the arduino
delay(14400000); // this it the pre heat time, maybe not enough, I make a check today
}
void loop()
{
value1= analogRead(AnalogPin);//reads the analaog value from the alcohol sensor's AOUT pin I guess it is Parts per Million
value2=value1 - 98; // here I made a small work around, this sensor has a very long pre heat time, when it is not heated to working temperature, it will show values too high even when there is no alcohol, thats why I removed the amount which was over 0, when no alcohol was next to it
// limit= digitalRead(DOUTpin);//reads the digital value from the alcohol sensor's DOUT pin
Serial.print("Alcohol value: ");
Serial.println(value2);//prints the alcohol value
promille = (0.4/200)*value2*2;
Serial.print("Promillewert: ");
Serial.println(promille,3);
//Serial.print("Limit: ");
//Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
delay(100) ;
}

Next I will try to improve my code, with a array for the promille values for 4 or 5 seconds, and I want to calculate the middel value of the 40 or 50 values. The idea behind the values are quite a bit unstable from one value to the other one.

I hope this helps you, some people maybe don't need all the infos here, but I thought I also read a lot in the forum, and profited from it, so I also wanted to do a post about my project.

Hi @electronicbeginner

Welcome to the forum!

I am a little confused by your falstad simulation :face_with_raised_eyebrow: .

In your code, I noticed a couple things:

  • Your "AnalogPin" is set at "0" instead of "A0". I would instead put "#define AnalogPin A0".
  • I would change "value1" and "value2" to floats instead of ints.
  • You first delay is Long ! "delay" is in milliseconds, so you set the delay for 14400 seconds, or 240 minutes, or 4 hours. (I'm not sure if that was purposeful)

Not a problem.

analogRead returns an int.

Yeah, I realize that. I was just suggesting that he put A0, as it makes it clearer.

It's an analogRead - that couldn't be much clearer.

It clearly says in the comment

Don’t assume anything

The comment only says : "// this it the pre heat time, maybe not enough, I make a check today"

I can read, thanks

It doesn't say in the comment that the code doesn't run for the first 4 hours once the Arduino is powered. To me it sounds like a mistake, like too many zeros, unless he meant 4 hours.

Hello,

I'm very surprised that there are so many answers in such a short time. Thats quite cool.

Just to give a bit more clarity, the 4 hours, is maybe really not enough.

When you look to the details of the datasheet it say's:
Preheat time Over 24 hour

1 Like

You would only find that out once you scrolled down in the code, or if @electronicbeginner told us (which he did). In general, it is better to make your code as clear as possible as you go, starting at the beginning.

Also, it was just a suggestion. It doesn't hurt anything, only makes it clearer what pin is being referred to.

My brother later arrives at my appartement, and I just put a few beers in the fridge. So we can check the sensor this evening.

I just want to tell you: The sensor values are wrong without enough pre heat time. That's why I added the value1 - value of wrong result.

What I don't know if they are completely wrong or just something small wrong. I will try to test with the calculations of alcohol drinking which are well known, and compare with the sensor output.

1 Like

Hey just one more info:
Youmile 9PACK / 1Set MQ-2 MQ-3 MQ-4 MQ-5 MQ-6 MQ-7 MQ-8 MQ-9 MQ-135 Gaswarnmelder-Sensormodul zur Erkennung von Gasrauch für Arduino Starter DIY Kit Mit Dupont 30cm 40pin : Amazon.de: Gewerbe, Industrie & Wissenschaft

This was the sensor pack which I bought, I will have some more checks with them, later on. But now I don't want to interrupt the sensor heating, to have good values this evening.

So I just made a test measurement after 8,5 hours of sensor warm up time. I had two Pilsner Urquell in 2 hours and it is showing between 0,750 and 0,950 promille as alcohol value.

The function with middel value of 50 values I couldn't realize, because the delay function is not working in a for loop with my arduino ide. Does anybody has a idea how to solve this problem?

Also I need to admit the sensor has quite a few seconds cool down time, after you had the contact with alcoholic air, it will need like 30s to a minute to be back at normal.

So my conclusion is the sensor is not too bad, for the cheap price. Of course not totally accurate, but better as nothing.

I just forwarded my code to my brother and he will try to find a solution for the middel value.

When we have something working I will post it here.

Hi you can check my posts from HW MQ-2 Sensors here in the forum, there is a code to get the middel value.

The problem is that the delay function is not working when it is directly in a for loop, so I made a if function in the for loop and used if i==i, then delay(100); and I sum up the values for each measuring point, and if i=50 I devide the sum by 50. The last part is to reset the sum back to zero when you are done with your for loop.

I will try to get a visual interface working today, I bought a display. When I have a working code for the display I will post it here.

72 hours "burn in" would be better. 24 h is the manufacturer's recommended minimum.

Hello to the community,

I improved my code now for a display output and a middel value for 5 seconds.

The display needs to be connected to analog in 4 and 5, and to 5V and ground. SCL from Display is AnalogIn=5; SDA from Display is AnalogIn=4.

The rest from my circuit plan remains the same.

#include <LiquidCrystal_I2C.h>
  
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display


  int AnalogPin = 0; //the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
  float value1 = 0;
  int i=0;
  int value2 = 0;
  float promille = 0;
  float summe=0;
  float mittelwert=0;

void setup()
{
  Serial.begin(115200);//sets the baud rate
  pinMode(AnalogPin, INPUT);//sets the pin as an input to the arduino 
  lcd.init();                      // initialize the lcd 
  lcd.backlight(); 
  //lcd.setCursor(0, 0);
  //lcd.print("Promillewert nach 5");
  //lcd.setCursor(0, 1);
  //lcd.print("Sekunden Messung = ");
  delay(100);
  lcd.clear();
}

void loop()
{

 value1 = analogRead(AnalogPin); //reads the analaog value from the alcohol sensor's AOUT pin I guess it is Parts per Million
 value2 = value1 - 98; // here I made a small work around, this sensor has a very long pre heat time, when it is not heated to working temperature, it will show values too high even when there is no alcohol, thats why I removed the amount which was over 0, when no alcohol was next to it
//limit= digitalRead(DOUTpin);//reads the digital value from the alcohol sensor's DOUT pin

  lcd.setCursor(0, 0);
  lcd.print("Alcohol value = ");
 //Serial.print("Alcohol value: ");
  lcd.setCursor(16, 0);
  lcd.print(value2);
 // Serial.println(value2);//prints the alcohol value
 promille = (0.4/200)*value2*2;
  lcd.setCursor(0, 1);
  lcd.print("Promillewert = ");
  lcd.setCursor(15, 1);
  lcd.print(promille, 3);
// Serial.print("Promillewert: ");
 //Serial.println(promille, 3);
 //Serial.print("Limit: ");
 //Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
 delay(100) ;
 mittelwert=0;
 summe=0;

 for (i=1; i<51; i++)
 {
  summe= summe + promille;
  if (i==i)
  {
    delay(100);
  }
  if (i==50)
  {
    mittelwert = summe /50;
    lcd.setCursor(0, 2);
    lcd.print("5 Sekunden Durchsch-");
    lcd.setCursor(0,3);
    lcd.print("nitt =");
    lcd.setCursor(7, 3);
    lcd.print(mittelwert, 3);
    delay(2000);
  } 
 }
  
lcd.clear();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.