티스토리 뷰

728x90
반응형

I2C 통신에서 각 장치는 고유한 주소를 가지고 있어야 한다.

이 주소를 통해 마이크로컨트롤러가 특정 장치와 통신할 수 있으며,

같은 버스상에 여러 장치가 연결되어 있더라도 각각을 구분하여 제어할 수 있다. 

 

아두이노에서 I2C 주소 찾는코드

#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial); // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for (address = 1; address < 127; address++ ) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmission to see if
    // a device did acknowledge to the address.
    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\n");
  } else {
    Serial.println("done\n");
  }

  // Wait 5 seconds for the next scan
  delay(5000);
}

Scanning...
I2C device found at address 0x23 !
I2C device found at address 0x3C !
I2C device found at address 0x5A !
I2C device found at address 0x60 !
I2C device found at address 0x6A !
done

0x23: 이 주소는 일반적으로 BH1750 조도 센서

0x3C 주소는 일반적으로 SSD1306 기반의 OLED 디스플레이

728x90
반응형
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함
반응형