ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
esphome::i2c::I2CDevice Class Reference

This Class provides the methods to read/write bytes from/to an i2c device. More...

#include <i2c.h>

Inheritance diagram for esphome::i2c::I2CDevice:
esphome::max170xx::MAX170xxComponent< max170xx::Model::MODEL_MAX17043 > esphome::max170xx::MAX170xxComponent< max170xx::Model::MODEL_MAX17048 > esphome::ade7880::ADE7880 esphome::ade7953_i2c::AdE7953I2c esphome::ads1115::ADS1115Component esphome::ags10::AGS10Component esphome::aht10::AHT10Component esphome::aic3204::AIC3204 esphome::am2315c::AM2315C esphome::am2320::AM2320Component esphome::apds9306::APDS9306 esphome::apds9930::APDS9930Component esphome::apds9960::APDS9960 esphome::as3935_i2c::I2CAS3935Component esphome::as5600::AS5600Component esphome::as7341::AS7341Component esphome::as734x::AS734XComponent esphome::at581x::AT581XComponent esphome::axs15231::AXS15231Touchscreen esphome::bh1745::BH1745Component esphome::bh1750::BH1750Sensor esphome::bh1900nux::BH1900NUXSensor esphome::bm8563::BM8563 esphome::bme280_i2c::BME280I2CComponent esphome::bme680::BME680Component esphome::bme680_bsec::BME680BSECComponent esphome::bme68x_bsec2_i2c::BME68xBSEC2I2CComponent esphome::bmi160::BMI160Component esphome::bmi270::BMI270Component esphome::bmp085::BMP085Component esphome::bmp280_i2c::BMP280I2CComponent esphome::bmp3xx_i2c::BMP3XXI2CComponent esphome::bmp581_i2c::BMP581I2CComponent esphome::bq25186::BQ25186Component esphome::bq27220::BQ27220Component esphome::bq76972::BQ76972Component esphome::cap1188::CAP1188Component esphome::ccs811::CCS811Component esphome::ch422g::CH422GComponent esphome::ch423::CH423Component esphome::chsc6x::CHSC6XTouchscreen esphome::cst226::CST226Touchscreen esphome::cst328::CST328Touchscreen esphome::cst816::CST816Touchscreen esphome::cst9220::CST9220Touchscreen esphome::cw2017::CW2017 esphome::dac7678::DAC7678Output esphome::dfrobot_sen0465_i2c::DFRobotSEN0465I2CComponent esphome::dht12::DHT12Component esphome::dps310::DPS310Component esphome::ds1307::DS1307Component esphome::ds2484::DS2484OneWireBus esphome::ds248x::DS248xComponent esphome::ds3231::DS3231Component esphome::ee895::EE895Component esphome::ektf2232::EKTF2232Touchscreen esphome::emc2101::Emc2101Component esphome::ens160_i2c::ENS160I2CComponent esphome::ens210::ENS210Component esphome::es7210::ES7210 esphome::es7243e::ES7243E esphome::es8156::ES8156 esphome::es8311::ES8311 esphome::es8388::ES8388 esphome::es9038q2m_katana::ES9038Q2MKatana esphome::ezo::EZOSensor esphome::ezo_pmp::EzoPMP esphome::fs3000::FS3000Component esphome::ft5x06::FT5x06Touchscreen esphome::ft63x6::FT63X6Touchscreen esphome::fusb302b::FUSB302B esphome::gdk101::GDK101Component esphome::gl_r01_i2c::GLR01I2CComponent esphome::gp8403::GP8403Component esphome::grove_gas_mc_v2::GroveGasMultichannelV2Component esphome::grove_tb6612fng::GroveMotorDriveTB6612FNG esphome::gsl3670::GSL3670Touchscreen esphome::gt911::GT911Touchscreen esphome::hdc1080::HDC1080Component esphome::hdc2010::HDC2010Component esphome::hdc2080::HDC2080Component esphome::hdc302x::HDC302XComponent esphome::hm3301::HM3301Component esphome::hmc5883l::HMC5883LComponent esphome::honeywell_hih_i2c::HoneywellHIComponent esphome::honeywellabp2_i2c::HONEYWELLABP2Sensor esphome::hte501::HTE501Component esphome::htu21d::HTU21DComponent esphome::htu31d::HTU31DComponent esphome::husb238::Husb238Component esphome::hyt271::HYT271Component esphome::i2c_device::I2CDeviceComponent esphome::iaqcore::IAQCore esphome::icnt86::ICNT86Touchscreen esphome::ina219::INA219Component esphome::ina226::INA226Component esphome::ina260::INA260Component esphome::ina2xx_i2c::INA2XXI2C esphome::ina3221::INA3221Component esphome::inkplate::Inkplate esphome::kmeteriso::KMeterISOComponent esphome::lc709203f::Lc709203f esphome::lcd_pcf8574::PCF8574LCDDisplay esphome::lilygo_t5_47::LilygoT547Touchscreen esphome::lis2dw12_i2c::LIS2DW12I2CComponent esphome::lis3dh::LIS3DHComponent esphome::lm75b::LM75BComponent esphome::lp5562::LP5562Output esphome::lps22::LPS22Component esphome::lsm6ds::LSM6DSComponent esphome::ltr390::LTR390Component esphome::ltr501::LTRAlsPs501Component esphome::ltr_als_ps::LTRAlsPsComponent esphome::m5stack_4relay::M5Stack4Relay esphome::m5stack_8angle::M5Stack8AngleComponent esphome::max170xx::MAX170xxComponent< MODEL > esphome::max44009::MAX44009Sensor esphome::max6956::MAX6956 esphome::max9611::MAX9611Component esphome::mcp23008::MCP23008 esphome::mcp23016::MCP23016 esphome::mcp23017::MCP23017 esphome::mcp3221::MCP3221Sensor esphome::mcp4461::Mcp4461Component esphome::mcp4725::MCP4725 esphome::mcp4728::MCP4728Component esphome::mcp47a1::MCP47A1 esphome::mcp9600::MCP9600Component esphome::mcp9808::MCP9808Sensor esphome::mics_4514::MICS4514Component esphome::mlx90393::MLX90393Cls esphome::mlx90614::MLX90614Component esphome::mmc5603::MMC5603Component esphome::mmc5983::MMC5983Component esphome::mpl3115a2::MPL3115A2Component esphome::mpr121::MPR121Component esphome::mpu6050::MPU6050Component esphome::mpu6886::MPU6886Component esphome::ms5611::MS5611Component esphome::ms8607::MS8607Component esphome::ms8607::MS8607HumidityDevice esphome::msa3xx::MSA3xxComponent esphome::mt6701_i2c::MT6701I2CComponent esphome::nau7802::NAU7802Sensor esphome::npi19::NPI19Component esphome::ns2009::NS2009Component esphome::opt3001::OPT3001Sensor esphome::pca6416a::PCA6416AComponent esphome::pca9505::PCA9505Component esphome::pca9554::PCA9554Component esphome::pca9685::PCA9685Output esphome::pcf85063::PCF85063Component esphome::pcf8563::PCF8563Component esphome::pcf8574::PCF8574Component esphome::pcm5122::PCM5122 esphome::pi4ioe5v6408::PI4IOE5V6408Component esphome::pm2005::PM2005Component esphome::pmsa003i::PMSA003IComponent esphome::pmwcs3::PMWCS3Component esphome::pn532_i2c::PN532I2C esphome::pn7150_i2c::PN7150I2C esphome::pn7160_i2c::PN7160I2C esphome::qmc5883l::QMC5883LComponent esphome::qmi8658::QMI8658Component esphome::qmp6988::QMP6988Component esphome::qwiic_pir::QwiicPIRComponent esphome::rc522_i2c::RC522I2C esphome::rx8025t::RX8025TComponent esphome::rx8111::RX8111Component esphome::rx8130::RX8130Component esphome::seeed_multi_channel_relay::SeeedMultiChannelRelay esphome::sen0321_sensor::Sen0321Sensor esphome::sen21231_sensor::Sen21231Sensor esphome::sensirion_common::SensirionI2CDevice esphome::spa06_i2c::SPA06I2CComponent esphome::spd2010::SPD2010Touchscreen esphome::ssd1306_i2c::I2CSSD1306 esphome::ssd1327_i2c::I2CSSD1327 esphome::st7123::ST7123Touchscreen esphome::st7567_i2c::I2CST7567 esphome::stts22h::STTS22HComponent esphome::sx1509::SX1509Component esphome::sy6970::SY6970Component esphome::tas2780::TAS2780 esphome::tas58xx::TAS58xx esphome::tc74::TC74Component esphome::tca8418::TCA8418Component esphome::tca9548a::TCA9548AComponent esphome::tca9555::TCA9555Component esphome::tcs34725::TCS34725Component esphome::tee501::TEE501Component esphome::tem3200::TEM3200Component esphome::tfluna::TFLuna esphome::tlc59208f::TLC59208FOutput esphome::tmp102::TMP102Component esphome::tmp1075::TMP1075Sensor esphome::tmp117::TMP117Component esphome::tof10120::TOF10120Sensor esphome::tsl2561::TSL2561Sensor esphome::tsl2591::TSL2591Component esphome::tt21100::TT21100Touchscreen esphome::ttp229_lsf::TTP229LSFComponent esphome::ufire_ec::UFireECComponent esphome::ufire_ise::UFireISEComponent esphome::veml3235::VEML3235Sensor esphome::veml6040::VEML6040Component esphome::veml7700::VEML7700Component esphome::vl53l0x::VL53L0XSensor esphome::vl53l1x::VL53L1XSensor esphome::waveshare_io_ch32v003::WaveshareIOCH32V003Component esphome::weikai_i2c::WeikaiComponentI2C esphome::xdb401::XDB401Component esphome::xgzp68xx::XGZP68XXComponent esphome::xl9535::XL9535Component esphome::zio_ultrasonic::ZioUltrasonicComponent

Public Member Functions

 I2CDevice ()=default
 we use the C++ default constructor
 
void set_i2c_address (uint8_t address)
 We store the address of the device on the bus.
 
uint8_t get_i2c_address () const
 Returns the I2C address of the object.
 
void set_i2c_bus (I2CBus *bus)
 we store the pointer to the I2CBus to use
 
I2CRegister reg (uint8_t a_register)
 calls the I2CRegister constructor
 
I2CRegister16 reg16 (uint16_t a_register)
 calls the I2CRegister16 constructor
 
ErrorCode read (uint8_t *data, size_t len) const
 reads an array of bytes from the device using an I2CBus
 
ErrorCode read_register (uint8_t a_register, uint8_t *data, size_t len)
 reads an array of bytes from a specific register in the I²C device
 
ErrorCode read_register16 (uint16_t a_register, uint8_t *data, size_t len)
 reads an array of bytes from a specific register in the I²C device
 
ErrorCode write (const uint8_t *data, size_t len) const
 writes an array of bytes to a device using an I2CBus
 
ErrorCode write_read (const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const
 writes an array of bytes to a device, then reads an array, as a single transaction
 
ErrorCode write_register (uint8_t a_register, const uint8_t *data, size_t len) const
 writes an array of bytes to a specific register in the I²C device
 
ErrorCode write_register16 (uint16_t a_register, const uint8_t *data, size_t len) const
 write an array of bytes to a specific register in the I²C device
 
bool write_registers (const I2CRegValue *table, size_t count) const
 Write each register and value of table in order, stopping at the first failure.
 
template<size_t N>
bool write_registers (const I2CRegValue(&table)[N]) const
 
bool check_chip_id (Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
 Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
 
bool read_bytes (uint8_t a_register, uint8_t *data, uint8_t len)
 Compat APIs All methods below have been added for compatibility reasons.
 
bool read_bytes_raw (uint8_t *data, uint8_t len) const
 
template<size_t N>
optional< std::array< uint8_t, N > > read_bytes (uint8_t a_register)
 
template<size_t N>
optional< std::array< uint8_t, N > > read_bytes_raw ()
 
bool read_bytes_16 (uint8_t a_register, uint16_t *data, uint8_t len)
 
bool read_byte (uint8_t a_register, uint8_t *data)
 
optional< uint8_t > read_byte (uint8_t a_register)
 
bool read_byte_16 (uint8_t a_register, uint16_t *data)
 
bool read_bytes_16_le (uint8_t a_register, uint16_t *data, uint8_t len)
 Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endian hosts.
 
bool read_bytes_16_le (uint8_t a_register, int16_t *data, uint8_t len)
 
bool read_byte_16_le (uint8_t a_register, uint16_t *data)
 Read a 16-bit value stored least significant byte first.
 
bool write_bytes (uint8_t a_register, const uint8_t *data, uint8_t len) const
 
bool write_bytes (uint8_t a_register, const std::vector< uint8_t > &data) const
 
template<size_t N>
bool write_bytes (uint8_t a_register, const std::array< uint8_t, N > &data)
 
bool write_bytes_16 (uint8_t a_register, const uint16_t *data, uint8_t len) const
 
bool write_byte (uint8_t a_register, uint8_t data) const
 
bool write_byte_16 (uint8_t a_register, uint16_t data) const
 
bool write_byte_16_le (uint8_t a_register, uint16_t data) const
 Write a 16-bit value least significant byte first.
 
bool update_register (uint8_t a_register, uint8_t mask, uint8_t value)
 Read a register, replace the bits in mask with those of value and write it back.
 

Protected Attributes

I2CBus * bus_ {nullptr}
 pointer to I2CBus instance
 
uint8_t address_ {0x00}
 store the address of the device on the bus
 

Detailed Description

This Class provides the methods to read/write bytes from/to an i2c device.

Objects keep a list of devices found on bus as well as a pointer to the I2CBus in use.

Definition at line 144 of file i2c.h.

Constructor & Destructor Documentation

◆ I2CDevice()

esphome::i2c::I2CDevice::I2CDevice ( )
default

we use the C++ default constructor

Member Function Documentation

◆ check_chip_id()

bool esphome::i2c::I2CDevice::check_chip_id ( Component * component,
uint8_t a_register,
uint8_t expected,
uint8_t mask = 0xFF )
inline

Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.

Otherwise mark component failed and return false.

Definition at line 232 of file i2c.h.

◆ get_i2c_address()

uint8_t esphome::i2c::I2CDevice::get_i2c_address ( ) const
inline

Returns the I2C address of the object.

Returns
the I2C address

Definition at line 155 of file i2c.h.

◆ read()

ErrorCode esphome::i2c::I2CDevice::read ( uint8_t * data,
size_t len ) const
inline

reads an array of bytes from the device using an I2CBus

Parameters
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 175 of file i2c.h.

◆ read_byte() [1/2]

optional< uint8_t > esphome::i2c::I2CDevice::read_byte ( uint8_t a_register)
inline

Definition at line 274 of file i2c.h.

◆ read_byte() [2/2]

bool esphome::i2c::I2CDevice::read_byte ( uint8_t a_register,
uint8_t * data )
inline

Definition at line 272 of file i2c.h.

◆ read_byte_16()

bool esphome::i2c::I2CDevice::read_byte_16 ( uint8_t a_register,
uint16_t * data )
inline

Definition at line 281 of file i2c.h.

◆ read_byte_16_le()

bool esphome::i2c::I2CDevice::read_byte_16_le ( uint8_t a_register,
uint16_t * data )
inline

Read a 16-bit value stored least significant byte first.

Definition at line 296 of file i2c.h.

◆ read_bytes() [1/2]

template<size_t N>
optional< std::array< uint8_t, N > > esphome::i2c::I2CDevice::read_bytes ( uint8_t a_register)
inline

Definition at line 255 of file i2c.h.

◆ read_bytes() [2/2]

bool esphome::i2c::I2CDevice::read_bytes ( uint8_t a_register,
uint8_t * data,
uint8_t len )
inline

Compat APIs All methods below have been added for compatibility reasons.

They do not bring any functionality and therefore on new code it is not recommend to use them.

Definition at line 249 of file i2c.h.

◆ read_bytes_16()

bool esphome::i2c::I2CDevice::read_bytes_16 ( uint8_t a_register,
uint16_t * data,
uint8_t len )

Definition at line 70 of file i2c.cpp.

◆ read_bytes_16_le() [1/2]

bool esphome::i2c::I2CDevice::read_bytes_16_le ( uint8_t a_register,
int16_t * data,
uint8_t len )
inline

Definition at line 291 of file i2c.h.

◆ read_bytes_16_le() [2/2]

bool esphome::i2c::I2CDevice::read_bytes_16_le ( uint8_t a_register,
uint16_t * data,
uint8_t len )
inline

Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endian hosts.

Definition at line 284 of file i2c.h.

◆ read_bytes_raw() [1/2]

template<size_t N>
optional< std::array< uint8_t, N > > esphome::i2c::I2CDevice::read_bytes_raw ( )
inline

Definition at line 262 of file i2c.h.

◆ read_bytes_raw() [2/2]

bool esphome::i2c::I2CDevice::read_bytes_raw ( uint8_t * data,
uint8_t len ) const
inline

Definition at line 253 of file i2c.h.

◆ read_register()

ErrorCode esphome::i2c::I2CDevice::read_register ( uint8_t a_register,
uint8_t * data,
size_t len )

reads an array of bytes from a specific register in the I²C device

Parameters
a_registeran 8 bits internal address of the I²C register to read from
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 28 of file i2c.cpp.

◆ read_register16()

ErrorCode esphome::i2c::I2CDevice::read_register16 ( uint16_t a_register,
uint8_t * data,
size_t len )

reads an array of bytes from a specific register in the I²C device

Parameters
a_registerthe 16 bits internal address of the I²C register to read from
datapointer to an array of bytes to store the information
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 32 of file i2c.cpp.

◆ reg()

I2CRegister esphome::i2c::I2CDevice::reg ( uint8_t a_register)
inline

calls the I2CRegister constructor

Parameters
a_registeraddress of the I²C register
Returns
an I2CRegister proxy object

Definition at line 164 of file i2c.h.

◆ reg16()

I2CRegister16 esphome::i2c::I2CDevice::reg16 ( uint16_t a_register)
inline

calls the I2CRegister16 constructor

Parameters
a_register16 bits address of the I²C register
Returns
an I2CRegister16 proxy object

Definition at line 169 of file i2c.h.

◆ set_i2c_address()

void esphome::i2c::I2CDevice::set_i2c_address ( uint8_t address)
inline

We store the address of the device on the bus.

Parameters
addressof the device

Definition at line 151 of file i2c.h.

◆ set_i2c_bus()

void esphome::i2c::I2CDevice::set_i2c_bus ( I2CBus * bus)
inline

we store the pointer to the I2CBus to use

Parameters
buspointer to the I2CBus object

Definition at line 159 of file i2c.h.

◆ update_register()

bool esphome::i2c::I2CDevice::update_register ( uint8_t a_register,
uint8_t mask,
uint8_t value )

Read a register, replace the bits in mask with those of value and write it back.

The write always happens; bits of value outside mask are ignored. Logs both values at VERBOSE.

Returns
false if the read or the write failed (nothing is written after a failed read)

Definition at line 78 of file i2c.cpp.

◆ write()

ErrorCode esphome::i2c::I2CDevice::write ( const uint8_t * data,
size_t len ) const
inline

writes an array of bytes to a device using an I2CBus

Parameters
datapointer to an array that contains the bytes to send
lenlength of the buffer = number of bytes to write
Returns
an i2c::ErrorCode

Definition at line 195 of file i2c.h.

◆ write_byte()

bool esphome::i2c::I2CDevice::write_byte ( uint8_t a_register,
uint8_t data ) const
inline

Definition at line 312 of file i2c.h.

◆ write_byte_16()

bool esphome::i2c::I2CDevice::write_byte_16 ( uint8_t a_register,
uint16_t data ) const

Definition at line 101 of file i2c.cpp.

◆ write_byte_16_le()

bool esphome::i2c::I2CDevice::write_byte_16_le ( uint8_t a_register,
uint16_t data ) const

Write a 16-bit value least significant byte first.

Definition at line 106 of file i2c.cpp.

◆ write_bytes() [1/3]

template<size_t N>
bool esphome::i2c::I2CDevice::write_bytes ( uint8_t a_register,
const std::array< uint8_t, N > & data )
inline

Definition at line 306 of file i2c.h.

◆ write_bytes() [2/3]

bool esphome::i2c::I2CDevice::write_bytes ( uint8_t a_register,
const std::vector< uint8_t > & data ) const
inline

Definition at line 302 of file i2c.h.

◆ write_bytes() [3/3]

bool esphome::i2c::I2CDevice::write_bytes ( uint8_t a_register,
const uint8_t * data,
uint8_t len ) const
inline

Definition at line 298 of file i2c.h.

◆ write_bytes_16()

bool esphome::i2c::I2CDevice::write_bytes_16 ( uint8_t a_register,
const uint16_t * data,
uint8_t len ) const

Definition at line 87 of file i2c.cpp.

◆ write_read()

ErrorCode esphome::i2c::I2CDevice::write_read ( const uint8_t * write_data,
size_t write_len,
uint8_t * read_data,
size_t read_len ) const
inline

writes an array of bytes to a device, then reads an array, as a single transaction

Parameters
write_datapointer to an array that contains the bytes to send
write_lenlength of the buffer = number of bytes to write
read_datapointer to an array to store the bytes read
read_lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 205 of file i2c.h.

◆ write_register()

ErrorCode esphome::i2c::I2CDevice::write_register ( uint8_t a_register,
const uint8_t * data,
size_t len ) const

writes an array of bytes to a specific register in the I²C device

Parameters
a_registerthe internal address of the register to read from
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 37 of file i2c.cpp.

◆ write_register16()

ErrorCode esphome::i2c::I2CDevice::write_register16 ( uint16_t a_register,
const uint8_t * data,
size_t len ) const

write an array of bytes to a specific register in the I²C device

Parameters
a_registerthe 16 bits internal address of the register to read from
datapointer to an array to store the bytes
lenlength of the buffer = number of bytes to read
Returns
an i2c::ErrorCode

Definition at line 46 of file i2c.cpp.

◆ write_registers() [1/2]

bool esphome::i2c::I2CDevice::write_registers ( const I2CRegValue * table,
size_t count ) const

Write each register and value of table in order, stopping at the first failure.

The table may be in PROGMEM.

Definition at line 56 of file i2c.cpp.

◆ write_registers() [2/2]

template<size_t N>
bool esphome::i2c::I2CDevice::write_registers ( const I2CRegValue(&) table[N]) const
inline

Definition at line 226 of file i2c.h.

Field Documentation

◆ address_

uint8_t esphome::i2c::I2CDevice::address_ {0x00}
protected

store the address of the device on the bus

Definition at line 327 of file i2c.h.

◆ bus_

I2CBus* esphome::i2c::I2CDevice::bus_ {nullptr}
protected

pointer to I2CBus instance

Definition at line 325 of file i2c.h.


The documentation for this class was generated from the following files: