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

2013-12-181.3万
AI 快速预览详细 收起
CMPS03电子罗盘是一款高精度的方向检测模块,通过I2C总线连接到Arduino控制器进行数据采集。其分辨率达到0.1度,适用于需要高精度方向检测的项目,如机器人导航和户外探险。代码示例展示了如何通过I2C总线读取方向数据并输出。

[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. }
复制代码




创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -