\doxysection{mag\+\_\+test.\+ino}
\hypertarget{mag__test_8ino_source}{}\label{mag__test_8ino_source}\index{examples/mag\_test/mag\_test.ino@{examples/mag\_test/mag\_test.ino}}
\mbox{\hyperlink{mag__test_8ino}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00001}00001\ /**}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00002}00002\ \ *\ @file\ mag\_test.ino}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00003}00003\ \ *\ @brief\ Magnetometer\ operation-\/mode\ sweep\ for\ the\ 7Semi\ ICM-\/20948.}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00004}00004\ \ *\ @details\ Initializes\ the\ AK09916\ magnetometer\ through\ the\ ICM-\/20948\ internal}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00005}00005\ \ *\ I2C\ master,\ iterates\ through\ continuous\ measurement\ modes,\ and\ prints\ CSV-\/like}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00006}00006\ \ *\ magnetic\ field\ samples\ to\ the\ Serial\ monitor.}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00007}00007\ \ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00008}00008\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00009}00009\ \#include\ <Wire.h>}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00010}00010\ \#include\ <DevLab\_ICM20948.h>}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00011}00011\ //\ -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00012}00012\ //\ PINES\ I2C\ —\ ESP32C6\ NANO\ Unit\ Electronics}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00013}00013\ //\ -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00014}00014\ /**\ @brief\ I2C\ SDA\ pin\ used\ by\ the\ example\ board.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00015}00015\ \#define\ SDA\_PIN\ \ \ 6}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00016}00016\ /**\ @brief\ I2C\ SCL\ pin\ used\ by\ the\ example\ board.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00017}00017\ \#define\ SCL\_PIN\ \ \ 7}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00018}00018\ /**\ @brief\ I2C\ bus\ speed\ in\ hertz.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00019}00019\ \#define\ I2C\_FREQ\ \ 400000UL}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00020}00020\ /**\ @brief\ ICM-\/20948\ I2C\ address\ selected\ by\ the\ AD0\ pin.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00021}00021\ \#define\ ICM\_ADDR\ \ 0x69}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00022}00022\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00023}00023\ /**\ @brief\ Global\ ICM-\/20948\ driver\ instance.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00024}00024\ DevLab\_ICM20948\ imu;}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00025}00025\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00026}00026\ /**\ @brief\ Print\ a\ separator\ line\ to\ Serial.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00027}00027\ void\ printLinea()\ \ \ \ \ \ \{\ Serial.println(F("{}-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/"{}));\ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00028}00028\ /**\ @brief\ Print\ a\ double\ separator\ line\ to\ Serial.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00029}00029\ void\ printDobleLinea()\ \{\ Serial.println(F("{}============================================================"{}));\ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00030}00030\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00031}00031\ /**\ @brief\ Magnetometer\ operating\ modes\ tested\ by\ the\ sweep.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00032}00032\ const\ ICM20948\_Op\_Mode\ opMode[]\ =\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00033}00033\ \ \ ICM20948\_Op\_Mode::MODE\_POWER\_DOWN,}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00034}00034\ \ \ ICM20948\_Op\_Mode::MODE\_SINGLE\_MEASUREMENT,}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00035}00035\ \ \ ICM20948\_Op\_Mode::MODE\_CONTINUOUS\_1,}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00036}00036\ \ \ ICM20948\_Op\_Mode::MODE\_CONTINUOUS\_2,}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00037}00037\ \ \ ICM20948\_Op\_Mode::MODE\_CONTINUOUS\_3,}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00038}00038\ \ \ ICM20948\_Op\_Mode::MODE\_CONTINUOUS\_4,}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00039}00039\ \ \ ICM20948\_Op\_Mode::MODE\_SELF\_TEST}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00040}00040\ \};}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00041}00041\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00042}00042\ /**\ @brief\ Text\ labels\ matching\ the\ operating\ modes\ printed\ in\ the\ sweep.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00043}00043\ const\ char*\ etiquetasOpModeCfg[]\ =\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00044}00044\ \ \ "{}PowerDown"{},"{}SingleMeasure"{},"{}Continuo1"{},"{}Continuo2"{},"{}Continuo3"{},"{}Continuo4"{}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00045}00045\ \};}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00046}00046\ /**\ @brief\ Number\ of\ printable\ magnetometer\ operation\ modes.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00047}00047\ const\ uint8\_t\ N\_OPM\ =\ 6;}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00048}00048\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00049}00049\ /**}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00050}00050\ \ *\ @brief\ Initialize\ the\ magnetometer\ and\ collect\ samples\ for\ each\ mode.}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00051}00051\ \ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00052}00052\ void\ applySettings()\{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00053}00053\ \ /*\ Initialize\ magnetometer.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00054}00054\ \ \ if\ (!imu.initMag())\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00055}00055\ \ \ \ \ Serial.println(F("{}ERROR:\ initMag()\ failed"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00056}00056\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00057}00057\ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00058}00058\ \ \ for(int\ i\ =\ 2;\ i\ <\ N\_OPM;\ i++)\{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00059}00059\ \ \ \ \ imu.setMagOpMode(opMode[i]);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00060}00060\ \ \ \ \ \ delay(100);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00061}00061\ \ \ \ \ Serial.println(F("{}Magnetometer\ ready"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00062}00062\ \ \ \ \ \ float\ mx,\ my,\ mz;}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00063}00063\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00064}00064\ \ \ \ \ /*\ Read\ magnetometer\ data}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00065}00065\ \ \ \ \ *\ -\/\ Output\ in\ microtesla\ (uT)}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00066}00066\ \ \ \ \ *\ -\/\ Returns\ true\ on\ success}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00067}00067\ \ \ \ \ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00068}00068\ \ \ \ \ for(int\ r\ =\ 0;\ r\ \ <\ 500\ ;\ r++)\{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00069}00069\ \ \ \ \ \ \ if\ (imu.readMag(mx,\ my,\ mz))\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00070}00070\ \ \ \ \ \ \ \ \ Serial.printf("{}[\%d]"{},r);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00071}00071\ \ \ \ \ \ \ \ \ Serial.print("{};"{});}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00072}00072\ \ \ \ \ \ \ \ \ Serial.print(etiquetasOpModeCfg[i]);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00073}00073\ \ \ \ \ \ \ \ \ Serial.print("{};"{});}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00074}00074\ \ \ \ \ \ \ \ \ Serial.print(mx,\ 2);\ Serial.print(F("{};\ "{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00075}00075\ \ \ \ \ \ \ \ \ Serial.print(my,\ 2);\ Serial.print(F("{};\ "{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00076}00076\ \ \ \ \ \ \ \ \ Serial.println(mz,\ 2);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00077}00077\ \ \ \ \ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00078}00078\ \ \ \ \ \ \ \ \ Serial.println(F("{}Mag\ read\ failed"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00079}00079\ \ \ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00080}00080\ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00081}00081\ \ \ \ \ Serial.println(F("{}-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00082}00082\ \ \ \ \ delay(500);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00083}00083\ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00084}00084\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00085}00085\ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00086}00086\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00087}00087\ /**}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00088}00088\ \ *\ @brief\ Initialize\ I2C,\ verify\ the\ IMU,\ enable\ sensors,\ and\ start\ the\ sweep.}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00089}00089\ \ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00090}00090\ void\ setup()\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00091}00091\ \ \ //\ put\ your\ setup\ code\ here,\ to\ run\ once:}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00092}00092\ \ Serial.begin(115200);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00093}00093\ \ \ delay(200);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00094}00094\ \ \ Serial.println(F("{}ICM-\/20948\ (I2C)\ —\ Magnetometer\ Example"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00095}00095\ \ \ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00096}00096\ \ \ Wire.begin(SDA\_PIN,SCL\_PIN);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00097}00097\ \ \ /*\ Initialize\ IMU.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00098}00098\ \ \ if\ (!imu.beginI2C(ICM\_ADDR,\ Wire,\ 400000))\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00099}00099\ \ \ \ \ Serial.println(F("{}ERROR:\ beginI2C()\ failed."{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00100}00100\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00101}00101\ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00102}00102\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00103}00103\ \ \ Serial.println(F("{}ICM-\/20948\ ready\ (I2C)."{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00104}00104\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00105}00105\ \ \ /*\ Verify\ device.\ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00106}00106\ \ \ uint8\_t\ who;}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00107}00107\ \ \ if\ (!imu.readWhoAmI(who)\ ||\ who\ !=\ 0xEA)\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00108}00108\ \ \ \ \ Serial.println(F("{}ERROR:\ WHO\_AM\_I\ mismatch"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00109}00109\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00110}00110\ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00111}00111\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00112}00112\ \ \ Serial.print(F("{}WHO\_AM\_I\ =\ 0x"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00113}00113\ \ \ Serial.println(who,\ HEX);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00114}00114\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00115}00115\ \ \ /*\ Enable\ required\ sensors\ (mag\ uses\ internal\ I2C\ master)}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00116}00116\ \ \ \ *\ -\/\ accel/gyro\ not\ strictly\ required\ but\ safe\ to\ keep\ ON}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00117}00117\ \ \ \ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00118}00118\ \ \ if\ (!imu.setSensors(true,\ true,\ false))\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00119}00119\ \ \ \ \ Serial.println(F("{}setSensors\ failed."{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00120}00120\ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00121}00121\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00122}00122\ \ \ Serial.println(F("{}\ \ Sensor\ listo.\(\backslash\)n"{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00123}00123\ \ \ Serial.println(F("{}\ \ Sensor\ PLANO\ y\ QUIETO\ sobre\ la\ mesa."{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00124}00124\ \ \ Serial.println(F("{}\ \ Iniciando\ en\ 5\ segundos..."{}));}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00125}00125\ \ \ for\ (int\ i\ =\ 5;\ i\ >\ 0;\ i-\/-\/)\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00126}00126\ \ \ \ \ Serial.printf("{}\ \ \%d...\(\backslash\)n"{},\ i);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00127}00127\ \ \ \ \ delay(1000);}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00128}00128\ \ \ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00129}00129\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00130}00130\ \ \ applySettings();}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00131}00131\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00132}00132\ \}}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00133}00133\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00134}00134\ /**}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00135}00135\ \ *\ @brief\ Empty\ loop;\ this\ sketch\ runs\ the\ magnetometer\ sweep\ once\ in\ setup().}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00136}00136\ \ */}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00137}00137\ void\ loop()\ \{}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00138}00138\ \ \ //\ put\ your\ main\ code\ here,\ to\ run\ repeatedly:}
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00139}00139\ }
\DoxyCodeLine{\Hypertarget{mag__test_8ino_source_l00140}00140\ \}}

\end{DoxyCode}
