Testing sketch to scan for attached I2C devices.
Testing sketch to scan for attached I2C devicesVersion 1 This program (or code that looks like it) can be found in many places. For example on the Arduino.cc forum. The original author is not know. Version 2, Juni 2012, Using Arduino 1.0.1 Adapted to be as simple as possible by Arduino.cc user Krodal Version 3, Feb 26 2013 V3 by louarnold Version 4, March 3, 2013, Using Arduino 1.0.3 by Arduino.cc user Krodal. Changes by louarnold removed. Scanning addresses changed from 0...127 to 1...119, according to the i2c scanner by Nick Gammon http://www.gammon.com.au/forum/?id=10896 Version 5, March 28, 2013 As version 4, but address scans now to 127. A sensor seems to use address 120. Version 6, November 27, 2015. Added waiting for the Leonardo serial communication.
This sketch tests the standard 7-bit addresses. Devices with higher bit address might not be seen properly.
1/** =========================================================================
2 * @example{lineno} i2c_scanner.ino
3 * @brief Testing sketch to scan for attached I2C devices
6 * This program (or code that looks like it)
7 * can be found in many places.
8 * For example on the Arduino.cc forum.
9 * The original author is not know.
10 * Version 2, Juni 2012, Using Arduino 1.0.1
11 * Adapted to be as simple as possible by Arduino.cc user Krodal
12 * Version 3, Feb 26 2013
14 * Version 4, March 3, 2013, Using Arduino 1.0.3
15 * by Arduino.cc user Krodal.
16 * Changes by louarnold removed.
17 * Scanning addresses changed from 0...127 to 1...119,
18 * according to the i2c scanner by Nick Gammon
19 * http://www.gammon.com.au/forum/?id=10896
20 * Version 5, March 28, 2013
21 * As version 4, but address scans now to 127.
22 * A sensor seems to use address 120.
23 * Version 6, November 27, 2015.
24 * Added waiting for the Leonardo serial communication.
27 * This sketch tests the standard 7-bit addresses.
28 * Devices with higher bit address might not be seen properly.
30 * @m_examplenavigation{page_extra_helper_sketches,}
31 * ======================================================================= */
35#include <SoftwareWire.h> // Testato's Software I2C
38const int8_t softwareSDA = 5; // data in pin
39const int8_t softwareSCL = 4; // data out pin
40SoftwareWire softWire(5, 4);
42template <typename THEWIRE>
43THEWIRE createWire(int8_t sda = -1, int8_t scl = -1) {
44 return THEWIRE(sda, scl);
47TwoWire createWire<TwoWire>(int8_t sda, int8_t scl) {
52template <typename THEWIRE>
53void startWire(THEWIRE i2c) {
55 // i2c.printStatus(Serial);
59// void scan(int8_t sda = -1, int8_t scl = -1)
61// THEWIRE i2c = createWire<THEWIRE>(sda, scl);
63template <typename THEWIRE>
64void scan(THEWIRE i2c) {
69 for (address = 1; address < 127; address++) {
70 // The i2c_scanner uses the return value of
71 // the Write.endTransmisstion to see if
72 // a device did acknowledge to the address.
73 i2c.beginTransmission(address);
74 error = i2c.endTransmission();
77 Serial.print(" I2C device found at address 0x");
78 if (address < 16) Serial.print("0");
79 Serial.print(address, HEX);
83 } else if (error == 4) {
84 Serial.print(" Unknown error at address 0x");
85 if (address < 16) Serial.print("0");
86 Serial.println(address, HEX);
89 if (nDevices == 0) Serial.println("No I2C devices found");
97 while (!Serial); // Leonardo: wait for serial monitor
98 Serial.println("\nI2C Scanner");
99 digitalWrite(22, HIGH);
104 Serial.println("Hardware I2C Objects:");
106 Serial.print("Software I2C Objects on SDA=:");
107 Serial.print(softwareSDA);
108 Serial.print(", SCL=");
109 Serial.print(softwareSCL);
111 scan<SoftwareWire>(softWire);
114 delay(5000); // wait 5 seconds for next scan