Arduino-Geomagnetic Sensor GY-271 Geomagnetic Detection Experiment

Arduino-Geomagnetic Sensor GY-271 Geomagnetic Detection Experiment

hardware preparation

Arduino Uno ( other versions are applicable )
insert image description here
Geomagnetic sensor GY-271
Please add image description

Wiring part

GY271 Arduino
VCC 5V
GND GND
SCL SCL
SDA SDA

insert image description here
( Note: SCL, SDA can be directly connected to the Arduino board )

code section

Before running this code, you need to prepare the library file MechaQMC5883.h
, which can be downloaded in Arduino.
Please add image description
If it is not feasible, I will provide a download of MechaQMC5883.h here .

code section

#include <MechaQMC5883.h> //调用地磁MechaQMC5883库

MechaQMC5883 qmc;     //实例化MechaQMC5883
void setup(){
    
    
   Wire.begin();  
   Serial.begin(9600);   //设置串口波特率  
   qmc.init();  
}

void loop() {
    
    
  /*初始化定义*/
  int x,y,z;
  int yaw;
  
  /*调用库文件进行读取*/
  qmc.read(&x,&y,&z);
  yaw = qmc.azimuth(&y,&x);

/*串口输出部分*/
  Serial.print("X轴: ");
  Serial.print(x);
  Serial.print(" Y轴: ");
  Serial.print(y);
  Serial.print(" Z轴: ");
  Serial.print(z);
  Serial.print(" 偏转角: ");
  Serial.print(yaw);
  Serial.println();
  delay(200);
}

Show results

You can change the position of the sensor to observe the geomagnetic detection of the sensor.
Please add image description
Good luck! ! !

Guess you like

Origin blog.csdn.net/weixin_50679163/article/details/119656671