# Description of Code and Circuit

A project log for The Road to Zero Tolerance

The purpose of this project was to create a device that would be an improvement on the ignition interlock device.

Joy Shah 07/28/2015 at 18:250 Comments

The Circuit:

To power our microcontroller, we required 5V DC. Since we were plugged into a wall outlet we were receiving 120V AC. To convert the 120V AC to 5V DC we used a 5V AC wall adaptor or a "wall wart". The way the wall wart works is that inside of it, are two wire windings that wrap around a single iron core. When the first winding receives the 120V AC supplied from the wall outlet it creates an electric field in the iron core. The second wire winding converts the newly created electric field into a smaller alternating current. The voltage of the new current depends on the ratio of the number of coils in the first winding in comparison to the second winding; therefore if the second winding has half as many coils as the first winding, the resulting voltage will be half that of the original voltage. Now the question remains how to convert the AC current into a DC current? The answer to this is that behind the two windings and the iron core are two rubber wrapped diodes that convert AC to DC by allowing the current to flow in one direction. Even though the wall wart was supposed to supply 5V DC, it wasn’t. This is because a wall wart uses cheap tricks to get the voltage to drop. We actually got an output of around 9V DC that was very noisy, meaning there was a ripple and the output would fluctuate.

To get the voltage down to 5V DC and eliminate the ripple, we had to use a regulator. The regulator we used was the LM7805 with a TO-220 package. Our regulator is classified as a linear regulator. The way a linear regulator works is very simple, it has three pins: an input pin, a ground, and an output pin. The input voltage could receive any voltage between 7V and 30V. In our case the regulator had an input of about 9V. When it received the voltage it converted it to a steady output of 5V. The regulator takes the input voltage of 9V and then turns it into an output of 5V by turning the difference between the input and output in to waste heat energy.

Next we hooked up a switch to the output of our regulator to allow us to easily turn power on and off. After the switch, we had a LED, along with a 330 ohm resistor to protect the LED from too much current, which was plugged into our 5V power supply and then to ground. This is to show us that we are getting power to our circuit rather than using a multimeter every time we needed to check if there was power. It was also a safety requirement for the science fair.

Once we had verified that we had a working 5V power supply, we hooked up our microcontroller. The microcontroller has 4 ports: A, B, C, and D. Port A is powered separately from Port B, C, and D. So the first thing we did was power all four ports up by connecting the VCC (power) pins to the 5V power supply and connecting the ground pins to ground. Once the microcontroller was powered up and working, we had to program it. To program it we used the AVR Pocket Programmer which had six pins: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (Programming Clock), RESET, VCC (Power), and GND (Ground). To connect the pocket programmer to the microcontroller we used jumper wires. Once we could program the microcontroller, we hooked up three LEDs with a 330 ohm resistor. The LEDs went to port B in pins PB0, PB1, and PB2. This is because port B has simple I/O (input/output) pins. Now depending on the output of the alcohol sensor, either the green, yellow, or red LED would turn on.

Next, we connected the MQ-3 alcohol sensor to our microcontroller. The MQ-3 alcohol sensor has six pins; 2 go to the 5 volt power supply and two go to ground. One of the ground pins also goes to the microcontroller, and we chose to put it into port A. Port A was actually the only choice as it was the only port that can do analog to digital conversion (ADC). This was a must as the sensor has an output of analog information and the microcontroller understands digital.

Lastly we connected a motor to the microcontroller to represent the engine of a car that would shut down when alcohol is detected. We connected the motor to port C for easy access as again we needed simple I/O pins.

Description of Code:

To program the microcontroller, we installed winAVR from the internet. In that file we got a lot of different programs. Out of them we used Programmers Notepad and MFile. The notepad program is the IDE (integrated development environment) where we typed the code. The code created in the notepad program will be a .c file.

The include statements at the top of the code add the header files that we will be using during the code. The header file has a lot of code that is important for the microcontroller to work. Using a header file makes your actual code a lot smaller. The “define” command is used to set the value of the yellow limit and the green limit. The yellow and green limits were set using known alcohol values. It is also used tell the microcontroller that the motor is plugged into PORTC. The next line of code creates the routine that will initialize the analog digital converter (PORT A). The line after that creates a routine that will read the voltage coming in from PINA0 and convert it to digital information.

The main routine has the main part of the code. In that routine, we used the routines created earlier and will tell the microcontroller what to do when it reads certain values. We first need to store the ADC (analog digital conversion) to a variable. This is the “uint16_t adc_result0;” That portion is declaring adc_result0 as a 16bit integer. The next two portions of code are the data direction registers for PORT C and D. They assign PINC0 and PINB0, 1, 2 as outputs. Port C is for the motor and port B is for the indicator LEDs.

Then we tell the microcontroller to follow the adc_init routine which will initialize the ADC. Here we introduce a delay so that the ADC can finish initializing and so that the sensor has time to warm up. The while loop tells the microcontroller to repeat that portion of the code until either the power is shut off or there is an interrupt introduced. In our code there will be no interrupts so to restart the code, the microcontroller must be turned off.

The first thing we will do in the while loop is reset the LEDs. Then we will read the analog value of the sensor at pinA0. The routine also converts it to digital in that step and assigns that value to another variable. Next we have if statements. We first check if the reading is below or equal to the green limit. If so, voltage is applied to pinB2, turning on the green LED. Voltage is also applied to pinC0, turning the motor on. If it isn’t less than or equal to the green value, the next if statement checks whether the value was below or equal to the yellow limit. If so, voltage is applied to pinB2, turning on the yellow LED. Voltage is also applied to pinC0, turning on the motor. If not, then the red LED turns on and there is no voltage to the motor, turning it off. There is a delay at the end of the sequence so that the results can be displayed for two seconds.