\doxysection{i2c\+\_\+basic.\+ino}
\hypertarget{i2c__basic_8ino_source}{}\label{i2c__basic_8ino_source}\index{examples/i2c\_basic/i2c\_basic.ino@{examples/i2c\_basic/i2c\_basic.ino}}
\mbox{\hyperlink{i2c__basic_8ino}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00001}00001\ /***************************************************************}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00002}00002\ \ *\ @file\ \ \ \ I2C\_Basic.ino}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00003}00003\ \ *\ @brief\ \ \ Minimal\ I2C\ bring-\/up\ for\ 7Semi\ ICM-\/20948\ +}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00004}00004\ \ *\ \ \ \ \ \ \ \ \ \ basic\ Accel/Gyro/Temp/Mag\ readout\ (updated\ API).}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00005}00005\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00006}00006\ \ *\ Features}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00007}00007\ \ *\ -\/\ I2C\ init\ (UNO\ /\ ESP32)}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00008}00008\ \ *\ -\/\ beginI2C()\ initialization}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00009}00009\ \ *\ -\/\ Manual\ sensor\ enable\ (setSensors)}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00010}00010\ \ *\ -\/\ Read\ Accel\ /\ Gyro\ /\ Temp\ /\ Mag}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00011}00011\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00012}00012\ \ *\ Notes}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00013}00013\ \ *\ -\/\ WHO\_AM\_I\ must\ be\ 0xEA}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00014}00014\ \ *\ -\/\ Sensors\ must\ be\ explicitly\ enabled}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00015}00015\ \ *\ -\/\ Magnetometer\ requires\ initMag()}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00016}00016\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00017}00017\ \ *\ Wiring\ (Arduino\ UNO\ I2C)}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00018}00018\ \ *\ -\/\ SDA\ -\/>\ A4}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00019}00019\ \ *\ -\/\ SCL\ -\/>\ A5}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00020}00020\ \ *\ -\/\ VCC\ -\/>\ 3V3\ (or\ 5V\ if\ supported)}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00021}00021\ \ *\ -\/\ GND\ -\/>\ GND}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00022}00022\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00023}00023\ \ *\ Wiring\ (ESP32\ default\ I2C)}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00024}00024\ \ *\ -\/\ SDA\ -\/>\ GPIO21}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00025}00025\ \ *\ -\/\ SCL\ -\/>\ GPIO22}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00026}00026\ \ ***************************************************************/}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00027}00027\ \#include\ <Wire.h>}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00028}00028\ \#include\ <DevLab\_ICM20948.h>}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00029}00029\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00030}00030\ /*\ ======================\ User\ Config\ =======================\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00031}00031\ /**\ @brief\ I2C\ SDA\ pin\ used\ by\ the\ example\ board.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00032}00032\ \#define\ SDA\_PIN\ \ \ 6}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00033}00033\ /**\ @brief\ I2C\ SCL\ pin\ used\ by\ the\ example\ board.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00034}00034\ \#define\ SCL\_PIN\ \ \ 7}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00035}00035\ /**\ @brief\ I2C\ bus\ speed\ in\ hertz.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00036}00036\ \#define\ I2C\_FREQ\ \ 400000UL}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00037}00037\ /**\ @brief\ ICM-\/20948\ I2C\ address\ selected\ by\ the\ AD0\ pin.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00038}00038\ \#define\ ICM\_ADDR\ \ 0x69}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00039}00039\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00040}00040\ /**\ @brief\ Global\ ICM-\/20948\ driver\ instance.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00041}00041\ DevLab\_ICM20948\ imu;}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00042}00042\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00043}00043\ /**}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00044}00044\ \ *\ @brief\ Configure\ Serial,\ I2C,\ sensor\ identity\ check,\ and\ enabled\ sensors.}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00045}00045\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00046}00046\ \ *\ The\ sketch\ stops\ in\ an\ infinite\ loop\ if\ the\ IMU\ cannot\ be\ initialized\ or}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00047}00047\ \ *\ the\ WHO\_AM\_I\ register\ does\ not\ match\ the\ expected\ ICM-\/20948\ value.}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00048}00048\ \ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00049}00049\ void\ setup()\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00050}00050\ \ \ Serial.begin(115200);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00051}00051\ \ \ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00052}00052\ \ \ Serial.println(F("{}ICM-\/20948\ —\ I2C\ Basic"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00053}00053\ \ \ Wire.begin(SDA\_PIN,\ SCL\_PIN);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00054}00054\ \ \ /*\ Initialize\ IMU.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00055}00055\ \ \ if\ (!imu.beginI2C(ICM\_ADDR,\ Wire,\ 400000))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00056}00056\ \ \ \ \ Serial.println(F("{}ERROR:\ beginI2C()\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00057}00057\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00058}00058\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00059}00059\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00060}00060\ \ \ /*\ WHO\_AM\_I\ check.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00061}00061\ \ \ uint8\_t\ who;}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00062}00062\ \ \ if\ (!imu.readWhoAmI(who)\ ||\ who\ !=\ 0xEA)\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00063}00063\ \ \ \ \ Serial.println(F("{}ERROR:\ WHO\_AM\_I\ mismatch"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00064}00064\ \ \ \ \ while\ (1)\ delay(200);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00065}00065\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00066}00066\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00067}00067\ \ \ Serial.print(F("{}WHO\_AM\_I\ =\ 0x"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00068}00068\ \ \ Serial.println(who,\ HEX);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00069}00069\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00070}00070\ \ \ /*\ Enable\ all\ sensors.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00071}00071\ \ \ if\ (!imu.setSensors(true,\ true,\ true))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00072}00072\ \ \ \ \ Serial.println(F("{}ERROR:\ setSensors\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00073}00073\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00074}00074\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00075}00075\ \ \ /*\ Initialize\ magnetometer.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00076}00076\ \ \ if\ (!imu.initMag())\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00077}00077\ \ \ \ \ Serial.println(F("{}Mag\ init\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00078}00078\ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00079}00079\ \ \ \ \ Serial.println(F("{}Mag\ initialized"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00080}00080\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00081}00081\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00082}00082\ \ \ Serial.println(F("{}Ready.\(\backslash\)n"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00083}00083\ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00084}00084\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00085}00085\ /**}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00086}00086\ \ *\ @brief\ Read\ accelerometer,\ gyroscope,\ magnetometer,\ and\ temperature\ data.}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00087}00087\ \ *}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00088}00088\ \ *\ Values\ are\ printed\ periodically\ to\ the\ Serial\ monitor\ in\ engineering\ units.}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00089}00089\ \ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00090}00090\ void\ loop()\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00091}00091\ \ \ float\ ax,\ ay,\ az;}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00092}00092\ \ \ float\ gx,\ gy,\ gz;}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00093}00093\ \ \ float\ mx,\ my,\ mz;}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00094}00094\ \ \ float\ tC;}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00095}00095\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00096}00096\ \ \ /*\ Read\ accelerometer.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00097}00097\ \ \ if\ (imu.readAccel(ax,\ ay,\ az))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00098}00098\ \ \ \ \ Serial.print(F("{}ACC\ [g]:\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00099}00099\ \ \ \ \ Serial.print(ax,\ 3);\ Serial.print("{},\ "{});}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00100}00100\ \ \ \ \ Serial.print(ay,\ 3);\ Serial.print("{},\ "{});}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00101}00101\ \ \ \ \ Serial.println(az,\ 3);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00102}00102\ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00103}00103\ \ \ \ \ Serial.println(F("{}ACC\ read\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00104}00104\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00105}00105\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00106}00106\ \ \ /*\ Read\ gyroscope.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00107}00107\ \ \ if\ (imu.readGyro(gx,\ gy,\ gz))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00108}00108\ \ \ \ \ Serial.print(F("{}GYR\ [dps]:\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00109}00109\ \ \ \ \ Serial.print(gx,\ 2);\ Serial.print("{},\ "{});}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00110}00110\ \ \ \ \ Serial.print(gy,\ 2);\ Serial.print("{},\ "{});}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00111}00111\ \ \ \ \ Serial.println(gz,\ 2);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00112}00112\ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00113}00113\ \ \ \ \ Serial.println(F("{}GYR\ read\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00114}00114\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00115}00115\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00116}00116\ \ \ /*\ Read\ magnetometer.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00117}00117\ \ \ if\ (imu.readMag(mx,\ my,\ mz))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00118}00118\ \ \ \ \ Serial.print(F("{}MAG\ [uT]:\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00119}00119\ \ \ \ \ Serial.print(mx,\ 2);\ Serial.print("{},\ "{});}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00120}00120\ \ \ \ \ Serial.print(my,\ 2);\ Serial.print("{},\ "{});}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00121}00121\ \ \ \ \ Serial.println(mz,\ 2);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00122}00122\ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00123}00123\ \ \ \ \ Serial.println(F("{}MAG\ read\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00124}00124\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00125}00125\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00126}00126\ \ \ /*\ Read\ temperature.\ */}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00127}00127\ \ \ if\ (imu.readTemperature(tC))\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00128}00128\ \ \ \ \ Serial.print(F("{}TMP\ [C]:\ "{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00129}00129\ \ \ \ \ Serial.println(tC,\ 2);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00130}00130\ \ \ \}\ else\ \{}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00131}00131\ \ \ \ \ Serial.println(F("{}TMP\ read\ failed"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00132}00132\ \ \ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00133}00133\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00134}00134\ \ \ Serial.println(F("{}-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/"{}));}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00135}00135\ \ \ delay(500);}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00136}00136\ \}}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00137}00137\ }
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00138}00138\ /*\ Note\ on\ magnetometer:}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00139}00139\ \ *\ -\/\ Uses\ internal\ I2C\ master\ (AK09916\ via\ ICM20948)}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00140}00140\ \ *\ -\/\ initMag()\ must\ be\ called\ before\ readMag()}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00141}00141\ \ *\ -\/\ If\ values\ are\ zero:}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00142}00142\ \ *\ \ \ →\ check\ initMag()}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00143}00143\ \ *\ \ \ →\ verify\ power\ +\ pull-\/ups}
\DoxyCodeLine{\Hypertarget{i2c__basic_8ino_source_l00144}00144\ \ */}

\end{DoxyCode}
