Connecting an Accelerometer to the ADC (Analog to Digital Converter)
Let's go down this metal street in Breadboardville and meet this tipsy Mr. Gravity,
shall we? While we're there, we will poke and prood him to see how he reacts. This
is ok since we need to determine his sensitivity and what type of currency he will
give us. I gotta watch out with these tipsy gravity folks. They may provide you
with analog voltage currency, or currency that involves time! Mr. Gravity that gives
time relative currency, then he is better suited to interact with Mrs. PWM. Remember
her? She's the one with the stopwatch.
If Mr. Gravity is the type to provide us with the voltage currency, he is perfect
for the ADC. The really sick Mr. Gravities can also have multiple personalities.
Which ever way you push Mr. Gravity, he could regurgitate his voltage in one of
three of his output ports. Actually, he is constantly regurgitating. At full stance
(no tilt), he is at about mid level regurgitation with his voltage currency. If
pushed him forward, he starts to regurgitate the currency proportional to the amount
of force on his back. This force is actually measured in G's. Just to get a good
reference, one G is the force of gravity at a standstill and with the average amount
of earth's crust in-tact (yep, there is less gravity if there are large holes below
you, but that get's into heavy physics, so I will leave that alone for now!).
Back to Mr. Gravity. guess what happens if you push him backward. You are right,
he regurgitates less relative to the gravity. However! If you push him hard, he
feels that "force" (in g's, remember) and he will regurgitate in proportion to the
push of acceleration.
The program for the ADC is pretty basic. All we are doing is changing the reference
voltage. Instead of using the AVCC pin to determine the top voltage for the ADC,
we are using an internal reference voltage (a voltage that the microcontroller "King
Core" has devised himself). Why are we changing this voltage reference? Why isn't
the AVCC good enough. Well, we actually could use the AVCC, but the resoltion of
the ADC result would be reduced. In the case of the AVCC reference voltage, the
microcontroller thinks that you are going to send it a sensor that provides a voltage
all the way up to 5v. Mr. Gravity is only regurgitating up to about 2.4 volts, so
the voltage from 2.4 to 5v is essentially waisted. so we want to use a voltage reference
closer to the top voltage that Mr. Gravity will provide.
To set this internal voltage, all we do is set REFS0 and REFS1. It's as easy as
that.
Here is what the program may look like:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "MrLCD.h"
int main(void)
{
InitializeMrLCD();
Send_A_StringToMrLCDWithLocation(1,1,"ADC Result:");
ADCSRA |= 1<<ADPS2;
ADMUX |= (1<<REFS0) | (1<<REFS1);
ADCSRA |= 1<<ADIE;
ADCSRA |= 1<<ADEN;
sei();
ADCSRA |= 1<<ADSC;
while (1)
{
}
}
ISR(ADC_vect)
{
uint8_t theLowADC = ADCL;
uint16_t theTenBitResults = ADCH<<8 | theLowADC;
Send_An_IntegerToMrLCD(13,1,theTenBitResults, 4);
ADCSRA |= 1<<ADSC;
}