Introduction: 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
- 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.