{"id":151,"date":"2019-07-18T19:30:23","date_gmt":"2019-07-18T19:30:23","guid":{"rendered":"https:\/\/tobyslab.com\/?p=151"},"modified":"2019-07-18T19:33:31","modified_gmt":"2019-07-18T19:33:31","slug":"arduino-i2c-scanner","status":"publish","type":"post","link":"https:\/\/tobyslab.com\/?p=151","title":{"rendered":"Arduino I2C Scanner"},"content":{"rendered":"\n<p>I&#8217;ve got an I2C 16&#215;2 display module for an Arduino project (the upcoming &#8216;Arduino Giardino&#8217; Aerogarden clone). Instead of trying to manually find the I2C address, I discovered that a number of people have contributed to an <a href=\"https:\/\/playground.arduino.cc\/Main\/I2cScanner\/\">Arduino sketch<\/a> that scan the I2C bus and returns the addresses of the devices on the bus. This makes it fantastically easy to see the attached devices.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ --------------------------------------\n\/\/ i2c_scanner\n\/\/\n\/\/ Version 1\n\/\/    This program (or code that looks like it)\n\/\/    can be found in many places.\n\/\/    For example on the Arduino.cc forum.\n\/\/    The original author is not know.\n\/\/ Version 2, Juni 2012, Using Arduino 1.0.1\n\/\/     Adapted to be as simple as possible by Arduino.cc user Krodal\n\/\/ Version 3, Feb 26  2013\n\/\/    V3 by louarnold\n\/\/ Version 4, March 3, 2013, Using Arduino 1.0.3\n\/\/    by Arduino.cc user Krodal.\n\/\/    Changes by louarnold removed.\n\/\/    Scanning addresses changed from 0...127 to 1...119,\n\/\/    according to the i2c scanner by Nick Gammon\n\/\/    https:\/\/www.gammon.com.au\/forum\/?id=10896\n\/\/ Version 5, March 28, 2013\n\/\/    As version 4, but address scans now to 127.\n\/\/    A sensor seems to use address 120.\n\/\/ Version 6, November 27, 2015.\n\/\/    Added waiting for the Leonardo serial communication.\n\/\/\n\/\/\n\/\/ This sketch tests the standard 7-bit addresses\n\/\/ Devices with higher bit address might not be seen properly.\n\/\/\n \n#include &lt;Wire.h>\n \n \nvoid setup()\n{\n  Wire.begin();\n \n  Serial.begin(9600);\n  while (!Serial);             \/\/ Leonardo: wait for serial monitor\n  Serial.println(\"\\nI2C Scanner\");\n}\n \n \nvoid loop()\n{\n  byte error, address;\n  int nDevices;\n \n  Serial.println(\"Scanning...\");\n \n  nDevices = 0;\n  for(address = 1; address &lt; 127; address++ )\n  {\n    \/\/ The i2c_scanner uses the return value of\n    \/\/ the Write.endTransmisstion to see if\n    \/\/ a device did acknowledge to the address.\n    Wire.beginTransmission(address);\n    error = Wire.endTransmission();\n \n    if (error == 0)\n    {\n      Serial.print(\"I2C device found at address 0x\");\n      if (address&lt;16)\n        Serial.print(\"0\");\n      Serial.print(address,HEX);\n      Serial.println(\"  !\");\n \n      nDevices++;\n    }\n    else if (error==4)\n    {\n      Serial.print(\"Unknown error at address 0x\");\n      if (address&lt;16)\n        Serial.print(\"0\");\n      Serial.println(address,HEX);\n    }    \n  }\n  if (nDevices == 0)\n    Serial.println(\"No I2C devices found\\n\");\n  else\n    Serial.println(\"done\\n\");\n \n  delay(5000);           \/\/ wait 5 seconds for next scan\n}<\/code><\/pre>\n\n\n\n<p>The serial output for my I2C display is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>I2C Scanner\nScanning...\nI2C device found at address 0x27  !\ndone<\/code><\/pre>\n\n\n\n<p>Couldn&#8217;t be easier!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve got an I2C 16&#215;2 display module for an Arduino project (the upcoming &#8216;Arduino Giardino&#8217; Aerogarden clone). Instead of trying to manually find the I2C address, I discovered that a number of people have contributed to an Arduino sketch that scan the I2C bus and returns the addresses of the devices on the bus. This makes it fantastically easy to see the attached devices. The serial output for my I2C display is as follows: Couldn&#8217;t be easier!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[6,7,8],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tobyslab.com\/index.php?rest_route=\/wp\/v2\/posts\/151"}],"collection":[{"href":"https:\/\/tobyslab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tobyslab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tobyslab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tobyslab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":2,"href":"https:\/\/tobyslab.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/tobyslab.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions\/153"}],"wp:attachment":[{"href":"https:\/\/tobyslab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobyslab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobyslab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}