Lesson 27 – ADXL345 Acceleration Sensor


1. Introduction

In this course, we will directly connect ADXL345 Three Axis Acceleration Module to the Keyestudio RPI GPIO-PCF8591 Shield, then swing ADXL345, getting different three-axis value in the terminal.

2. Hardware Required

  • ADXL345 Three Axis Acceleration Module*1
  • Keyestudio RPI GPIO-PCF8591 Shield*1
  • USB Cable * 1
  • Raspberry Motherboard * 1
  • Dupont Wires* several

3. Connection Diagram

4. Programming

  • a. Raspberry pi default not open the I2C function, on the terminal, input sudo raspi-config to open raspberry configuration interface.
    b. Select the 7 Advanced Options, then select the A7 I2C option to enable the raspberry pi I2C function.
    c. Use winSCP to put the lesson26_ADXL345 into the lesson folder of raspberry system.
    d. In the terminal, input cd lesson and cd lesson26_ADXL345 to enter the folder.
    e. In the terminal, execute make to generate an ADXL345 executable file inside the folder.
    f. Wiring as the above diagram, then enter the lesson26_ADXL345 folder, execute sudo ./ADXL345 at the terminal. Then, swing ADXL345 sensor, you can get different three-axis value in the terminal.
    g. Ctrl + c can exit the processing program.Please refer to the figure below:

5. Sample Code

The first four lines below are –

  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <errno.h>
  • #include <wiringPiI2C.h>


#define  DevAddr  0x53  //device address

struct acc_dat{
	int x;
	int y;
	int z;

void adxl345_init(int fd)
	wiringPiI2CWriteReg8(fd, 0x31, 0x0b);
	wiringPiI2CWriteReg8(fd, 0x2d, 0x08);
//	wiringPiI2CWriteReg8(fd, 0x2e, 0x00);
	wiringPiI2CWriteReg8(fd, 0x1e, 0x00);
	wiringPiI2CWriteReg8(fd, 0x1f, 0x00);
	wiringPiI2CWriteReg8(fd, 0x20, 0x00);
	wiringPiI2CWriteReg8(fd, 0x21, 0x00);
	wiringPiI2CWriteReg8(fd, 0x22, 0x00);
	wiringPiI2CWriteReg8(fd, 0x23, 0x00);

	wiringPiI2CWriteReg8(fd, 0x24, 0x01);
	wiringPiI2CWriteReg8(fd, 0x25, 0x0f);
	wiringPiI2CWriteReg8(fd, 0x26, 0x2b);
	wiringPiI2CWriteReg8(fd, 0x27, 0x00);
	wiringPiI2CWriteReg8(fd, 0x28, 0x09);
	wiringPiI2CWriteReg8(fd, 0x29, 0xff);
	wiringPiI2CWriteReg8(fd, 0x2a, 0x80);
	wiringPiI2CWriteReg8(fd, 0x2c, 0x0a);
	wiringPiI2CWriteReg8(fd, 0x2f, 0x00);
	wiringPiI2CWriteReg8(fd, 0x38, 0x9f);

struct acc_dat adxl345_read_xyz(int fd)
	char x0, y0, z0, x1, y1, z1;
	struct acc_dat acc_xyz;

	x0 = 0xff - wiringPiI2CReadReg8(fd, 0x32);
	x1 = 0xff - wiringPiI2CReadReg8(fd, 0x33);
	y0 = 0xff - wiringPiI2CReadReg8(fd, 0x34);
	y1 = 0xff - wiringPiI2CReadReg8(fd, 0x35);
	z0 = 0xff - wiringPiI2CReadReg8(fd, 0x36);
	z1 = 0xff - wiringPiI2CReadReg8(fd, 0x37);

	acc_xyz.x = (int)(x1 << 8) + (int)x0;
	acc_xyz.y = (int)(y1 << 8) + (int)y0;
	acc_xyz.z = (int)(z1 << 8) + (int)z0;

	return acc_xyz;

int main(void)
	int fd;
	struct acc_dat acc_xyz;

	fd = wiringPiI2CSetup(DevAddr);
	if(-1 == fd){
		perror("I2C device setup error");	


		acc_xyz = adxl345_read_xyz(fd);
		printf("x: %05d  y: %05d  z: %05d\n", acc_xyz.x, acc_xyz.y, acc_xyz.z);
	return 0;



  1. This development track is based on the Rasbperry Pi and the Ultimate Starter Kit for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi SBC and the electronics components part of the Ultimate Starter Kit for the Raspberry Pi kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 4 B yet please head over to our store and purchase one now. You can pick up the Ultimate Starter Kit for the Raspberry Pi from OztoyLib.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and Ultimate Starter Kit for the Raspberry Pi at your local electronics hobby store.

About the Ultimate Starter Kit for the Raspberry Pi

The Ultimate Starter Kit for the Raspberry Pi comes packed with ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.) including tutorials to get started. You will learn how to connect up the ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.), create circuits using the Raspberry Pi including learning to write code in C. As you progress through the different tutorials you will explore the different capabilities of the Raspberry Pi including the intricacies of integrating the Raspberry Pi with the different electronic bits i.e. sensors, LED’s, switches, servos, etc. included in the starter kit.

The Ultimate Starter Kit for the Raspberry Pi comes along with detailed tutorials including code samples. Ultimate Starter Kit for the Raspberry Pi walks you through the basics of using the Raspberry Pi in a hands-on way. You’ll learn the fundamentals of electronics, programming in C and gain hands on experience through working on the Raspberry Pi building creative projects. The kit includes a selection of the most common and useful electronic components with an ebook of 37 projects. Starting the basics of electronics, to more complex projects, the kit will get you interacting with the physical world using sensor and actuators. Along with the kit you get access to detailed tutorials and wiring diagrams.

About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.