Breathalyzer with LCD 16x2 I2C buzzer and push button

hi to everyone, it's the first time that i start a new topic (i'm not english so i'm sorry if i'll do some mistakes) :-X

as you can see from the title i'm trying to do a breathalyzer with a lcd 16x2 I2c a buzzer and a push button for a school project. i'm using arduino uno

i tried to follow this tutorial https://openhardwarehealthcare.com/arduino-breathalyzer/

but i've noticed that the sensor in the turorial is mq-x while should be mq-3 that has 4 pins (i think that is this the name ) gnd vcc dout and aout Scheda con sensore di GAS - MQ-3

the lcd that i've bought is this http://www.futurashop.it/display-lcd-16x2-con-interfaccia-i²c-2846-lcd16x2ai2c

this is the buzzer Buzzer con elettronica 5V - 12 mm

ad this the push button Minipulsante da circuito stampato- 6x6 mm

the resistor used is of 10k ohm as wrote on the tutorial

the first question is where i should connect the dout and aout of the mq-3 sensor than is it right the program ( arduino sketch)?
can someone if it's not right draw me the scheme of the project and the program if there are some mistakes?

i tried to make everything right ( from the photo) but when i load the program the only thing that happens is that the buzzer makes noise for 5 sec in loop, the lcd just turns on but no letters (i added the lcd 16x2 i2c library)

if you can made a better program with this components with also a better calibration should be better

thanks to everyone that will help me

here is the sketch

#include <LiquidCrystal_I2C.h>

// Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Warmup
int TIME_UNTIL_WARMUP = 20;
unsigned long time;

// Measurement
int TIME_UNTIL_MEASURE = 5;
unsigned long measurement_start;

// Sensor readings
int analogPin = A0;
int val = 0;

// Buzzer
const int buzzerPin = 7;

// Button
const int buttonPin = 2;

// Modes
bool measurement_mode;
bool measurement_done;

// LCD
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {

// Serial
Serial.begin(115200);

// Init LCD
lcd.init();
lcd.backlight();

}

void loop() {

// Read button state
int button_state = digitalRead(buttonPin);

if (button_state && !measurement_mode) {
lcd.clear();
measurement_mode = true;
measurement_start = millis()/1000;
measurement_done = false;
}

// Wait
delay(100);

// Get time
time = millis()/1000;

// Warmup
if(time<=TIME_UNTIL_WARMUP)
{
int progress_time = map(time, 0, TIME_UNTIL_WARMUP, 0, 100);
printWarming(progress_time);
}

else
{
if (measurement_mode == false && !measurement_done) {

// Instruction
printPress();
}
if (measurement_mode && !measurement_done) {

// Instruction
printMeasure();

// Sound
tone(buzzerPin, 1000);

// Read alcohol level
val = readAlcohol();

}

if (measurement_mode && !measurement_done && ((time - measurement_start)> TIME_UNTIL_MEASURE)){

noTone(buzzerPin);
measurement_mode = false;
measurement_done = true;
lcd.clear();
}

if(measurement_done) {

printAlcohol(val);
printAlcoholLevel(val);
}
}

}

void printWarming(int progress)
{
lcd.setCursor(0,0);
lcd.print("Riscaldamento: ");
lcd.print(progress);
lcd.print("%");
}

void printPress()
{
lcd.setCursor(0,0);
lcd.print("Premere per iniziare");
}

void printMeasure()
{
lcd.setCursor(0,0);
lcd.print("Soffia fino a...");
lcd.setCursor(0,1);
lcd.print("sound stops ...");
}

void printAlcohol(int value)
{
lcd.setCursor(0,0);
lcd.print("Sensor reading: ");
lcd.print(val);
}

void printAlcoholLevel(int value)
{
lcd.setCursor(0,2);
if(value<200)
{
lcd.print("You are sober.");
}
if (value>=200 && value<280)
{
lcd.print("You had a beer.");
}
if (value>=280 && value<350)
{
lcd.print("Two or more beers.");
}
if (value>=350 && value <450)
{
lcd.print("Too much ...");
}
if(value>450)
{
lcd.print("You are drunk!");
}
}

int readAlcohol()
{

// Number measurements
int nb_measurements = 5;
int measurements;

// Measure
for (int i = 0; i < nb_measurements; i++) {
measurements = measurements + analogRead(analogPin);
}
measurements = measurements/nb_measurements;

return measurements;
}

Please use code tags </> to present code.

Did you check that the LCD is accessible? It looks like you configure it for a 20 character 4 line display.
Show a start message on the LCD?

When the value is read using analogRead(analogPin), and analogPin is A0, connect the signal to A0.

DrDiettrich:
Please use code tags </> to present code.

Did you check that the LCD is accessible? It looks like you configure it for a 20 character 4 line display.
Show a start message on the LCD?

When the value is read using analogRead(analogPin), and analogPin is A0, connect the signal to A0.

sorry DrDiettrich could you edit the program directly ? because i'm not good with this kind of thing, because i've studied mechanics here in italy. the only thing that happens to the display is that the backlight turns on but nothing wrote on it ( i've tried also changing the 20,4 in 16,2 )

Check the I2C address and lines (SCL, SDA).

(deleted)

spycatcher2k:
We don't do homework here >:(

i'm not saying to do my homework, i'm asking to help me solving this because i don't study this kind of thing.
the best thing for me is a scheme ( i mean the images made with fritzing) of the photo in the first link that i put before, for being sure of the connections

than i ask to see if the sketch is correct or not

DrDiettrich:
Check the I2C address and lines (SCL, SDA).

the sda and scl are connected to pin a5 and a4

Quoting robtilaart:

SOmewhere on this page - Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino - is an I2C sniffer that can see if you use the right address, and a lot more information about I2C..

I2C Scanner is about 2/3 of the way down the page.

CrossRoads:
Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino
I2C Scanner is about 2/3 of the way down the page.

i tried that sketch but on the display just has the backlight on, nothing wrote on it

CrossRoads:
Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino
I2C Scanner is about 2/3 of the way down the page.

https://www.futurashop.it/image/catalog/data/Download/LCD1602A%2BINTERFACE.rar

this is a link from the site where i bought the lcd i2c 16x2 with some files that i don't know what are used for

i've noticed that on my arduino uno rev3 i have 2 pins called sda and scl , i connected the jumpers to them but nothing, always backlight but no letters

The I2C Scanner prints all valid I2C addresses on Serial Monitor.
What do you get?

DrDiettrich:
The I2C Scanner prints all valid I2C addresses on Serial Monitor.
What do you get?

I2C scanner. Scanning ...
Found address: 63 (0x3F)
Done.
Found 1 device(s).

So you know now that your display has the I2C address 0x3F, not 0x27. Update your sketch and try again.

DrDiettrich:
So you know now that your display has the I2C address 0x3F, not 0x27. Update your sketch and try again.

i tried it but nothing .

i tried with this test sketch

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
lcd.init(); // initialize the lcd

// Print a message to the LCD.
lcd.backlight();
lcd.print("Test Display I2C");
lcd.setCursor(0,1);
lcd.print("Versione 1.0");

}

void loop()
{
}

I could download the archive now. Have a look at "Address Reference.jpg", how to select the I2C address. The address 0x3F, found by the I2C sniffer, is not listed there. Try to select one of the listed addresses, by setting A0, A1 and A2 as listed (connect to Gnd or Vcc), then run the sniffer again. If it cannot find the selected address, something is wrong with your module.

DrDiettrich:
So you know now that your display has the I2C address 0x3F, not 0x27. Update your sketch and try again.

finally works. :grin: :grin:

my new question is it's possible and how to make the lcd write a few words for some seconds till the value of alcohol decrease under 200 for example

the same thing with a buzzer, so the tone goes for some seconds till the value of alcohol decrease

the other question is if it's possible to make the value of the mq-3 sensor can goes from, for example, 1000 (for 2 seconds) to under 200 directly

This because with the sketch that i'm using the buzzer tones, when the value is between 200 and 400 the buzzer tones with 500 hz , over 400 with 1000hz but it tones 1000hz till the value decrease to 400 and needs some time to decrease.
I tried writing tone(1000, 2000) but after 2 sec it restart making noise for 2 sec

thanks

I have the right code, and everything is working with the breathalyzer and the lcd, however, when the beep comes up and we breathe into the sensor for a few seconds the lcd gets stuck at "Sensor reading:" and nothing else comes up. Any suggestions? Could the MQ-3 be faulty?

OK, given two choices:

  • MQ-3 is faulty
  • Code is faulty
    I know which one I am picking! :grinning: