ADXL345 (GY291)
ADXL345 measures the acceleration along three mutually perpendicular
axes with a programmable full scale range of ±2g, ±4g, ±8g and ±16g.
This measurement includes the acceleration due to gravity.
Connecting to Raspberry Pi
For I2C connection you need only 4 pins. Connect SCL, SDA, GND and VCC to the original Raspberry pi GPIO connector as shown below. The same pin numbers work for Raspberry Pi 3.
Communication with the module
After connecting the module you have to find the device in the I2C bus. For that, give the following command:
i2cdetect -l
You will get a display like this:
if not i2c detect give command, and enable I2C for raspberry pi.
pi@raspberrypi:~ $ sudo raspi-config
This shows that there are two i2c buses with the names i2c-0 and i2c-2.
In order to find to which of these your module is connected, give the following command.
i2cdetect -y -r 0
and
i2cdetect -y -r 2
The responses will be as follows.
This shows that the module is attached to the bus i2c-2 and that its address is 0x68.
Now, in order to communicate with either the MPU6050, HMC5983 or ADXL345 through a C program, we have to open the device file using a statement like this:
fd = open("/dev/i2c-2", O_RDWR);
and then select the device with a call to a function like this:
address=0x68; (The address is 0x68 for the MPU6050. But it will be different for others)
selectDevice(fd,address,"adxl345");
C codes for each of the modules are given to you. But you may have to edit them to change the i2c bus, as mentioned above.
Download the relevant code for you and save it in the PC, in the directory which is mounted in the Beaglebone via NFS.
Next ssh to the Beaglebone, change directory to that location (using cd /mnt).
To compile the code give the command
gcc -o adxl345 adxl345.c
This will create an executable with the name mpu5060 and you can run it with the command
./adxl345
When you run this code, information read out from the module will be printed to the screen.
C code
// Build with: gcc -o adxl345 adxl345.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/i2c-dev.h>
#define ADXL345_I2C_ADDR 0x53
void selectDevice(int fd, int addr, char * name)
{
if (ioctl(fd, I2C_SLAVE, addr) < 0)
{
fprintf(stderr, "%s not present\n", name);
//exit(1);
}
}
void selectRegister(int fd, int reg)
{
char buf[1];
buf[0]=reg;
if (write(fd, buf, 1) != 1) {
fprintf(stderr, "Can't write to i2c slave\n");
}
}
void writeToDevice(int fd, int reg, int val)
{
char buf[2];
buf[0]=reg; buf[1]=val;
if (write(fd, buf, 2) != 2)
{
fprintf(stderr, "Can't write to ADXL345\n");
}
}
int main(int argc, char **argv)
{
unsigned int range;
int count, b;
short x, y, z;
float xa, ya, za;
int fd;
unsigned char buf[16];
if ((fd = open("/dev/i2c-2", O_RDWR)) < 0)
{
// Open port for reading and writing
fprintf(stderr, "Failed to open i2c bus\n");
exit(1);
}
selectDevice(fd, ADXL345_I2C_ADDR, "ADXL345");
writeToDevice(fd, 0x2d, 0);
writeToDevice(fd, 0x2d, 16);
writeToDevice(fd, 0x2d, 8);
writeToDevice(fd, 0x31, 0);
writeToDevice(fd, 0x31, 11);
while (1)
{
selectDevice(fd, ADXL345_I2C_ADDR, "ADXL345");
selectRegister(fd,0x32 );
if (read(fd, buf, 6) != 6) { // X, Y, Z accelerations
fprintf(stderr, "Unable to read from ADXL345\n");
}
else
{
x = buf[1]<<8| buf[0];
y = buf[3]<<8| buf[2];
z = buf[5]<<8| buf[4];
printf("x: %d y: %d z: %d\n", x, y, z);
}
sleep(1);
}
return 0;
}
Connecting to Raspberry Pi
For I2C connection you need only 4 pins. Connect SCL, SDA, GND and VCC to the original Raspberry pi GPIO connector as shown below. The same pin numbers work for Raspberry Pi 3.
Communication with the module
After connecting the module you have to find the device in the I2C bus. For that, give the following command:
i2cdetect -l
You will get a display like this:
if not i2c detect give command, and enable I2C for raspberry pi.
pi@raspberrypi:~ $ sudo raspi-config
This shows that there are two i2c buses with the names i2c-0 and i2c-2.
In order to find to which of these your module is connected, give the following command.
i2cdetect -y -r 0
and
i2cdetect -y -r 2
The responses will be as follows.
This shows that the module is attached to the bus i2c-2 and that its address is 0x68.
Now, in order to communicate with either the MPU6050, HMC5983 or ADXL345 through a C program, we have to open the device file using a statement like this:
fd = open("/dev/i2c-2", O_RDWR);
and then select the device with a call to a function like this:
address=0x68; (The address is 0x68 for the MPU6050. But it will be different for others)
selectDevice(fd,address,"adxl345");
C codes for each of the modules are given to you. But you may have to edit them to change the i2c bus, as mentioned above.
Download the relevant code for you and save it in the PC, in the directory which is mounted in the Beaglebone via NFS.
Next ssh to the Beaglebone, change directory to that location (using cd /mnt).
To compile the code give the command
gcc -o adxl345 adxl345.c
This will create an executable with the name mpu5060 and you can run it with the command
./adxl345
When you run this code, information read out from the module will be printed to the screen.
C code
// Build with: gcc -o adxl345 adxl345.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/i2c-dev.h>
#define ADXL345_I2C_ADDR 0x53
void selectDevice(int fd, int addr, char * name)
{
if (ioctl(fd, I2C_SLAVE, addr) < 0)
{
fprintf(stderr, "%s not present\n", name);
//exit(1);
}
}
void selectRegister(int fd, int reg)
{
char buf[1];
buf[0]=reg;
if (write(fd, buf, 1) != 1) {
fprintf(stderr, "Can't write to i2c slave\n");
}
}
void writeToDevice(int fd, int reg, int val)
{
char buf[2];
buf[0]=reg; buf[1]=val;
if (write(fd, buf, 2) != 2)
{
fprintf(stderr, "Can't write to ADXL345\n");
}
}
int main(int argc, char **argv)
{
unsigned int range;
int count, b;
short x, y, z;
float xa, ya, za;
int fd;
unsigned char buf[16];
if ((fd = open("/dev/i2c-2", O_RDWR)) < 0)
{
// Open port for reading and writing
fprintf(stderr, "Failed to open i2c bus\n");
exit(1);
}
selectDevice(fd, ADXL345_I2C_ADDR, "ADXL345");
writeToDevice(fd, 0x2d, 0);
writeToDevice(fd, 0x2d, 16);
writeToDevice(fd, 0x2d, 8);
writeToDevice(fd, 0x31, 0);
writeToDevice(fd, 0x31, 11);
while (1)
{
selectDevice(fd, ADXL345_I2C_ADDR, "ADXL345");
selectRegister(fd,0x32 );
if (read(fd, buf, 6) != 6) { // X, Y, Z accelerations
fprintf(stderr, "Unable to read from ADXL345\n");
}
else
{
x = buf[1]<<8| buf[0];
y = buf[3]<<8| buf[2];
z = buf[5]<<8| buf[4];
printf("x: %d y: %d z: %d\n", x, y, z);
}
sleep(1);
}
return 0;
}
No comments:
Post a Comment