{"id":144588,"date":"2024-01-04T14:55:43","date_gmt":"2024-01-04T14:55:43","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=144588"},"modified":"2025-01-17T15:28:51","modified_gmt":"2025-01-17T15:28:51","slug":"raspberry-pi-pico-i2c-scanner-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/","title":{"rendered":"Raspberry Pi Pico: I2C Scanner (MicroPython) &#8211; Finding the Address of I2C Devices"},"content":{"rendered":"\n<p>This is a quick guide to show you how to find the address of I2C devices with the Raspberry Pi Pico programmed using MicroPython firmware.<\/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\/Raspberry-Pi-Pico-I2C-Scanner.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico I2C Scanner MicroPython Finding the Address of I2C Devices\" class=\"wp-image-144597\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/Raspberry-Pi-Pico-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\/Raspberry-Pi-Pico-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\/Raspberry-Pi-Pico-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\/Raspberry-Pi-Pico-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\">Already familiar with the Raspberry Pi Pico? <a href=\"#i2c-scanner\" title=\"\">Jump to the I2C scanner sketch<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Raspberry Pi Pico &#8211; Default I2C Pins<\/h2>\n\n\n\n<p>In the Raspberry Pi Pico, there are two I2C peripherals available, named <strong>I2C0 <\/strong>and <strong>I2C1<\/strong>. You can use two different buses (I2C0 and I2C1) simultaneously, but you can&#8217;t use two I2C0 or two I2C1 at the same time. You can use I2C communication on any of the I2C pins available.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\">The default I2C pins are <strong>GPIO 4 (SDA) <\/strong>and <strong>GPIO 5 (SCL)<\/strong> &#8211; <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">read our Raspberry Pi Pico Pinout<\/a>.<\/p>\n\n\n\n<p>The following table shows all the pins you can use for I2C communication.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>I2C Controller<\/strong><\/td><td><strong>SDA GPIOs<\/strong><\/td><td><strong>SCL GPIOs<\/strong><\/td><\/tr><tr><td><strong>I2C0<\/strong><\/td><td>GPIO0, GPIO4, GPIO8, GPIO12, GPIO16, GPIO20<\/td><td>GPIO1, GPIO5, GPIO9, GPIO13, GPIO17, GPIO21<\/td><\/tr><tr><td><strong>I2C1<\/strong><\/td><td>GPIO2, GPIO6, GPIO10, GPIO14, GPIO18, GPIO26<\/td><td>GPIO3, GPIO7, GPIO11, GPIO15, GPIO19, GPIO27<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i2c-scanner\">I2C Scanner &#8211; MicroPython<\/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 Raspberry Pi Pico I2C pins and then run the I2C scanner code provided.<\/p>\n\n\n\n<p>Open <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Thonny IDE<\/a>, or the IDE of your choice, and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># I2C Scanner MicroPython\nfrom machine import Pin, SoftI2C\n\n# You can choose any other combination of I2C pins\ni2c = SoftI2C(scl=Pin(5), sda=Pin(4))\n\nprint('I2C SCANNER')\ndevices = i2c.scan()\n\nif len(devices) == 0:\n  print(&quot;No i2c device !&quot;)\nelse:\n  print('i2c devices found:', len(devices))\n\n  for device in devices:\n    print(&quot;I2C hexadecimal address: &quot;, hex(device))\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/I2C_Scanner.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"588\" height=\"693\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2c-scanner-micropython-1.png?resize=588%2C693&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico I2C Scanner MicroPython script code\" class=\"wp-image-144595\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2c-scanner-micropython-1.png?w=588&amp;quality=100&amp;strip=all&amp;ssl=1 588w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2c-scanner-micropython-1.png?resize=255%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 255w\" sizes=\"(max-width: 588px) 100vw, 588px\" \/><\/figure><\/div>\n\n\n<p>After copying the code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>connect the Raspberry Pi Pico to your computer, if it isn&#8217;t already;<\/li>\n\n\n\n<li>make sure you have your I2C peripheral properly connected to your board on the right I2C pins (SCL=GPIO5 ; SDA =GPIO4). If you&#8217;re using different pins, make sure you adjust that on the code.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = SoftI2C(scl=Pin(5), sda=Pin(4))<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>run the previous code (I2C scanner sketch). If you&#8217;re using Thonny IDE, you just need to click on the <strong>green <\/strong>run icon.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"run Raspberry Pi Pico I2C Scanner MicroPython script\" class=\"wp-image-144594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>The I2C address of your peripheral will be printed on the shell.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"229\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2c-scanner-micropython-devices-found.png?resize=578%2C229&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"testing example Raspberry Pi Pico I2C Scanner MicroPython script shell output\" class=\"wp-image-144596\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2c-scanner-micropython-devices-found.png?w=578&amp;quality=100&amp;strip=all&amp;ssl=1 578w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/I2c-scanner-micropython-devices-found.png?resize=300%2C119&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure><\/div>\n\n\n<p>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\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 Raspberry Pi Pico and run the I2C scanner code.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this guide useful.<\/p>\n\n\n\n<p>Learn more about the Raspberry Pi Pico with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico with MicroPython (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">Raspberry Pi Pico Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>We also have guides for other popular microcontroller boards:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\" title=\"\">ESP32 Projects and Tutorials<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\" title=\"\">ESP8266 Projects and Tutorials<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-esp8266-micropython\/\" title=\"\">MicroPython Projects with the ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi\/\" title=\"\">Raspberry Pi Projects and Guides<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick guide to show you how to find the address of I2C devices with the Raspberry Pi Pico programmed using MicroPython firmware. Already familiar with the Raspberry &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: I2C Scanner (MicroPython) &#8211; Finding the Address of I2C Devices\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/#more-144588\" aria-label=\"Read more about Raspberry Pi Pico: I2C Scanner (MicroPython) &#8211; Finding the Address of I2C Devices\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":144597,"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":[324,326],"tags":[],"class_list":["post-144588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/Raspberry-Pi-Pico-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\/144588","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=144588"}],"version-history":[{"count":9,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/144588\/revisions"}],"predecessor-version":[{"id":166692,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/144588\/revisions\/166692"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/144597"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=144588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=144588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=144588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}