Smoking and alcohol are dangerous to your health. Furthermore, alcohol consumption is one of the reasons behind many anti-social incidents. This is why smoking and drinking alcohol are prohibited in many areas, as well as in schools, colleges, hospitals and public transport such as buses and trains. In fact, this type of activity should be prohibited in all types of public places. Despite the prohibition rules, there are many incidents where people are found not following the rules. In this case, for law enforcement, there must be some system to detect violation of such laws and authorities must receive a timely alert. In this tutorial, an electronic system has been designed for the same.
In this project, an implantable electronic circuit is designed that can detect alcohol consumption in public places by the smell of alcohol using the MQ3 alcohol sensor. Likewise, the programmable circuit is equipped with MQ6 smoke sensor to detect smoke in public places. The circuit has an interconnected GSM-GPRS module that sends an SMS alert to the police control room or authorized inspection body about the location of the public place where alcohol consumption or smoking is detected despite the ban. The module is also equipped with a buzzer that starts to sound when alcohol consumption or smoking is detected. The buzzer acts as a warning and continues to ring until the smell of alcohol or smoke disappears. If, despite the bell alert for a long period, tobacco or alcohol is removed from the premises, an SMS alert is sent to the authorities who can arrive at the scene to take appropriate action.
The device is built around the AVR Atmega 32. The controller is programmed to read the alcohol and smoke sensor values and invoke the audible alarm and send an SMS via GSM module as soon as the sensor values exceed the limits. The GSM module used in the project is the SIM900A. A character LCD also interfaces into the device circuitry to monitor and calibrate sensor values during device testing. The LCD module and its related code sections can be removed later when the device is finally tested and ready for deployment. The AVR controller is programmed and loaded with executable code using AVR Studio. The device built in this project is battery operated and uses a 12V battery to function. This is a portable device that can be reprogrammed when appropriate and can be installed anywhere.

Fig. 1: Alcohol and smoke detector prototype based on AVR Atmega32
Required components –

Figure 2: List of components required for the Atmega32 AVR-based alcohol and smoke detector
Block diagram –

Fig. 3: Block diagram of alcohol and smoke detector based on AVR Atmega32
Circuit Connections –
The AVR Atmega 32 microcontroller is the MCU installed in the device. MQ3 alcohol sensor, MQ6 smoke sensor, SIM900A GSM module, buzzer and LCD display are interfaced with the controller IC to build the device. The circuit connections are as follows –
Power Supply – The controller, as well as the other modules in the circuit except the GSM module, require a 5V power supply for their operation. The AVR Atmega 32 microcontroller, MQ3 alcohol sensor, MQ6 smoke sensor and character LCD run on 5V DC, while the GSM-GPRS module needs 12V power. To supply power to the circuit, a 12V battery is used. Battery power is regulated to 5V and 12V DC using 7805 and 7812 voltage regulator ICs respectively. Pin 1 of the voltage regulator ICs is connected to the battery anode and their pin 2 is connected to ground. The regulated voltage output is taken from pin 3 of the ICs. An LED along with a 10K Ω pull-up resistor is also connected between the common ground and the output pin to get a visual cue of power continuity. The output of the regulator ICs is connected to the power pins of the respective modules and controller while their ground is connected to the common ground of the circuit.
AVR Atmega 32 – This is an 8-bit AVR RISC based microcontroller. It comes in a 40-pin package and has 2 KB of RAM, 32 KB of flash memory, 1 KB EEPROM, 32 general purpose input and output (GPIO) pins, 8 10-bit ADC channels, one SPI, one UART and an on-chip TWI interface. The controller has three built-in timers, of which 2 are 8-bit timers and one is a 16-bit timer. The controller operates up to a clock frequency of 16 MHz. By executing powerful instructions in a single clock cycle, the Atmega 32 achieves transfer rates approaching 1 MIPS per MHz, allowing system designers to optimize power consumption relative to processing speed. The controller is available in a 40-pin Dual Inline Package (DIP). Check out the pin diagram and pin configuration of this AVR controller here.
In this project 13 GPIO pins of the controller are used, of which 11 pins are used to interface with character LCD, 1 pin is used to interface with MQ3 alcohol sensor and 1 pin is used for interface with the MQ6 smoke sensor. The RX and TX pins of the controller are used to interface with the GSM SIM900A module.
16X2 LCD: The 16X2 LCD display is used to monitor sensor values. It interfaces with the AVR microcontroller by connecting its data pins to the controller's B port. Character LCD data pins DB0 to DB7 interface with AVR Atmega 32 pins PB0 to PB7, respectively. The RS, RW and E pins of the LCD are connected to the PC6, PC5 and PC4 pins of the AVR respectively. Character LCD circuit connections to the AVR controller are summarized in the following table –

Fig. 4: Table listing the circuit connections between the AVR ATMega32 and the Character LCD
MQ6 Gas Sensor – The MQ6 gas sensor is a gas sensor module. The module has 4 pins for interface, of which two pins are VCC and ground, one pin is analog output and one pin is digital. The module's analog output pin is used to detect the concentration level of leaking gas or smoke and interfaces with the AVR controller's PA1 analog input pin. The sensor measures the concentration of gas or smoke in ppm according to the following formulas –
Concen = 1036.5*R^-2.392 Where
Concen is the concentration of gas or smoke in ppm
R is the ratio between Rs, the sensor resistance, and R0, which is the resistance at 1000 ppm at 20 degrees Celsius and 65% humidity
The sensor resistance Rs is given by the formulas –
Rs = (1024/ADC_DATA-1)*RL where
Rs is the sensor resistance
ADC_DATA is a digital reading that ranges from 0 to 1023
RL is a load resistance that ranges from 10K to 40K ohms
Its analog pin sensor outputs analog voltage that is proportional to the concentration of gas or smoke in ppm. The voltage is detected by the controller and converted to a digital value using the integrated ADC channel. For a fixed load resistance, the ADC reading is directly proportional to the gas concentration in ppm.
The relationship between concentration and sensor resistance is provided in the data sheet. There is a graph provided for normal conditions of 20 degrees Celsius and 65% humidity where Rs = R0 for the curve. This way the gas concentration in ppm becomes equal to the ADC reading. The AVR controller has 10-bit long ADC channels, so the ADC reading will vary between 0 and 1023. In the design, the threshold value for smoke detection is set to 200, which means that if the smoke concentration in a local exceeds 200 ppm, the controller will invoke the alert.
MQ3 Sensor – MQ3 is an analog and digital sensor that detects alcohol consumption by the smell of your breath. The sensor has four pins – Analog Output, Digital Output, VCC and Ground. VCC and ground are connected to common VCC and ground. The digital output pin is not used, so it is kept unconnected. The sensor output is taken from the analog output pin which is connected to the PA0 pin of the AVR controller. As with the gas sensor, the controller detects the analog voltage from the alcohol sensor and converts it to a digital reading using the integrated ADC channel. Again, since the ADC channels are 10 bits long in the AVR controller, the digitized reading ranges from 0 to 1023. The threshold value for alcohol detection is set to 700. When the sensor value exceeds 700, the controller invokes an alert .
SIM900A GSM Module -SIM900A is a dual-band GSM GPRS modem that operates at frequencies of 900 or 1800 MHz. The modem has an integrated UART port for connecting to a computer, SBC or controller. The modem has a configurable baud rate between 9600 and 115200. The baud rate can be set by passing AT commands to the modem via serial communication. This modem can be used to make voice calls, send and receive SMS and connect to the mobile internet. Internet connectivity on the modem is facilitated via GPRS and has a TCP/IP stack to handle Internet connectivity. The modem has four terminals – RX, TX, VCC and ground. The VCC pin must be connected to a 12V DC source. The modem has a built-in voltage regulator, so it can also be connected to any unregulated power source. The ground pin is connected to the common ground of the circuit. The RX and TX pins (for serial communication with the controller) interface with the TX and RX pins of the AVR controller. The modem sends and receives data through the UART.
Note that the character LCD is interfaced into the circuit to monitor sensor values during sensor calibration and circuit testing for the intended application. The LCD module can be removed later and the code sections written for it can also be removed from the source code if the circuit size has to be reduced.
How the circuit works –
The operation of this device is simple and straightforward. After the device is powered on by connecting a battery and installed in a location, it loads the program onto the chip. The AVR program starts reading data from the MQ3 alcohol sensor and the MQ6 smoke sensor. The analog output pins of both sensors are interfaced with the controller and the analog voltage is detected by the controller of both sensor modules. The analog voltage from both sensors is converted to digital values using integrated ADC channels. There are 10-bit ADC channels in the Atmega 32 controller, so sensor readings range from 0 to 1023. The digitized value for the smoke sensor is directly proportional to the smoke concentration in parts per million. The threshold for this sensor is set to 200, so when the smoke concentration detected by the sensor exceeds 200, the controller sends an alert and triggers the audible alarm. The digitized value of the alcohol sensor is again proportional to the gas concentration coming from the alcohol smell. The threshold value for this sensor is set to 700. As soon as the gas concentration from the alcohol smell exceeds the value of 700, the controller sends an alert and triggers the audible alarm. To test the alcohol sensor, listerine cool mint liquid is used, which contains some alcohol content.

Fig. 5: Image of AVR-based alcohol and smoke detector Atmega32
During circuit testing, sensor values are passed to the LCD display where they can be monitored for proper device calibration.
When alcohol consumption is detected by the MQ3 sensor in a location, an SMS alert is sent to a cell phone number encoded in the AVR code with the text “ALCOOL”. Likewise, when smoke is detected by the MQ6 sensor in a location, an SMS alert is sent to a cell phone number encoded in the AVR code with the text “SMOKE”. The location of the device installation is considered known, otherwise a device ID or location can also be sent via SMS to authorized security personnel. At the same time, a bell is triggered and continues to ring until the alcohol or smoke is removed from the area. The SMS is sent after a delay if smoke or alcohol is still detected after the audible alarm.
Check out the programming guide to learn how the AVR controller reads sensor values, compares them to threshold values, and sends an SMS alert and activates the buzzer when sensor values exceed their threshold.
Programming guide –
To program Atmega 32 microcontroller, AVR Studio 4 and GCC compiler are the required software tools. To learn how AVR Studio 4 is used, see the following tutorial –
Working with AVR Studio
First of all, separate header files are imported for LCD and ADC initialization. The lcd.h and adc.h are included for programming the LCD and reading data from the sensor modules via ADC, respectively.
#include
#include
For the header files to work, they must be copied to the following folder – C > WinAVR-20090313 > avr > include > avr and paste the downloaded header files into the folder.
Note that in the path WinAVR-20090313, 20090313 there is a number appended to the installation folder. This number may be different on a different AVR Studio installation.
After importing the required libraries, variables and constants are defined to denote circuit connections with the sensors and GSM module. Some arrays are defined that store the AT commands that will be used to configure the GSM module and send SMS through it.
The main function is called in which the controller pins interfaced with the LCD are defined as digital output, while the pins interfaced with the sensors are defined as analog input. The LCD and ADC channels are initialized using the lcd_init and adc_init functions and some initial messages are passed to the LCD display.
An infinite while loop is called in which the LCD is configured to display the sensor values and the sensor values are compared with the threshold values to send the appropriate SMS alert.
To send the alert message to the security room from the GSM modem, AT commands must be passed to the modem. First, AT commands must be used to set the modem baud rate for serial communication with the controller. AT commands are passed as follows –
unsigned character cmd1 ={“AT”}
The following AT is used to check the response of the GSM modem regardless of whether the serial connection is established or not.
unsigned character cmd2 ={“AT+CMGF=1”};
Here AT+CMGF=1 command is used to configure GSM modem in SMS text mode.
unsigned character cmd3 ={“AT+CMGS=”};
unsigned character cmd5 ={“094********”};
The AT+CMGS command is used to send the SMS text to the specific mobile number that is encoded in the AVR program.
unsigned char cmd4 ={“Alcohol alert detected”};
After configuring the GSM modem with AT commands, the content of the message that will be sent is mentioned.
The sendSMS function is defined to read array elements containing AT commands required to send SMS alerts and pass them to the GSM modem using serial communication. Serial communication between the modem and the controller is established by calling the usatinit function. In the usatinit function, the baud rate for serial communication is declared and the bits in the USART control and status registers B and C are set for synchronous communication. In the USART control and status register B, the receiver enable (bit 4) and transmitter enable (bit 5) are set high to enable the receiver and transmitter pins. the USART control and C status register are set for writing by setting the URSEL bit high and both the UCSZ0 and UCSZ1 character size bits are set high to configure the transmission and reception of 8-bit characters.
Check out the full code and try it out.
Project source code
###
//Program to #ifndef _ADC_H_ #define _ADC_H_ 1 #include#include // This function is declared to initialize the Analog to Digital Converter of AVR microcontrollers void adc_init(void); //This function is declared to read the digital value of the ADC conversion int read_adc_channel(unsigned char channel); /*Function definitions*/ void adc_init(void) { ADCSRA=(1< ###
Circuit diagrams
Circuit Diagram-AVR-Atmega32-Alcohol Smoke Detector | ![]() |