Arduino连接HMC5883L三轴电子罗盘传感器

用途:测量地磁方向,测量物体静止时候的方向,测量传感器周围磁力线的方向。注意,测量地磁时候容易受到周围磁场影响,

主芯片HMC5883三轴磁阻传感器特点(抄自网上):
1,数字量输出:I2C数字量输出接口,设计使用非常方便。
2,尺寸小: 3x3x0.9mm LCC封装,适合大规模量产使用。
3,精度高:1-2度,内置12位A/D,OFFSET, SET/RESET 电路,不会出现磁饱和现象,不会有累加误差。
4,支持自动校准程序,简化使用步骤,终端产品使用非常方便。
5,内置自测试电路,方便量产测试,无需增加额外昂贵的测试设备。
6,功耗低:供电电压1.8V, 功耗睡眠模式-2.5uA 测量模式-0.6mA
1.jpg


连接方法:
只要连接VCC,GND,SDA,SCL四条线。
  • Arduino GND -> HMC5883L GND
  • Arduino 3.3V -> HMC5883L VCC
  • Arduino A4 (SDA) -> HMC5883L SDA
  • Arduino A5 (SCL) -> HMC5883L SCL

    (注意,接线是A4,A5,不是D4,D5)



    程序编写:

    #include < Wire.h>
    #include < HMC5883L.h>
    HMC5883L compass;
    void setup()
    {
       Serial. begin(9600);
       Wire. begin();
      compass = HMC5883L();
      compass. SetScale(1.3);
      compass. SetMeasurementMode( Measurement_Continuous);
    }
    void loop()
    {
       MagnetometerRaw raw = compass. ReadRawAxis();
       MagnetometerScaled scaled = compass. ReadScaledAxis();
       float xHeading = atan2(scaled. YAxis, scaled. XAxis);
       float yHeading = atan2(scaled. ZAxis, scaled. XAxis);
       float zHeading = atan2(scaled. ZAxis, scaled. YAxis);
       if(xHeading < 0) xHeading += 2* PI;
       if(xHeading > 2* PI) xHeading -= 2* PI;
       if(yHeading < 0) yHeading += 2* PI;
       if(yHeading > 2* PI) yHeading -= 2* PI;
       if(zHeading < 0) zHeading += 2* PI;
       if(zHeading > 2* PI) zHeading -= 2* PI;
       float xDegrees = xHeading * 180/M_PI;
       float yDegrees = yHeading * 180/M_PI;
       float zDegrees = zHeading * 180/M_PI;
       Serial. print(xDegrees);
       Serial. print( ",");
       Serial. print(yDegrees);
       Serial. print( ",");
       Serial. print(zDegrees);
       Serial. println( ";");
       delay(100);
    }


    打开Arduino串口监视器即可看到结果(X平面角度,Y平面角度,Z平面角度):
    3.jpg

    HMC5883L.zip(5.55 KB, 下载次数: 2164)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值