• 【毕业设计】 NodeMCU使用mpu6050惯性传感器 - 单片机 物联网嵌入式



    1 简介

    Hi,大家好,这里是丹成学长,今天向大家介绍

    NodeMCU如何使用mpu6050惯性传感器


    单片机-嵌入式毕设选题大全及项目分享:

    https://blog.csdn.net/m0_71572576/article/details/125409052


    MPU6050传感器

    MPU6050传感器是一个集成了6轴运动跟踪装置的模块,分别是3轴陀螺仪和3轴加速度计,同时集成了数字运动处理器和温度传感器。通过I2C总线,他还可以接受来自其他传感器的输入,如3轴磁力计或压力传感器,因此如果将MPU6050与外部的3轴磁力计连接起来,它就可以提供完整的9轴输出了。

    MPU6050在X轴、Y轴和Z轴上的陀螺仪和加速计的值是二进制补码,温度值是整数形式。陀螺仪的读数是以每秒(dps)为单位的;加速度计的读数是以 g 为单位的;温度读数的单位是摄氏度。

    NodeMCU可以使用I2C通信协议与MPU6050传感器模块进行通信。

    引脚连接

    在这里插入图片描述

    测试的过程是通过Nodemcu从MPU6050传感器模块读取加速计、陀螺仪和温度值,并在串行监视器上显示出来。

    首先,我们需要按照上面Nodemcu与MPU6050的接口图将电路连接起来。

    然后让我们通过下面的程序来读取MPU6050的加速度计、陀螺仪和温度的值。

    使用Arduino IDE来编写c/c++代码。

    相关代码

    #include <Wire.h>
    
    // MPU6050 Slave Device Address
    const uint8_t MPU6050SlaveAddress = 0x68;
    
    // Select SDA and SCL pins for I2C communication 
    const uint8_t scl = D6;
    const uint8_t sda = D7;
    
    // sensitivity scale factor respective to full scale setting provided in datasheet 
    const uint16_t AccelScaleFactor = 16384;
    const uint16_t GyroScaleFactor = 131;
    
    // MPU6050 few configuration register addresses
    const uint8_t MPU6050_REGISTER_SMPLRT_DIV   =  0x19;
    const uint8_t MPU6050_REGISTER_USER_CTRL    =  0x6A;
    const uint8_t MPU6050_REGISTER_PWR_MGMT_1   =  0x6B;
    const uint8_t MPU6050_REGISTER_PWR_MGMT_2   =  0x6C;
    const uint8_t MPU6050_REGISTER_CONFIG       =  0x1A;
    const uint8_t MPU6050_REGISTER_GYRO_CONFIG  =  0x1B;
    const uint8_t MPU6050_REGISTER_ACCEL_CONFIG =  0x1C;
    const uint8_t MPU6050_REGISTER_FIFO_EN      =  0x23;
    const uint8_t MPU6050_REGISTER_INT_ENABLE   =  0x38;
    const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H =  0x3B;
    const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET  = 0x68;
    
    int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
    
    void setup() {
      Serial.begin(9600);
      Wire.begin(sda, scl);
      MPU6050_Init();
    }
    
    void loop() {
      double Ax, Ay, Az, T, Gx, Gy, Gz;
      
      Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
      
      //divide each with their sensitivity scale factor
      Ax = (double)AccelX/AccelScaleFactor;
      Ay = (double)AccelY/AccelScaleFactor;
      Az = (double)AccelZ/AccelScaleFactor;
      T = (double)Temperature/340+36.53; //temperature formula
      Gx = (double)GyroX/GyroScaleFactor;
      Gy = (double)GyroY/GyroScaleFactor;
      Gz = (double)GyroZ/GyroScaleFactor;
    
      Serial.print("Ax: "); Serial.print(Ax);
      Serial.print(" Ay: "); Serial.print(Ay);
      Serial.print(" Az: "); Serial.print(Az);
      Serial.print(" T: "); Serial.print(T);
      Serial.print(" Gx: "); Serial.print(Gx);
      Serial.print(" Gy: "); Serial.print(Gy);
      Serial.print(" Gz: "); Serial.println(Gz);
    
      delay(100);
    }
    
    void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){
      Wire.beginTransmission(deviceAddress);
      Wire.write(regAddress);
      Wire.write(data);
      Wire.endTransmission();
    }
    
    // read all 14 register
    void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
      Wire.beginTransmission(deviceAddress);
      Wire.write(regAddress);
      Wire.endTransmission();
      Wire.requestFrom(deviceAddress, (uint8_t)14);
      AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
      AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
      AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
      Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
      GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
      GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
      GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
    }
    
    //configure MPU6050
    void MPU6050_Init(){
      delay(150);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
      I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    串口监视器成功读取到mpu6050数据
    在这里插入图片描述

    数据格式:

    Arduino IDE串口监视器的输出如下所示:
    Ax = Accelerometer x axis data in g unit

    Ay = Accelerometer y axis data in g unit

    Az = Accelerometer z axis data in g unit

    T = temperature in degree/celcius

    Gx = Gyro x axis data in degree/seconds unit

    Gy = Gyro y axis data in degree/seconds unit

    Gz = Gyro z axis data in degree/seconds unit


    单片机-嵌入式毕设选题大全及项目分享:

    https://blog.csdn.net/m0_71572576/article/details/125409052


    6 最后

  • 相关阅读:
    MySQL的主从复制与读写分离详解
    ROS功能包编译报错fatal error: xxxxConfig.h: 没有那个文件或目录的解决方法及原理介绍
    干货 | 携程鸿蒙应用开发实践
    Android dumpsys 常用命令
    Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
    AntPathMatcher【Spring中提供的路径匹配器】
    bodipy荧光标记染料BDP 558/568 COOH/羧基羧酸,CAS:150173-72-1
    阿里云Maven和Gradle仓库最新配置
    【opencv】多版本安装
    element ui el-table分页多选功能
  • 原文地址:https://blog.csdn.net/m0_71572576/article/details/125477920