New to arduino and was wondering if someone would be able to help me figure out how to switch this sensor from UART to I2C Mode? Sensor works great in UART but for the life of me I cant figure out how to switch to I2C since I want to be able to use three of these sensors off of one board.
I assume I need to switch the mode somewhere but the provided sample I2C code doesn't seem to include that code.
Manual has this but I can't think of how to do this prior to switching to I2C from UART mode without moving wires around mid program running?
/**************************************************************************************
* Copyright (C) 2022 Xintan Technology Corporation
*
* Author: Marco
s1的mode 引脚要接地,S1上电时检查进入哪种通讯模式
Hardware Connection
S1 328p
Vcc 5v
Gnd Gnd
SCL SCL(ADC5)
SDA SDA(ADC4)
Mode Gnd
***************************************************************************************/
#include <Wire.h>
/*************************kalman Filter*******************************/
bool filters_klm_on = false;
uint16_t klm_factor = 300;
uint16_t klm_threshold = 300;
int last_time = 0;
uint16_t last_distance = 0;
void doKlmFilter(uint16_t & distance)
{
if(filters_klm_on)
{
bool bfilterstart = false;
int currentTime = millis();
if(abs(currentTime - last_time)<300)//300ms
bfilterstart = true;
last_time = currentTime;
if((distance < 64000)&&(last_distance < 64000)&& bfilterstart)
{
int32_t diff = distance - last_distance;
if (abs(diff) < klm_threshold)
{
uint32_t result = 0;
result = (((distance * klm_factor) + (last_distance * (1000-klm_factor))) / 1000);
distance = result;
}
}
last_distance = distance;
}
}
/********************************************************/
int S1_ADDRESS = 0x10; //S1 I2c address is 0x20
//用于判断设备连接是否正常的变量
uint16_t timeout_count = 0;
bool isConneced = false;
//Scan for S1 device on the I2C bus
void scandev() {
int nDevices = 0;
Serial.println("Scanning...");
for (byte address = 1; address < 127; ++address) {
Wire.beginTransmission(address);
byte 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(" !");
S1_ADDRESS = address;
++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");
}
}
void setup() {
Serial.begin(115200);//Serial Bitrate 115200
Wire.begin();
Serial.println("wait connect");
scandev();
}
void s1_writeReg(uint8_t regaddr, uint16_t value)
{
Wire.beginTransmission(S1_ADDRESS);
Wire.write(byte(regaddr));
Wire.write(byte(0));
Wire.write(byte(value & 0xFF));
Wire.write(byte(value >> 8));
Wire.endTransmission();
}
void recfgsensor()
{
s1_writeReg(0, 0); //电流寄存器设置为0(Set the current register to 0)
s1_writeReg(61, 0x000); //设置为主动读取结果 (Set the outgoing mode to 0x0000 for active reading result mode).
s1_writeReg(66, 200);//周期寄存器 (Set the measurement cycle register to 200ms)
s1_writeReg(3, 0x0001); //start measuring
//s1_writeReg(3, 0x0002); //stop measuring
}
void loop() {
uint8_t data[10];
//传输寄存器地址(set register address)
Wire.beginTransmission(S1_ADDRESS);
Wire.write(byte(23));
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(S1_ADDRESS, 2);//请求从设备读取2个字节的数据(Request to read 2 bytes of data from the device)
if (Wire.available() >= 2) {
int lowByte = Wire.read(); //Read low byte
int highByte = Wire.read(); //Read high byte
uint16_t distance = (highByte << 8) | lowByte;
if(isConneced == false)
{
isConneced = true;
timeout_count = 0;
recfgsensor();
Serial.println("connected");
}else
{
doKlmFilter(distance);
if(distance < 64000)
{
Serial.print(distance);
Serial.println(" mm");
}else
{
Serial.print("ErrorCode:");
Serial.println(distance);
}
}
}else
{
if(isConneced)
{
timeout_count ++;
if(timeout_count > 2)//3次连续超时,就认为模组已断开(If there are three consecutive timeouts, it is assumed that the module has been disconnected)
{
isConneced = false;
Serial.println("Disconnect");
}
}
//Serial.println("FAILED");
}
delay(200); //delay 0.2s
}

