3 Axis Accelerometer and 3 Axis Gyroscope With Raspberry Pi

by austinhander in Circuits > Raspberry Pi

20 Views, 0 Favorites, 0 Comments

3 Axis Accelerometer and 3 Axis Gyroscope With Raspberry Pi

thumbnail_DataExample.png

This project involves using a Raspberry Pi, connecting it to a 6-axis accelerometer, and sending the data to a MySQL server. The device can be activated with a button and has an RGB LED indicating the state of the device. This is useful as a motion data logger. The design contains a Voltage converter so it can be connected to a 24V DC power source often found in industrial motion systems.

Supplies

Connect Components

Screenshot 2025-07-28 194906.png

All of the electrical components need to be connected based on how the diagram describes in order for the code to work properly. If the IO pins are changed, the variables for those pins must be updated in order for it to work properly. A Raspberry Pi breakout board was used along with jumper wires in order to make the connections easier to understand and create. The MPU6050 needs to be connected to SCL, SCA, ground, and 5V. The RGB LED module needs to be connected to 3 IO pins (in this case, 14, 15, and 18), and ground. The button needs to be connected to an IO pin (in this case, it was connected to IO pin 16), and ground.

Create Database

In order for the data to be stored, there first needs to be a database to store it. This database is going to run on the Raspberry Pi itself. Raspberry Pi's do not have the ability to use MySQL, so MariaDB is used instead. I have attached a tutorial to install MariaDB. The video by Coding with Ashwin can be found here.

sudo apt install mariadb-server mariadb-client -y
sudo mysql_secure_installation
sudo mariadb -u root -p
CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
bind-address = 127.0.0.1
bind-address = 0.0.0.0


CREATE TABLE `gyrodb`.`gyrocycle` (
`cID` INT NOT NULL AUTO_INCREMENT,
`cTime` TIMESTAMP NULL DEFAULT current_timestamp,
`cLengthSec` INT NULL,
PRIMARY KEY (`cID`),
UNIQUE INDEX `cID_UNIQUE` (`cID` ASC) VISIBLE);

CREATE TABLE `gyrodb`.`gyrodata` (
`dID` INT NOT NULL AUTO_INCREMENT,
`cID` INT NULL,
`cMS` INT NULL,
`cGX` DECIMAL(8,3) NULL,
`cGY` DECIMAL(8,3) NULL,
`cGZ` DECIMAL(8,3) NULL,
`cAX` DECIMAL(8,3) NULL,
`cAY` DECIMAL(8,3) NULL,
`cAZ` DECIMAL(8,3) NULL,
PRIMARY KEY (`dID`),
UNIQUE INDEX `dID_UNIQUE` (`dID` ASC) VISIBLE,
INDEX `cID_idx` (`cID` ASC) VISIBLE,
CONSTRAINT `cID`
FOREIGN KEY (`cID`)
REFERENCES `gyrodb`.`gyrocycle` (`cID`)
ON DELETE CASCADE
ON UPDATE CASCADE);

Programming

The purpose of this device is to start recording gyroscope/accelerometer data when a button is pressed, and then end when it is pressed again. Every time the button is newly pressed, the LED changes color based on whether it is writing data or not, and it creates a data point in a MySQL table, then logs the gyroscope/accelerometer data in a second table, with a value associated with the point in the first table. The program uses a state machine to do this. First, it detects when the button is pressed and changes the state of a variable representing its data sending mode and idle mode. When it is first activated, it goes into the first state "WriteFirst", where it puts the cycle number in the first column. After that, it loops through the second and third states, where it continuously writes the data to the second table every 50 milliseconds. The loop stops when the button is pressed again. The program is attached. In order for the program to work correctly, some dependencies need to be installed, including MySQLdb, which allows you to interface with a MySQL or MariaDB server.

pip install MySQL-python

Downloads

Design Case

In order to house all the components, I designed a case for all of the components. The components all screw in using M3 hardware, and the wires are all housed within the case. There is a hole in the lid where the LED can fit, which is flush with the surface of the lid. There are also holes for where the button can fit, and where the serial connections can be accessed on the Raspberry Pi. Lastly, there is a marker on the lid for which direction the case should be oriented. The design is attached below.

Downloads

Assemble Components

thumbnail_20250727_125957.jpg
thumbnail_20250727_143923.jpg

Once the case is 3D printed, screw the Raspberry Pi into the bottom holder, and use double sided tape to attach the power converter, MPU6050, and LED module to the base. Use an M3 tap to tap on the holes in the base, and drill out holes in the top to screw in. Use M3 screws to screw into the base.

Final Product

thumbnail_20250728_173638.jpg
thumbnail_DataExample.png

This is an example of data collected using this device. In this case, pivot motion was controlled by a servo motor, and a pneumatically powered pivot motor. The data show the servo motor offers greater motion profile capability compared to that of an air powered motor.