MPU6500和BMP280的代码
时间: 2024-06-11 20:08:40 浏览: 192
由于您没有指定使用哪种开发板或编程语言,我提供两个不同的代码示例,一个是Arduino代码,另一个是STM32代码。
Arduino代码:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <MPU6050.h>
MPU6050 mpu;
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!bmp.begin(0x76)) {
Serial.println("Could not find BMP280 sensor!");
while (1);
}
mpu.initialize();
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void loop() {
sensors_event_t temp_event, pressure_event;
bmp.getEvent(&pressure_event);
bmp.getTemperature(&temp_event);
float temperature = temp_event.temperature;
float pressure = pressure_event.pressure;
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" deg C, Pressure: ");
Serial.print(pressure / 100.0);
Serial.print(" hPa, Accel: ");
Serial.print(ax);
Serial.print(", ");
Serial.print(ay);
Serial.print(", ");
Serial.print(az);
Serial.print(" Gyro: ");
Serial.print(gx);
Serial.print(", ");
Serial.print(gy);
Serial.print(", ");
Serial.println(gz);
delay(500);
}
STM32代码:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <MPU6050.h>
MPU6050 mpu;
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!bmp.begin(0x76)) {
Serial.println("Could not find BMP280 sensor!");
while (1);
}
mpu.initialize();
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void loop() {
sensors_event_t temp_event, pressure_event;
bmp.getEvent(&pressure_event);
bmp.getTemperature(&temp_event);
float temperature = temp_event.temperature;
float pressure = pressure_event.pressure;
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" deg C, Pressure: ");
Serial.print(pressure / 100.0);
Serial.print(" hPa, Accel: ");
Serial.print(ax);
Serial.print(", ");
Serial.print(ay);
Serial.print(", ");
Serial.print(az);
Serial.print(" Gyro: ");
Serial.print(gx);
Serial.print(", ");
Serial.print(gy);
Serial.print(", ");
Serial.println(gz);
delay(500);
}
阅读全文