Introduction: Breathalyzer in Arduino

Picture of Breathalyzer in Arduino

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.

Step 1: Buy the Materials

Picture of 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

Picture of Do This Electric Circuit

Step 3: Download U8glib Library

Picture of 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

Picture of 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

Picture of 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

Comments

EdwardM142 (author)2017-11-16

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

abhra0897 (author)2016-12-28

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);

}

}

}

PedroD91 (author)abhra08972016-12-31

Thank you so much for this :)

anagac (author)PedroD912017-04-17

Did this code work for you?

PedroD91 (author)2016-12-13

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

PedroD91 (author)PedroD912016-12-13

Please respond as soon as is convenient for you

abhra0897 (author)PedroD912016-12-28

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);

}

}

}

Ana CarolinaB2 (author)PedroD912016-12-18

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.

PedroD91 (author)Ana CarolinaB22016-12-19

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.

Sensath (author)2016-12-20

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);

}

}

}

About This Instructable

3,965views

69favorites

License:

More by Ana CarolinaB2:Breathalyzer in Arduino
Add instructable to: