/**
* I2CScanner.pde -- I2C bus scanner for Arduino
*
* 2009, Tod E. Kurt, http://todbot.com/blog/
*
*/
#include "Wire.h"
extern "C" {
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}
// Scan the I2C bus between addresses from_addr and to_addr.
// On each address, call the callback function with the address and result.
// If result==0, address was found, otherwise, address wasn't found
// (can use result to potentially get other status on the I2C bus, see twi.c)
// Assumes Wire.begin() has already been called
void scanI2CBus(byte from_addr, byte to_addr,
void(*callback)(byte address, byte result) )
{
byte rc;
byte data = 0; // not used, just an address to feed to twi_writeTo()
for( byte addr = from_addr; addr <= to_addr; addr++ ) {
rc = twi_writeTo(addr, &data, 0, 1);
callback( addr, rc );
}
}
// Called when address is found in scanI2CBus()
// Feel free to change this as needed
// (like adding I2C comm code to figure out what kind of I2C device is there)
void scanFunc( byte addr, byte result ) {
Serial.print("addr: ");
Serial.print(addr,DEC);
Serial.print(",");
Serial.print(addr, HEX);
Serial.print(",");
Serial.print(addr, BIN);
Serial.print(",");
Serial.print( (result==0) ? " found!":" ");
Serial.print( (addr%4) ? "\t":"\n");
}
byte start_address = 1;
byte end_address = 120;
// standard Arduino setup()
void setup()
{
Wire.begin();
Serial.begin(19200);
Serial.println("\nI2CScanner ready!");
Serial.print("starting scanning of I2C bus from ");
Serial.print(start_address,DEC);
Serial.print(" to ");
Serial.print(end_address,DEC);
Serial.println("...");
// start the scan, will call "scanFunc()" on result from each address
scanI2CBus( start_address, end_address, scanFunc );
Serial.println("\ndone");
}
// standard Arduino loop()
void loop()
{
// Nothing to do here, so we'll just blink the built-in LED
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
delay(300);
}
Y me devuelve que está en la dirección 105:
I2CScanner ready!
starting scanning of I2C bus from 1 to 120...
addr: 1,1,1, addr: 2,2,10, addr: 3,3,11, addr: 4,4,100,
addr: 5,5,101, addr: 6,6,110, addr: 7,7,111, addr: 8,8,1000,
addr: 9,9,1001, addr: 10,A,1010, addr: 11,B,1011, addr: 12,C,1100,
addr: 13,D,1101, addr: 14,E,1110, addr: 15,F,1111, addr: 16,10,10000,
addr: 17,11,10001, addr: 18,12,10010, addr: 19,13,10011, addr: 20,14,10100,
addr: 21,15,10101, addr: 22,16,10110, addr: 23,17,10111, addr: 24,18,11000,
addr: 25,19,11001, addr: 26,1A,11010, addr: 27,1B,11011, addr: 28,1C,11100,
addr: 29,1D,11101, addr: 30,1E,11110, addr: 31,1F,11111, addr: 32,20,100000,
addr: 33,21,100001, addr: 34,22,100010, addr: 35,23,100011, addr: 36,24,100100,
addr: 37,25,100101, addr: 38,26,100110, addr: 39,27,100111, addr: 40,28,101000,
addr: 41,29,101001, addr: 42,2A,101010, addr: 43,2B,101011, addr: 44,2C,101100,
addr: 45,2D,101101, addr: 46,2E,101110, addr: 47,2F,101111, addr: 48,30,110000,
addr: 49,31,110001, addr: 50,32,110010, addr: 51,33,110011, addr: 52,34,110100,
addr: 53,35,110101, addr: 54,36,110110, addr: 55,37,110111, addr: 56,38,111000,
addr: 57,39,111001, addr: 58,3A,111010, addr: 59,3B,111011, addr: 60,3C,111100,
addr: 61,3D,111101, addr: 62,3E,111110, addr: 63,3F,111111, addr: 64,40,1000000,
addr: 65,41,1000001, addr: 66,42,1000010, addr: 67,43,1000011, addr: 68,44,1000100,
addr: 69,45,1000101, addr: 70,46,1000110, addr: 71,47,1000111, addr: 72,48,1001000,
addr: 73,49,1001001, addr: 74,4A,1001010, addr: 75,4B,1001011, addr: 76,4C,1001100,
addr: 77,4D,1001101, addr: 78,4E,1001110, addr: 79,4F,1001111, addr: 80,50,1010000,
addr: 81,51,1010001, addr: 82,52,1010010, addr: 83,53,1010011, addr: 84,54,1010100,
addr: 85,55,1010101, addr: 86,56,1010110, addr: 87,57,1010111, addr: 88,58,1011000,
addr: 89,59,1011001, addr: 90,5A,1011010, addr: 91,5B,1011011, addr: 92,5C,1011100,
addr: 93,5D,1011101, addr: 94,5E,1011110, addr: 95,5F,1011111, addr: 96,60,1100000,
addr: 97,61,1100001, addr: 98,62,1100010, addr: 99,63,1100011, addr: 100,64,1100100,
addr: 101,65,1100101, addr: 102,66,1100110, addr: 103,67,1100111, addr: 104,68,1101000,
addr: 105,69,1101001, found! addr: 106,6A,1101010, addr: 107,6B,1101011, addr: 108,6C,1101100,
addr: 109,6D,1101101, addr: 110,6E,1101110, addr: 111,6F,1101111, addr: 112,70,1110000,
addr: 113,71,1110001, addr: 114,72,1110010, addr: 115,73,1110011, addr: 116,74,1110100,
addr: 117,75,1110101, addr: 118,76,1110110, addr: 119,77,1110111, addr: 120,78,1111000,
done
En Hexadecimal es la 0x69 y en el código de ejemplo pone que por defecto está configurado en el 0x68, por eso supongo que no funciona.
Pero mi duda es como cambiar la dirección ?
en el código dice:
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
Que es el AD0 ?? , como lo paso a HIGH ?
Que instrucción hay que poner ?
Gracias por vuesta ayuda, la verdad es que nunca he tocado el tema de comunicación por I2C y pensaba que sería más fácil.
Saludos