Sunday, February 21, 2010

The Brushduino

Ever have an idea that people tell you "you should TOTALLY patent that!" Well, I did, and they did, and I thought about it... but instead I decided to open source the whole thing. It seemed like kind of an obvious idea to me and going after a patent made me feel kind of greedy. Besides, all of my prototyping was done with open source tools!

Anyways, I hope someone can improve upon this. I think it's a useful thing.

The Story

My daughters don't brush their teeth long enough, if left to their own devices. When my wife or I brush their teeth, we do a nice thorough job that takes a little while but when the girls do it themselves, they tend to cut it short. Timing them doesn't help, as they just stand there and chit-chat until the timer goes off. It's not that they don't brush their teeth LONG enough, it's that they don't brush their teeth WELL enough.

It occurred to me that if could make something that monitored how WELL they brush their teeth---and maybe even guided them through the process a little---then they'd eventually establish better habits for the future. I started thinking about what I could build that would help them and settled on the notion that an accelerometer was the perfect sort of thing to keep tabs on what constitutes a brush "stroke" while they're in there brushing their teeth. All I'd need to do is come up with a little hardware and software to make it happen.

About the Hardware

For my birthday last year, I got myself an Arduino starter kit from and hadn't used it for anything yet. I was curious about the Arduino platform and have read a lot about it. I figured it was time to build something on it and this toothbrush thing seemed like the perfect fit.

I started looking for a low-cost accelerometer I could base my project on. I found one with a breakout board on Adafruit and one on Sparkfun but I didn't want to spend that much. Plus, it needed to be small---I wanted this thing to fit on the end of a toothbrush. I continued scouring the web and found this article over on Hack a Day for Digital Dice. This was based around the Freescale MMA7260QT and he fearlessly did all of the fine soldering work himself. I figured I could do that too. I ordered up a couple of MMA7260QT's from Mouser and a couple of the MMA7368LT's (BGA package version) and set to work with 30 AWG tynar coated solid core wire. This stuff is usually used for wire-wrapping, but I like to use it for low-current point to point stuff and general purpose project wire.

I wired one of the MMA7368LT's up to a .1" pin header so I could start experimenting.

It's pretty fiddly work and requires lots of swearing. I drink a lot of caffeine, which makes it even harder. I recommend finding great inner peace before you begin. Some solder paste also helps.

I got everything going with the Arduino just jumpering things around and with a bit of breadboard. It all went very quickly compared thanks to the Arduino environment which took care of a lot of the usual setup I normally go through (soldering up a voltage regulator, breaking out the SPI pins for programming AVR chips, setting fuses, burn-and-test-and-burn-and-test, etc.) It was nice!

If the Arduino is the brain of this project and the accelerometer is the eyes, then the Adafruit ProtoShield is the heart. It's only like $13 and it's brilliant.

It's basically all LED's. There are 6 white LED's that correspond to the six regions of the mouth that I want to highlight, there are four green LED's for showing progress and there's a speaker I pulled out of some broken toy that's there to entertain the children while they brush. The multi-strand wire connected to the three analog inputs on the Arduino also supplies 3.3v and ground to the accelerometer. The cable itself is an old ADB keyboard cable from my old Mac SE/30 from back in the day. It's been laying in my junk box for ages. There are also some current limiting resistors for the LED's and a bunch more of that lovely AWG30 solid core wire I like so much.

At the toothbrush end of the cable, I encased the whole thing in hot glue to keep it safe from water and from yanking by children:

This "puck" on the end was meant to be attached to the toothbrush handle via some mechanism (I ended up using rubber bands, which was only somewhat successful and I hope to improve upon) and held in the user's hands.

For an enclosure, I used a cool plastic enclosure I got from PAC TEC a few years ago as a free sample. It was perfect for housing the Arduino with attached ProtoShield and had a 9 Volt Battery compartment too. I had to modify the battery compartment and I added a barrel connector so I didn't have to solder to the Arduino at all.

To allow the indicator lights to show through the case to the front and still remain waterproof, I used some more hot glue. I drilled holes roughly the size of the LED's directly above where they would fall once installed in the case, filled the holes with hot glue, and snipped off the tops and bottoms to be flush against the case on the top side and flush against the LED's on the bottom side. For the reset button, I used the same technique, but lubricated the hole so the hot glue piece can slide up and down. It all proved to be pretty water tight and while the hot glue is a little opaque, it's clear enough to transfer the light up to the surface where they can see it. It's a sort of a "light pipe" type of thing.

Above you can see the barrel connector for the arduino peeking out and the SPST slide switch I installed in the side of the case.

I repurposed some orange suction cups from a cheap nerf hoop knockoff toy I got as a Christmas present one year to stick the enclosure to the girls' mirror in the bathroom to keep it at eye level and keep it out of the water.

Here it is installed over their sink:

When you fire it up, it plays the opening bars of the Super Mario Brothers theme song while randomly flashing the white LED's. Then it starts instructing on what area of the mouth to brush.

I draw a mouth on a piece of paper and glue-stick it to the front. That way I can rotate the pictures out and keep them funny/interesting. The green LED that's lit rotates around between the four as each "stroke" is counted. A "stroke" is defined as hard acceleration in one direction followed by hard acceleration in the opposite direction within a specified time window. I tweaked the parameters of what defines a "stroke" until I got pretty good results. It still picks up false strokes when they're moving the toothbrush around but it does a pretty good job. At least---good enough for this purpose.

Each time a section of the mouth has been brushed sufficiently (stroke count was based on experimentation and dentist recommendations) a few more bars of the Super Mario Brothers theme song is played (along with light show) and then the next white LED starts flashing. It's easier to see the pattern than it is to describe it. The section they're supposed to be brushing at any point in time is blinking with the already-brushed portions of the mouth lit fully on.

Here's a video of the whole process with me fake-brushing my way through it. Pardon the nausea-cam photography, I couldn't find my little tabletop tripod.


It works pretty well! And my first Arduino project was a smashing success. Let's hear it for easy floating point number support on microcontrollers.

My next step is to replace that puck of hot glue on the end with a Sugru "cup" that they stick on the end of the toothbrush. I was lucky enough to get some Sugru for Christmas (actually, after Christmas) and have been itching to use it for something electronic. For those not familiar with Sugru, it's an air-curing silicone putty made by a snazzy little outfit in the UK.

I'll post again once I have the Sugru version going. I just ripped out of few of the wires to the accelerometer when cutting the hot glue off so I have to go back to the workshop for a while. :-/

Eventually I'll replace the Arduino with a couple of voltage regulators, a pushbutton and an ATmega328 and make this piece of hardware fully "legit." Or maybe I won't.

About the Software

Coding for Arduino is easy-cheesy compared to working directly with WinAVR or Atmel's AVR Studio. For one thing, you get printf() which is HUGE! I used LED indicators to debug previously. Being able to log output to a scrollback is an immense boon. It's really pretty luxurious! The Arduino environment is basically a front end for a WinAVR backend and it also gives you some pretty awesome libraries to use straight out of the box.

For the software, I needed to light pretty lights, play pretty music and most importantly count brush strokes.

For those of you who have worked with three axis accelerometers, you know that gravity is always represented in the reading. To get a good sense of movement around three space, you need to subtract out the gravity component from the readings you're getting. This is harder than it sounds if you consider that in my case the toothbrush could be oriented any which way and therefore the gravity could be registering along any axis or any combination of axes.

To solve this problem, I turned to a cheap-o solution which is to use a sort of bargain basement "high pass filter" to remove the gravity component. Since gravity is going to be constant, it stands to reason that whatever elements of motion that the accelerometer is registering, the ones that have been going on for a long time (low frequency data) should be ignored while the recent and rapid changes (high frequency data) should be allowed to go through for analysis. I did a little searching around and found a small Objective C code snippet in the iPhone developers' realm that did just the thing I needed... simulated a high pass filter:

// Read the 3 axes analog values from the accelerometer
int rawX = analogRead(analogXPin) - 512;    // read the value from the sensor
int rawY = analogRead(analogYPin) - 512;    // read the value from the sensor
int rawZ = analogRead(analogZPin) - 512;    // read the value from the sensor

// Our High Pass filter works better with signed floating point values
float floatX = (float)rawX,
floatY = (float)rawY,
floatZ = (float)rawZ;

// Low pass filter
float kFilteringFactor = 0.1f;

// We're keeping 90% of the rolling value and adding in 10% of the new value each
// time. This filters out recent values and retains historical values.
rollingX = (floatX * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (floatY * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (floatZ * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));

// High pass filter

// Now we remove the historical values to emphasize current values more.  The high
// pass filter helps us remove gravity from the reading so we can get orientation-
// independent acceleration values from the device
accelX = floatX - rollingX;
accelY = floatY - rollingY;
accelZ = floatZ - rollingZ;

My processed ( accelX, accelY, accelZ ) vector now represents interesting accelerometer values to feed into a little stroke counter routine.

For the music side of the project, I found the Arduino Playground "freqout()" function, which I modified to handle rests and a wider range of note values. I then found someone's sheet music transcription of the Super Mario Brothers theme song and converted it to data.

Here's the full source code of the Arduino sketch. This was compiled against arduino-0017 but should work with the latest too. Full source after the break below: