Cansat Kursus
Around
Jens
ESA
diving
Analog sensors
I2C Sensors
Download
I2C Scanner
Code
Shield
Radiolink
sdkort Openlog
Div SW
Presentations
Litt
Online Courses
Arduino stuff
Old Presentations
Obsolete Srp
|
I2C scannerUse it for scanning i2c bus for devices and sensors Simple I2C scannerA simple I2C scanner for an Arduino Just scanning and showwing adresses. Some devices are known like BMP280
#include <Wire.h>
#include <I2Cdev.h>
/*
scanner for devices on i2c bus
It can be used to detect sensors on gy-91 IMU
and i2c devices in general
There i
*/
//JDN testet dor uno,mega, esp32 nodemcu
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void testSlave(byte address)
{
byte resp[10];
switch (address) {
case 0x68: // mpu6050
case 0x69:
Wire.beginTransmission(address);
Wire.write(byte(0x75)); // get id register from register addr 0x75
Wire.endTransmission();
Wire.requestFrom(address, 1);
resp[0] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print("\tIMU: mpu6050: 0x68/0x72?(clone?), MPU6000: 0X70, mpu9250: 0x71)");
break;
case 0x76: // bmp280
case 0x77: // bmp
Wire.beginTransmission(address);
Wire.write(byte(0xd0)); // get id register on addr 0xd0
Wire.endTransmission();
Wire.requestFrom(address, 1);
resp[0] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print("\tbosch pressure sensor bmp085: 0x55, bmp180: 0x55, bmp285: 0x58)");
break;
case 0x1e: // hmc5883L /hmc5983
Wire.beginTransmission(address);
Wire.write(byte(0x0a)); // get id register from addr 0x0a
Wire.endTransmission();
Wire.requestFrom(address, 3);
resp[0] = Wire.read();
resp[1] = Wire.read();
resp[2] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print(" ");
Serial.print("0x"); Serial.print(resp[1], HEX); Serial.print(" ");
Serial.print("0x"); Serial.print(resp[2], HEX); Serial.print(" ");
Serial.print(" HMC: 5983 0x48 0x34 33, HMC5883L 0x48 0x34 0x33 ! same ...");
break;
case 0x0d: // qmc5883l
Wire.beginTransmission(address);
Wire.write(byte(0x0d)); // get id register from addr 0x0d
Wire.endTransmission();
Wire.requestFrom(address, 1);
resp[0] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print(" ");
Serial.print("\tQMC5883L 0xff");
break;
default :;
}
}
void scann(int all)
{
byte error, address;
int nDevices;
Serial.println("\n\n\n\nScanning...");
nDevices = 0;
for (address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device: 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.print(" (");
Serial.print(address);
Serial.print(") ");
testSlave(address);
Serial.println("");
nDevices++;
}
else if (all == 1) {
Serial.print("0X");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
switch (error) {
case 1: Serial.println(" err 1: data to long to fit in tx buffer");
break;
case 2: Serial.println(" err 2: received NACK on TX of address - NO DEVICE ?!");
break;
case 3: Serial.println(" err 3: received NACK on TX of data");
break;
case 4: Serial.println(" err 4: other err");
break;
case 5: Serial.println(" err 4: timeout");
break;
}
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else {
Serial.print("found: "); Serial.print(nDevices); Serial.println(" devices");
}
}
void loop()
{
Serial.println("show all i2c device addresses");
scann(1);
delay(1000); // wait 1 sec
Serial.println("show only found found devices");
scann(0);
delay(5000);
}
/*
Wire.endtransmission() returns ...
0: success.
1: data too long to fit in transmit buffer.
2: received NACK on transmit of address.
3: received NACK on transmit of data.
4: other error.
5: timeout
XXXXXXXXXXXXXXXXXXXXXXXX
mpu6500 et al
https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1
PROBMLEM 9250 IS A 6500 ??
https://github.com/kriswiner/ESP32/issues/24
https://www.tindie.com/products/onehorse/all-st-motion-sensor-breakout-board/
*/
I2C scanner with mpu6050 bypassSome IMU boards uses the secondary i2c bus behind the mpu 6050 This scanner do activate the gateway so also devcies behind the mpu6050 can be seen You need to have mpu6050 installed (click for mpu6050 i2c scanner code)
#include <Wire.h>
#include <MPU6050.h>
/*
scanner for devices on i2c bus
and i2c devices in general
*/
MPU6050 mpu;
//JDN testet dor uno,mega, esp32 nodemcu
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
mpu.initialize();
mpu.setI2CBypassEnabled(true);
delay(500);
Serial.println("\nI2C Scanner MPU6050");
}
void testSlave(byte address)
{
byte resp[10];
switch (address) {
case 0x68: // mpu6050
case 0x69:
Wire.beginTransmission(address);
Wire.write(byte(0x75)); // get id register from register addr 0x75
Wire.endTransmission();
Wire.requestFrom(address, 1);
resp[0] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print("\tIMU: mpu6050: 0x68/0x72?(clone?), MPU6000: 0X70, mpu9250: 0x71)");
break;
case 0x76: // bmp280
case 0x77: // bmp
Wire.beginTransmission(address);
Wire.write(byte(0xd0)); // get id register on addr 0xd0
Wire.endTransmission();
Wire.requestFrom(address, 1);
resp[0] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print("\tbosch pressure sensor bmp085: 0x55, bmp180: 0x55, bmp280: 0x58)");
break;
case 0x1e: // hmc5883L /hmc5983
Wire.beginTransmission(address);
Wire.write(byte(0x0a)); // get id register from addr 0x0a
Wire.endTransmission();
Wire.requestFrom(address, 3);
resp[0] = Wire.read();
resp[1] = Wire.read();
resp[2] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print(" ");
Serial.print("0x"); Serial.print(resp[1], HEX); Serial.print(" ");
Serial.print("0x"); Serial.print(resp[2], HEX); Serial.print(" ");
Serial.print(" HMC: 5983 0x48 0x34 33, HMC5883L 0x48 0x34 0x33 ! same ...");
break;
case 0x0d: // qmc5883l
Wire.beginTransmission(address);
Wire.write(byte(0x0d)); // get id register from addr 0x0d
Wire.endTransmission();
Wire.requestFrom(address, 1);
resp[0] = Wire.read();
Serial.print("id: 0x"); Serial.print(resp[0], HEX); Serial.print(" ");
Serial.print("\tQMC5883L 0xff");
break;
default :;
}
}
void scann(int all)
{
byte error, address;
int nDevices;
Serial.println("\n\n\n\nScanning...");
nDevices = 0;
for (address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device: 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.print(" (");
Serial.print(address);
Serial.print(") ");
testSlave(address);
Serial.println("");
nDevices++;
}
else if (all == 1) {
Serial.print("0X");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
switch (error) {
case 1: Serial.println(" err 1: data to long to fit in tx buffer");
break;
case 2: Serial.println(" err 2: received NACK on TX of address - NO DEVICE ?!");
break;
case 3: Serial.println(" err 3: received NACK on TX of data");
break;
case 4: Serial.println(" err 4: other err");
break;
case 5: Serial.println(" err 4: timeout");
break;
}
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else {
Serial.print("found: "); Serial.print(nDevices); Serial.println(" devices");
}
}
void loop()
{
Serial.println("show all i2c device addresses");
scann(1);
delay(1000); // wait 1 sec
Serial.println("show only found found devices");
Serial.println("\nI2C Scanner MPU6050");
scann(0);
delay(5000);
}
/*
Wire.endtransmission() returns ...
0: success.
1: data too long to fit in transmit buffer.
2: received NACK on transmit of address.
3: received NACK on transmit of data.
4: other error.
5: timeout
XXXXXXXXXXXXXXXXXXXXXXXX
mpu6500 et al
https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1
PROBMLEM 9250 IS A 6500 ??
https://github.com/kriswiner/ESP32/issues/24
https://www.tindie.com/products/onehorse/all-st-motion-sensor-breakout-board/
*/
Jens
THE BEER-WARE LICENSE (Revision v 42)
jensd@jensd.dk wrote this file.
As long as you retain this notice you can do whatever you want with this stuff on these pages. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return :-) (C) Jens Dalsgaard Nielsen credit for license this goes to phk@FreeBSD.ORG
|