2013-12-18 13:36:34 [显示全部楼层]
6503浏览
查看: 6503|回复: 0

[进阶] [转载] [Arduino模块]CMPS03 罗盘

[复制链接]

[Arduino模块]

CMPS03电子罗盘能够检测当前传感器与地球磁场之间的偏角,其分辨率可达0.1度。此传感器可以方便地通过I2C总线连接到控制器上进行数据采集。


代码示例:

  1. /*
  2. CMPS03 with arduino I2C example
  3. This will display a value of 0 - 359 for a full rotation of the compass.
  4. The SDA line is on analog pin 4 of the arduino and is connected to pin 3 of the CMPS03.
  5. The SCL line is on analog pin 5 of the arduino and is conected to pin 2 of the CMPS03.
  6. Both SDA and SCL are also connected to the +5v via a couple of 1k8 resistors.
  7. A switch to callibrate the CMPS03 can be connected between pin 6 of the CMPS03 and the ground.
  8. */
  9. #include <Wire.h>
  10. #define ADDRESS 0x60 //defines address of compass
  11. void setup(){
  12.   Wire.begin(); //conects I2C
  13.   Serial.begin(9600);
  14. }
  15. void loop(){
  16.   byte highByte;
  17.   byte lowByte;
  18.   
  19.    Wire.beginTransmission(ADDRESS);      //starts communication with cmps03
  20.    Wire.write(2);                         //Sends the register we wish to read
  21.    Wire.endTransmission();
  22.    Wire.requestFrom(ADDRESS, 2);        //requests high byte
  23.    while(Wire.available() < 2);         //while there is a byte to receive
  24.    highByte = Wire.read();           //reads the byte as an integer
  25.    lowByte = Wire.read();
  26.    int bearing = ((highByte<<8)+lowByte)/10;
  27.    
  28.    Serial.println(bearing);
  29.    delay(100);
  30. }
复制代码



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail