[ESP32 xBee EVM] 보드의 I2C는 IO22(SCL), IO22(SDA) 에 할당되어 있다.
ESP32 I2C
SDA (default : GPIO 21)
SCL (default : GPIO 22)
I2C GPIO 확장 PCF8574 SSM 모듈 보드와 거리 측정 센서 VL53L01 모듈을 이용하여 거리값에 따라 LED 거리정보를 측정해 보자
#include <Wire.h>
#include <VL53L0X.h>
#define PIN_LED1 13
#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();
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) {}
}
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;
//Serial.println(i);
if(i>7)i=7;
write8(PCF_8574_ADDR, ~(disp[i] ));
}
반응형