\doxysection{i2c\+\_\+magneto.\+ino}
\hypertarget{i2c__magneto_8ino_source}{}\label{i2c__magneto_8ino_source}\index{examples/i2c\_magneto/i2c\_magneto.ino@{examples/i2c\_magneto/i2c\_magneto.ino}}
\mbox{\hyperlink{i2c__magneto_8ino}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00001}00001\ /***************************************************************}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00002}00002\ \ *\ @file\ \ \ \ I2C\_Magneto.ino}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00003}00003\ \ *\ @brief\ \ \ Minimal\ I2C\ bring-\/up\ for\ 7Semi\ ICM-\/20948\ +}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00004}00004\ \ *\ \ \ \ \ \ \ \ \ \ AK09916\ magnetometer\ readout\ (updated\ API).}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00005}00005\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00006}00006\ \ *\ Features}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00007}00007\ \ *\ -\/\ I2C\ init\ on\ default/custom\ pins}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00008}00008\ \ *\ -\/\ IMU\ beginI2C()\ initialization}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00009}00009\ \ *\ -\/\ Manual\ sensor\ enable}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00010}00010\ \ *\ -\/\ Magnetometer\ initialization\ (initMag)}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00011}00011\ \ *\ -\/\ Read\ magnetometer\ (uT)\ at\ \string~2\ Hz}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00012}00012\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00013}00013\ \ *\ Wiring\ (Arduino\ UNO,\ I2C)}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00014}00014\ \ *\ -\/\ SDA\ \ -\/>\ A4}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00015}00015\ \ *\ -\/\ SCL\ \ -\/>\ A5}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00016}00016\ \ *\ -\/\ VCC\ \ -\/>\ 3V3\ (or\ 5V\ if\ board\ supports\ it)}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00017}00017\ \ *\ -\/\ GND\ \ -\/>\ GND}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00018}00018\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00019}00019\ \ *\ Wiring\ (ESP32,\ I2C\ default)}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00020}00020\ \ *\ -\/\ SDA\ \ -\/>\ GPIO21}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00021}00021\ \ *\ -\/\ SCL\ \ -\/>\ GPIO22}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00022}00022\ \ *\ -\/\ VCC\ \ -\/>\ 3V3}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00023}00023\ \ *\ -\/\ GND\ \ -\/>\ GND}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00024}00024\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00025}00025\ \ *\ Notes}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00026}00026\ \ *\ -\/\ WHO\_AM\_I\ must\ be\ 0xEA}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00027}00027\ \ *\ -\/\ initMag()\ must\ be\ called\ before\ readMag()}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00028}00028\ \ *\ -\/\ Magnetometer\ runs\ via\ internal\ I2C\ master}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00029}00029\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00030}00030\ \ *\ Author\ \ :\ 7Semi\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00031}00031\ \ *\ License\ :\ MIT}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00032}00032\ \ ***************************************************************/}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00033}00033\ \#include\ <Wire.h>}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00034}00034\ \#include\ <DevLab\_ICM20948.h>}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00035}00035\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00036}00036\ /*\ ======================\ User\ Config\ =======================\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00037}00037\ /**\ @brief\ I2C\ SDA\ pin\ used\ by\ the\ example\ board.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00038}00038\ \#define\ SDA\_PIN\ \ \ 6}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00039}00039\ /**\ @brief\ I2C\ SCL\ pin\ used\ by\ the\ example\ board.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00040}00040\ \#define\ SCL\_PIN\ \ \ 7}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00041}00041\ /**\ @brief\ I2C\ bus\ speed\ in\ hertz.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00042}00042\ \#define\ I2C\_FREQ\ \ 400000UL}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00043}00043\ /**\ @brief\ ICM-\/20948\ I2C\ address\ selected\ by\ the\ AD0\ pin.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00044}00044\ \#define\ ICM\_ADDR\ \ 0x69}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00045}00045\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00046}00046\ /**\ @brief\ Global\ ICM-\/20948\ driver\ instance.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00047}00047\ DevLab\_ICM20948\ imu;}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00048}00048\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00049}00049\ /**}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00050}00050\ \ *\ @brief\ Configure\ I2C,\ verify\ the\ IMU,\ and\ initialize\ the\ magnetometer.}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00051}00051\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00052}00052\ \ *\ The\ magnetometer\ is\ accessed\ through\ the\ ICM-\/20948\ internal\ I2C\ master,\ so}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00053}00053\ \ *\ initMag()\ must\ succeed\ before\ loop()\ can\ read\ magnetic\ field\ data.}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00054}00054\ \ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00055}00055\ void\ setup()\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00056}00056\ \ \ Serial.begin(115200);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00057}00057\ \ \ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00058}00058\ \ \ Serial.println(F("{}ICM-\/20948\ (I2C)\ —\ Magnetometer\ Example"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00059}00059\ \ \ Wire.begin(SDA\_PIN,\ SCL\_PIN);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00060}00060\ \ \ /*\ Initialize\ IMU.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00061}00061\ \ \ if\ (!imu.beginI2C(ICM\_ADDR,\ Wire,\ 400000))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00062}00062\ \ \ \ \ Serial.println(F("{}ERROR:\ beginI2C()\ failed."{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00063}00063\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00064}00064\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00065}00065\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00066}00066\ \ \ Serial.println(F("{}ICM-\/20948\ ready\ (I2C)."{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00067}00067\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00068}00068\ \ \ /*\ Verify\ device.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00069}00069\ \ \ uint8\_t\ who;}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00070}00070\ \ \ if\ (!imu.readWhoAmI(who)\ ||\ who\ !=\ 0xEA)\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00071}00071\ \ \ \ \ Serial.println(F("{}ERROR:\ WHO\_AM\_I\ mismatch"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00072}00072\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00073}00073\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00074}00074\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00075}00075\ \ \ Serial.print(F("{}WHO\_AM\_I\ =\ 0x"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00076}00076\ \ \ Serial.println(who,\ HEX);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00077}00077\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00078}00078\ \ \ /*\ Enable\ required\ sensors\ (mag\ uses\ internal\ I2C\ master)}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00079}00079\ \ \ \ *\ -\/\ accel/gyro\ not\ strictly\ required\ but\ safe\ to\ keep\ ON}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00080}00080\ \ \ \ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00081}00081\ \ \ if\ (!imu.setSensors(true,\ true,\ false))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00082}00082\ \ \ \ \ Serial.println(F("{}setSensors\ failed."{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00083}00083\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00084}00084\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00085}00085\ \ \ /*\ Initialize\ magnetometer.\ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00086}00086\ \ \ if\ (!imu.initMag())\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00087}00087\ \ \ \ \ Serial.println(F("{}ERROR:\ initMag()\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00088}00088\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00089}00089\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00090}00090\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00091}00091\ \ \ Serial.println(F("{}Magnetometer\ ready"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00092}00092\ \}}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00093}00093\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00094}00094\ /**}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00095}00095\ \ *\ @brief\ Read\ and\ print\ magnetometer\ data\ in\ microtesla.}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00096}00096\ \ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00097}00097\ void\ loop()\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00098}00098\ \ \ float\ mx,\ my,\ mz;}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00099}00099\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00100}00100\ \ \ /*\ Read\ magnetometer\ data}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00101}00101\ \ \ \ *\ -\/\ Output\ in\ microtesla\ (uT)}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00102}00102\ \ \ \ *\ -\/\ Returns\ true\ on\ success}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00103}00103\ \ \ \ */}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00104}00104\ \ \ if\ (imu.readMag(mx,\ my,\ mz))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00105}00105\ \ \ \ \ Serial.print(F("{}MAG\ [uT]:\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00106}00106\ \ \ \ \ Serial.print(mx,\ 2);\ Serial.print(F("{},\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00107}00107\ \ \ \ \ Serial.print(my,\ 2);\ Serial.print(F("{},\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00108}00108\ \ \ \ \ Serial.println(mz,\ 2);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00109}00109\ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00110}00110\ \ \ \ \ Serial.println(F("{}Mag\ read\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00111}00111\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00112}00112\ }
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00113}00113\ \ \ Serial.println(F("{}-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/"{}));}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00114}00114\ \ \ delay(500);}
\DoxyCodeLine{\Hypertarget{i2c__magneto_8ino_source_l00115}00115\ \}}

\end{DoxyCode}
