具体讲解
传感器的整个感应范围为±3 g。它可以测量倾斜感应应用中由于重力引起的静态加速度,以及由于运动,冲击或振动而产生的动态加速度。
电路连接
该传感器的工作电压为1.8V至3.6VDC(最佳3.3V),通常仅消耗350µA的电流。但是,板载3.3V稳压器使其成为与Arduino等5V微控制器接口的理想选择。
这款面包板友好型电路板将ADXL335的每个引脚都分为6引脚,0.1英寸间距接头。其中包括3个用于X,Y和Z轴测量的模拟输出,2个电源引脚和一个自测引脚,可让在最终应用中检查传感器的功能。
代码实现
它仅在串行接口的每个轴上显示已校准的传感器输出。
const int xInput = A0;
const int yInput = A1;
const int zInput = A2;
// initialize minimum and maximum Raw Ranges for each axis
int RawMin = 0;
int RawMax = 1023;
// Take multiple samples to reduce noise
const int sampleSize = 10;
void setup()
{
analogReference(EXTERNAL);
Serial.begin(9600);
}
void loop()
{
//Read raw values
int xRaw = ReadAxis(xInput);
int yRaw = ReadAxis(yInput);
int zRaw = ReadAxis(zInput);
// Convert raw values to 'milli-Gs"
long xScaled = map(xRaw, RawMin, RawMax, -3000, 3000);
long yScaled = map(yRaw, RawMin, RawMax, -3000, 3000);
long zScaled = map(zRaw, RawMin, RawMax, -3000, 3000);
// re-scale to fractional Gs
float xAccel = xScaled / 1000.0;
float yAccel = yScaled / 1000.0;
float zAccel = zScaled / 1000.0;
Serial.print("X, Y, Z :: ");
Serial.print(xRaw);
Serial.print(", ");
Serial.print(yRaw);
Serial.print(", ");
Serial.print(zRaw);
Serial.print(" :: ");
Serial.print(xAccel,0);
Serial.print("G, ");
Serial.print(yAccel,0);
Serial.print("G, ");
Serial.print(zAccel,0);
Serial.println("G");
delay(200);
}
// Take samples and return the average
int ReadAxis(int axisPin)
{
long reading = 0;
analogRead(axisPin);
delay(1);
for (int i = 0; i < sampleSize; i++)
{
reading += analogRead(axisPin);
}
return reading/sampleSize;
}