Skip to content

Commit d5d4995

Browse files
committed
add new example i2c_scanner to Wire library
1 parent ba75a26 commit d5d4995

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../../../../../sduino.mk
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// --------------------------------------
2+
// i2c_scanner
3+
// https://playground.arduino.cc/Main/I2cScanner
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+
// Version 6c, November 30, 2018
26+
// Modified to plain C for use with SDuino by Michael Mayer
27+
//
28+
//
29+
// This sketch tests the standard 7-bit addresses
30+
// Devices with higher bit address might not be seen properly.
31+
//
32+
33+
#include <Wire.h>
34+
35+
36+
void setup()
37+
{
38+
Wire_begin();
39+
40+
Serial_begin(9600);
41+
while (!Serial); // Leonardo: wait for serial monitor
42+
Serial_println_s("\nI2C Scanner");
43+
}
44+
45+
46+
void loop()
47+
{
48+
byte error, address;
49+
int nDevices;
50+
51+
Serial_println_s("Scanning...");
52+
53+
nDevices = 0;
54+
for(address = 1; address < 127; address++ )
55+
{
56+
// The i2c_scanner uses the return value of
57+
// the Write_endTransmisstion to see if
58+
// a device did acknowledge to the address.
59+
Wire_beginTransmission(address);
60+
error = Wire_endTransmission();
61+
62+
if (error == 0)
63+
{
64+
Serial_print_s("I2C device found at address 0x");
65+
if (address<16)
66+
Serial_print_s("0");
67+
Serial_print_ub(address,HEX);
68+
Serial_println_s(" !");
69+
70+
nDevices++;
71+
}
72+
else if (error==4)
73+
{
74+
Serial_print_s("Unknown error at address 0x");
75+
if (address<16)
76+
Serial_print_s("0");
77+
Serial_println_ub(address,HEX);
78+
}
79+
}
80+
if (nDevices == 0)
81+
Serial_println_s("No I2C devices found\n");
82+
else
83+
Serial_println_s("done\n");
84+
85+
delay(5000); // wait 5 seconds for next scan
86+
}

0 commit comments

Comments
 (0)