Magnetic sensor

  1. 示例代码

在arduino IDE中,点击「File」→「Examples」→「MMC56x3」→「magsensor」打开示例。

注意

掌控板MMC5603磁传感器连接到IIC接口,SDA接P20(GPIO44),SCL接P19(GPIO443).

示例-MMC56x3.ino
 1#include <MMC56x3.h>
 2#include <Wire.h>
 3
 4/* Assign a unique ID to this sensor at the same time */
 5MMC5603 mmc = MMC5603();
 6
 7void setup(void) {
 8  Serial.begin(115200);
 9  while (!Serial)
10    delay(10); // will pause Zero, Leonardo, etc until serial console opens
11
12  Serial.println("MMC5603 Magnetometer Test");
13  Serial.println("");
14
15  Wire.begin(44,43,400000);
16  /* Initialise the sensor */
17  if (!mmc.begin(MMC56X3_DEFAULT_ADDRESS, Wire)) {  // I2C mode
18    /* There was a problem detecting the MMC5603 ... check your connections */
19    Serial.println("Ooops, no MMC5603 detected ... Check your wiring!");
20    while (1) delay(10);
21  }
22}
23
24MMC5603_Data_t data;
25
26void loop(void) {
27  // Get a new sensor event 
28  mmc.getData(&data);
29
30  // Display the results (magnetic vector values are in micro-Tesla (uT))
31  Serial.print("X: ");
32  Serial.print(data.magX);
33  Serial.print("  ");
34  Serial.print("Y: ");
35  Serial.print(data.magY);
36  Serial.print("  ");
37  Serial.print("Z: ");
38  Serial.print(data.magZ);
39  Serial.print("  ");
40  Serial.println("uT");
41
42  // Read and display temperature
43  float temp_c = mmc.readTemperature();
44  Serial.print("Temp: "); Serial.print(temp_c); Serial.println(" *C");
45  // Delay before the next sample
46  delay(100);
47}

  1. 运行效果

串口打印磁传感器采集数据。