{"id":145018,"date":"2024-01-23T16:31:17","date_gmt":"2024-01-23T16:31:17","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=145018"},"modified":"2024-01-23T16:31:18","modified_gmt":"2024-01-23T16:31:18","slug":"esp32-i2c-scanner-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-i2c-scanner-arduino\/","title":{"rendered":"ESP32: I2C Scanner (Arduino IDE) &#8211; Finding the Address of I2C Devices"},"content":{"rendered":"\n<p>This is a quick guide that shows how to find the address of I2C devices with the ESP32 programmed using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/ESP32-I2C-Scanner.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 I2C Scanner Arduino IDE Finding the Address of I2C Devices\" class=\"wp-image-145027\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/ESP32-I2C-Scanner.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/ESP32-I2C-Scanner.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/ESP32-I2C-Scanner.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/ESP32-I2C-Scanner.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">Want to learn more about I2C with the ESP32? Check this tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\" title=\"\">ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32 &#8211; Default I2C Pins<\/h2>\n\n\n\n<p>When using the ESP32 with Arduino IDE, the default I2C pins are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GPIO 22&nbsp;(SCL)<\/strong><\/li>\n\n\n\n<li><strong>GPIO 21<\/strong>&nbsp;<strong>(SDA)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>You can configure your code to use any other pins. <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/#3\" title=\"\">You can &#8211; check this tutorial to learn how to use different I2C pins<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i2c-scanner\">I2C Scanner Sketch &#8211; Arduino IDE<\/h2>\n\n\n\n<p>If you want to find the I2C address of a specific sensor, display, or any other I2C peripheral, connect it to the ESP32 I2C pins and then run the I2C scanner sketch provided.<\/p>\n\n\n\n<p>Copy the following code to the Arduino IDE and upload it to the ESP32 board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n#include &lt;Wire.h&gt;\n \nvoid setup() {\n  Wire.begin();\n  Serial.begin(115200);\n  Serial.println(&quot;\\nI2C Scanner&quot;);\n}\n \nvoid loop() {\n  byte error, address;\n  int nDevices;\n  Serial.println(&quot;Scanning...&quot;);\n  nDevices = 0;\n  for(address = 1; address &lt; 127; address++ ) {\n    Wire.beginTransmission(address);\n    error = Wire.endTransmission();\n    if (error == 0) {\n      Serial.print(&quot;I2C device found at address 0x&quot;);\n      if (address&lt;16) {\n        Serial.print(&quot;0&quot;);\n      }\n      Serial.println(address,HEX);\n      nDevices++;\n    }\n    else if (error==4) {\n      Serial.print(&quot;Unknow error at address 0x&quot;);\n      if (address&lt;16) {\n        Serial.print(&quot;0&quot;);\n      }\n      Serial.println(address,HEX);\n    }    \n  }\n  if (nDevices == 0) {\n    Serial.println(&quot;No I2C devices found\\n&quot;);\n  }\n  else {\n    Serial.println(&quot;done\\n&quot;);\n  }\n  delay(5000);          \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/LCD_I2C\/I2C_Scanner.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>After uploading the code, make sure you have your I2C peripheral properly connected to your board on the right I2C pins (<strong>SCL=GPIO22<\/strong> ; <strong>SDA =GPIO21<\/strong>). <\/p>\n\n\n\n<div class=\"wp-block-group rntbox rntclgray\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>If you&#8217;re using different pins, make sure you adjust that on the code. Replace:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Wire.begin();<\/code><\/pre>\n\n\n\n<p>With the following line, in which <strong>I2C_SDA<\/strong> corresponds to the GPIO number you&#8217;re using as SDA and <strong>I2C_SCL <\/strong>to the GPIO number you&#8217;re using as SCL.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Wire.begin(<strong>I2C_SDA<\/strong>, <strong>I2C_SCL<\/strong>);<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/10\/Serial-Monitor.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"34\" height=\"30\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/10\/Serial-Monitor.png?resize=34%2C30&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open Arduino IDE Serial Monitor baud rate of 115200\" class=\"wp-image-120313\"\/><\/a><\/figure><\/div>\n\n\n<p>Wait a couple of seconds, and the I2C address of your peripheral will be printed in the Serial Monitor. If you have more than one device connected to the same I2C bus, it will display the address of all devices.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"601\" height=\"381\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2C-Scanner-Raspberry-Pi-Pico-Arduino-IDE.png?resize=601%2C381&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 I2C Scanner Arduino IDE Serial Monitor\" class=\"wp-image-144605\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2C-Scanner-Raspberry-Pi-Pico-Arduino-IDE.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2C-Scanner-Raspberry-Pi-Pico-Arduino-IDE.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this guide, you learned how to quickly find the address of I2C devices. You just need to connect your I2C peripheral to the ESP32 board and upload the I2C scanner sketch provided.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this guide useful. To learn more about I2C with the ESP32, we recommend reading the following article: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\" title=\"\">ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)<\/a>. <\/p>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\">SMART HOME with Raspberry Pi, ESP32, and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick guide that shows how to find the address of I2C devices with the ESP32 programmed using Arduino IDE. Want to learn more about I2C with the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32: I2C Scanner (Arduino IDE) &#8211; Finding the Address of I2C Devices\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-scanner-arduino\/#more-145018\" aria-label=\"Read more about ESP32: I2C Scanner (Arduino IDE) &#8211; Finding the Address of I2C Devices\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":145027,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-145018","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/ESP32-I2C-Scanner.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/145018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=145018"}],"version-history":[{"count":4,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/145018\/revisions"}],"predecessor-version":[{"id":146226,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/145018\/revisions\/146226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/145027"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=145018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=145018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=145018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}