arduino 一次性读取serial_MPU9250 299DOF 九轴/9轴姿态加速度陀螺仪指南针磁场传感器Arduino...

4af2d8cdbe073102c543c492e42d358b.png

目标:利用Arduino读取传感器数据,并在串口中输出;

准备:Arduino、MPU9250九轴感器、杜邦线;

接线方式:(arduino-->MPU9250)

3.3v/5v-->VCC;

GND-->GND;

A4-->SDA;

A5-->SCL;

库文件与包:链接: https://pan.baidu.com/s/1oncDbpngT05Fmnxnc1O3zg 提取码: inwx

步骤

1、按上面的接线要求连接Arduino与MPU9250,注意:确保线线路板没有虚焊!;

2、下载测试代码。下载后,将libraries压缩文件解压后,将里面I2Cdev、MPU9150两个文件夹复制到Arduino IDE的libraries文件夹下;

107c928a0187fa67114becf220f7e6c6.png

3、打开arduino IDE软件,文件-->示例-->MPU9150,下面有两个实例,任选一下即可,这里选择MPU9150_raw02.ino;

// I2C device class (I2Cdev) demonstration Arduino sketch for MPU9150
// 1/4/2013 original by Jeff Rowberg <jeff@rowberg.net> at https://github.com/jrowberg/i2cdevlib
//          modified by Aaron Weiss <aaron@sparkfun.com>
//
// Changelog:
//     2011-10-07 - initial release
//     2013-1-4 - added raw magnetometer output

/* ============================================
I2Cdev device library code is placed under the MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"

// I2Cdev and MPU9150 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU9150.h"
#include "helper_3dmath.h"

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU9150 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;

#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(19200);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU9150 connection successful" : "MPU9150 connection failed");
    //Serial.println(accelgyro.testConnection());
    // configure Arduino LED pin for output
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);

    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);

    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g/m:t");
    Serial.print(ax); Serial.print("t");
    Serial.print(ay); Serial.print("t");
    Serial.print(az); Serial.print("t");
    Serial.print(gx); Serial.print("t");
    Serial.print(gy); Serial.print("t");
    Serial.print(gz); Serial.print("t");
    Serial.print(mx); Serial.print("t");
    Serial.print(my); Serial.print("t");
    Serial.println(mz);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

4、代码弄好后,写进Arduino,调出串口监视器如下所示,这样9组数据就读出来了。

c64510108081abf1bb4c9c34bd0b362d.png

7d1ab81e7e8e98f95804f66b65779134.png

问题:

1、可能会问为什么代码中是MPU9150而不是9250?

答:MPU6050、MPU9150、MPU9250等在网上的资料并不多,如果一个新手手头没有一份完全信得过的资料,很难找到合适的。因为网上很多复制、变版,都不知道哪个是可用,有时会怀疑自己的板子是不是坏的。MPU9150代码兼容MPU9250的板子,目前我手头找到的MPU9250代码问题太多,至少我是没有运行起来。

2、坑在哪里?

答:本示例代码仓库中也有很多对应的实例,这里可以保证MPU9150文件夹中的是可以运行的(与本文提供的实例有点差别)。按照实例,运行初始化接口时,会显示MPU9150 connection failed,但有数据输出。修改代码MPU9150如下图,还不知道这句是啥意思,这里暂时把0x34删除。

7340403575fbd3755a350292f46d562a.png
修改后

0efc8cf51a17f58a7615c95f05876898.png
修改前
2807e694700ed8991965f213f0278a7b.png
Arduino读取MPU9250九轴陀螺仪数https://www.zhihu.com/video/1115722865789693952
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值