SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By partypants69
#64041
When I ordered the MQ-3 sensor, I was hoping to be able to make a functional breathalyzer. However, I have come to the realization that this may not be practical for two reasons.
1. I have no way of creating an accurate concentrated atmosphere with a known amount of alcohol for calibration purposes.
2. Professional gas calibration units are many hundreds of dollars.

This being said, the best I can really hope for is to take readings and compare them together to discover the relative amount of alcohol. To this end I have written some code to analyze data from the sensor. I thought I would share it with those who are interested.

First an explanation of the code. The declared variables at the top should be configured according to your setup. Personally I am using a 10k resistor with the sensor which gives me readings in open air that hover around 150. I have the lowerLimit set at 200, so everything below 200 is ignored. when the sensor reads above 200 we start comparing values searching for the highest one. Once this value is found we display it and wait for the sensor to go back down to the coolDown value. For me this is set at 170. Once we have reached 170 we are ready to take another reading. If ledIndicator is equal to one we also turn on an LED while the system is searching for the high value.

Here is the code, any feedback is welcome:
Code: Select all
/* MQ-3 Alcohol Sensor Highest Value code by partypants69 */

int result = 0;            //Used to store highest reading
int sensorPin = 0;         //Pin the sensor is plugged into (analog)
int sensorValue = 0;       //Value of sensor
int lowerLimit = 200;      //The lowest point you wish to begin reading data
int coolDown = 170;        //The highest value the sensor should return to before starting a new reading
int ledIndicator = 1;      //Optional LED read indication 1 = on, anything else = off
int iPin = 10;             //Pin for LED

void setup() {
Serial.begin (9600);            //Begin serial for output
pinMode (sensorPin, INPUT);     //Set pin as input
pinMode (iPin,OUTPUT);
} 

void loop() {
sensorValue = analogRead(sensorPin);      //Read sensor value
if(sensorValue > lowerLimit){             //If sensor has gone above the threshold begin reading
  Serial.println("Breath detected... Analyzing...");
if(ledIndicator==1)                    //Optional led indication while reading
  digitalWrite(iPin,HIGH);
}

while(sensorValue > lowerLimit){        //While above threshold find highest value
  if(result < sensorValue){
    result=sensorValue;
  }
  sensorValue = analogRead(sensorPin);
}

digitalWrite(iPin,LOW);

if(result!=0){                //If we have a non-zero value output the result
  Serial.print("Result = ");
  Serial.println(result);
  while(sensorValue>coolDown){      //Wait to "cool" back down so we are ready for another reading
    delay(100);
    sensorValue = analogRead(sensorPin);
  }
  
  result=0;
  Serial.println("Ready to go!");
  
  if(ledIndicator==1){
   digitalWrite(iPin,HIGH);
   delay(400);
   digitalWrite(iPin,LOW);
   delay(200);
   digitalWrite(iPin,HIGH);
   delay(400);
   digitalWrite(iPin,LOW);
  }
}

delay(1000);
//Serial.println(sensorValue); //Debug (Use to view current sensor value)
}
By tintenfisch
#64457
Is there any chance that you could post the schematic you used, or a photo of the setup? I've been trying different approaches with no results.. think I'm destroying my sensors :/

Any help would be great :)

Cheers!
By filmo
#64643
Hi,

I would also like to see a schematic. I'm kind of a newbie with sensors, and I fear that I may be hooking it up incorrectly.

I've got the Spark Fun Alcohol Sensor plus breakout board.

The board has H1, GND, A1, and B1

I've hooked H1 to 5v dc from Aurduino
I've hooked GND to ground
I've hooked A1 to 5v dc from Aurduino
I've hooked B1 to Aurdion AD Pin 1, then tied that to a 100K ohm resister and then attached the other end of the resister to GND.

I'm not getting much in the way of readings. Varies between 920 and 1023 and the Sensor itself is getting very warm/hot to the touch if I leave it on for more than a few seconds.

What am I doing wrong?

Image
Image
Image[/img]
By partypants69
#64894
I apologize that I don't have a schematic. I used the one from the datasheet to wire the sensor. I will work on drawing one, or at least getting some pictures up. Filmo, it seems you are connecting everything properly. The sensor works based off a chemical reaction and it needs heat to work. My sensor gets quite hot to the touch when in use. I ran it over night while recording readings from it when I was trying to determine the lowest stable reading in normal air, it still works fine. I would not worry too much about it getting hot, the heater is ceramic and should be able to take it. As for the high readings, it looks like you are using a 200k resistor in for the voltage divider (i might research voltage dividers, it helped me to understand how the circuit works) I ended up using only a 10k resistor in order to get a wider range of values. If you happen to have a 100k pot you might play around with that to see how the resistor you use affects the readings.

Tintenfisch, are you able to get any readings at all? Try looking at the schematic in the datasheet. Basically you need to give the heater 5v. Then connect either A or B to 5v and the other side (A or B) to analog in on the arduino and a resistor then ground. If you connect A to 5v you should have B going to anolog in. Basically there are two A pins and two B pins on the sensor. A is connected to the other A, if you use a meter you will see there is no resistance between the two A's, same for the two B's. I wired it wrong at first and that was my problem. Obviously I got no meaningful readings, only a constant 1023.

Let me know if this answers your questions.
By emkman
#69668
thanks for the info and the pics guys. I am currently toying with the idea of hooking one of these up to a Fusion Brain in my carPC for an onscreen digital breathalyzer. And no I am not hooking it into the ignition system in any way. I will keep you posted if it happens.
By filmo
#69670
I think my problem was that the poorly written datasheet called for a 470K ohm resistor when it should have been a 4.7K ohm resistor.

Once I stepped down the resistor to 4.7K instead of one two orders of magnitude larger, I ended up getting readings.

As to calibrating it, I think that might be an exercise in futility. I'm using mine in a more 'relative' manner.

If somebody has successfully calibrated these to measure blood alcohol content in the traditional ppm let us know. Preferably even posting the code and resistor size you end up using.
By Philba
#69676
You should be able to create a base line air-alcohol mixture by placing a known quantity of alcohol in a known volume container and lightly heating it up so the alcohol evaporates. Once you know the amount of alcohol in the air you record the sensor reading. Do this for different quantities of alcohol and you can do a straight line interpolation. If the measurements aren't linear then you'll need to curve fit. More work but not that hard.

This page http://en.wikipedia.org/wiki/Breathalyzer gives a method for estimating BAC from volume of ethanol in 2100 ml of breath.

Yeah, it's not that bad.

Another approach that might be more fun is consume a known amount of alcohol and measure your BAC every 15 minutes. Chart the readings. From your body mass, you can estimate your BAC and then correlate to your chart. Heck, have a geek party!
By filmo
#69678
Philba wrote: Another approach that might be more fun is consume a known amount of alcohol and measure your BAC every 15 minutes. Chart the readings. From your body mass, you can estimate your BAC and then correlate to your chart. Heck, have a geek party!
The project I made sort of does this already. I've got a potentiometer set up for "Amatuer-to-Pro" drinker settings which varies the response curve. I'll post some photos soon.
By emkman
#70144
Has anyone attempted to incorporate a temperature reading into the calculations? IIRC from the datasheet, the output should be temperature adjusted.
By shrutee
#105415
i'm using MQ3 sensor and have some problems in d application. basically i want to use it as an on/off sensor so i'm using a comparator at its output. but have some problems in deciding threshold value.
1. can any1 please tell me what should be the range of output voltage? i tried with 4.7k and 100k as pull down resistor for sensor but output voltage is varying between 0.1v to 2.5v.
2. due to continuous change in the output voltage, presence of alcohol cannot be detected.
2. also what is the resistance of heater coil? i'm getting it as 31ohms which is loading VCC from 5volts upto 3 volts.
By KBelisle
#105731
Philba wrote:You should be able to create a base line air-alcohol mixture by placing a known quantity of alcohol in a known volume container and lightly heating it up so the alcohol evaporates. Once you know the amount of alcohol in the air you record the sensor reading. Do this for different quantities of alcohol and you can do a straight line interpolation. If the measurements aren't linear then you'll need to curve fit. More work but not that hard.

This page http://en.wikipedia.org/wiki/Breathalyzer gives a method for estimating BAC from volume of ethanol in 2100 ml of breath.

Yeah, it's not that bad.

Another approach that might be more fun is consume a known amount of alcohol and measure your BAC every 15 minutes. Chart the readings. From your body mass, you can estimate your BAC and then correlate to your chart. Heck, have a geek party!
I've got a friend who's studying chemistry and I think I'm gonna try and calibrate my sensor with a method similar to this.

I'll try and use the purest form of alcohol I can find, so that heating it up doesn't also increase humidity - which would affect the readings. Have to find some kind of container which is transparent and seals well - something like a beaker with a rubber cap. Also have to heat it as little as possible to get the alcohol to vaporize, as heat affects the readings as well.

I'll get back to you guys with the info when I get around to doing this!