Dash Cam Front And Back,2-Inch Front And Rear Dashcam,2 " Mini Dual Dashcams,Front Rear Dash Cam SHENZHEN ROSOTO TECHNOLOGY CO., LTD. , https://www.rdtkdashcam.com
Arduino reads the address of the connected device on the I2C bus
**I2C Communication and Address Setup**
The I2C address is similar to a home address. Each device on the I2C bus has its own unique address. Since there is only one master on the bus, and the master controls communication with the slaves, the master doesn't need an address.
You can set the I2C address of a slave device as long as it's not already used by another device on the same bus. This allows for flexibility in connecting multiple devices.
To configure a slave board, you simply use `Wire.begin(address);` where the address is a hexadecimal value. The master, on the other hand, just needs to call `Wire.begin();` without specifying an address.
Here’s an example of how to set up a slave device with the address 0x12:
```cpp
#include "Wire.h"
#define SLAVE_ADDRESS 0x12
void setup() {
Wire.begin(SLAVE_ADDRESS); // Join the I2C bus as a slave with address 0x12
}
void loop() {
// Nothing to do here
}
```
**I2C Address Scanning**
Once you have a slave device connected, you may want to check if it's detected by the master. This is where an I2C scanner comes in handy. It’s one of the most basic examples of an I2C master that scans the bus for connected devices.
The I2C scanner works by sending a `beginTransmission()` request to every possible address (from 0x00 to 0x7F). After each attempt, it calls `endTransmission()` and checks the result.
If the return value is 0, it means a device was found at that address. If it returns 4, it indicates a possible error or an unknown issue. Otherwise, no device is present.
Here’s a simple I2C scanner sketch:
```cpp
#include "Wire.h"
#define SERIAL_BAUD 57600
void setup() {
Wire.begin();
Serial.begin(SERIAL_BAUD);
Serial.println("I2C Scanner started");
Serial.println();
}
void loop() {
uint8_t error, i2cAddress, devCount = 0, unCount = 0;
Serial.println("Scanning...");
for (i2cAddress = 1; i2cAddress <= 127; i2cAddress++) {
Wire.beginTransmission(i2cAddress);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at 0x");
if (i2cAddress < 16) Serial.print("0");
Serial.println(i2cAddress, HEX);
devCount++;
} else if (error == 4) {
Serial.print("Unknown error at 0x");
if (i2cAddress < 16) Serial.print("0");
Serial.println(i2cAddress, HEX);
unCount++;
}
}
if (devCount + unCount == 0) {
Serial.println("No I2C devices found");
} else {
Serial.print(devCount);
Serial.print(" device(s) found");
if (unCount > 0) {
Serial.print(", and unknown error in ");
Serial.print(unCount);
Serial.print(" address");
}
Serial.println();
}
Serial.println();
delay(5000);
}
```
When you run this code, you should see output like this:
```
I2C Scanner started
Scanning...
I2C device found at 0x12
1 device(s) found
```
This helps confirm that your I2C devices are properly connected and recognized by the master. Always make sure that the addresses are correctly set and don’t conflict with others on the same bus.