首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到有效的MPU6050传感器

找不到有效的MPU6050传感器
EN

Stack Overflow用户
提问于 2022-10-18 12:22:18
回答 1查看 68关注 0票数 0

我正在使用下面的代码与arduino-uno一起使用,但是经常得到“找不到有效的MPU6050传感器”。

代码语言:javascript
复制
#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup() {
  Serial.begin(115200);

  Serial.println("Initialize MPU6050");

  while (!mpu.begin()) {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }
}

void loop() {
 
}

我的阿迪诺工作得很好

所以,我使用下面的代码检查了MPU6050,

代码语言:javascript
复制
#include <Wire.h>
 
void setup()
{
  Wire.begin();
 
  Serial.begin(115200);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
} 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

按预期获得产出

扫描.

I2C设备地址0x68 !

done

从上面的输出中,我希望GPU6050能够工作

如何从GPU6050中获得值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-18 13:33:40

您的代码按预期工作,它告诉安装程序()当它无法连接到设备时,直到它能够连接到设备。所以当它停止打印消息时,它就被连接起来了。

现在,在循环()中,您应该编写代码来与设备协商。

这是一个很好的起点

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74110897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档