Arduino示例代码讲解:ADXL3xx 加速传感器
Arduino示例代码讲解:ADXL3xx 加速传感器
- ADXL3xx 加速传感器
-
- 功能概述
-
-
- 硬件部分:
- 软件部分:
-
- 代码逐行解释
-
-
- 定义常量
- `setup()` 函数
- `loop()` 函数
-
- 工作原理
ADXL3xx 加速传感器
这段代码是一个Arduino示例程序,用于读取Analog Devices的ADXL3xx系列加速度传感器的三个轴的值,并通过串行通信将加速度值发送到计算机。代码中提到的硬件连接方式是为兼容SparkFun的加速度传感器模块设计的。它适用于需要测量加速度的场景,例如运动检测或倾斜测量。
/*
ADXL3xx
Reads an Analog Devices ADXL3xx accelerometer and communicates the
acceleration to the computer. The pins used are designed to be easily
compatible with the breakout boards from Sparkfun, available from:
http://www.sparkfun.com/commerce/categories.php?c=80
http://www.arduino.cc/en/Tutorial/ADXL3xx
The circuit:
analog 0: accelerometer self test
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc
created 2 Jul 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
// these constants describe the pins. They won't change:
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)
void setup()
{
// initialize the serial communications:
Serial.begin(9600);
// Provide ground and power by using the analog inputs as normal