{"id":71,"date":"2026-02-26T11:04:15","date_gmt":"2026-02-26T10:04:15","guid":{"rendered":"https:\/\/elrebo.de\/?p=71"},"modified":"2026-02-26T12:44:04","modified_gmt":"2026-02-26T11:44:04","slug":"i2c_master","status":"publish","type":"post","link":"https:\/\/www.elrebo.de\/index.php\/2026\/02\/26\/i2c_master\/","title":{"rendered":"i2c_master"},"content":{"rendered":"\n<p id=\"block-0cfb8a78-7f9c-4098-b4fe-b30608000cbc\">An ESP-IDF component for&nbsp;<a href=\"https:\/\/elrebo.de\/index.php\/2026\/02\/20\/physical-computing\/\">Physical Computing<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-4b033aba-2a31-4956-b5d1-36a5f063d6a0\">Purpose<\/h2>\n\n\n\n<p id=\"block-d1d08013-14c7-41d6-bc31-31fd7f00b092\">Define an I2C bus master and add I2C devices for ESP32 systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-c0ec4b6c-cd95-4697-b800-d5a8e2dcf602\">How it is done<\/h2>\n\n\n\n<p id=\"block-f813434c-ae67-4bed-8e0b-5e80c7f0b3e2\">You need to include&nbsp;<code>i2c_master.hpp<\/code>.<\/p>\n\n\n\n<p id=\"block-f813434c-ae67-4bed-8e0b-5e80c7f0b3e2\">Then you define the I2C bus master by defining an instance of class <code>I2cMaster<\/code>.<\/p>\n\n\n\n<p>For every I2C device you define an instance of class <code>I2cDevice<\/code> and add it to <code>I2cMaster<\/code> with method <code>AddDevice<\/code>.<\/p>\n\n\n\n<p>Now the device handles are available through method <code>GetDeviceHandle(std::string deviceName)<\/code> of class <code>I2cMaster<\/code>.<\/p>\n\n\n\n<p>To transmit data to an I2C device and to receive data from an I2C device you use the original ESP functions. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-c316dbcb-c3c9-4a23-a3c7-d4fd1e0eb867\">Where to find it<\/h2>\n\n\n\n<p id=\"block-cc2afcf8-a2c3-4f6c-842a-a2443cfb1062\"><code>i2c_master<\/code>&nbsp;is published on the ESP Registry at&nbsp;<a href=\"https:\/\/components.espressif.com\/components\/elrebo-de\/i2c_master\">https:\/\/components.espressif.com\/components\/elrebo-de\/i2c_master<\/a>.<\/p>\n\n\n\n<p id=\"block-2ecaabd6-5bb7-4287-bcc4-985ff1c232d5\">The source code can be found at\u00a0<a href=\"https:\/\/github.com\/elrebo-de\/i2c_master\">https:\/\/github.com\/elrebo-de\/i2c_master<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-cf4dee0e-a35e-4163-9fb2-e2fc7ece1f67\">How to use it<\/h2>\n\n\n\n<p id=\"block-931617c6-8adf-4eb4-85d7-1ac4f0d6a9ed\">As an ESP-IDF component&nbsp;<code>i2c_master<\/code>&nbsp;must be included into idf_component.yml as a dependency. As usual this can be done by executing this command:<\/p>\n\n\n\n<p id=\"block-37ac3466-38c9-435c-9301-59bc3384f2e1\"><code>idf.py add-dependency \"elrebo-de\/i2c_master^1.1.1\"<\/code><\/p>\n\n\n\n<p id=\"block-c35797fb-de02-421b-aecd-512cb88904a4\">In CMakeLists.txt&nbsp;<code>\"elrebo-de__i2c_master\"<\/code>&nbsp;has to be added as a requirement.<\/p>\n\n\n\n<p id=\"block-e64a7152-e2b3-46ce-bc41-7ca22badb118\">Now you can include&nbsp;<code>i2c_master.hpp<\/code>&nbsp;and use the I2cMaster and I2cDevice classes in your program.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"811\" height=\"1024\" src=\"https:\/\/elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.53.57-811x1024.png\" alt=\"\" class=\"wp-image-119\" srcset=\"https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.53.57-811x1024.png 811w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.53.57-238x300.png 238w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.53.57-768x970.png 768w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.53.57.png 1012w\" sizes=\"auto, (max-width: 811px) 100vw, 811px\" \/><\/figure>\n\n\n\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"677\" src=\"https:\/\/elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.57.11-1024x677.png\" alt=\"\" class=\"wp-image-120\" srcset=\"https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.57.11-1024x677.png 1024w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.57.11-300x198.png 300w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.57.11-768x508.png 768w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.57.11-1536x1016.png 1536w, https:\/\/www.elrebo.de\/wp-content\/uploads\/2026\/02\/Bildschirmfoto-2026-02-26-um-10.57.11.png 1824w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>An ESP-IDF component for&nbsp;Physical Computing Purpose Define an I2C bus master and add I2C devices for ESP32 systems. How it is done You need to include&nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":121,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oss"],"_links":{"self":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":3,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions\/130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/media\/121"}],"wp:attachment":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}