Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Jun 28, 2013 in Atmel AVR, Microcontrollers | 22 comments

Using an Analog Accelerometer

Using an Analog Accelerometer

Everybody would have at least once heard of  “accelerometer“, most probably, in some way associated to smartphones. But what actually are they?

Accelerometers

What are they?

Well, according to the dictionary, an accelerometer is a device (a sensor) that is typically used to measure the acceleration (very obvious from the name!).

An accelerometer is an electromechanical device that will measure 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.

There are two types of accelerometers available:

  1. Analog
  2. Digital

In this post, we will be discussing about the analog accelerometers. We will discuss about digital ones later.

Where are they used?

Accelerometers find a variety of uses in the modern world. Nowadays, they are an integral part of your smartphones! Yes! They are the ones, which are responsible for screen rotation when you change the orientation of your phone! Motion gaming is another use of the accelerometer. They are also used to measure the acceleration of automobiles, ships, aircrafts, spacecraft, or in those applications wherein vibrations of a machine, a building etc. need to be found out.

So we see that there are a vast number of uses of this device. Wondering how it works?

How they work?

There are many ways in which accelerometers work, some of them are:

  1. Some use the piezoelectric effect i.e. they contain microscopic crystal structures which upon getting stressed by acceleration forces generate a voltage across its ends.
  2. Another way to do it is by sensing changes in capacitance. If you have two microstructures next to each other, they have a certain capacitance between them. If an acceleration force moves one of the structures, then the capacitance will change. Add some circuitry to convert from capacitance to voltage, and you will get an accelerometer.
  3. Accelerometers use a quartz crystal mounted between a fixed point and a free floating mass. The mass puts pressure on the crystal and generates a tiny voltage (some work on resistance or capacitance change). As the direction of gravity’s pull changes, so does the force of the crystal and the signal it generates.
  4. There are even more methods, including use of the piezoresistive effect, hot air bubbles, and light.

3-Axis Accelerometer

We are using a 3-Axis Accelerometer. But why 3-axis? 3-axis accelerometers are the most convenient to use, and they provide us the flexibility to use all the 3 spatial axes, viz the x-axis, the y-axis, and the z-axis. Of course you can go for  a single axis or a dual axis accelerometer if only the respective axes are required, but if you are buying an accelerometer module for experimental purposes, go for a 3-axis one!

Since ours is an Analog Accelerometer, it gives an output of varying voltage depending upon the acceleration, but obviously it has some minimum output voltage and a maximum output voltage. There are many IC packages available for 3-axis accelerometers. Most popular of them are the ADXL330 and ADXL335 by Analog Devices.

slide6 slide8

They have a sensitivity of ±3g. There are many more available, which have better sensitivity, such as ADXL326 which has a sensitivity of ±16g, etc. I use A7260 based module, which has selectable sensitivity from ±1.5g to ±6g. It also has a ‘sleep mode’ to reduce power consumption when not in use. But what does sensitivity of an accelerometer exactly mean?

Sensitivity

Sensitivity is the ratio of change in acceleration (input) to change in the output signal. It is the absolute minimum amount of change in input signal which can be detected by the measuring device. This defines the ideal, straight-line relationship between acceleration and output.  For analog-output sensors, sensitivity is ratiometric to supply voltage; doubling the supply, for example, doubles the sensitivity. In terms of accelerometers, sensitivity refers to the minimum change in acceleration which can be detected by the device.

Pin Description

Below is the pin description of the ADXL ICs, which are available only in TQFP packages:

ADXL330 Pinout

ADXL330 Pinout

The pinout for all the ADXL ICs is the same as shown above.

ADXL Pinouts

ADXL Pinouts

Before we proceed to the Pin Description, we will get acquainted with some basic terms:

  1. Input supply voltage: this is the Supply voltage (VSS) for the IC to work on. The ADXL330 and ADXL335 are capable of working on supply voltages ranging from 1.8 volts (minimum) to 3.6 volts (maximum)
  2. Ratiometric Output: This means that the output voltage from the pins (ZOUT, YOUT, XOUT) depend on the supply voltage given at the Vss pin.
  3. Zero g Bias level: this means the voltage output through the Out pins at the mean position, i.e. when there is no net acceleration in the respective axis.

ADXL330 vs ADXL335

Now here comes the difference between the two ICs! While in ADXL330, the Zero g bias level for all the Axes stands same at 1.5 volts, typical min at 1.2 volts and typical max at 1.8 volts, in the ADXL335, the Zero g Bias Level is 1.5 volts for all axes as well, but the typical extremes differ: they are 1.35v to 1.65v for the X, Y axes, while they range from 1.2v to 1.8v in the Z-axis.

You might feel there is not much difference between the two. Indeed you are right, but when it comes to precision, ADXL335 is better. Since the typical extremes range is lesser in ADXL335, one will have easier calibration issues and lesser inaccuracies.

PIN Description

  1. NO CONNECTION: you have to leave these pins floating. They do not need to be connected anywhere.
  2. COMMON: these pins need to be connected to the ground.
  3. VSS: this is the supply input voltage for the IC to work. The recommended input supply voltage is 3.0 volts.
  4. ZOUT, YOUT, XOUT: These are the analog output pins for the respective axes.
  5. Self-Test: The ST pin controls the self-test feature. When this pin is set to VSS, an electrostatic force is exerted on the accelerometer beam. The resulting movement of the beam allows the user to test if the accelerometer is functional. The typical change in output is −1.08 g (corresponding to −325 mV) in the X-axis, +1.08 g (or +325 mV) on the Y-axis, and +1.83 g (or +550 mV) on the Z-axis. The ±g are with respect to the Zero g bias level, and hence we always get a +voltage output.

This ST pin can be left open-circuit or connected to common (COM) in normal use.

In the market, we mostly find the Accelerometer Modules… i.e. with all the connections already made. A number of them are available here, and here and here and here and here as well! So don’t you worry, you wont have to solder the circuit all by yourself! ;)

I have an MMA7260 based Accelerometer module, which looks like this:

MMA7260 Accelerometer

MMA7260 Accelerometer

MMA7260 Accelerometer

MMA7260 Accelerometer

MMA7260 Accelerometer

MMA7260 Accelerometer

MMA7260 is an accelerometer by Freescale Semiconductor. Recently Freescale has stopped manufacturing this unit and hence you may not find it available in the market. Although this is not ADXL based, but it’s working is similar.

The ADC – Analog to Digital Conversion

Well, obviously, one cannot use an analog accelerometer without the prior knowledge of ADC. So, if you are not clear with the concepts of ADC, take a detour and go through Mayank’s awesome Post on ADC!

Hardware Connections

These are the schematics for ADXL3xx based accelerometer modules:

Slide1

ADXL Schematics (Click to Enlarge)

Some points to note are:

  1. The Vss Pin is to be connected to +5V supply. This is because all the modules have a 3V voltage regulator.
  2. The Vdd Pin is to be connected to the ground.
  3. The Self Test Pin is to be left floating/ grounded.
  4. The Pins ZOUT, YOUT, XOUT have to be connected to the ADC Inputs of the Microcontroller.

I will be using ATMega16A to demonstrate the use. As an example, I will display the output on a character LCD. If you are not well versed with the use of LCD, read Mayank’s great post on LCD here!

Problem Statement

Let us define a problem statement for this. Our task is to initialize the Accelerometer module and display the results of all the axes on a Character LCD.

Coding

Now if you follow Mayank’s ADC tutorial, there is no more explanation necessary. Here is the final code! This code is compiled using Atmel Studio 6.

This code is also available for viewing/downloading at the AVR Code Gallery and Pastebin.

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
 
#include <avr/io.h>
#include <stdlib.h>     //include Library for 'itoa' function
#include "lcd.h"        //include LCD Library
#include <util/delay.h>
 
void adc_init(void)
{
    ADCSRA|=(1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2); //ENABLE ADC, PRESCALER 128
    ADMUX|=(1<<REFS0);        //PC0, AVcc AS REFERENCE VOLTAGE
}
 
uint16_t adc_read(uint8_t ch)
{
    ch&=0b00000111;         //ANDing to limit input to 7
    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)
{
    uint16_t x,y,z;
    char bufferx[10], buffery[10], bufferz[10]; //Initialize character arrays for each
 
    adc_init();         //INITIALIZE ADC
    lcd_init(LCD_DISP_ON);  //INITIALIZE LCD
    lcd_clrscr();       //CLEAR LCD SCREEN
 
    while(1)
    {
        lcd_home();         //LCD - GO TO HOME POSITION
 
        x=adc_read(0);      //READ ADC VALUE FROM CHANNEL 0
        y=adc_read(1);      //READ ADC VALUE FROM CHANNEL 1
        z=adc_read(2);      //READ ADC VALUE FROM CHANNEL 2
 
        itoa(x,bufferx,10);     //STORE THE ADC VALUES IN ARRAYS
        itoa(y,buffery,10);
        itoa(z,bufferz,10);
 
        lcd_puts("x=");     //DISPLAY THE RESULTS ON LCD
        lcd_gotoxy(2,0);
        lcd_puts(bufferx);
 
        lcd_gotoxy(0,1);
        lcd_puts("y=");
        lcd_gotoxy(2,1);
        lcd_puts(buffery);
 
        lcd_gotoxy(6,0);
        lcd_puts("z=");
        lcd_gotoxy(6,1);
        lcd_puts(bufferz);
    }
}

Though the code is self explanatory, a few points need to be highlighted. As you would see in the video (scroll down), the output from the ADC Conversion shows values ranging from ~300 to ~750 for all the three axes. This is because we have set the reference voltage as 5V, but the accelerometer is giving an output ranging from ~1.46V (corresponding to -ve extreme) to ~3.6V (corresponding to +ve extreme).

Experimental Setup and Video

My final Setup looks like this:

slide5

The Final Setup

The Final Setup

The Final Setup

Video


So this is it! We will be posting a few more accelerometer based projects soon. More interesting posts to come up! So subscribe to stay updated! And don’t forget to jot down any comment which will help in improving the quality of this and upcoming posts on maxEmbedded.com!

Thanks and Regards,

Yash Tambi
yash@delta.robovitics.in

22 Comments

  1. Nice tutorial :D. Well currently i don’t have an accelerometer module but i somehow managed to found the ADXL377 ic.How can i connect it to various component to make a module so that it can be interfaced with the microcontroller ?

    • Hello Tushar!
      Thanks! :)
      I believe that ADXL337 is available only in MLF, so you would find it very difficult to solder it.
      Anyways, to make a module, few things that you will need to know:
      1. The IC operates in ~1.8 to 3.6V range. So you will have to use a suitable voltage regulator.
      2. The typical supply current at the Vcc pin is ~300µA. So select a suitable resistor to be used.
      3. To decouple from supply noise, a capacitor of 0.1µF, connected from the supply line to the ground is enough.
      4. Similarly, Capacitors must be added at the X, Y, and Z output pins to implement low-pass filtering for antialiasing and noise reduction. A 4.7µF capacitor connected to each output line, the other end of the capacitor grounded, will serve the purpose.

      Hope this helps!

  2. SUPERB TUTORIAL………!
    i have a doubt in this tutorial.Why the voltage value is changing when the accelerometer is tilted.

    • This is what an accelerometer does.It’s a MEMS based device and gives the voltage proportional to it’s tilt position just like a temperature sensor,which gives voltage according to the temperature detected

  3. thanks a lot for quick reply.Is there any good link to learn the working of ADXL335

  4. very nice tutorial………..thanks for tutorial …..
    please tell me how can i interface my smartphone’s accelerometer to any bot?????????

    • Hello Mohan!
      Yes definitely you can! Youll need an app which interprets the accelerometer data and sends it, most suitably, through bluetooth (the protocol would be UART).

  5. What is these two lines says
    and what is itoa function ??
    I am getting error here. can u plz help me out??
    #include //include Library for ‘itoa’ function
    #include “lcd.h” //include LCD Library

  6. Sir, i have done it and it goes superb ……..but i have a problem …… why the readings are so fluctuating…….they didn’t stabilize…….Any help would be appreciated ……!!

    Thanks Sir

    • That’s because you don’t have a delay in the loop. Give some 50ms or 100ms delay, and the readings should stabilize.

      • Boss you r simply great…

  7. can you make similar tutorials on gyroscope

    • Disha,
      The concept of ADC remains the same. We cannot feed you everything. All you need to do is to hook up the gyroscope to one of the ADC pins and then process the received data as per the specifications given in the datasheet. And thanks for the suggestion, we might be writing one soon, but it may not be anytime soon. Thanks!

      • listen,
        what i actually meant was:
        Not to give any ready made code to interface gyro.
        Only to make a tutorial where in one gets to know how actually a gyro works that’s it.

        • Yes and I understand that and only if you knew how much of an effort and time it takes to create one tutorial. That’s why I said thanks, we have added it to our wishlist, but please don’t expect it anytime soon.

  8. thank you sir for your efforts,
    i faces agreat problem, i use PROTEUS as a simulation program ,but ADXL330 is not included in it.what should i do,please

    thank you sir

  9. Thanks for your efforts… May I know how to use it for vibration check(dynamic motion), the above tutorial shows tilt application as such…

    • First of all, your sampling rate must be very high. Second, the delay due to ADC and computation should be able to keep up with the high sampling rate. The best way to measure vibration would be to count how many times the values cross the zero level. If the zero-crossing rate is high, it means that the system is vibrating.

  10. hi, I wonder if u can tell me how/where to add adxl into proteus?i am trying to do simulation for the circuit. I am using adxl345 fyi. thanx in advance

  11. hi, im using the ADXL335. I need to converte the values that the X,Y,Z to vibration. Can someone spare me a arduino code to do that? and by that way. why X,Y,Z are high values? why not degrees? sorry im new at this

Trackbacks/Pingbacks

  1. Sensor Fundamentals | maxEmbedded - […] Accelerometer - An accelerometer is a device which can measure acceleration in any direction (X, Y, Z). MMA7260 is one…

Leave a Reply

%d bloggers like this: