|
Post by deadman1966 on Oct 14, 2019 20:27:37 GMT
I used this on my mega to scan the i2c bus i currently have a ds3232 and a pressure sensor on pins 20 and 21. It compiles but locks after sending "scanning" to the serial port
// -------------------------------------- // i2c_scanner // // 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 known. // // This sketch tests the standard 7-bit addresses // from 0 to 127. Devices with higher bit address // might not be seen properly. // // Adapted to be as simple as possible by Arduino.cc user Krodal // // June 2012 // Using Arduino 1.0.1 // //This download brought to you by //http://projectsfromtech.blogspot.com/
#include <Wire.h>
void setup() { Wire.begin(); Wire.setWirePins(UsePins_20_21); SerialASC.begin(115200); SerialASC.println("\nI2C Scanner"); }
void loop() { byte error, address; int nDevices;
SerialASC.println("Scanning...");
nDevices = 0; for(address = 0; 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) { SerialASC.print("I2C device found at address 0x"); if (address<16) SerialASC.print("0"); SerialASC.print(address,HEX); SerialASC.println(" !");
nDevices++; } else if (error==4) { SerialASC.print("Unknow error at address 0x"); if (address<16) SerialASC.print("0"); SerialASC.println(address,HEX); } } if (nDevices == 0) SerialASC.println("No I2C devices found\n"); else SerialASC.println("done\n");
delay(8000); // wait 8 seconds for next scan }
|
|
|
Post by deadman1966 on Oct 14, 2019 21:58:34 GMT
This was a hardware issue the mega2560 board had an extra set of I2C pins after the analog reference input that were connected to pins 20 and 21. On the shieldbuddy these pins are actually connected to SDA1 and SCL1. Once in changed the pins i can now see the ds3232 rtc, it eeprom and the pressure sensor in the scan. I also moved the pin definition before the wire begin command.
// -------------------------------------- // i2c_scanner // // 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 known. // // This sketch tests the standard 7-bit addresses // from 0 to 127. Devices with higher bit address // might not be seen properly. // // Adapted to be as simple as possible by Arduino.cc user Krodal // // June 2012 // Using Arduino 1.0.1 // //This download brought to you by //http://projectsfromtech.blogspot.com/
#include <Wire.h>
void setup() { Wire.setWirePins(UsePins_SDA1_SCL1); Wire.begin(); SerialASC.begin(115200); SerialASC.println("\nI2C Scanner"); }
void loop() { byte error, address; int nDevices;
SerialASC.println("Scanning...");
nDevices = 0; for(address = 0; 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) { SerialASC.print("I2C device found at address 0x"); if (address<16) SerialASC.print("0"); SerialASC.print(address,HEX); SerialASC.println(" !");
nDevices++; } else if (error==4) { SerialASC.print("Unknow error at address 0x"); if (address<16) SerialASC.print("0"); SerialASC.println(address,HEX); } } if (nDevices == 0) SerialASC.println("No I2C devices found\n"); else SerialASC.println("done\n");
delay(8000); // wait 8 seconds for next scan }
17:53:22.544 -> I2C Scanner 17:53:22.544 -> Scanning... 17:53:22.544 -> I2C device found at address 0x57 ! 17:53:22.578 -> I2C device found at address 0x68 ! 17:53:22.578 -> I2C device found at address 0x77 ! 17:53:22.578 -> done
|
|