본문 바로가기

RaspberryPi/RP2350

[RP2350_W6100] I2C 테스트 - VL53L0x, PCF8574 거리 측정 테스트

 

RP2350의 I2C는 GP5(SCL), GP4(SDA)에 할당되어 있다.

 

 

ST의 거리감지 센서 VL53L0x 모듈을 이용해서 I2C 동작 테스트를 해 보자.

 

 

#include <Wire.h>
#include <VL53L0X.h>

#define PIN_LED1            25

VL53L0X sensor;

void setup()
{
  Serial.begin(115200);

  Serial.println("initialize sensor!");
  Wire.begin();
  
  pinMode(PIN_LED1, OUTPUT); digitalWrite(PIN_LED1, 0);

  sensor.setTimeout(500);

  Serial.println(read8(PCF_8574_ADDR, 0));
  Serial.println("start");
  if (!sensor.init(1))
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1) {}
  }

  // Start continuous back-to-back mode (take readings as
  // fast as possible).  To use continuous timed mode
  // instead, provide a desired inter-measurement period in
  // ms (e.g. sensor.startContinuous(100)).
  sensor.startContinuous();
}

int cnt = 0;
void loop()
{
	int disp[] = {1,3,7,0xf,0x51f,0x3f,0x7f,0xff};
	int i;
	delay(20);

    cnt = sensor.readRangeContinuousMillimeters();
    if(cnt>2000)
    {
      cnt = 2000;
    }
    
  	Serial.println(cnt);

  	i = cnt/64;
  	if(i>7)i=7;

  	write8(PCF_8574_ADDR, ~(disp[i] ));

  	//if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
}

 

 

 

VL53L0x 모듈 데이터가 정상적으로 출력 되는것을 확인했으니 SSM EVM 타입의 PCF8574 I2C IO Expander 모듈의 출력을 테스트 해 보자

 

#include <Wire.h>
#include <VL53L0X.h>

#define PIN_LED1            25
#define PCF_8574_ADDR      0x20
void write8(uint8_t Address, uint8_t Value)
{
   Wire.beginTransmission(Address);
   Wire.write(Value);
   Wire.endTransmission();
}

uint8_t read8(uint8_t Address, uint8_t Value)
{
   uint8_t ret;
    
   Wire.beginTransmission(Address);
   Wire.write(Value);
   Wire.endTransmission();

Serial.println("1");

  Wire.requestFrom(Address, (uint8_t)1);
  ret = Wire.read();

  return ret;   
}

VL53L0X sensor;

void setup()
{
	Serial.begin(115200);
	Serial.println("initialize sensor!");
  
  	Wire.begin();
    pinMode(PIN_LED1, OUTPUT); digitalWrite(PIN_LED1, 0);

  	sensor.setTimeout(500);
	write8(PCF_8574_ADDR, 0xf0);

  	Serial.println(read8(PCF_8574_ADDR, 0));
   	Serial.println("start");
  	if (!sensor.init(1))
  	{
    	Serial.println("Failed to detect and initialize sensor!");
    	while (1) {}
  	}

  // Start continuous back-to-back mode (take readings as
  // fast as possible).  To use continuous timed mode
  // instead, provide a desired inter-measurement period in
  // ms (e.g. sensor.startContinuous(100)).
  sensor.startContinuous();
}

int cnt = 0;
void loop()
{
	int disp[] = {1,3,7,0xf,0x51f,0x3f,0x7f,0xff};
	int i;
  
	delay(100);

    cnt = sensor.readRangeContinuousMillimeters();
    if(cnt>2000)
    {
      cnt = 2000;
    }
    
  	Serial.println(cnt);

  	i = cnt/64;
  	if(i>7)i=7;
  
  	write8(PCF_8574_ADDR, ~(disp[i] ));
	//if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
}
반응형