# I2C(IIC) 設備掃描
一般模組(設備)使用時, 往往會有複雜的接線需求, 這時可以使用 I2C 協定, 與模組進行通訊。然而, 在使用 I2C 與模組通訊時, 常會發現不知道模組的位址, 例如透過 I2C 接1602LCD, 這時可用下列程式進行掃描, 取得位址。
Arduino 透過 I2C 連接 1602LCD

樹莓派RaspberryPi 連接 1602LCD

### Arduino 掃描 I2C 位址
```
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600); //鮑率
while (!Serial); //等待序列埠監控視窗
Serial.println("nI2C Scanner");
}
void loop(){
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
// i2c_scanner 使用 Write.endTransmisstion 的回傳值來查看設備是否確認該位址。
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4) {
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found");
else
Serial.println("donen");
delay(5000); //等待5秒進行下一次掃描
}
```
上傳到Arduino後, 打開序列埠監控視窗

設定鮑率(BaudRate), 需與程式一致, 可為 9600、19200、...

找到 1602LCD 位址在0x27

### 樹莓派RaspberryPi 掃描 I2C 位址
先打開 I2C 權限
```
#sudo raspi-config
```




找到了, 27 就是指 0x27