arduino教程系列12-ADXL345加速度传感器实验 加速度,arduino入门,arduino教程

前两天我们做了温度传感器实验,大家一定还有印象。今天我们来研究另外一种传感器加速度传感器。



什么是加速度传感器



加速度传感器,作用是测量在加速过程中产生的力。最基本的如咱们平常所熟悉的是重力加速度,大小是1g。



加速度传感器一般用于什么地方



通过测量由重力引起的加速度,你可以计算出设备相对于水平面的倾斜角度。通过分析动态加速度,你可以分析出设备的移动方式。自平衡车中就是使用加速度传感器与陀螺仪进行卡尔曼滤波进行姿态矫正。



本次试验使用的ADXL345数字传感器,通过I2C或者SPI接口直接输出数字信号。在1g的加速度下,输出数值为256.


3206_1226.jpg

2011-7-28 22:56 上传
(24.89 KB)





下面是硬件连接图


Snap2.jpg

2011-7-29 22:16 上传
(112.73 KB)




Snap1.jpg

2011-8-16 20:31 上传
(157.36 KB)




IMGP0018_调整大小.JPG

2011-7-28 22:56 上传
(203.65 KB)





下面是002X IDE下的代码
ARDUINO

#include <Wire.h>  //调用arduino自带的I2C库
#include <LiquidCrystal.h>   //调用arduino自带的LiquidCrystal库
 
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
 
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);//设置接口
 
int ADXAddress = 0xA7>>1;  //转换为7位地址
int reading = 0;
int val = 0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;
 
void setup()
{
  lcd.begin(16, 2);  //初始化LCD
  delay(100);
  Wire.begin();  //初始化I2C
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.send(Register_2D);
  Wire.send(8);
  Wire.endTransmission();
}
 
void loop()
{
  Wire.beginTransmission(ADXAddress);
  Wire.send(Register_X0);
  Wire.send(Register_X1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    X0 = Wire.receive();
    X1 = Wire.receive();
    X1 = X1<<8;
    X_out = X0+X1;
  }
 
  Wire.beginTransmission(ADXAddress);
  Wire.send(Register_Y0);
  Wire.send(Register_Y1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Y0 = Wire.receive();
    Y1 = Wire.receive();
    Y1 = Y1<<8;
    Y_out = Y0+Y1;
  }
 
  Wire.beginTransmission(ADXAddress);
  Wire.send(Register_Z0);
  Wire.send(Register_Z1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Z0 = Wire.receive();
    Z1 = Wire.receive();
    Z1 = Z1<<8;
    Z_out = Z0+Z1;
  }
 
  Xg = X_out/256.00;//把输出结果转换为重力加速度g,精确到小数点后2位。
  Yg = Y_out/256.00;
  Zg = Z_out/256.00;
  lcd.clear(); //清屏
  lcd.print("X="); //使屏幕显示文字X=
  lcd.print(Xg);
  lcd.setCursor(8, 0);
  lcd.print("Y=");
  lcd.print(Yg);
  lcd.setCursor(0, 1);
  lcd.print("Z=");
  lcd.print(Zg);
  delay(300);  //延时0.3秒,刷新频率这里进行调整
 
}

试验效果如下


附件为ADXL345中文手册


网盘下载附件http://pan.baidu.com/s/1dExdYV3
ADXL345中文PDF.pdf



1.0以后Arduino语法修改很大,下面的代码是1.0以后的IDE使用的代码。

ARDUINO

#include <Wire.h>  //调用arduino自带的I2C库
#include <LiquidCrystal.h>   //调用arduino自带的LiquidCrystal库
 
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
 
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);//设置接口
 
int ADXAddress = 0xA7>>1;  //转换为7位地址
int reading = 0;
int val = 0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;
 
void setup()
{
  lcd.begin(16, 2);  //初始化LCD
  delay(100);
  Wire.begin();  //初始化I2C
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);
  Wire.endTransmission();
}
 
void loop()
{
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_X0);
  Wire.write(Register_X1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    X0 = Wire.read();
    X1 = Wire.read();
    X1 = X1<<8;
    X_out = X0+X1;
  }
 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Y0);
  Wire.write(Register_Y1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Y0 = Wire.read();
    Y1 = Wire.read();
    Y1 = Y1<<8;
    Y_out = Y0+Y1;
  }
 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Z0);
  Wire.write(Register_Z1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {
    Z0 = Wire.read();
    Z1 = Wire.read();
    Z1 = Z1<<8;
    Z_out = Z0+Z1;
  }
 
  Xg = X_out/256.00;//把输出结果转换为重力加速度g,精确到小数点后2位。
  Yg = Y_out/256.00;
  Zg = Z_out/256.00;
  lcd.clear(); //清屏
  lcd.print("X="); //使屏幕显示文字X=
  lcd.print(Xg);
  lcd.setCursor(8, 0);
  lcd.print("Y=");
  lcd.print(Yg);
  lcd.setCursor(0, 1);
  lcd.print("Z=");
  lcd.print(Zg);
  delay(300);  //延时0.3秒,刷新频率这里进行调整
 
}


via - 极客工坊

标签: Arduino教程