[ Log In ]
Image of the Atmega324p

Atmega324P

$8.50
Qty:
USB AVR programmer

USB AVR Programmer

$9.95
Qty:
Breadboard adapter for the USBasp AVR Programmer

USBasp Breadboard Breakout Adapter

$4.90
Qty:
Thumbnail: Crystal Oscillator 18.432 MHz for UART

18.432 MHz Crystal Oscillator 18pf 30ppm

$0.94
Qty:
Thumbnail: 22 pF Capacitor

22 pF Multilayer Ceramic Capacitor

$0.43
Qty:
Thumbnail: Quartz crystal oscillator - 16 MHz

16 MHz Crystal Oscillator 20 pF Through Hole

$0.75
Qty:
3 pin slide switch

SPDT Slide Switch 3 pin 30V

$1.49
Qty:
Serial to USB converter with Micro USB cable

USB to Serial Converter

$10.95
Qty:
Thumbnail: 4x4 keypad top view.

4x4 Keypad with Adhesive Backing

$3.80
Qty:
Thumbnail: quad buffer line driver 74HC126E

Quad Buffer Line Driver (Through Hole)

$0.69
Qty:
Skip Navigation Links

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;
}