i2c_scanner.ino example

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
4 *
5 * Version 1
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
13 * V3 by louarnold
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.
25 *
26 *
27 * This sketch tests the standard 7-bit addresses.
28 * Devices with higher bit address might not be seen properly.
29 *
30 * @m_examplenavigation{page_extra_helper_sketches,}
31 * ======================================================================= */
32
33#include <Arduino.h>
34#include <Wire.h>
35#include <SoftwareWire.h> // Testato's Software I2C
36
37
38const int8_t softwareSDA = 5; // data in pin
39const int8_t softwareSCL = 4; // data out pin
40SoftwareWire softWire(5, 4);
41
42template <typename THEWIRE>
43THEWIRE createWire(int8_t sda = -1, int8_t scl = -1) {
44 return THEWIRE(sda, scl);
45}
46template <>
47TwoWire createWire<TwoWire>(int8_t sda, int8_t scl) {
48 return Wire;
49}
50
51
52template <typename THEWIRE>
53void startWire(THEWIRE i2c) {
54 i2c.begin();
55 // i2c.printStatus(Serial);
56}
57
58
59// void scan(int8_t sda = -1, int8_t scl = -1)
60// {
61// THEWIRE i2c = createWire<THEWIRE>(sda, scl);
62// i2c.begin();
63template <typename THEWIRE>
64void scan(THEWIRE i2c) {
65 byte error, address;
66 int nDevices;
67
68 nDevices = 0;
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();
75
76 if (error == 0) {
77 Serial.print(" I2C device found at address 0x");
78 if (address < 16) Serial.print("0");
79 Serial.print(address, HEX);
80 Serial.println(" !");
81
82 nDevices++;
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);
87 }
88 }
89 if (nDevices == 0) Serial.println("No I2C devices found");
90}
91
92
93void setup() {
94 pinMode(22, OUTPUT);
95
96 Serial.begin(115200);
97 while (!Serial); // Leonardo: wait for serial monitor
98 Serial.println("\nI2C Scanner");
99 digitalWrite(22, HIGH);
100}
101
102
103void loop() {
104 Serial.println("Hardware I2C Objects:");
105 scan<TwoWire>(Wire);
106 Serial.print("Software I2C Objects on SDA=:");
107 Serial.print(softwareSDA);
108 Serial.print(", SCL=");
109 Serial.print(softwareSCL);
110 Serial.println(":");
111 scan<SoftwareWire>(softWire);
112 Serial.println();
113
114 delay(5000); // wait 5 seconds for next scan
115}