| 1 | #include <Adafruit_I2CDevice.h> |
| 2 | |
| 3 | #define I2C_ADDRESS 0x60 |
| 4 | Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS); |
| 5 | |
| 6 | void setup() { |
| 7 | while (!Serial) { |
| 8 | delay(10); |
| 9 | } |
| 10 | Serial.begin(115200); |
| 11 | Serial.println("I2C device read and write test"); |
| 12 | |
| 13 | if (!i2c_dev.begin()) { |
| 14 | Serial.print("Did not find device at 0x"); |
| 15 | Serial.println(i2c_dev.address(), HEX); |
| 16 | while (1) |
| 17 | ; |
| 18 | } |
| 19 | Serial.print("Device found on address 0x"); |
| 20 | Serial.println(i2c_dev.address(), HEX); |
| 21 | |
| 22 | uint8_t buffer[32]; |
| 23 | // Try to read 32 bytes |
| 24 | i2c_dev.read(buffer, 32); |
| 25 | Serial.print("Read: "); |
| 26 | for (uint8_t i = 0; i < 32; i++) { |
| 27 | Serial.print("0x"); |
| 28 | Serial.print(buffer[i], HEX); |
| 29 | Serial.print(", "); |
| 30 | } |
| 31 | Serial.println(); |
| 32 | |
| 33 | // read a register by writing first, then reading |
| 34 | buffer[0] = 0x0C; // we'll reuse the same buffer |
| 35 | i2c_dev.write_then_read(buffer, 1, buffer, 2, false); |
| 36 | Serial.print("Write then Read: "); |
| 37 | for (uint8_t i = 0; i < 2; i++) { |
| 38 | Serial.print("0x"); |
| 39 | Serial.print(buffer[i], HEX); |
| 40 | Serial.print(", "); |
| 41 | } |
| 42 | Serial.println(); |
| 43 | } |
| 44 | |
| 45 | void loop() {} |