ADXL345 三轴加速度计 Arduino 姿态追踪教程
本教程讲解如何使用 ADXL345 加速度计 与 Arduino 测量倾角、追踪姿态,并用 Processing 实现 3D 可视化。 先讲原理与读数,再做 Processing 3D 可视化演示。 三轴加速度计,可测静态(重力)与动态(振动/运动)加速度。单位 m/s²,常用 g(1g = 9.8 m/s²)。 水平放置、Z 轴向上时:Z 输出 1g,X/Y 为 0。翻转则 Z 为 -1g。输出范围 -1g 到 +1g,据此用三角函数可解算倾角。 I2C 通信,2 根信号线 + 电源。 实测 Z 轴有 0.1g 偏差。校准方法: 校准值掉电丢失,每次上电需重写。 详见 Freescale 应用笔记 AN3461。 低通滤波让画面更平滑,但响应变慢。仅加速度计无法解算 Yaw(Z 轴旋转),且易受手部抖动干扰。 进阶方案:融合陀螺仪(如 MPU6050 集成 6 轴),或配合磁力计组成 9 DoF。下一教程将详解 MPU6050。 希望本教程对您有帮助。欢迎评论区提问,别忘了查看我的 Arduino 项目合集!概览

ADXL345 工作原理



Arduino 读取 ADXL345 数据

所需元件
基础读取代码
/* Arduino + ADXL345 加速度计教程 by Dejan, https://howtomechatronics.com */
#include 代码要点



零偏校准


// 放在 setup() 中
// X轴
Wire.beginTransmission(ADXL345); Wire.write(0x1E); Wire.write(1); Wire.endTransmission(); delay(10);
// Y轴
Wire.beginTransmission(ADXL345); Wire.write(0x1F); Wire.write(-2); Wire.endTransmission(); delay(10);
// Z轴
Wire.beginTransmission(ADXL345); Wire.write(0x20); Wire.write(-7); Wire.endTransmission(); delay(10);解算 Roll 与 Pitch
roll = atan(Y_out / sqrt(pow(X_out,2) + pow(Z_out,2))) * 180 / PI;
pitch = atan(-X_out / sqrt(pow(Y_out,2) + pow(Z_out,2))) * 180 / PI;Processing 3D 可视化

Arduino 发送端(含低通滤波)
/* Arduino + ADXL345 - 3D 可视化 by Dejan */
#include Processing 接收端
/* ADXL345 3D 可视化 by Dejan */
import processing.serial.*;
Serial myPort; String data=""; float roll, pitch;
void setup() {
size(960, 640, P3D);
myPort = new Serial(this, "COM8", 9600);
myPort.bufferUntil('\n');
}
void draw() {
translate(width/2, height/2, 0);
background(33);
textSize(22);
text("Roll: " + int(roll) + " Pitch: " + int(pitch), -100, 265);
rotateX(radians(roll));
rotateZ(radians(-pitch));
textSize(30); fill(0, 76, 153);
box(386, 40, 200);
textSize(25); fill(255);
text("www.HowToMechatronics.com", -183, 10, 101);
}
void serialEvent(Serial myPort) {
data = myPort.readStringUntil('\n');
if (data != null) {
data = trim(data);
String[] items = split(data, '/');
if (items.length > 1) {
roll = float(items[0]);
pitch = float(items[1]);
}
}
}
制造工艺
- Arduino + MPU6050 姿态控制舵机:SG90 与 MG996R 精准跟随实战
- u-blox LEA-6H GPS 模块:Arduino 与 Python 集成指南
- Blynk + Arduino 读取 DHT11 温湿度数据教程
- 在 Arduino Due 上实现语音识别与语音合成 - BitVoicer Server 方案
- Arduino 合成乐音:从零实现 88 键钢琴全音域
- Arduino 连接 NMEA-0183 设备全攻略:从硬件接线到协议解析
- Arduino Modbus 从机开发全攻略:基于 RS485 扩展板的分步实现指南
- Arduino 智能咖啡机:蓝牙控制、温度曲线与自动启停
- 使用 Arduino 和 Alexa 打造动画智能 LED 灯带控制系统
- 语音控制 LED:Arduino Micro + BitVoicer Server 语音识别实战
- 自制 AI 助手机器人:Arduino 与 Python 协同实现语音交互与肢体动作
- 树莓派 3 + Arduino 驱动 NeoPixel 灯带:Web 网页端炫彩动画控制器