Turn on and off sensor MQ-3 with Arduino Uno

Hello everyone. I'm new on arduino comunity and I'm creating my first project.
My project consist in make a breathalyzer using one arduino uno, a mq-3 sensor and a led panel.
For now, i make everything work fine, i could read the sensor value, turn the int into a char and show the results on my panel.
The problem i'm facing now is that i want to make my sensor work and show the results on the panel just one time, so i can show a message with the value of the sensor like "Wow, 505?! You are too drunk!".
But, at this moment, i'm stuck on a problem, the sensor is always on and it keeps showing me the message, even after even after I have already blown, beacuse this sensor increase the value and then gradually decreases.
My question is, there is anyway where i can turn on the sensor when someone starts to blow, get the highest value, shows only this value with the message and then don't show anything untill another person starts to blow, using only this 3 things?

Here is my conde untill now

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW

#define MAX_DEVICES 4
#define DATA_PIN 12
#define CLK_PIN 11
#define CS_PIN 10

char sensorValue [20];

MD_Parola display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

void setup() {
  Serial.begin(9600);
  display.begin();

}

void loop() {
  updateData();
  Serial.println(analogRead(0), DEC);
  Serial.println(sensorValue);

  checkSensorLevel();  
}

void updateData() {
  sprintf (sensorValue, "%4d", analogRead(0));
}

void checkSensorLevel() {
  if (analogRead(0) > 100) {
    Serial.println("Entrou aqui!");
    if (display.displayAnimate())     
      updateData();
      display.displayText(sensorValue, PA_CENTER, 50, 0, PA_NO_EFFECT, PA_NO_EFFECT);
      display.displayAnimate();
      delay(4000);
  } else {
    showBafometro();
  }
}

void showBafometro() {
  Serial.println("Entrou aqui 2!");
  if (display.displayAnimate())
    display.displayText("Barfometro       ", PA_CENTER, 50, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
    display.displayAnimate();
}

Does the sensor reading change if someone blows who was not been drinking alcohol? What value of readings do you see before, during and after they blow?

If there are no changes, the arduino will not know that someone has blown and cannot display any message. Is this ok?

If someone who has been drinking alcohol blows, you can have the code detect the highest value, until the value begins to fall again.

Your code may need to smooth the readings because, otherwise, noise in the readings could give false readings.

I suggest you change the code to only print the sensor reading and use the Serial Plotter to see what range of readings are seen for drunk and sober users and post pictures of the Serial Plotter here for the forum to see. The forum can then suggest code changes to detect the peak reading.

Hi Paul.
When the sensor is idle, the readings shows values between 60 and 90. It depends the enviroment where it is, if is a closed room, an open field, or a room where someone is smoking, for example.

When someone who was not drinking alcohol blows, the sensor shows a little diference on the readings, so, if it is reading 65 on idle, it will read 70 with this blow. It's a very low difference, but it changes.

If someone who has been drinking blows, the sensor increase the value very fast, but then, it take some time to decrease.

I understand what you tell me about smooth my readings to create a range and i'll considerate that. But my problem continues. Because of the loop function happens all time, the values keep updating, i can't isolate a time box, like a reading of 4 seconds and show the highest result in this time.

So, i'm ok with the condition that i need some change to start a code that will make the reading i need, but if i coud ignore all the values when this code isn't running, it'll be great. I though on turno off the sensor as one way to solve the problem, but if i can solve it with code, it's fine too.

I'll post a video or pics with the code running.

I took this 3 prints. The first one is the sensor on idle. The second, i was smoking, didn't drink, and blow the sensor. The third, i put the sensor next to a glass with Isopropyl Alcohol.

So, as you can see, the sensor shows low values on idle, it increses a little if someone sober blow, and it increses a lot with alcohol. This is what my code can do until now.

If there is anyway to isolate the readings of 4 seconds, with keep updating, and just show on the led the highest value, it'll work. I just can't do it as i'm explaning.



Please don't post images of code or serial monitor. They contain only text and that text can be copied and pasted into a post between code tags.

I asked if you could post images of serial plotter, with more frequent readings perhaps one or two per second, so the forum can see the variation in individual readings and the overall shape of the plot as it rises to the peak and then falls back to normal levels. Also please make it clear which plot is sober/drunk, that's not clear in what you posted above.

Man, the code is all on the first post and the serial monitor just show the readings, só it'll be useless to copy, so i just show what's happening. It'll not make any difference the results os serial monitor on images or tags.

My serial plotter is always clear, even when the code is running, it not show anything on that, so i don't know how it could help in this problem.

And about how results define when it reads alcohol and when it reads only air, i thought it was very clear, but let's go again. As lower the readings, more pure is the air and as high is the reading, more alcohol and less oxygen on the reading. So, lower results, more sober the person, high results, more drunk is the person. Is that clear now?

Sorry, I thought you wanted help with your project.

Good luck.

Given that scenario, where do you turn off the display?

I never turn off the display, it only change the message it's showing. For example, if there are no blow detected, i show the name of the project on the screen "Barfometro". When it reads a value over 100, it show the value of the reading.

My intention is to turn of the sensor, so it don't keep making the readings all the time, only when it detect the blow. I'm trying to solve this to not need to add a new hardware, an airflow sensor, to turn the project cheapier.

That is crazy thinking. Just ignore the readings when you want to ignore them and use them when you want to use them!

I can't, because how it keeps reading, when the value is decreasing, it will consider a reading value too. I trying to fix it with code, but in the end, i think i will need another sensor to start the reading. This way, i'll really can ready only a time box and turn off the sensor i have now.

You write as though your program is a living thing not capable of being changed. Study the logic of the program and ignore the reading when you don't want it.

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