Nota
Ciao, benvenuto nella Community di SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts su Facebook! Approfondisci Raspberry Pi, Arduino e ESP32 con altri appassionati.
Perché unirsi?
Supporto esperto: Risolvi i problemi post-vendita e le sfide tecniche con l’aiuto della nostra community e del nostro team.
Impara e Condividi: Scambia consigli e tutorial per migliorare le tue competenze.
Anteprime Esclusive: Ottieni accesso anticipato agli annunci di nuovi prodotti e anteprime.
Sconti Speciali: Approfitta di sconti esclusivi sui nostri prodotti più recenti.
Promozioni Festive e Giveaway: Partecipa a giveaway e promozioni festive.
👉 Pronto a esplorare e creare con noi? Clicca [Qui] e unisciti oggi stesso!
Modulo GY-87 IMU
Il modulo GY-87 è dotato di tre chip sensori: MPU6050, QMC5883L e BMP180, ciascuno con capacità uniche. L’MPU6050 combina un giroscopio e un accelerometro per il tracciamento dei movimenti, il QMC5883L funge da magnetometro per il rilevamento della direzione e il BMP180 viene utilizzato per misurare la pressione barometrica e la temperatura. Questi possono essere interfacciati utilizzando il protocollo I2C per una comunicazione efficace con un Arduino.
Questi sensori sono progettati per un’integrazione senza soluzione di continuità tramite il protocollo I2C, garantendo una comunicazione efficiente con piattaforme come Arduino. Ogni sensore nel modulo GY-87 è accessibile attraverso indirizzi I2C unici: l’MPU6050 è accessibile a 0x68, il QMC5883L a 0x0D e il BMP180 a 0x77.
Tutorial individuali per ciascun chip sensore:
Se vuoi utilizzare questi tre chip contemporaneamente, ecco un semplice esempio:
Nota
Puoi aprire il file
09-gy87.inosotto il percorsoelite-explorer-kit-main\basic_project\09-gy87direttamente.Oppure copia questo codice nell’Arduino IDE.
Nota
Per installare la libreria, usa l’Arduino Library Manager.
1/*
2 This Arduino code is designed to work with the GY-87 IMU (Inertial Measurement Unit) module,
3 which is a multi-sensor module containing three individual sensors: MPU6050 (Accelerometer and Gyroscope),
4 QMC5883L Magnetometer, and BMP180 (Barometric Pressure and Temperature). The code initializes these
5 sensors and continuously prints their readings to the Serial Monitor.
6
7 Board: Arduino Uno R4
8 Component: GY-87 IMU Module
9 Library: https://github.com/adafruit/Adafruit_MPU6050 (Adafruit MPU6050 by Adafruit)
10 https://github.com/mprograms/QMC5883LCompass (QMC5883LCompass by MPrograms)
11 https://github.com/adafruit/Adafruit-BMP085-Library (Adafruit BMP085 Library by Adafruit)
12*/
13
14// Include required libraries
15#include <Adafruit_MPU6050.h>
16#include <Adafruit_Sensor.h>
17#include <Wire.h>
18#include <Adafruit_BMP085.h>
19#include <QMC5883LCompass.h>
20
21// Initialize sensor objects
22Adafruit_MPU6050 mpu;
23Adafruit_BMP085 bmp;
24QMC5883LCompass compass;
25
26void setup() {
27 // Initialize the serial communication with a baud rate of 9600
28 Serial.begin(9600);
29
30 // Initialize the MPU6050 sensor (accelerometer and gyroscope)
31 initializeMPU6050();
32
33 // Enable I2C bypass on MPU6050 to directly access the QMC5883L magnetometer
34 mpu.setI2CBypass(true);
35
36 // Initialize the QMC5883L magnetometer sensor
37 initializeQMC5883L();
38
39 // Initialize BMP180 barometric sensor
40 initializeBMP180();
41}
42
43
44void loop() {
45 // Print MPU6050 data
46 printMPU6050();
47
48 // Print QMC5883L data
49 printQMC5883L();
50
51 // Print BMP180 data
52 printBMP180();
53
54 delay(500);
55}
56
57void initializeQMC5883L() {
58
59 compass.init();
60
61 // You should replace the code below according to your calibration results
62 compass.setCalibrationOffsets(-458.00, -310.00, -362.00);
63 compass.setCalibrationScales(0.79, 0.79, 2.19);
64
65}
66
67void printQMC5883L() {
68
69 Serial.println();
70 Serial.println("QMC5883L ------------");
71
72 int x, y, z, a;
73 char myArray[3];
74
75 compass.read();
76
77 x = compass.getX();
78 y = compass.getY();
79 z = compass.getZ();
80
81 a = compass.getAzimuth();
82
83 compass.getDirection(myArray, a);
84
85 Serial.print("X: ");
86 Serial.print(x);
87
88 Serial.print(" Y: ");
89 Serial.print(y);
90
91 Serial.print(" Z: ");
92 Serial.print(z);
93
94 Serial.print(" Azimuth: ");
95 Serial.print(a);
96
97 Serial.print(" Direction: ");
98 Serial.print(myArray[0]);
99 Serial.print(myArray[1]);
100 Serial.println(myArray[2]);
101
102 Serial.println("QMC5883L ------------");
103 Serial.println();
104}
105
106
107void initializeMPU6050() {
108 // Check if the MPU6050 sensor is detected
109 if (!mpu.begin()) {
110 Serial.println("Failed to find MPU6050 chip");
111 while (1)
112 ; // Halt if sensor not found
113 }
114 Serial.println("MPU6050 Found!");
115
116 // set accelerometer range to +-8G
117 mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
118
119 // set gyro range to +- 500 deg/s
120 mpu.setGyroRange(MPU6050_RANGE_500_DEG);
121
122 // set filter bandwidth to 21 Hz
123 mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
124
125 Serial.println("");
126 delay(100);
127}
128
129void printMPU6050() {
130
131 Serial.println();
132 Serial.println("MPU6050 ------------");
133
134 /* Get new sensor events with the readings */
135 sensors_event_t a, g, temp;
136 mpu.getEvent(&a, &g, &temp);
137
138 /* Print out the values */
139 Serial.print("Acceleration X: ");
140 Serial.print(a.acceleration.x);
141 Serial.print(", Y: ");
142 Serial.print(a.acceleration.y);
143 Serial.print(", Z: ");
144 Serial.print(a.acceleration.z);
145 Serial.println(" m/s^2");
146
147 Serial.print("Rotation X: ");
148 Serial.print(g.gyro.x);
149 Serial.print(", Y: ");
150 Serial.print(g.gyro.y);
151 Serial.print(", Z: ");
152 Serial.print(g.gyro.z);
153 Serial.println(" rad/s");
154
155 Serial.print("Temperature: ");
156 Serial.print(temp.temperature);
157 Serial.println(" degC");
158
159 Serial.println("MPU6050 ------------");
160 Serial.println();
161}
162
163void initializeBMP180() {
164 // Start BMP180 initialization
165 if (!bmp.begin()) {
166 Serial.println("Could not find a valid BMP180 sensor, check wiring!");
167 while (1)
168 ; // Halt if sensor not found
169 }
170 Serial.println("BMP180 Found!");
171}
172
173void printBMP180() {
174 Serial.println();
175 Serial.println("BMP180 ------------");
176 Serial.print("Temperature = ");
177 Serial.print(bmp.readTemperature());
178 Serial.println(" *C");
179
180 Serial.print("Pressure = ");
181 Serial.print(bmp.readPressure());
182 Serial.println(" Pa");
183
184 // Calculate altitude assuming 'standard' barometric
185 // pressure of 1013.25 millibar = 101325 Pascal
186 Serial.print("Altitude = ");
187 Serial.print(bmp.readAltitude());
188 Serial.println(" meters");
189
190 Serial.print("Pressure at sealevel (calculated) = ");
191 Serial.print(bmp.readSealevelPressure());
192 Serial.println(" Pa");
193 Serial.println("BMP180 ------------");
194 Serial.println();
195}
Nota
I magnetometri devono essere calibrati (Calibrare QMC5883L) prima di poter essere utilizzati come bussole, devono essere mantenuti in piano durante l’uso e tenuti lontani da oggetti in ferro, materiali magnetizzati e fili che trasportano corrente.
Dopo che il codice è stato caricato con successo sul tuo Arduino Uno R4, il Monitor Seriale prenderà vita, stampando continuamente i dati dei sensori dal modulo GY-87 IMU. Questo modulo incorpora tre sensori individuali: l’MPU6050 per le letture di accelerometro e giroscopio, il QMC5883L per le letture del magnetometro e il BMP180 per le letture di pressione barometrica e temperatura.