HMC5883L Triaxial Magnetic Field Sensor
Module Source
Purchase Link:
https://detail.tmall.com/item.htm?abbucket=17&id=615694353550&ns=1&spm=a21n57.1.item.5.1a37523cltszZ0
Resource Download:
https://pan.baidu.com/s/1DfYH5OQHmD8zj1fMbI6ffg
Password:8888
Specifications
Hardware Connection
Make sure the VCC pin of the HMC5883L module is connected to the 3.3V pin of the development board, the GND pin is connected to GND, SDA is connected to A4 (the SDA pin on the development board can also be used), and SCL is connected to A5 (the SCL pin on the development board can also be used).
Usage Method
Install Library
Search for HMC5883L in the library management and find “Adafruit HMC5883 unified” to install.
Enter the code:
c
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 11, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void setup(void) {
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
/* 初始化HMC5883L传感器 */
if(!mag.begin())
{
/* 如果未检测到传感器则在此停止 */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
}
void loop(void) {
/* 声明一个结构体,用于存储传感器的数据 */
sensors_event_t event;
mag.getEvent(&event);
/* 以μTesla单位打印X、Y、Z轴的值 */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("μT");
/* 简单的延时 */
delay(500);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45