Introduction
The accelerometer is an electromechanical device that measures the force of acceleration due to gravity, typically expressed in “g” units. It is commonly used in tilt-sensing applications, such as in mobile phones and gaming devices.
The ADXL335 accelerometer measures acceleration along the X, Y, and Z axes. It provides an analog voltage output that is proportional to the acceleration along each of these axes.
These analog voltages can be converted into digital signals using an ADC, and then processed by a microcontroller to determine the tilt angle.
For more details on the ADXL335 accelerometer and how to use it, refer to the “ADXL335 Accelerometer Module” section in the Sensors and Modules topic.
Interfacing Accelerometer ADXL335 with PIC18f4550
- Since the ADXL335 module provides an analog output, we will measure these outputs using the ADC channels of the PIC18F4550. The ADC pins of the PIC18F4550 are located on PORT A.
- We will connect the analog output pins of the ADXL335 (X, Y, and Z axes) to three input ADC channels of the PIC18F4550, specifically channel 0, channel 1, and channel 2, respectively.
- After reading the ADC values corresponding to the X, Y, and Z axes from the ADXL335, we will send this data to a PC or laptop via USART for further processing or display.
Connection Diagram ADXL335 Accelerometer with PIC18F4550
PIC18F4550 Interfacing with ADXL335
ADXL3356 Code for X Y Z axis using PIC18F4550
/*
* Accelerometer interface with PIC18F4550
* http://www.electronicwings.com
*
*/
#include <pic18f4550.h>
#include "Configuration_header_file.h"
#include <stdio.h>
#include <stdlib.h>
#include "ADC_Header_File.h"
#include "USART_Header_File.h"
int main()
{
char Buffer[10];
OSCCON = 0x72; /* Internal Oscillator frequency 8 MHz */
ADC_Init(); /* Initialize ADC */
USART_Init(9600); /* Initialize USART with 9600 baud rate */
while(1)
{ /* Read ADC channel 0,1,2 & send values over USART */
sprintf(Buffer,"X = %d ",ADC_Read(0));
USART_SendString(Buffer);
USART_TxChar(0x09);
MSdelay(10);
sprintf(Buffer,"Y = %d ",ADC_Read(1));
USART_SendString(Buffer);
USART_TxChar(0x09);
MSdelay(10);
sprintf(Buffer,"Z = %d ",ADC_Read(2));
USART_SendString(Buffer);
USART_TxChar(0x0A);
USART_TxChar(0x0D);
MSdelay(10);
}
}