An ESP-IDF component for Physical Computing
Purpose
Define an I2C bus master and add I2C devices for ESP32 systems.
How it is done
You need to include i2c_master.hpp.
Then you define the I2C bus master by defining an instance of class I2cMaster.
For every I2C device you define an instance of class I2cDevice and add it to I2cMaster with method AddDevice.
Now the device handles are available through method GetDeviceHandle(std::string deviceName) of class I2cMaster.
To transmit data to an I2C device and to receive data from an I2C device you use the original ESP functions.
Where to find it
i2c_master is published on the ESP Registry at https://components.espressif.com/components/elrebo-de/i2c_master.
The source code can be found at https://github.com/elrebo-de/i2c_master.
How to use it
As an ESP-IDF component i2c_master must be included into idf_component.yml as a dependency. As usual this can be done by executing this command:
idf.py add-dependency "elrebo-de/i2c_master^1.1.1"
In CMakeLists.txt "elrebo-de__i2c_master" has to be added as a requirement.
Now you can include i2c_master.hpp and use the I2cMaster and I2cDevice classes in your program.
In this code snippet an I2C bus master is defined for an ESP32C3 and two devices are added to the bus, an MCP9808 thermometer device and an LCD display:

In this code snippet you see, how the temperature can be read from a MCP9808 temperature sensor using the original ESP I2C transmit and receive functions:


Leave a Reply to Physical Computing – elrebo Cancel reply