MicroProcessor_SBC/Arduino

아두이노(Arduino) I2C Scanner

LonleyEngineer 2021. 11. 18. 14:49
반응형

아두이노 I2C Scanner

  • 아두이노에서 I2C 인터페이스 장치를 이용하기 위해서는 주소가 필요하다.
  • I2C 주소를 보통 제품에서 알려주기도 하지만 그래도 사용 전에 주소를 확인해보는 것이 좋기 때문에 찾아보니 스캐너 코드가 있어서 따라서 작성해보았다.

 

테스트 환경

  • 테스트에 사용한 아두이노 보드는 UNO 이다.
  • I2C 인터페이스 장치는 SSD1306 OLED Display LCD이다.
  • SSD1306은 아래와 같은 모양이다. 4핀으로 VCC, GND, SCL, SDA 가 있다. 동작전압은 3.3V, 5V 모두 지원하는 것으로 확인되었다.

 

 

  • 뒷면에 I2C ADDRESS SELECT라고 해서 0x78과 0x7A 중 하나를 선택하도록 납땜해주는 곳이 있다.
  • 하지만 이것을 믿을 수가 없기 때문에 I2C Scanner가 필요했던 것이다.

 

 

  • 회로구성은 다음과 같다.

  • A4, A5핀을 사용해야 하는 이유는 Arduiono의 설명서와 Datasheet 상 A4, A5핀은 SCL, SDA와 매칭되도록 되어 있기 때문이다.

https://docs.arduino.cc/resources/datasheets/A000066-datasheet.pdf  8page
https://content.arduino.cc/assets/UNO-TH_Rev3e_sch.pdf
https://docs.arduino.cc/resources/datasheets/A000066-datasheet.pdf  9page

  • 소스코드에서 Wire를 사용하면 I2C 기능 핀으로 사용하게 된다.

 

소스코드 와 동작결과

/* I2C 통신을 지원하는 장치에 대한 주소를 스캐닝 후 시리얼 모니터로 결과값 전송 */
#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  
  while(!Serial){}; //시리얼모니터 준비시까지 대기
  Serial.println("\nI2C Scanner Functions");  
}



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

  Serial.println("Scanning Start");

  for(address = 0x01; address < 0x7F; address++){
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion 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 < 0x10){        
        Serial.print("0x00");        
      }
      // 디바이스가 검출되면 주소 출력
      Serial.print(address, HEX);
      Serial.println("  !");
            nDevices++;            
    }
    else if (error == 4){
      // 검출과정에서 에러발생하면 에러 발생 메시지를 시리얼로
      Serial.print("Unkown error at address 0x");
      if (address < 0x10){
        Serial.print("0x");        
      }
      Serial.println(address, HEX);
    }    
  }

  if (nDevices == 0){
    // 아무런 디바이스도 찾지 못했을 때
    Serial.println("No I2C devices found\n");    
  }
  else{
    Serial.println("Scanning Complete!!\n");    
  }

  // 다음 루프를 실행하기 위해 5초 대기
  delay(5000);
  
}
//https://gist.github.com/tfeldmann/5411375

 

  • 회로가 정상적으로 구성되었으니 주소 스캐닝을 해보니

  • 기판에 있는 주소와는 다른 주소가 나온다. 역시 해봐야 아는 것인가보다.
반응형