Overview of Accelerometer
An accelerometer is an electromechanical device used to measure acceleration forces, including gravity, in units of g.
It is commonly used in tilt-sensing applications, such as in mobile phones and gaming devices.
The ADXL335 accelerometer measures acceleration along three axes: X, Y, and Z. It provides an analog voltage output proportional to the acceleration experienced along these axes.
These analog signals can be converted into digital data using an ADC (Analog-to-Digital Converter) and processed by a microcontroller to determine the tilt.
For more details about the ADXL335 accelerometer and its usage, refer to the topic ADXL335 Accelerometer Module in the Sensors and Modules section.
For guidance on using the ADC in the ATmega16, refer to the topic ADC in AVR ATmega16/ATmega32 in the ATmega Inside section.
ADXL335 Accelerometer Module
Interfacing Accelerometer ADXL335 with AVR ATmega16
Since the ADXL335 module provides analog outputs, we will measure them using the ADC channels of the ATmega16 microcontroller.
The ATmega16 features an ADC with 8 input channels located on PORT A. We will connect the X, Y, and Z analog outputs of the ADXL335 module to three ADC input channels of the ATmega16, specifically channel 0, channel 1, and channel 2.
After reading the ADC values corresponding to the X, Y, and Z axes, the data will be transmitted to a PC or laptop using USART.
Connection Diagram of ADXL335 with ATmega16/32
Interfacing ADXL335 Accelerometer Module With ATmega 16
Accelerometer ADXL335 Code for ATmega16/32
/*
* ATmega16_Accelerometer.c
* http://www.electronicwings.com
*
*/
#define F_CPU 8000000UL /* Define CPU clock Frequency e.g. here its 8MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include defined delay header file */
#include <stdlib.h> /* Include standard library file */
#include <math.h> /* Include math header file */
#include "USART_RS232_H_file.h" /* Include USART header file */
void ADC_Init() /* ADC InitialiAzouttion function */
{
DDRA = 0x00; /* Make ADC port as input */
ADCSRA = 0x87; /* Enable ADC, with freq/128 */
ADMUX = 0x40; /* Vref: Avcc, ADC channel: 0 */
}
int ADC_Read(char channel) /* ADC Read function */
{
ADMUX = 0x40 | (channel & 0x07); /* set input channel to read */
ADCSRA |= (1<<ADSC); /* Start ADC conversion */
while (!(ADCSRA & (1<<ADIF))); /* Wait until end of conversion by polling ADC interrupt flag */
ADCSRA |= (1<<ADIF); /* Clear interrupt flag */
_delay_ms(1); /* Wait a little bit */
return ADCW; /* Return ADC word */
}
void SendSerial(char* str, double value, char unit)
{
char buffer[10];
dtostrf(value,4,2,buffer);
USART_SendString(str); /* Send Name string */
USART_SendString(buffer); /* Send value */
USART_TxChar(unit); /* Send unit char */
USART_TxChar('\t'); /* Send tab char */
_delay_ms(10);
}
int main(void)
{
int ADC_X_VALUE,ADC_Y_VALUE,ADC_Z_VALUE;
double Axout,Ayout,Azout,theta, psy, phi,roll,pitch,yaw;
USART_Init(9600); /* Initialize USART with 9600 Baud rate */
ADC_Init(); /* Initialize ADC */
while(1)
{
ADC_X_VALUE = ADC_Read(0); /* Read X, Y, Z axis ADC value */
ADC_Y_VALUE = ADC_Read(1);
ADC_Z_VALUE = ADC_Read(2);
/* Convert values in g unit */
Axout = (((double)(ADC_X_VALUE*5)/1.024)-1700.0)/330.0;
Ayout = (((double)(ADC_Y_VALUE*5)/1.024)-1700.0)/330.0;
Azout = (((double)(ADC_Z_VALUE*5)/1.024)-1700.0)/330.0;
/* Calculate angles */
theta = atan(Axout/(sqrt((pow (Ayout,2.0))+(pow (Azout,2.0)))))*57.29577951;
psy = atan(Ayout/(sqrt((pow (Axout,2.0))+(pow (Azout,2.0)))))*57.29577951;
phi = atan((sqrt((pow (Ayout,2.0))+(pow (Axout,2.0))))/Azout)*57.29577951;
roll = (atan2(Ayout,Azout))*57.29577951+180;
pitch = (atan2(Azout,Axout))*57.29577951+180;
yaw = (atan2(Axout,Ayout))*57.29577951+180;
SendSerial("Axout = ",Axout,'g');/* Send All value */
SendSerial("Ayout = ",Ayout,'g');
SendSerial("Azout = ",Azout,'g');
SendSerial("Theta = ",theta,248);
SendSerial("Psy = ",psy,248);
SendSerial("Phi = ",phi,248);
SendSerial("Roll = ",roll,248);
SendSerial("Pitch = ",pitch,248);
SendSerial("Yaw = ",yaw,248);
USART_SendString("\n\r");
}
}
Output Window
- Acceleration in g unit:
- Angle of Inclination
3. Angle of Rotation