2018-06-01 18:10:00 +02:00
.. _i2c:
2018-05-13 11:37:02 +02:00
I²C Bus
=======
2018-11-14 22:12:27 +01:00
.. seo ::
2019-02-24 12:26:26 +01:00
:description: Instructions for setting up the I²C bus to communicate with 2-wire devices in ESPHome
2021-11-16 03:19:33 +01:00
:image: i2c.svg
2018-11-14 22:12:27 +01:00
:keywords: i2c, iic, bus
2020-12-21 01:55:44 +01:00
This component sets up the I²C bus for your ESP32 or ESP8266. In order for these components
2019-02-24 12:26:26 +01:00
to work correctly, you need to define the I²C bus in your configuration. Please note the ESP
2019-01-09 14:43:18 +01:00
will enable its internal 10kΩ pullup resistors for these pins, so you usually don't need to
2021-02-20 22:02:46 +01:00
put on external ones. You can use multiple devices on one I²C bus as each device is given a
2021-05-03 12:55:12 +02:00
unique address for communicating between it and the ESP. You can do this by hopping
2021-02-20 22:02:46 +01:00
wires from the two lines (SDA and SCL) from each device board to the next device board or by
2020-12-21 01:55:44 +01:00
connecting the wires from each device back to the two I²C pins on the ESP.
2018-05-13 11:37:02 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-05-13 11:37:02 +02:00
2021-03-08 01:55:52 +01:00
# Example configuration entry for ESP32
2018-05-13 11:37:02 +02:00
i2c:
2024-05-07 07:40:12 +02:00
sda: GPIOXX
scl: GPIOXX
2021-07-28 23:56:11 +02:00
scan: true
2019-08-30 10:53:30 +02:00
id: bus_a
2018-05-13 11:37:02 +02:00
Configuration variables:
2018-08-24 22:44:01 +02:00
------------------------
2018-05-13 11:37:02 +02:00
2019-02-24 12:26:26 +01:00
- **sda** (*Optional* , :ref: `config-pin` ): The pin for the data line of the I²C bus.
2018-06-01 18:10:00 +02:00
Defaults to the default of your board (usually GPIO21 for ESP32 and GPIO4 for ESP8266).
2019-02-24 12:26:26 +01:00
- **scl** (*Optional* , :ref: `config-pin` ): The pin for the clock line of the I²C bus.
2018-06-01 18:10:00 +02:00
Defaults to the default of your board (usually GPIO22 for ESP32 and
GPIO5 for ESP8266).
2019-02-24 12:26:26 +01:00
- **scan** (*Optional* , boolean): If ESPHome should do a search of the I²C address space on startup.
2021-07-28 23:56:11 +02:00
Defaults to `` true `` .
2019-02-24 12:26:26 +01:00
- **frequency** (*Optional* , float): Set the frequency the I²C bus should operate on.
2021-09-13 22:20:44 +02:00
Defaults to `` 50kHz `` . Values are `` 10kHz `` , `` 50kHz `` , `` 100kHz `` , `` 200kHz `` , ... `` 800kHz ``
2024-04-09 03:11:49 +02:00
- **timeout** (*Optional* , :ref: `config-time` ): Set the I²C bus timeout.
Defaults to the framework defaults (`` 100us `` on `` esp32 `` with `` esp-idf `` , `` 50ms `` on `` esp32 `` with `` Arduino `` ,
`` 1s `` on `` esp8266 `` and `` 1s `` on `` rp2040 `` ). Maximum on `` esp-idf `` is 13ms.
2021-02-20 22:02:46 +01:00
- **id** (*Optional* , :ref: `config-id` ): Manually specify the ID for this I²C bus if you need multiple I²C buses.
2019-08-30 10:53:30 +02:00
.. note ::
2022-07-07 01:28:34 +02:00
If the device can support multiple I²C buses (ESP32 has 2, ESP8266 does not support more than one) these buses need to be defined as below and sensors need to be setup specifying the correct bus:
2019-08-30 10:53:30 +02:00
.. code-block :: yaml
# Example configuration entry
i2c:
- id: bus_a
2024-05-07 07:40:12 +02:00
sda: GPIOXX
scl: GPIOXX
2021-07-28 23:56:11 +02:00
scan: true
2019-08-30 10:53:30 +02:00
- id: bus_b
2024-05-07 07:40:12 +02:00
sda: GPIOXX
scl: GPIOXX
2021-07-28 23:56:11 +02:00
scan: true
2019-08-30 10:53:30 +02:00
# Sensors should be specified as follows
- platform: bme680
i2c_id: bus_b
address: 0x76
# ...
2018-05-14 21:15:49 +02:00
2022-11-28 12:44:38 +01:00
For I2C multiplexing see :doc: `/components/tca9548a` .
2021-03-29 21:50:43 +02:00
2018-06-01 18:10:00 +02:00
See Also
--------
2022-11-28 12:44:38 +01:00
- :doc: `/components/tca9548a`
2019-05-12 22:44:59 +02:00
- :apiref: `i2c/i2c.h`
2019-02-07 13:54:45 +01:00
- :ghedit: `Edit`