- Wed Jan 21, 2009 2:15 pm
#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:
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)
}