Breathalyzer in Arduino

9,616

77

10

This is DIY for a breathalyzer device, for its implementation will be used the Arduino development platform, a microcontroller that provides a large amount of study material, due to its popularity in academic world. An alcohol sensor will be used to make alcohol measurements and a liquid crystal display (LCD) and LEDs to present the results to the user.

The code is in portuguese if you want you can change it.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Buy the Materials

- Arduino Uno

- MQ-3 Alcohol Sensor

- LCD 128x64

- 7x 330 Ohm Resistor

- 7x LEDs (1 Red, 2 Yellow, 3 Green and 1 other color)

Wires or Jumpers Wires

Breadboard or a Prototype Boards Perforated

If you use a Prototype Boards Perforated you will need:

-Soldering Iron

-Solder Wire

Step 2: Do This Electric Circuit

Step 3: Download U8glib Library

This library is important for the operation of the display.

You can download a version of this library from this site: https://code.google.com/archive/p/u8glib/downloads

After download you need paste this folder on Arduino libraries folder, like the image.

Step 4: The Code

#include "U8glib.h"

U8GLIB_ST7920_128X64_1X u8g(5, 4, 3 ,6);

#define qtdLED 6

int LEDs[qtdLED];

int tempo;

int aquecimento = 300;

int valor_sensor = 0;

unsigned long time;

int status = 1;

String estado;

int posicao1;

int i;

int j;

int maximolido;

void draw(){

u8g.setFont(u8g_font_8x13B);

u8g.drawRFrame(0, 20, 128, 44, 4);

u8g.drawStr( 30, 10, "BAFOMETRO");

u8g.setFont(u8g_font_robot_de_niro);

u8g.drawStr( 8, 20, "Cuidado voce pode ser preso");

u8g.setFont(u8g_font_8x13B);

u8g.drawStr( 10, 37, "Aguarde");

if (status == 1){

u8g.drawBox(80, 25, 20, 30);

u8g.drawHLine(77, 24, 26);

u8g.drawRFrame(78, 25 , 24, 32, 0);

u8g.drawRFrame(77, 25 , 26, 32, 0);

u8g.drawHLine(76, 57, 28);

u8g.drawHLine(76, 58, 28);

u8g.drawRFrame(102, 30 , 7, 20, 2);

u8g.drawRFrame(102, 28 , 9, 24, 2);

u8g.setColorIndex(0);

if (time == 0){

time = 1;

}

u8g.drawBox(79, 25, 22, time);

u8g.setColorIndex(1);

}

if (status == 0){

u8g.setFont(u8g_font_fub20);

u8g.setColorIndex(0);

u8g.drawBox(10, 25, 110, 33);

u8g.setColorIndex(1);

if (valor_sensor <= 99){

posicao1 = 50;

}

else{

posicao1 = 43;

}

u8g.setPrintPos(posicao1, 45);

u8g.print(valor_sensor);

u8g.setFont(u8g_font_orgv01);

int tamanho = estado.length();

int posicao = (128/2) - ((tamanho * 5) / 2);

u8g.setPrintPos(posicao, 60);

u8g.print(estado);

}

}

void setup(void){

Serial.begin(9600);

pinMode(A0, INPUT);

if ( u8g.getMode() == U8G_MODE_R3G3B2 ){

u8g.setColorIndex(255);

}

else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {

u8g.setColorIndex(3);

}

else if ( u8g.getMode() == U8G_MODE_BW ) {

u8g.setColorIndex(1);

}

else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {

u8g.setHiColorByRGB(255, 255, 255);

}

i=0;

j=7;

while(i < qtdLED){

LEDs[i] = j;

i++;

j++;

}

for(i=0;i

pinMode(LEDs[i], OUTPUT);

}

digitalWrite(2, OUTPUT);

}

void loop(void){

valor_sensor = analogRead(A0);

time = millis() / 1000;

u8g.firstPage();

if (time <= aquecimento){

time = map(time, 0, aquecimento, 0, 30);

status = 1;

}

else{

status = 0;

}

if (valor_sensor >= 0 and valor_sensor <= 100){

estado = "Voce esta salvo";

}

else if (valor_sensor >= 101 and valor_sensor <= 200){

estado = "Bebeu cerveja que eu sei";

}

else if (valor_sensor >= 201 and valor_sensor <= 300){

estado = "Virou uma tequila neh";

}

else if (valor_sensor >= 301 and valor_sensor <= 400){

estado = "Bafo de onca";

}

else if (valor_sensor >= 401 and valor_sensor <= 550){

estado = "JACINTO voce na cadeia";

}

else if (valor_sensor >= 551){

estado = "Voce esta preso";

}

do{

draw();

}

while (u8g.nextPage() );

delay(50);

//LED

int sensor = analogRead(A0);

if(sensor >= 40){

digitalWrite(2, HIGH);

maximolido = 0;

for(tempo=0;tempo<=500;tempo++){

int sensor = analogRead(A0);

delay(1);

if(sensor > maximolido){

maximolido = sensor;

}

}

digitalWrite(2, LOW);

int nivel = map(maximolido, 0, 551, 0, qtdLED);

for(i=0;i

if (i < nivel){

digitalWrite(LEDs[i], HIGH);

delay(100);

}else{

digitalWrite(LEDs[i], LOW);

}

}

delay(500);

for(i=0;i

digitalWrite(LEDs[i],LOW);

}

}

}

Step 5: If You Want Do or Buy a Box

We did a wood box but you can do with your materials or buy a project box.

Step 6: Test Your Breathalyzer

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    10 Discussions

    0
    None
    EdwardM142

    1 year ago

    Hello there ma'am Ana creator of the awesome project can u please upload the video on how to assemble it? We just chosed this as our project. Please respond as soon as possible

    0
    None
    abhra0897

    2 years ago

    The code written by the author is completely buggy. I have debugged it and converted into English.. It's verified by Arduino IDE without any error.

    .

    .

    .

    #include "U8glib.h"

    U8GLIB_ST7920_128X64_1X u8g(5, 4, 3 ,6);

    #define qtdLED 6

    int LEDs[qtdLED];

    int count;

    int value_sensor = 0;

    unsigned long time;

    int maxTime = 300;

    int status = 1;

    String state;

    int position1;

    int i;

    int j;

    int maximumReading;

    void draw(){

    u8g.setFont(u8g_font_8x13B);

    u8g.drawRFrame(0, 20, 128, 44, 4);

    u8g.drawStr( 30, 10, "BREATHALYZER");

    u8g.setFont(u8g_font_robot_de_niro);

    u8g.drawStr( 8, 20, "Careful You can be arrested");

    u8g.setFont(u8g_font_8x13B);

    u8g.drawStr( 10, 37, "Wait");

    if (status == 1){

    u8g.drawBox(80, 25, 20, 30);

    u8g.drawHLine(77, 24, 26);

    u8g.drawRFrame(78, 25 , 24, 32, 0);

    u8g.drawRFrame(77, 25 , 26, 32, 0);

    u8g.drawHLine(76, 57, 28);

    u8g.drawHLine(76, 58, 28);

    u8g.drawRFrame(102, 30 , 7, 20, 2);

    u8g.drawRFrame(102, 28 , 9, 24, 2);

    u8g.setColorIndex(0);

    if (time == 0){

    time = 1;

    }

    u8g.drawBox(79, 25, 22, time);

    u8g.setColorIndex(1);

    }

    if (status == 0){

    u8g.setFont(u8g_font_fub20);

    u8g.setColorIndex(0);

    u8g.drawBox(10, 25, 110, 33);

    u8g.setColorIndex(1);

    if (value_sensor <= 99){

    position1 = 50;

    }

    else{

    position1 = 43;

    }

    u8g.setPrintPos(position1, 45);

    u8g.print(value_sensor);

    u8g.setFont(u8g_font_orgv01);

    int size = state.length();

    int position = (128/2) - ((size * 5) / 2);

    u8g.setPrintPos(position, 60);

    u8g.print(state);

    }

    }

    void setup(void){

    Serial.begin(9600);

    pinMode(A0, INPUT);

    if ( u8g.getMode() == U8G_MODE_R3G3B2 ){

    u8g.setColorIndex(255);

    }

    else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {

    u8g.setColorIndex(3);

    }

    else if ( u8g.getMode() == U8G_MODE_BW ) {

    u8g.setColorIndex(1);

    }

    else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {

    u8g.setHiColorByRGB(255, 255, 255);

    }

    i=0;

    j=7;

    while(i < qtdLED){

    LEDs[i] = j;

    i++;

    j++;

    }

    for (i = 0; i < qtdLED; i++)

    {

    pinMode(LEDs[i], OUTPUT);

    }

    digitalWrite(2, OUTPUT);

    }

    void loop(void){

    value_sensor = analogRead(A0);

    time = millis() / 1000;

    u8g.firstPage();

    if (time <= maxTime){

    time = map(time, 0, maxTime, 0, 30);

    status = 1;

    }

    else{

    status = 0;

    }

    if (value_sensor >= 0 and value_sensor <= 100){

    state = "You are safe";

    }

    else if (value_sensor >= 101 and value_sensor <= 200){

    state = "I know that you drank beer";

    }

    else if (value_sensor >= 201 and value_sensor <= 300){

    state = "Drank tequila, hm?";

    }

    else if (value_sensor >= 301 and value_sensor <= 400){

    state = "Very bad breath";

    }

    else if (value_sensor >= 401 and value_sensor <= 550){

    state = "You may go to jail";

    }

    else if (value_sensor >= 551){

    state = "You are trapped completely";

    }

    do{

    draw();

    }

    while (u8g.nextPage() );

    delay(50);

    //LED

    int sensor = analogRead(A0);

    if(sensor >= 40){

    digitalWrite(2, HIGH);

    maximumReading = 0;

    for(count=0;count<=500;count++){

    int sensor = analogRead(A0);

    delay(1);

    if(sensor > maximumReading){

    maximumReading = sensor;

    }

    }

    digitalWrite(2, LOW);

    int level = map(maximumReading, 0, 551, 0, qtdLED);

    for (i = 0; i < qtdLED; i++) {

    if (i < level){

    digitalWrite(LEDs[i], HIGH);

    delay(100);

    }else{

    digitalWrite(LEDs[i], LOW);

    }

    }

    delay(500);

    for (i = 0; i < qtdLED; i++) {

    digitalWrite(LEDs[i],LOW);

    }

    }

    }

    2 replies
    0
    None
    anagacPedroD91

    Reply 2 years ago

    Did this code work for you?

    0
    None
    PedroD91

    2 years ago

    I entered your code into Arduino and it is riddled with errors... How exactly did you get this to work? If you made changes to the code please tell me, thank you

    4 replies
    0
    None
    PedroD91PedroD91

    Reply 2 years ago

    Please respond as soon as is convenient for you

    0
    None
    abhra0897PedroD91

    Reply 2 years ago

    I debugged the code and converted it into English. You may try it. (Working code).

    .

    .

    .

    #include "U8glib.h"

    U8GLIB_ST7920_128X64_1X u8g(5, 4, 3 ,6);

    #define qtdLED 6

    int LEDs[qtdLED];

    int count;

    int value_sensor = 0;

    unsigned long time;

    int maxTime = 300;

    int status = 1;

    String state;

    int position1;

    int i;

    int j;

    int maximumReading;

    void draw(){

    u8g.setFont(u8g_font_8x13B);

    u8g.drawRFrame(0, 20, 128, 44, 4);

    u8g.drawStr( 30, 10, "BREATHALYZER");

    u8g.setFont(u8g_font_robot_de_niro);

    u8g.drawStr( 8, 20, "Careful You can be arrested");

    u8g.setFont(u8g_font_8x13B);

    u8g.drawStr( 10, 37, "Wait");

    if (status == 1){

    u8g.drawBox(80, 25, 20, 30);

    u8g.drawHLine(77, 24, 26);

    u8g.drawRFrame(78, 25 , 24, 32, 0);

    u8g.drawRFrame(77, 25 , 26, 32, 0);

    u8g.drawHLine(76, 57, 28);

    u8g.drawHLine(76, 58, 28);

    u8g.drawRFrame(102, 30 , 7, 20, 2);

    u8g.drawRFrame(102, 28 , 9, 24, 2);

    u8g.setColorIndex(0);

    if (time == 0){

    time = 1;

    }

    u8g.drawBox(79, 25, 22, time);

    u8g.setColorIndex(1);

    }

    if (status == 0){

    u8g.setFont(u8g_font_fub20);

    u8g.setColorIndex(0);

    u8g.drawBox(10, 25, 110, 33);

    u8g.setColorIndex(1);

    if (value_sensor <= 99){

    position1 = 50;

    }

    else{

    position1 = 43;

    }

    u8g.setPrintPos(position1, 45);

    u8g.print(value_sensor);

    u8g.setFont(u8g_font_orgv01);

    int size = state.length();

    int position = (128/2) - ((size * 5) / 2);

    u8g.setPrintPos(position, 60);

    u8g.print(state);

    }

    }

    void setup(void){

    Serial.begin(9600);

    pinMode(A0, INPUT);

    if ( u8g.getMode() == U8G_MODE_R3G3B2 ){

    u8g.setColorIndex(255);

    }

    else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {

    u8g.setColorIndex(3);

    }

    else if ( u8g.getMode() == U8G_MODE_BW ) {

    u8g.setColorIndex(1);

    }

    else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {

    u8g.setHiColorByRGB(255, 255, 255);

    }

    i=0;

    j=7;

    while(i < qtdLED){

    LEDs[i] = j;

    i++;

    j++;

    }

    for (i = 0; i < qtdLED; i++)

    {

    pinMode(LEDs[i], OUTPUT);

    }

    digitalWrite(2, OUTPUT);

    }

    void loop(void){

    value_sensor = analogRead(A0);

    time = millis() / 1000;

    u8g.firstPage();

    if (time <= maxTime){

    time = map(time, 0, maxTime, 0, 30);

    status = 1;

    }

    else{

    status = 0;

    }

    if (value_sensor >= 0 and value_sensor <= 100){

    state = "You are safe";

    }

    else if (value_sensor >= 101 and value_sensor <= 200){

    state = "I know that you drank beer";

    }

    else if (value_sensor >= 201 and value_sensor <= 300){

    state = "Drank tequila, hm?";

    }

    else if (value_sensor >= 301 and value_sensor <= 400){

    state = "Very bad breath";

    }

    else if (value_sensor >= 401 and value_sensor <= 550){

    state = "You may go to jail";

    }

    else if (value_sensor >= 551){

    state = "You are trapped completely";

    }

    do{

    draw();

    }

    while (u8g.nextPage() );

    delay(50);

    //LED

    int sensor = analogRead(A0);

    if(sensor >= 40){

    digitalWrite(2, HIGH);

    maximumReading = 0;

    for(count=0;count<=500;count++){

    int sensor = analogRead(A0);

    delay(1);

    if(sensor > maximumReading){

    maximumReading = sensor;

    }

    }

    digitalWrite(2, LOW);

    int level = map(maximumReading, 0, 551, 0, qtdLED);

    for (i = 0; i < qtdLED; i++) {

    if (i < level){

    digitalWrite(LEDs[i], HIGH);

    delay(100);

    }else{

    digitalWrite(LEDs[i], LOW);

    }

    }

    delay(500);

    for (i = 0; i < qtdLED; i++) {

    digitalWrite(LEDs[i],LOW);

    }

    }

    }

    0
    None
    Ana CarolinaB2PedroD91

    Reply 2 years ago

    Really sorry, I forgot one important thing before the code that is the u8glib library. Now I put one step more before the code explain this. If something get wrong or if this isn't the problem you can contact with me again I'll do everything that I can to help you.

    0
    None
    PedroD91Ana CarolinaB2

    Reply 2 years ago

    Thank you for your response. I am doing this for a school project due this Wednesday that is quite important and thus time is of the essence so your response means a lot. However, I copied the code into my Arduino program and attempted to download it to my uno but I could not get past the verification step as it was asking for many parenthases and semicolons to be added and other various forms of syntax errors all over the place. If I am doing something wrong then being guided would be much appreciated.

    0
    None
    Sensath

    2 years ago

    I think the code has some errors in the for loops. Is this correct?

    #include "U8glib.h"

    U8GLIB_ST7920_128X64_1X u8g(5, 4, 3 , 6);

    #define qtdLED 6

    int LEDs[qtdLED];

    int tempo;

    int aquecimento = 300;

    int valor_sensor = 0;

    unsigned long time;

    int status = 1;

    String estado;

    int posicao1;

    int i;

    int j;

    int maximolido;

    void draw() {

    u8g.setFont(u8g_font_8x13B);

    u8g.drawRFrame(0, 20, 128, 44, 4);

    u8g.drawStr( 30, 10, "BAFOMETRO");

    u8g.setFont(u8g_font_robot_de_niro);

    u8g.drawStr( 8, 20, "Cuidado voce pode ser preso");

    u8g.setFont(u8g_font_8x13B);

    u8g.drawStr( 10, 37, "Aguarde");

    if (status == 1) {

    u8g.drawBox(80, 25, 20, 30);

    u8g.drawHLine(77, 24, 26);

    u8g.drawRFrame(78, 25 , 24, 32, 0);

    u8g.drawRFrame(77, 25 , 26, 32, 0);

    u8g.drawHLine(76, 57, 28);

    u8g.drawHLine(76, 58, 28);

    u8g.drawRFrame(102, 30 , 7, 20, 2);

    u8g.drawRFrame(102, 28 , 9, 24, 2);

    u8g.setColorIndex(0);

    if (time == 0) {

    time = 1;

    }

    u8g.drawBox(79, 25, 22, time);

    u8g.setColorIndex(1);

    }

    if (status == 0) {

    u8g.setFont(u8g_font_fub20);

    u8g.setColorIndex(0);

    u8g.drawBox(10, 25, 110, 33);

    u8g.setColorIndex(1);

    if (valor_sensor <= 99) {

    posicao1 = 50;

    }

    else {

    posicao1 = 43;

    }

    u8g.setPrintPos(posicao1, 45);

    u8g.print(valor_sensor);

    u8g.setFont(u8g_font_orgv01);

    int tamanho = estado.length();

    int posicao = (128 / 2) - ((tamanho * 5) / 2);

    u8g.setPrintPos(posicao, 60);

    u8g.print(estado);

    }

    }

    void setup() {

    Serial.begin(9600);

    pinMode(A0, INPUT);

    if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {

    u8g.setColorIndex(255);

    }

    else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {

    u8g.setColorIndex(3);

    }

    else if ( u8g.getMode() == U8G_MODE_BW ) {

    u8g.setColorIndex(1);

    }

    else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {

    u8g.setHiColorByRGB(255, 255, 255);

    }

    i = 0;

    j = 7;

    while (i < qtdLED) {

    LEDs[i] = j;

    i++;

    j++;

    }

    for (i = 0; i < qtdLED; i++) {

    pinMode(LEDs[i], OUTPUT);

    }

    digitalWrite(2, OUTPUT);

    }

    void loop() {

    valor_sensor = analogRead(A0);

    time = millis() / 1000;

    u8g.firstPage();

    if (time <= aquecimento) {

    time = map(time, 0, aquecimento, 0, 30);

    status = 1;

    } else status = 0;

    if (valor_sensor >= 0 and valor_sensor <= 100) {

    estado = "Voce esta salvo";

    } else if (valor_sensor >= 101 and valor_sensor <= 200) {

    estado = "Bebeu cerveja que eu sei";

    } else if (valor_sensor >= 201 and valor_sensor <= 300) {

    estado = "Virou uma tequila neh";

    } else if (valor_sensor >= 301 and valor_sensor <= 400) {

    estado = "Bafo de onca";

    } else if (valor_sensor >= 401 and valor_sensor <= 550) {

    estado = "JACINTO voce na cadeia";

    } else if (valor_sensor >= 551) {

    estado = "Voce esta preso";

    }

    do {

    draw();

    }

    while (u8g.nextPage());

    delay(50);

    //LED

    int sensor = analogRead(A0);

    if (sensor >= 40) {

    digitalWrite(2, HIGH);

    maximolido = 0;

    for (tempo = 0; tempo <= 500; tempo++) {

    int sensor = analogRead(A0);

    delay(1);

    if (sensor > maximolido) {

    maximolido = sensor;

    }

    }

    digitalWrite(2, LOW);

    int nivel = map(maximolido, 0, 551, 0, qtdLED);

    for (i = 0; i < qtdLED; i++) {

    if (i < nivel) {

    digitalWrite(LEDs[i], HIGH);

    delay(100);

    } else {

    digitalWrite(LEDs[i], LOW);

    }

    }

    delay(500);

    for (i = 0; i < qtdLED; i++) {

    digitalWrite(LEDs[i], LOW);

    }

    }

    }