@@ -60,21 +60,37 @@ void setup() {
6060}
6161
6262void loop () {
63- Serial.println (" ===================\n I2C Chip Scanner\n ===================" );
63+ uint8_t num_adr_found = 0 ;
64+ uint8_t adr_found[128 ];
65+ String adr_msg[128 ];
6466
65- Serial.printf (" I2C: Scanning ...\n " );
66- int num_adr_found = 0 ;
67+ Serial.printf (" ===========================\n " );
68+ Serial.printf (" I2C Chip Scanner 2025-08-08\n " );
69+ Serial.printf (" ===========================\n " );
70+
71+ // scan addresses to find devices
72+ Serial.printf (" Step 1: Scanning Adressses ... " );
6773 for (uint8_t adr = 1 ; adr < 128 ; adr++) {
68- i2c-> beginTransmission ( adr); // Begin I2C transmission Address (i)
69- if (i2c-> endTransmission () == 0 ) { // Receive 0 = success (ACK response)
70- Serial. printf ( " I2C: Found address: 0x%02X (decimal %d) \n " , adr,adr) ;
74+ if ( i2c_TestAddress ( adr)) {
75+ Serial. printf ( " 0x%02X(%d) " ,adr,adr);
76+ adr_found[num_adr_found] = adr;
7177 num_adr_found++;
72- i2c_identify_chip (adr);
7378 }
74- delay (10 );
7579 }
76- Serial.printf (" I2C: Found %d device(s)\n " , num_adr_found);
77- delay (100 );
80+ Serial.printf (" - Found %d devices\n " , num_adr_found);
81+
82+ // try to identify the devices
83+ Serial.printf (" Step 2: Identifying Devices ...\n " );
84+ for (uint8_t i = 0 ; i < num_adr_found; i++) {
85+ i2c_identify_chip (adr_found[i], adr_msg[i]);
86+ }
87+
88+ Serial.printf (" \n Step 3: Summary\n\n " );
89+ for (uint8_t i = 0 ; i < num_adr_found; i++) {
90+ Serial.printf (" address 0x%02X (decimal %3d) %s\n " , adr_found[i], adr_found[i], adr_msg[i].c_str ());
91+ }
92+
93+ delay (1000 );
7894}
7995
8096struct test_struct {
@@ -101,25 +117,25 @@ test_struct tests[] = {
101117 {0x68 , 0x69 , 0x75 , 1 , 0x19 , " MPU6886 6DOF motion" },
102118 {0x68 , 0x69 , 0x75 , 1 , 0x47 , " ICM42688P 6DOF motion" },
103119 {0x68 , 0x69 , 0x75 , 1 , 0x68 , " MPU6000, MPU6050, or MPU9150 6/9DOF motion" },
104- {0x68 , 0x69 , 0x75 , 1 , 0x69 , " FAKE MPU6050, got WHO_AM_I=0x69, real chip returns 0x68" },
120+ {0x68 , 0x69 , 0x75 , 1 , 0x69 , " FAKE MPU6050, got WHO_AM_I=0x69, real chip returns 0x68" },
105121 {0x68 , 0x69 , 0x75 , 1 , 0x70 , " MPU6500 6DOF motion" },
106122 {0x68 , 0x69 , 0x75 , 1 , 0x71 , " MPU9250 9DOF motion" },
107123 {0x68 , 0x69 , 0x75 , 1 , 0x72 , " FAKE MPU6050, got WHO_AM_I=0x72, real chip returns 0x68" },
108- {0x68 , 0x69 , 0x75 , 1 , 0x73 , " MPU9255 9DOF motion" },
124+ {0x68 , 0x69 , 0x75 , 1 , 0x73 , " MPU9255 9DOF motion" },
109125 {0x68 , 0x69 , 0x75 , 1 , 0x74 , " MPU9515 motion" },
110126 {0x68 , 0x69 , 0x75 , 1 , 0x75 , " FAKE MPU9250, got WHO_AM_I=0x75, real chip returns 0x71" },
111127 {0x68 , 0x69 , 0x75 , 1 , 0x78 , " FAKE MPU9250, got WHO_AM_I=0x78, real chip returns 0x71" },
112128 {0x68 , 0x69 , 0x75 , 1 , 0x98 , " ICM20689 6DOF motion" },
113- {0x76 , 0x77 , 0x8F , 1 , 0x80 , " HP203B pressure" }, // needs to be first 0x76 sensor, otherwise this sensor will lock up
114129 {0x76 , 0x77 , 0x00 , 1 , 0x50 , " BMP388 pressure" },
115- {0x76 , 0x77 , 0x00 , 1 , 0x60 , " BMP390 pressure" },
130+ {0x76 , 0x77 , 0x00 , 1 , 0x60 , " BMP390 pressure" },
116131 {0x76 , 0x77 , 0x0D , 1 , 0x10 , " DPS310, HP303B, or SPL06 pressure" },
132+ {0x76 , 0x77 , 0x8F , 1 , 0x80 , " HP203B pressure" },
117133 {0x76 , 0x77 , 0xD0 , 1 , 0x55 , " BMP180 pressure" },
118134 {0x76 , 0x77 , 0xD0 , 1 , 0x56 , " BMP280 pressure" },
119135 {0x76 , 0x77 , 0xD0 , 1 , 0x57 , " BMP280 pressure" },
120136 {0x76 , 0x77 , 0xD0 , 1 , 0x58 , " BMP280 pressure" },
121137 {0x76 , 0x77 , 0xD0 , 1 , 0x60 , " BME280 pressure, temperature, humidity" },
122- {0x76 , 0x77 , 0xD0 , 1 , 0x61 , " BME680 pressure, temperature, humidity, gas sensor " },
138+ {0x76 , 0x77 , 0xD0 , 1 , 0x61 , " BME680 pressure, temperature, humidity, gas" },
123139 {0x7C , 0x7C , 0x00 , 1 , 0x90 , " QMC6309 magnetometer" },
124140
125141 {0 ,0 ,0 ,0 ,0 ," " } // end
@@ -139,11 +155,7 @@ adrtest_struct adrtests[] = {
139155 {0 ,0 ," " } // end
140156};
141157
142- void i2c_identify_chip (uint8_t adr) {
143- bool found = false ;
144-
145- uint32_t tried[256 ] = {0 };
146-
158+ void i2c_identify_chip (uint8_t adr, String &msg) {
147159 // try adr,reg,result match
148160 int i = 0 ;
149161 while (tests[i].adr1 ) {
@@ -155,58 +167,96 @@ void i2c_identify_chip(uint8_t adr) {
155167 uint32_t received = 0 ;
156168 if (adr1<=adr && adr<=adr2) {
157169 uint8_t data[4 ];
158- i2c_ReadRegs (adr, reg, data, len);
170+ Serial.printf (" try: %s --> read adr:0x%02X reg:0x%02X len:%d --> " , tests[i].descr .c_str (), adr, reg, len);
171+ i2c_ReadRegs (adr, reg, data, len, true );
159172 for (int i=0 ;i<len;i++) received = (received<<8 ) + data[i];
160- tried[reg] = received;
173+ Serial. printf ( " received:0x%02X " , ( int ) received);
161174 if (received == expected) {
162- Serial.printf (" MATCH: reg=0x%02X result=0x%02X -> %s\n " , reg, (int )received, tests[i].descr .c_str ());
163- found = true ;
164- }
175+ msg = tests[i].descr ;
176+ Serial.printf (" --> %s\n " ,msg.c_str ());
177+ return ;
178+ }
179+ Serial.printf (" \n " );
165180 }
166181 i++;
167182 }
168183
169- if (!found) {
170- // show tries
171- for (int i=0 ;i<256 ;i++) if (tried[i]!=0 ) {
172- Serial.printf (" tried: reg=0x%02X result=0x%02X\n " , i, (int )tried[i]);
173- }
174-
175- // try address only match
176- int i = 0 ;
177- while (adrtests[i].adr1 ) {
178- int adr1 = adrtests[i].adr1 ;
179- int adr2 = adrtests[i].adr2 ;
180- if (adr1<=adr && adr<=adr2) {
181- Serial.printf (" POTENTIAL MATCH (address 0x%02X match only) -> %s\n " , adr, adrtests[i].descr );
182- }
183- i++;
184+ // try address only match
185+ i = 0 ;
186+ while (adrtests[i].adr1 ) {
187+ int adr1 = adrtests[i].adr1 ;
188+ int adr2 = adrtests[i].adr2 ;
189+ if (adr1<=adr && adr<=adr2) {
190+ msg = adrtests[i].descr + String (" - ADDRESS MATCH ONLY" );
191+ return ;
184192 }
193+ i++;
185194 }
195+
196+ msg = " UNKNOWN" ;
197+ }
198+
199+ bool i2c_TestAddress (uint8_t adr) {
200+ i2c->beginTransmission (adr); // Begin I2C transmission Address (i)
201+ return (i2c->endTransmission () == 0 ); // Receive 0 = success (ACK response)
186202}
187203
188- void WriteReg ( uint8_t adr, uint8_t reg, uint8_t data ) {
204+ void i2c_WriteByte ( uint8_t adr, uint8_t reg ) {
189205 i2c->beginTransmission (adr);
190- i2c->write (reg);
191- i2c->write (data);
206+ i2c->write (reg);
192207 i2c->endTransmission ();
193208}
194209
195- unsigned int i2c_ReadReg ( uint8_t adr, uint8_t reg ) {
196- uint8_t data = 0 ;
197- i2c_ReadRegs (adr, reg, &data, 1 );
198- return data;
210+ // returns -1 on fail
211+ int i2c_ReadByte ( uint8_t adr ) {
212+ int rv = -1 ;
213+ uint8_t data;
214+ uint8_t bytesReceived = i2c->requestFrom (adr, 1 );
215+ if (bytesReceived == 1 ) {
216+ i2c->readBytes (&data, 1 );
217+ rv = data;
218+ }
219+ // i2c->endTransmission(); -- don't do this
220+ return rv;
199221}
200222
201- void i2c_ReadRegs ( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n ) {
223+ void i2c_WriteReg ( uint8_t adr, uint8_t reg, uint8_t data ) {
224+ i2c->beginTransmission (adr);
225+ i2c->write (reg);
226+ i2c->write (data);
227+ i2c->endTransmission ();
228+ }
229+
230+
231+
232+ int i2c_ReadRegs ( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n, bool stop ) {
202233 i2c->beginTransmission (adr);
203234 i2c->write (reg);
204- i2c->endTransmission (false ); // false = repeated start
205- uint8_t bytesReceived = i2c->requestFrom (adr, n);
206- if (bytesReceived == n ) {
235+ i2c->endTransmission (stop ); // false = repeated start, true = stop + start
236+ int bytesReceived = i2c->requestFrom (adr, n);
237+ if (bytesReceived > 0 ) {
207238 i2c->readBytes (data, bytesReceived);
208239 }
209- i2c->endTransmission ();
240+ // i2c->endTransmission(); -- don't do this
241+ return bytesReceived;
242+ }
243+
244+ int i2c_ReadRegs ( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n) {
245+ return i2c_ReadRegs ( adr, reg, data, n, false );
246+ }
247+
248+ // returns -1 on fail
249+ int i2c_ReadReg ( uint8_t adr, uint8_t reg, bool stop ) {
250+ uint8_t data = 0 ;
251+ int bytesReceived = i2c_ReadRegs (adr, reg, &data, 1 );
252+ if (bytesReceived == 1 ) {
253+ return data;
254+ }
255+ return -1 ;
256+ }
257+
258+ int i2c_ReadReg ( uint8_t adr, uint8_t reg) {
259+ return i2c_ReadReg ( adr, reg, false );
210260}
211261
212262void i2c_scan () {
0 commit comments