|
| 1 | +### |
| 2 | +I2C |
| 3 | +### |
| 4 | + |
| 5 | +About |
| 6 | +----- |
| 7 | + |
| 8 | +I2C (Inter-Integrated Circuit) / TWI (Two-wire Interface) is a widely used serial communication to connect devices in a short distance. This is one of the most common peripherals used to connect sensors, EEPROMs, RTC, ADC, DAC, displays, OLED, and many other devices and microcontrollers. |
| 9 | + |
| 10 | +This serial communication is considered as a low-speed bus, and multiple devices can be connected on the same two-wires bus, each with a unique 7-bits address (up to 128 devices). These two wires are called SDA (serial data line) and SCL (serial clock line). |
| 11 | + |
| 12 | +.. note:: The SDA and SCL lines require pull-up resistors. See the device datasheet for more details about the resistors' values and the operating voltage. |
| 13 | + |
| 14 | +I2C Modes |
| 15 | +********* |
| 16 | + |
| 17 | +The I2C can be used in two different modes: |
| 18 | + |
| 19 | +* **I2C Master Mode** |
| 20 | + * In this mode, the ESP32 generates the clock signal and initiates the communication with the slave device. |
| 21 | + |
| 22 | +.. figure:: ../_static/arduino_i2c_master.png |
| 23 | + :align: center |
| 24 | + :width: 720 |
| 25 | + :figclass: align-center |
| 26 | + |
| 27 | +* **I2C Slave Mode** |
| 28 | + * The slave mode, the clock is generated by the master device and responds to the master if the destination address is the same as the destination. |
| 29 | + |
| 30 | +.. figure:: ../_static/arduino_i2c_slave.png |
| 31 | + :align: center |
| 32 | + :width: 520 |
| 33 | + :figclass: align-center |
| 34 | + |
| 35 | +Arduino-ESP32 I2C API |
| 36 | +--------------------- |
| 37 | + |
| 38 | +The ESP32 I2C library is based on the `Arduino Wire Library`_ and implements a few more APIs, described in this documentation. |
| 39 | + |
| 40 | +I2C Common API |
| 41 | +************** |
| 42 | + |
| 43 | +Here are the common functions used for master and slave modes. |
| 44 | + |
| 45 | +begin |
| 46 | +^^^^^ |
| 47 | + |
| 48 | +This function is used to start the peripheral using the default configuration. |
| 49 | + |
| 50 | +.. code-block:: arduino |
| 51 | +
|
| 52 | + bool begin(); |
| 53 | +
|
| 54 | +This function will return ``true`` if the peripheral was initialized correctly. |
| 55 | + |
| 56 | +setPins |
| 57 | +^^^^^^^ |
| 58 | + |
| 59 | +This function is used to define the ``SDA`` and ``SCL`` pins. |
| 60 | + |
| 61 | +.. note:: Call this function before ``begin`` to change the pins from the default ones. |
| 62 | + |
| 63 | +.. code-block:: arduino |
| 64 | +
|
| 65 | + bool setPins(int sdaPin, int sclPin); |
| 66 | +
|
| 67 | +* ``sdaPin`` sets the GPIO to be used as the I2C peripheral data line. |
| 68 | + |
| 69 | +* ``sclPin`` sets the GPIO to be used as the I2C peripheral clock line. |
| 70 | + |
| 71 | +The default pins may vary from board to board. On the *Generic ESP32* the default I2C pins are: |
| 72 | + |
| 73 | +* ``sdaPin`` **GPIO21** |
| 74 | + |
| 75 | +* ``sclPin`` **GPIO22** |
| 76 | + |
| 77 | +This function will return ``true`` if the peripheral was configured correctly. |
| 78 | + |
| 79 | +setClock |
| 80 | +^^^^^^^^ |
| 81 | + |
| 82 | +Use this function to set the bus clock. The default value will be used if this function is not used. |
| 83 | + |
| 84 | +.. code-block:: arduino |
| 85 | +
|
| 86 | + bool setClock(uint32_t frequency); |
| 87 | +
|
| 88 | +* ``frequency`` sets the bus frequency clock. |
| 89 | + |
| 90 | +This function will return ``true`` if the clock was configured correctly. |
| 91 | + |
| 92 | +getClock |
| 93 | +^^^^^^^^ |
| 94 | + |
| 95 | +Use this function to get the bus clock. |
| 96 | + |
| 97 | +.. code-block:: arduino |
| 98 | +
|
| 99 | + uint32_t getClock(); |
| 100 | +
|
| 101 | +This function will return the current frequency configuration. |
| 102 | + |
| 103 | +setTimeOut |
| 104 | +^^^^^^^^^^ |
| 105 | + |
| 106 | +Set the bus timeout given in milliseconds. The default value is 50ms. |
| 107 | + |
| 108 | +.. code-block:: arduino |
| 109 | +
|
| 110 | + void setTimeOut(uint16_t timeOutMillis); |
| 111 | +
|
| 112 | +* ``timeOutMillis`` sets the timeout in ms. |
| 113 | + |
| 114 | +getTimeOut |
| 115 | +^^^^^^^^^^ |
| 116 | + |
| 117 | +Get the bus timeout in milliseconds. |
| 118 | + |
| 119 | +.. code-block:: arduino |
| 120 | +
|
| 121 | + uint16_t getTimeOut(); |
| 122 | +
|
| 123 | +This function will return the current timeout configuration. |
| 124 | + |
| 125 | +.. _i2c write: |
| 126 | + |
| 127 | +write |
| 128 | +^^^^^ |
| 129 | + |
| 130 | +This function writes data to the buffer. |
| 131 | + |
| 132 | +.. code-block:: arduino |
| 133 | +
|
| 134 | + size_t write(uint8_t); |
| 135 | + |
| 136 | +or |
| 137 | + |
| 138 | +.. code-block:: arduino |
| 139 | +
|
| 140 | + size_t write(const uint8_t *, size_t); |
| 141 | +
|
| 142 | +The return will be the size of the data added to the buffer. |
| 143 | + |
| 144 | +.. _i2c end: |
| 145 | + |
| 146 | +end |
| 147 | +^^^ |
| 148 | + |
| 149 | +This function will finish the communication and release all the allocated resources. After calling ``end`` you need to use ``begin`` again in order to initialize the I2C driver again. |
| 150 | + |
| 151 | +.. code-block:: arduino |
| 152 | +
|
| 153 | + bool end(); |
| 154 | +
|
| 155 | +
|
| 156 | +I2C Master Mode |
| 157 | +*************** |
| 158 | + |
| 159 | +This mode is used to initiate communication to the slave. |
| 160 | + |
| 161 | +Basic Usage |
| 162 | +^^^^^^^^^^^ |
| 163 | + |
| 164 | +To start using I2C master mode on the Arduino, the first step is to include the ``Wire.h`` header to the sketch. |
| 165 | + |
| 166 | +.. code-block:: arduino |
| 167 | +
|
| 168 | + #include "Wire.h" |
| 169 | +
|
| 170 | +Now, we can start the peripheral configuration by calling ``begin`` function. |
| 171 | + |
| 172 | +.. code-block:: arduino |
| 173 | +
|
| 174 | + Wire.begin(); |
| 175 | +
|
| 176 | +By using ``begin`` without any arguments, all the settings will be done by using the default values. To set the values by your own, see the function description. This function is described here: `i2c begin`_ |
| 177 | + |
| 178 | +After calling ``begin``, we can start the transmission by calling ``beginTransmission`` and passing the I2C slave address: |
| 179 | + |
| 180 | +.. code-block:: arduino |
| 181 | +
|
| 182 | + Wire.beginTransmission(I2C_DEV_ADDR); |
| 183 | +
|
| 184 | +To write some bytes to the slave, use the ``write`` function. |
| 185 | + |
| 186 | +.. code-block:: arduino |
| 187 | +
|
| 188 | + Wire.write(x); |
| 189 | +
|
| 190 | +You can pass different data types using ``write`` function. This function is described here: `i2c write`_ |
| 191 | + |
| 192 | +.. note:: The ``write`` function does not write directly to the slave device but adds to the I2C buffer. To do so, you need to use the ``endTransmission`` function to send the buffered bytes to the slave device. |
| 193 | + |
| 194 | +.. code-block:: arduino |
| 195 | +
|
| 196 | + Wire.endTransmission(true); |
| 197 | +
|
| 198 | +After calling ``endTransmission``, the data stored in the I2C buffer will be transmitted to the slave device. |
| 199 | + |
| 200 | +Now you can request a reading from the slave device. The ``requestFrom`` will ask for a readout to the selected device by giving the address and the size. |
| 201 | + |
| 202 | +.. code-block:: arduino |
| 203 | +
|
| 204 | + Wire.requestFrom(I2C_DEV_ADDR, SIZE); |
| 205 | +
|
| 206 | +and the ``readBytes`` will read it. |
| 207 | + |
| 208 | +.. code-block:: arduino |
| 209 | +
|
| 210 | + Wire.readBytes(temp, error); |
| 211 | +
|
| 212 | +.. _i2c begin: |
| 213 | + |
| 214 | +I2C Master APIs |
| 215 | +*************** |
| 216 | + |
| 217 | +Here are the I2C master APIs. These function are intended to be used only for master mode. |
| 218 | + |
| 219 | +begin |
| 220 | +^^^^^ |
| 221 | + |
| 222 | +In master mode, the ``begin`` function can be used by passing the **pins** and **bus frequency**. Use this function only for the master mode. |
| 223 | + |
| 224 | +.. code-block:: arduino |
| 225 | +
|
| 226 | + bool begin(int sdaPin, int sclPin, uint32_t frequency) |
| 227 | +
|
| 228 | +Alternatively, you can use the ``begin`` function without any argument to use all default values. |
| 229 | + |
| 230 | +This function will return ``true`` if the peripheral was initialized correctly. |
| 231 | + |
| 232 | +beginTransmission |
| 233 | +^^^^^^^^^^^^^^^^^ |
| 234 | + |
| 235 | +This function is used to star a communication process with the slave device. Call this function by passing the slave ``address`` before writing the message to the buffer. |
| 236 | + |
| 237 | +.. code-block:: arduino |
| 238 | +
|
| 239 | + void beginTransmission(uint16_t address) |
| 240 | +
|
| 241 | +endTransmission |
| 242 | +^^^^^^^^^^^^^^^ |
| 243 | + |
| 244 | +After writing to the buffer using `i2c write`_, use the function ``endTransmission`` to send the message to the slave device address defined on the ``beginTransmission`` function. |
| 245 | + |
| 246 | +.. code-block:: arduino |
| 247 | +
|
| 248 | + uint8_t endTransmission(bool sendStop); |
| 249 | +
|
| 250 | +* ``sendStop`` enables **(true)** or disables **(false)** the stop signal *(only used in master mode)*. |
| 251 | + |
| 252 | +Calling the this function without ``sendStop`` is equivalent to ``sendStop = true``. |
| 253 | + |
| 254 | +.. code-block:: arduino |
| 255 | + |
| 256 | + uint8_t endTransmission(void); |
| 257 | +
|
| 258 | +This function will return the error code. |
| 259 | + |
| 260 | +requestFrom |
| 261 | +^^^^^^^^^^^ |
| 262 | + |
| 263 | +To read from the slave device, use the ``requestFrom`` function. |
| 264 | + |
| 265 | +.. code-block:: arduino |
| 266 | +
|
| 267 | + uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop) |
| 268 | +
|
| 269 | +* ``address`` set the device address. |
| 270 | + |
| 271 | +* ``size`` define the size to be requested. |
| 272 | + |
| 273 | +* ``sendStop`` enables (true) or disables (false) the stop signal. |
| 274 | + |
| 275 | +This function will return the number of bytes read from the device. |
| 276 | + |
| 277 | +Example Application - WireMaster.ino |
| 278 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 279 | + |
| 280 | +Here is an example of how to use the I2C in Master Mode. |
| 281 | + |
| 282 | +.. literalinclude:: ../../../libraries/Wire/examples/WireMaster/WireMaster.ino |
| 283 | + :language: arduino |
| 284 | + |
| 285 | + |
| 286 | +I2C Slave Mode |
| 287 | +************** |
| 288 | + |
| 289 | +This mode is used to accept communication from the master. |
| 290 | + |
| 291 | +Basic Usage |
| 292 | +^^^^^^^^^^^ |
| 293 | + |
| 294 | +To start using I2C as slave mode on the Arduino, the first step is to include the ``Wire.h`` header to the scketch. |
| 295 | + |
| 296 | +.. code-block:: arduino |
| 297 | +
|
| 298 | + #include "Wire.h" |
| 299 | +
|
| 300 | +Before calling ``begin`` we must create two callback functions to handle the communication with the master device. |
| 301 | + |
| 302 | +.. code-block:: arduino |
| 303 | +
|
| 304 | + Wire.onReceive(onReceive); |
| 305 | +
|
| 306 | +and |
| 307 | + |
| 308 | +.. code-block:: arduino |
| 309 | +
|
| 310 | + Wire.onRequest(onRequest); |
| 311 | +
|
| 312 | +The ``onReceive`` will handle the request from the master device uppon a slave read request and the ``onRequest`` will handle the answer to the master. |
| 313 | + |
| 314 | +Now, we can start the peripheral configuration by calling ``begin`` function with the device address. |
| 315 | + |
| 316 | +.. code-block:: arduino |
| 317 | +
|
| 318 | + Wire.begin((uint8_t)I2C_DEV_ADDR); |
| 319 | +
|
| 320 | +By using ``begin`` without any arguments, all the settings will be done by using the default values. To set the values by your own, see the function description. This function is described here: `i2c begin`_ |
| 321 | + |
| 322 | + |
| 323 | +**For ESP32 only!** |
| 324 | + |
| 325 | +Use the function ``slaveWrite`` in order to pre-write to the slave response buffer. This is used only for the ESP32 in order to add the slave capability on the chip and keep compatability with Arduino. |
| 326 | + |
| 327 | +.. code-block:: arduino |
| 328 | +
|
| 329 | + Wire.slaveWrite((uint8_t *)message, strlen(message)); |
| 330 | +
|
| 331 | +I2C Slave APIs |
| 332 | +************** |
| 333 | + |
| 334 | +Here are the I2C slave APIs. These function are intended to be used only for slave mode. |
| 335 | + |
| 336 | +begin |
| 337 | +^^^^^ |
| 338 | + |
| 339 | +In slave mode, the ``begin`` function must be used by passing the **slave address**. You can also define the **pins** and the **bus frequency**. |
| 340 | + |
| 341 | +.. code-block:: arduino |
| 342 | +
|
| 343 | + bool Wire.begin(uint8_t addr, int sdaPin, int sclPin, uint32_t frequency) |
| 344 | +
|
| 345 | +This function will return ``true`` if the peripheral was initialized correctly. |
| 346 | + |
| 347 | +onReceive |
| 348 | +^^^^^^^^^ |
| 349 | + |
| 350 | +The ``onReceive`` function is used to define the callback for the data received from the master. |
| 351 | + |
| 352 | +.. code-block:: arduino |
| 353 | +
|
| 354 | + void onReceive( void (*)(int) ); |
| 355 | +
|
| 356 | +onRequest |
| 357 | +^^^^^^^^^ |
| 358 | + |
| 359 | +The ``onRequest`` function is used to define the callback for the data to be send to the master. |
| 360 | + |
| 361 | +.. code-block:: arduino |
| 362 | +
|
| 363 | + void onRequest( void (*)(void) ); |
| 364 | +
|
| 365 | +slaveWrite |
| 366 | +^^^^^^^^^^ |
| 367 | + |
| 368 | +The ``slaveWrite`` function writes on the slave response buffer before receiving the response message. This function is only used for adding the slave compatability for the ESP32. |
| 369 | + |
| 370 | +.. warning:: This function is only required for the ESP32. You **don't** need to use for ESP32-S2 and ESP32-C3. |
| 371 | + |
| 372 | +.. code-block:: arduino |
| 373 | +
|
| 374 | + size_t slaveWrite(const uint8_t *, size_t); |
| 375 | +
|
| 376 | +Example Application - WireSlave.ino |
| 377 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 378 | + |
| 379 | +Here is an example of how to use the I2C in Slave Mode. |
| 380 | + |
| 381 | +.. literalinclude:: ../../../libraries/Wire/examples/WireSlave/WireSlave.ino |
| 382 | + :language: arduino |
| 383 | + |
| 384 | +.. _Arduino Wire Library: https://www.arduino.cc/en/reference/wire |
0 commit comments