+Add Sign In
(/login#login) (/lo
(/) Platforms (/explore) Components
(/components) Projects (/projects)
AVR ATmega Controllers
AVR ATmega is popular 8 bit controller family manufactured by ATmel.
It can be used for tilt sensing applications (Example: In mobile phones, gaming applications etc).
It gives analog voltage output proportional to the acceleration along the 3 axes.
These voltages can be converted to digital signal using ADC and then processed by microcontroller to
nd out the tilt.
For more information about ADXL335 accelerometer and how to use it, refer the topic ADXL335
Accelerometer Module (http://electronicwings.com/sensors-modules/adxl335-accelerometer-module)
in the sensors and modules section.
For information about ADC in ATmega16 and how to use it, refer the topic ADC in AVR
ATmega16/ATmega32 (http://www.electronicwings.com/avr-atmega/atmega1632-adc) in the ATmega
inside section.
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 1/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
+Add Sign In
(/login#login) (/lo
(/) Platforms (/explore) Components
(/components) Projects (/projects)
Interfacing diagram
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 2/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
+Add Sign In
(/login#login) (/lo
(/) Platforms (/explore) Components
(/components) Projects (/projects)
Program 1
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 3/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
/* +Add Sign In
* ATmega16_Accelerometer.c
* http://www.electronicwings.com (/login#login) (/lo
* (/) Platforms (/explore) Components
*/ (/components) Projects (/projects)
int main(void)
{
char buffer[50];
int ADC_X_VALUE,ADC_Y_VALUE,ADC_Z_VALUE;
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);
Output Window
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 4/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
On PC/Laptop’s serial terminal application, we can see directly X, Y, Z’s ADC values as shown in below
+Add
gure. For testing tilt the module in X and Y direction and observe the changes Sign In
in X and Y values.
(/login#login) (/lo
If we tilt module on X-axis, we get variations in X and Z values whereas Y will remain nearly
(/) Platforms (/explore) Components
constant.
(/components)
If we tilt module Projects
on Y-axis, we get (/projects)
variations in Y and Z values whereas Y will remain nearly
constant.
Program 2
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 5/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
/* +Add Sign In
* ATmega16_Accelerometer.c
* http://www.electronicwings.com (/login#login) (/lo
* (/) Platforms (/explore) Components
*/ (/components) Projects (/projects)
#de ne F_CPU 8000000UL /* De ne CPU clock Frequency e.g. here its 8MHz */
#include <avr/io.h> /* Include AVR std. library le */
#include <util/delay.h> /* Include de ned delay header le */
#include <stdlib.h> /* Include standard library le */
#include <math.h> /* Include math header le */
#include "USART_RS232_H_ le.h" /* Include USART header le */
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);
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 6/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
ADC_Z_VALUE = ADC_Read(2);
+Add Sign In
/* Convert values in g unit */
Axout = (((double)(ADC_X_VALUE*5)/1.024)-1700.0)/330.0;
(/login#login) (/lo
(/) Platforms (/explore)
Ayout = ((( Components
double)(ADC_Y_VALUE*5)/1.024)-1700.0)/330.0;
Azout = (((double)(ADC_Z_VALUE*
(/components) Projects (/projects)5)/1.024)-1700.0)/330.0;
/* Calculate angles */
theta = atan(Axout/(sqrt((pow (Ayout,2.0))+(pow (Azout,2.0)))))*57.2957795
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("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
1. Acceleration in g unit:
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 7/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
+Add Sign In
(/login#login) (/lo
(/) Platforms (/explore) Components
(/components) Projects (/projects)
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 8/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
+Add Sign In
(/login#login) (/lo
(/) Platforms (/explore) Components
(/components) Projects (/projects)
Video
Supporting Files
Source Code
ATmega16 Accelerometer Project le
Download 487 (/download/attachment=Thu-06-17-20-19-34.Thu-09-16-03-08-
21.ATmega16_Accelerometer_Project_ le.zip)
Attached File
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 9/10
7/21/2019 Accelerometer ADXL335 Interfacing with AVR ATmega16 | AVR ATmega ...
ADXL335 Datasheet
+Add
Download 501 (/download/attachment=Thu-09-16-03-09-12.ADXL335_Datasheet.pdf) Sign In
(/login#login) (/lo
(/) Platforms (/explore) Components
Comments (/components) Projects (/projects)
ysbohorquezg
(/users/ysbohorquezg/profile)
2018-01-13 11:18:05
asd
(/users/asd/profile)
2018-07-20 10:19:51
https://www.electronicwings.com/avr-atmega/accelerometer-adxl335-interfacing-with-atmega16 10/10