Friday! Time to stop working and start partying!
For today I have prepared a little prank: a talking drunkometer – or breathalizer if you prefer the more scientific name, a cool device that tells you if you still need to drink, or sends you home before you fall asleep on the street.
As in many other projects featured on this blog, I use MikroElektronika hardware: I start with the most important part, the alcohol sensor: one MQ-3 gas sensor Alcohol click board. This click board is designed to work on 5V power supply only, so I have to choose a development board that operates at 5V. I already have the EasyPIC v7 board, with a PIC18F45K22 microcontroller, and it can be configured to work on 5V. Great!
Now I need something that talks. The MP3 click featured in the Theremin project works only at 3.3V, so it doesn’t help me much. What I need is an MP3 player that operates at 5V: my choice is the Smart MP3 board.
This little board has all I need: it integrates all the power and level-shifting circuits that allow it to work for both 3.3V and 5V supplies. It has a VS1053 MP3 player, plus a pair of LM4864 amplifiers and a speaker. There’s also an SD card slot – all the required hardware to get some sound.
The final project looks like this:
The board settings are as follows:
- External power supply (highly recommended as the MQ-3 sensor is power-hungry)
- 5V supply
- Put PORTA switches in the middle (neutral) position.
- Put PORTD switches in the pull-down position and put Button Press Level jumper J17 in the VCC position.
- Put J15 to RA3 position
- Alcohol click is in mikroBUS socket #1
- SmartMP3 connected to PORTC
Before going to the software part, a fair warning: this little contraption is not calibrated in any way, and its output might not reflect actually how drunk you are. It’s just a device designed for having fun, and all the thresholds were set arbitrarily. Even if I would have had access to a calibrated Breathalyzer, calibrating this device would have been impossible. The response of the MQ-3 sensor is non-linear, is influenced by temperature, humidity, and by other factors, I can’t take into account. To compensate for all this I would have to build some lookup tables based by comparing the output of the MQ-3 sensor with the response of a calibrated device. The major problem is that the code, as it is now, occupies 99% of the RAM of the PIC18F45K22, leaving little room for improvements.
The only thing that I did is to insert a manual threshold to compensate for the background reading. This is done by using the potentiometer onboard EasyPICv7 as a voltage divider, the value is used to update threshold levels.
Talking breathalyzer: software
The software builds upon the code example for the SmartMP3 board and has the same way to set the volume: RD0 and RD2 are used to increase volume for left and right channels, RD1 and RD3 decrease volume for left and right channels, respectively.
The MP3_Play was changed to accept the filename as an argument – you can compare it with the original to see how I did this. I also commented on most of the LCD outputs to gain some precious memory space.
Reading MP3 files from the SD card relies on the FAT library – high capacity FAT32 SDHC cards are not supported. I found an old 4Gb simple SD in my junk box and I have formatted it to FAT, and it works fine. The only other issue is there’s no support for long filenames, so all files must obey the 8.3 naming standard.
I know there’s also a FAT32 library in MikroC, but the functions in that library are different than those in the simple FAT library. Perhaps one day I will come with a code example for playing MP3 files from the newer, high capacity SD cards…
Anyway, here are the listings for the MP3 code, and you’ll find that except a few changed lines it’s just like in the code example (double click on code to expand it):
The MQ-3 sensor needs a warming phase to measure correctly – don’t blow onto it during the warmup. I have to say here that it also needs some 100mA to work, and this is why I recommended the use of an external power supply, able to provide at least 1A.
The above code first starts a “calibration” phase, where you should adjust the ADC input potentiometer onboard EasyPIC V7 until the value displayed on LCD is close to zero – this will compensate for the reading of the background gases.
Then we’re good to go. The code waits for RE0 to be pressed, starting a new measurement cycle. The actual measurement is done after five seconds from the button press, and you should blow over the sensor for this whole period of time to get an accurate measurement.
Once a reading is done, the result is compared against some thresholds, taking the background response into account. One can compensate for changes in the background (smoke, for example) by simply adjusting the potentiometer onboard EasyPIC v7.
Another possibility is to play with the thresholds – you need some practice with those, as you wish to get all of the messages played before you get plastered. Anyway, drinking is fun, but you should always remember that hangover comes as a penalty for drinking too much…
As for the voices, I used some Homer Simpson’s .wav files that I got from http://www.wavsource.com/ and I have converted to MP3 using an online converter from http://audio.online-convert.com/convert-to-mp3. I can’t post those files here due to copyright reasons, but you can easily recreate them.
As I have said before, but I feel the need to repeat it, this is just a mere toy and is not designed as a replacement for a calibrated Breathalyzer. Don’t use the results as an excuse to drink too much…