MQ3 alcohol sensor connects with 3 LED colors

hallo
we are making an alcohol test mask,
trying to connect the mq3 sensor with leds responded on number of alcohol level.
now we have for sensor , but we don't know how to make codes for the sequence and output of the sensor to the different leds.
for example, if your alcohol level is over than 800, we want to make red led blink,
if your level is over than 400, yellow led blink,
if your level is lower than 400, green led blink, etc..
This is the code that we used so far. On the screen in the computer (arduino program) we can see the numbers already appear.
This is the exact gas sensor that we used:
MQ-3 Gas Sensor Module - MQ3
CODE:

int mq3_analogPin = A0; // connected to the output pin of MQ3

void setup(){
Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
// give ample warmup time for readings to stabilize

int mq3_value = analogRead(mq3_analogPin);
Serial.println(mq3_value);

delay(100); //Just here to slow down the output.

}

Hope you guys can help us!

Try this

int mq3_analogPin = A0; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remember 100 to 1000 ohm resistor in series with each LED

void setup()
{
  pinMode(red,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(green,OUTPUT);
  Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
  // give ample warmup time for readings to stabilize

  int mq3_value = analogRead(mq3_analogPin);
  Serial.println(mq3_value);

  if (mq3_value<400)
  {
    digitalWrite(green,HIGH);
    delay(100);
    digitalWrite(green,LOW);
  }
  else if(mq3_value>800)
  {
    digitalWrite(red,HIGH);
    delay(100);
    digitalWrite(red,LOW);
  }
  else
  {
    digitalWrite(yellow,HIGH);
    delay(100);
    digitalWrite(yellow,LOW);
  }
  delay(100); //Just here to slow down the output.
}

Hi again! we are making alcohol test mask, so we are using MQ3 alcohol sensor.

We did succeed to connect 3 LED colors to arduino depends on alcohol level.

Now we want to use 3 LED Stripes.

This is the code that you replyed for us.

int mq3_analogPin = A0; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remenber 100 to 1000 ohm resistor in series with each led

void setup()
{
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
// give ample warmup time for readings to stabilize

int mq3_value = analogRead(mq3_analogPin);
Serial.println(mq3_value);

if (mq3_value<100)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
}
else if(mq3_value>500)
{
digitalWrite(red,HIGH);
delay(100);
digitalWrite(red,LOW);
}
else
{
digitalWrite(yellow,HIGH);
delay(100);
digitalWrite(yellow,LOW);
}
delay(100); //Just here to slow down the output.
}

Do you know how to connect 1 LED strip for 3 colors instead of the red, green, yellow LED each?

Also, we would like to know the code for arduino!

http://www.tinytronics.nl/shop/WS2812B-Digitale-5050-RGB-LED-Strip---60-LEDs-1m?search=led%20strip

This is the LED strip that we are using.

WS2812B ? Those are NeoPixels ? Adafruit has a good library for them.
In the link you provided they refer to the Adafruit NeoPixel guide and library.

The NeoPixel library can be installed with the Library Manager. It is in the menu of the Arduino IDE. Install the library "NeoPixel". After that some examples have been added to the menu. Try an example and see how it works.

so in the site you told me, now I found this basic code for LED stirp! :slight_smile: thank you :slight_smile:
and we want to mix this code with the code that we used for 3 RGB LEDs.

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic ‘v1’ (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();
strip.show(); // Initialize all pixels to ‘off’
}

void loop() {
// Some example procedures showing how to display to the pixels:
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(0, 0, 255), 50); // Blue
// Send a theater pixel chase in…
theaterChase(strip.Color(127, 127, 127), 50); // White
theaterChase(strip.Color(127, 0, 0), 50); // Red
theaterChase(strip.Color( 0, 0, 127), 50); // Blue

rainbow(20);
rainbowCycle(20);
theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}

void rainbow(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();

delay(wait);

for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();

delay(wait);

for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

It is the code that can make light red when the alcohol level is over 500,
light green when the alcohol level is lower than 100, and in between light yellow LED on.

int mq3_analogPin = A0; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remenber 100 to 1000 ohm resistor in series with each led

void setup()
{
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
// give ample warmup time for readings to stabilize

int mq3_value = analogRead(mq3_analogPin);
Serial.println(mq3_value);

if (mq3_value<100)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
}
else if(mq3_value>500)
{
digitalWrite(red,HIGH);
delay(100);
digitalWrite(red,LOW);
}
else
{
digitalWrite(yellow,HIGH);
delay(100);
digitalWrite(yellow,LOW);
}
delay(100); //Just here to slow down the output.
}

We are using digital LED strip 1 meter with 60 LEDs.
this one!!
http://www.tinytronics.nl/shop/WS2812B-Digitale-5050-RGB-LED-Strip---60-LEDs-1m?search=led%20strip

and we want to divide 1 meter in 3 parts as 3 colors, red, yellow, green.
Do you have an idea of the code?

I suppose that the "Adafruit_NeoPixel" can be created three times for three pins, but I think that just one strip is easier. If you cut it into three parts, connect them with wires, so the Arduino can use it as one long strip.

Both the NeoPixels and the RGB leds use a lot delay(), to be able to use them both independent of each other, you have to remove the delay() and rewrite the code for millis().

Perhaps it is possible to use the RGB leds in between the calls to the NeoPixel functions.

thank you! at the end, we will be going to use only the LED strip and MQ3 gas sensor for alcohol mask :slight_smile:
The RGB pin leds were the try out for the connection between arduino and gas sensor.
Now we know it works, so we want to work on LED strip!
We thought maybe if we just develop the code for RGB pin leds and gas sensor to LED strip and gas sensor
, we can use.

This is the code that I made for LED strip.
I developed from the code that we used for RGB pin leds.
Can you check if it is correct or not? While the compiling, Arduino program says there is a problem…:frowning:
Thank you:)

#include “FastLED.h”
#define NUM_LEDS 60
#define DATA_PIN 6
CRGB leds [NUM_LEDS];
int mq3_analogPin = A0;

void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
// put your main code here, to run repeatedly:
int mq3_value = analogRead(mq3_analogPin);
Serial.println(mq3_value);

if (mq3_value<100)
{
if(led < = 20)leds[led] =CRGB::Green;

}
else if(mq3_value>500)
{
if(led >20 & led < = 40)leds[led] =CRGB::Red;

}
else
{
if(led > 40)leds[led] =CRGB::Yellow;

}

FastLED.setBrightness(50);

delay(100); //Just here to slow down the output.
}
}

I don't know. I use a single NeoPixel led in a project, but I have forgotten how to use it. I also don't know what your code should do.
If you print the value of the MQ-3 to the serial monitor, does it give good values ? and they change with certain gasses ?

The Adafruit NeoPixel and the FastLED are the two most used libraries for those RGB NeoPixel leds.

yes! the number of the alcohol sensor shows in the serial monitor,

ok, I will check the sites thank you:)

Hi HALLO

we are making alcohol mask with MQ3 alcohol sensor!

we will going to use 1m digital LED stripe, adafriut Neopixels.

If the alcohol level is over than 500, 1-20 pixels will turn on as RED color.
100<alcohol level<500 - 21~40 YELLOW color.
under 100 alcohol level - 41~60 GREEN color.

(IN THE ATTACHED PICTURE ARDUINO IS NOT TURNED ON, WE KNOW :slight_smile: )

This is the code that we used:
#include <Adafruit_NeoPixel.h>
int mq3_analogPin = A0; // connected to the output pin of MQ3
#define PIN 6
// remenber 100 to 1000 ohm resistor in series with each led

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{

Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
// give ample warmup time for readings to stabilize

int mq3_value = analogRead(mq3_analogPin);
Serial.println(mq3_value);

if (mq3_value<100)
{
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, 0,255,0);
strip.show();
delay(50);

}
} else if(mq3_value>500)
{
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, 255,0,0);
strip.show();
delay(50);
}
}
else
{
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, 255,255,0);
strip.show();
delay(50);
}
delay(100); //Just here to slow down the output.
}}

It is not working now, and we dont know if it is because of the code or the connection to the arduino/power supply, someone can maybe help?

Thank you in advance!