Arduino FFT : Visualize vibrations with an accelerometer
May 15, 2025
In this article, we will explore how to use an accelerometer with Arduino to analyze and visualize vibrations in real time.
Hardware Setup
The LSM9DS1 is a 9-axis sensor that combines an accelerometer, gyroscope, and magnetometer. For this project, we will focus on the accelerometer to capture vibration data.
Arduino Code
The following code reads accelerometer data and sends it via the serial port:
#include <Arduino_LSM9DS1.h>
float x, y, z;
void setup() {
// Connect to the serial port
IMU.setContinuousMode();
}
void loop() {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
int angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
int angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
int angleZ = atan2(z, sqrt(x * x + y * y)) * 180 / PI;
// Send data in JSON format
}
delay(100);
}
Reading data from serial
Once the Arduino is connected and sending data via USB, we need to capture and process this stream on our computer.
You can use Node.js along with the serialport package to read the incoming accelerometer data.
Install it first:
npm install serialport
Then, create a small script to parse the data:
import { SerialPort } from 'serialport'
import { ReadlineParser } from '@serialport/parser-readline'
const port = new SerialPort({ path: '/dev/tty.usbmodemXXXX', baudRate: 9600 })
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }))
const samples = []
parser.on('data', (line) => {
try {
const json = JSON.parse(line)
samples.push(json.z) // Assuming you're analyzing Z-axis vibrations
} catch (e) {
console.error('Invalid JSON:', line)
}
})
Make sure your Arduino is sending data in JSON format like:
{"x":0.01,"y":0.02,"z":0.98}
Result
The resulting graph below displays the vibration data captured by the accelerometer in real time.
Peaks in the chart correspond to moments of increased vibration, allowing you to easily identify patterns or anomalies.
Thank you for reading!