驴友花雕
发表于 2021-9-30 07:40:55
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十二:使用简单的代码扫描三轴磁场传感器GY-271的 I2C 地址
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十二:使用简单的代码扫描三轴磁场传感器GY-271的 I2C 地址
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <Wire.h> //include Wire.h library
void setup()
{
Wire.begin(); // Wire communication begin
Serial.begin(9600); // The baudrate of Serial monitor is set in 9600
while (!Serial); // Waiting for Serial Monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address; //variable for error and I2C 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 the next I2C scan
}
驴友花雕
发表于 2021-9-30 07:42:59
实验串口返回情况
驴友花雕
发表于 2021-9-30 08:10:46
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十三:尝试不使用驱动库来读取XYZ
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十三:尝试不使用驱动库来读取XYZ
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <Wire.h> //I2C Arduino Library
#define HMC5883L_ADDR 0x0D //0011110b, I2C 7bit address of HMC5883
bool haveHMC5883L = false;
bool detectHMC5883L ()
{
// read identification registers
Wire.beginTransmission(HMC5883L_ADDR); //open communication with HMC5883
Wire.write(10); //select Identification register A
Wire.endTransmission();
Wire.requestFrom(HMC5883L_ADDR, 3);
if(3 == Wire.available()) {
char a = Wire.read();
char b = Wire.read();
char c = Wire.read();
if(a == 'H' && b == '4' && c == '3')
return true;
}
return false;
}
void setup()
{
//Initialize Serial and I2C communications
Serial.begin(9600);
Serial.println("GY271 TEST");
Wire.begin();
// lower I2C clock http://www.gammon.com.au/forum/?id=10896
TWBR = 78;// 25 kHz
TWSR |= _BV (TWPS0);// change prescaler
}
void loop()
{
bool detect = detectHMC5883L();
if(!haveHMC5883L)
{
if(detect)
{
haveHMC5883L = true;
Serial.println("We have HMC5883L, moving on");
// Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(HMC5883L_ADDR); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
else
{
Serial.println("No HMC5883L detected!");
delay(2000);
return;
}
}
else
{
if(!detect) {
haveHMC5883L = false;
Serial.println("Lost connection to HMC5883L!");
delay(2000);
return;
}
}
int x,y,z; //triple axis data
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(HMC5883L_ADDR);
Wire.write(0x0D); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(HMC5883L_ADDR, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print("y: ");
Serial.print(y);
Serial.print("z: ");
Serial.println(z);
delay(250);
}
驴友花雕
发表于 2021-9-30 08:13:23
实验串口返回情况(未能识别国产芯片)
驴友花雕
发表于 2021-9-30 09:54:31
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十四:QMC5883LCompass三轴磁力计罗盘传感器
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十四:QMC5883LCompass三轴磁力计罗盘传感器
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
void setup(void) {
// start serial port
Serial.begin(115200);
Serial.println("\n\n\nQMC5883 磁力计测试");
Serial.println("");
compass.init();
}
void loop() {
int x, y, z, a, b;
char myArray;
compass.read();
x = compass.getX();
y = compass.getY();
z = compass.getZ();
a = compass.getAzimuth();
b = compass.getBearing(a);
compass.getDirection(myArray, a);
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.print(z);
Serial.print(" 方位角: ");
Serial.print(a);
Serial.print(" 方位: ");
Serial.print(b);
Serial.print(" 方向: ");
Serial.print(myArray);
Serial.print(myArray);
Serial.print(myArray);
Serial.println();
delay(500);
}
驴友花雕
发表于 2021-9-30 09:55:50
实验串口返回情况
驴友花雕
发表于 2021-9-30 10:02:43
实验串口绘图器返回情况
驴友花雕
发表于 2021-9-30 10:11:57
实验场景图