19
OLED Breathalyzer

In this project we’ll use the MQ3 alcohol sensor and an OLED LCD screen to make a mini-breathalyzer.

Image
Image

PARTS REQUIRED

Arduino board

Female-to-male jumper wires

Keyes MQ3 alcohol sensor module

OLED monochrome screen (128×64)

LIBRARIES REQUIRED

SPI

Wire

Adafruit_GFX

Adafruit_SSD1306

HOW IT WORKS

The MQ3 is part of a family of gas sensors that also includes the MQ2, sensitive to methane, butane, and smoke; the MQ4, sensitive to compressed natural gas; the MQ6, sensitive to butane and LPG gas; and the MQ7, sensitive to carbon monoxide. The MQ3 is sensitive to alcohol and ethanol, so it’s the one we’ll use in our breathalyzer.

DISCLAIMER

This project is for amusement only and should not be used to accurately determine anyone’s alcohol intake.

The Keyes MQ3 module (Figure 19-1) has the wiring we need for this project, including a built-in potentiometer and resistor. The three pin connections on the module are OUT, VCC, and GND.

FIGURE 19-1: The Keyes MQ3 alcohol sensor module. As with most MQ sensors, the module has a small heater inside with an electrochemical sensor used to measure the gas level. The value of the reading is sent to the OUT pin, which is then read by an analog pin on our Arduino.

Image

To display the sensor readings, we’ll use an OLED screen (Figure 19-2). OLED, which stands for organic light-emitting diode, is a light-emitting technology composed of a thin, multilayered organic film placed between an anode and cathode. When voltage is applied, an image is created through electroluminescence, which means the screen does not require a backlight. Our OLED screen is an I2C 128×64 monochrome version, meaning we can control it using only two pins to the Arduino and it measures 128 pixels by 64 pixels. This screen uses the same communication protocol as our serial LCD in Project 16 and is explained further there.

FIGURE 19-2: 128×64 OLED monochrome screen. When the MQ3 reads the value, the Arduino sends a message to the OLED screen indicating whether or not alcohol has been detected.

Image

WARNING

As mentioned earlier, the MQ3 uses an internal heater as part of the sensor process. This heater can reach 120–140 degrees when powered, so take care when handling it when it’s in use.

THE BUILD

  1. Before you use the sensor for the first time, you need to “burn it in.” This process, which simply involves powering it up for a few hours to heat the mechanism inside, improves the sensor’s accuracy. To do this, connect the VCC and GND pins of the sensor to +5V and GND on your Arduino, respectively, using female-to-male jumper wires. When you power the Arduino, it will send the correct voltage to the MQ3. Leave it powered for two to three hours—you may notice a burning smell and the sensor will get hot, but this is normal.

  2. Once the sensor is burned in, disconnect the power to the Arduino and connect the sensor to the Arduino using the female-to-male jumper wires, with the MQ3’s OUT pin connected to Arduino pin A0, and the power and GND still connected as before (see the following table).

    MQ3 ALCOHOL SENSOR

    ARDUINO

    OUT

    Pin A0

    VCC

    +5V

    GND

    GND

  3. Next, connect the OLED screen to the Arduino as shown in the following table, with SCL connected to pin A5, SDA to pin A4, VCC to +3.3V, and GND to GND.

    OLED SCREEN

    ARDUINO

    SCL

    Pin A5

    SDA

    Pin A4

    VCC

    +3.3V

    GND

    GND

  4. This project requires a number of libraries to work correctly; the SPI and Wire libraries are built into the Arduino IDE, but we also need the Adafruit_GFX and Adafruit_SSD1306 libraries to control the OLED screen. Both are available from https://www.nostarch.com/arduinohandbook2/. Refer to the primer if you need a reminder on how to add libraries to the IDE.

  5. Check that your setup matches the circuit diagram in Figure 19-3, and upload the code in “The Sketch” below.

    FIGURE 19-3: The circuit diagram for the OLED breathalyzer

    Image
  6. The heater inside the MQ3 sensor needs to heat up for about 4 minutes before it can operate accurately. The sketch has a timer so that when you power it up for the first time, the values won’t appear onscreen until the required time has passed. The “Warming up” text will display with a small countdown bar until the sensor is ready.

THE SKETCH

The sketch starts by calling on the SPI, Wire, Adafruit_GFX, and Adafruit_SSD1306 libraries to control communication and the OLED screen. We assign a time for the warm-up session (4 minutes) and set the analog pin as Arduino A0.

Next we set up the OLED screen. The Arduino sends different messages to the screen depending on the value read from the analog pin. For instance, if the sensor reading is above 200, the Arduino will ask you if you’ve had a beer. If the reading is below this value, the Arduino will say you’re sober. The minimum level of alcohol the MQ3 will read is about 180. For anything over 450, the breathalyzer will let you know you’re drunk!

The sketch loops every second to read the analog sensor. To use the breathalyzer, wait for the sensor to heat up for 4 minutes, then gently breathe onto the sensor. Try not to get the sensor wet or expose it to a smoky environment, as this will affect the reading.

// Re-created with kind permission from Nick Koumaris educ8s.tv

// Call the SPI, Wire, Adafruit_GFX, and Adafruit_SDD1306 libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4 // Define the OLED screen
int TIME_UNTIL_WARMUP = 4; // Time for the warm-up delay in minutes
unsigned long time;
int analogPin = 0; // Set analog pin as A0
int val = 0; // Set a value to read from the analog pin
Adafruit_SSD1306 display(OLED_RESET);

void setup() { // Set up the OLED screen
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}

void loop() { // Take the reading and show it onscreen
  delay(100);
  val = readAlcohol();
  printTitle();
  printWarming();
  time = millis() / 1000;
  time /= 60;
  if (time <= TIME_UNTIL_WARMUP) { // If warm-up is less than 4 mins
    time = map(time, 0, TIME_UNTIL_WARMUP, 0, 100); // Show countdown
    display.drawRect(10, 50, 110, 10, WHITE); // Empty bar
    display.fillRect(10, 50, time, 10, WHITE);
  } else { // When warm-up time has passed
           // the value and message are printed on the screen
     printTitle();
     printAlcohol(val);
     printAlcoholLevel(val);
  }
  display.display();
}

void printTitle() { // Position and text of title on the screen
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(22, 0);
  display.println("Breath Analyzer");
}
void printWarming() { // Warm-up message
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(30, 24);
  display.println("Warming up");
}

void printAlcohol(int value) { // Print alcohol value to screen
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(50, 10);
  display.println(val);
}

void printAlcoholLevel(int value) { // Print message to screen
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20, 25);
  if (value < 200) { // If value read is less than 200, you are sober
    display.println("You are sober...");
  }
  if (value >= 200 && value < 280) {
    display.println("You had a beer?");
  }
  if (value >= 280 && value < 350) {
    display.println("Two or more beers.");
  }
  if (value >= 350 && value < 450) {
    display.println("I smell VODKA!");
  }
  if (value > 450) {
     display.println("You are drunk!");
  }
}

// Finds average by summing three readings and
// dividing by 3 for better accuracy
int readAlcohol() {
  int val = 0;
  int val1;
  int val2;
  int val3;
  display.clearDisplay();
  val1 = analogRead(analogPin);
  delay(10);
  val2 = analogRead(analogPin);
  delay(10);
  val3 = analogRead(analogPin);
  val = (val1 + val2 + val3) / 3;
  return val;
}

TROUBLESHOOTING

Q. The display is not showing readings correctly.

• Recheck that your wiring matches the diagram in Figure 19-3.

• If all your wiring is in the correct place, make sure you’ve carried out the earlier step to burn the sensor in by leaving it powered for a few hours.

• To check whether your components are faulty, temporarily swap a potentiometer in for the sensor. Connect the center pin of the potentiometer to A0 and add power to either side. If the potentiometer is working okay, it means your sensor is probably faulty, so replace your sensor—they are very inexpensive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.195.24