Accelerometer Interfacing with AVR

The article covers how to interface an accelerometer with the atmega32/atmega16. Before proceeding, the user must know the basics of ADC (Analogy to digital converter) of the AVR.

ADXL335 size

An accelerometer is an electromechanical device that measures acceleration forces. These forces may be static, like the constant force of gravity pulling at your feet, or they could be dynamic – caused by moving or vibrating the accelerometer. Accelerometers are of two types Analog and Digital. In this post, we will be discussing Analog accelerometers. They give voltage as output which is proportional to acceleration. The digital one gives the PWM output or direct binary digital data

Accelerometer Description

The objective of the post will be to interface a 3-axis accelerometer (ADXL335) and a JHD162A LCD. The microcontroller will sense the different Analog values of the accelerometer, convert them into digital and then display on the LCD.

ADXL335_tilt

ADXL335 have a sensitivity of +-3G.

Kindly note the ST pin has to be connected to the ground as it’s a self teat feature and we don’t require one. Also, maximum modules operate on 3.3V rather than 5v. Also, we will be using an LCD library which can be found in the code files.

Circuit Diagram

ADXL335_AVR_circuit

Code Files

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include "lcd.h"        //include LCD Library
#include <util/delay.h>
void InitADC(void)
{
    ADMUX|=(1<<REFS0);    
    ADCSRA|=(1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2); //ENABLE ADC, PRESCALER 128

}
uint16_t readadc(uint8_t ch)
{
    ch&=0b00000111;         //ANDing to limit input to 75.1
    ADMUX = (ADMUX & 0xf8)|ch;  //Clear last 3 bits of ADMUX, OR with ch
    ADCSRA|=(1<<ADSC);        //START CONVERSION
    while((ADCSRA)&(1<<ADSC));    //WAIT UNTIL CONVERSION IS COMPLETE
    return(ADC);        //RETURN ADC VALUE
}
int main(void)
{
    char a[20], b[20], c[20];   
    uint16_t x,y,z;

    InitADC();         //INITIALIZE ADC
    lcd_init(LCD_DISP_ON);  //INITIALIZE LCD
    lcd_clrscr();      

    while(1)
    {
        lcd_home();         

        x=readadc(0);      //READ ADC VALUE FROM PA.0
        y=readadc(1);      //READ ADC VALUE FROM PA.1
        z=readadc(2);      //READ ADC VALUE FROM PA.2
        itoa(x,a,10);    
        itoa(y,b,10);
        itoa(z,c,10);
        lcd_puts("x=");     //DISPLAY THE RESULTS ON LCD
        lcd_gotoxy(2,0);
        lcd_puts(a);
        lcd_gotoxy(7,0);
        lcd_puts("y=");
        lcd_gotoxy(9,0);
        lcd_puts(b);
        lcd_gotoxy(0,1);
        lcd_puts("z=");
        lcd_gotoxy(2,1);
        lcd_puts(c);
    }
}

 

Download AVRStudio project files here: Accelerometer

By: Tushar Gupta

4 Comments:

  1. Is above code usable with atmega8 also? If not will you be kind enough to tell me how can I interface accelerometer with atmega8, I am doing it as a hobby project

  2. Should be no problem. You might need to edit couple paces to match chip configuration.

  3. Can you please send me the above code edited to be suitable for atmega8 with its circuit diagram. I am really new to this programming.
    Thanks in advance 🙂

  4. can you please share a link for CV AVR Too ?

Comments are closed