• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
arduino读取mpu6050加速度传感器

 

 

 

 

 

 

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void MpuInt(){
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  //setupt motion detection
  mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
  mpu.setMotionDetectionThreshold(1);
  mpu.setMotionDetectionDuration(20);
  mpu.setInterruptPinLatch(true);  // Keep it latched.  Will turn off when reinitialized.
  mpu.setInterruptPinPolarity(true);
  mpu.setMotionInterrupt(true);
  }

void MpuGet(){
   if(mpu.getMotionInterruptStatus()) {

    sensors_event_t a, g, temp;
    mpu.getEvent(&a, &g, &temp);
    
    String msg=String()+
               +"AccelX:"+String(a.acceleration.x)
               +" AccelY:"+String(a.acceleration.x)
               +" AccelZ:"+String(a.acceleration.x)
               +" GyroX:"+String(g.gyro.x)
               +" GyroY:"+String(g.gyro.x)
               +" GyroZ:"+String(g.gyro.x)
               ;     
    Serial.println(msg);                  
  }
}
  
void setup(void) {
  Serial.begin(9600);
  while (!Serial)
    delay(10); 

  Serial.println("Adafruit MPU6050 test!");

  MpuInt();


  Serial.println("MPU6050 Found!");
  delay(100);
}

void loop() {

  MpuGet();

  delay(10);
}

  

 

posted on 2022-04-02 17:09  MKT-porter  阅读(833)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3