/************* After having configured TFmini plus sensors with dedicated I2C address , set communication mode to I2C with WINCC_TF.exe (benawake resource software) via TTL usb adapter you can connect to the I2C bus and request data with arduino nano. The data parsing is the same as UART mode given by benawake as example I wanted to make a function for connecting to the I2C device, ask for data an parse it but the Wire.requestFrom() function seems not to accept a variable byte as input address nor a pointer * * * ******************/ #include const byte Sensor1=0x20; const byte Sensor2=0x21; const byte Sensor3=0x22; const byte Sensor4=0x23; const byte Sensor5=0x24; int dist1=0; //actual distance measurements of LiDAR int dist2=0; int dist3=0; int dist4=0; int dist5=0; byte error; int strength; //signal strength of LiDAR float temprature; int check; //save check value int i; int uart[9]; //save data measured by LiDAR const int HEADER=0x59; //frame header of data package void setup() { Wire.begin(); Serial.begin(9600); //while (!Serial); // Leonardo: wait for serial monitor Serial.println("\nI2C TFmini plus"); } void loop() { /**************************SENSOR 1******************************/ Wire.beginTransmission(0x20); //delay(70); //After the sending the config-frame from the master to the slave, one needs to wait for a period for the command to be processed RequestData(); Wire.requestFrom(0x20, 9); // request 9 bytes from slave device @ fixed address while (Wire.available()) { // slave may send less than requested if(Wire.read() == HEADER) { //assess data package frame header 0x59 uart[0]=HEADER; if (Wire.read() == HEADER) { //assess data package frame header 0x59 uart[1] = HEADER; for (i = 2; i < 9; i++) { //save data in array uart[i] = Wire.read(); } check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7]; if (uart[8] == (check & 0xff)) { //verify the received data as per protocol dist1 = uart[2] + uart[3] * 256; //calculate distance value //Serial.print("distance 1="); //Serial.println(dist1); //output measure distance value of LiDAR } } } } error = Wire.endTransmission(); //delay(10); if (error != 0) { //Serial.println("I2C device not found"); } /******************************************************* */ /**************************SENSOR 2******************************/ Wire.beginTransmission(0x21); //delay(70); //After the sending the config-frame from the master to the slave, one needs to wait for a period for the command to be processed RequestData(); Wire.requestFrom(0x21, 9); // request 9 bytes from slave device @ fixed address while (Wire.available()) { // slave may send less than requested if(Wire.read() == HEADER) { //assess data package frame header 0x59 uart[0]=HEADER; if (Wire.read() == HEADER) { //assess data package frame header 0x59 uart[1] = HEADER; for (i = 2; i < 9; i++) { //save data in array uart[i] = Wire.read(); } check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7]; if (uart[8] == (check & 0xff)) { //verify the received data as per protocol dist2 = uart[2] + uart[3] * 256; //calculate distance value } } } } error = Wire.endTransmission(); // delay(10); if (error != 0) { //Serial.println("I2C device 2 not found"); } /********************************************************/ /**************************SENSOR 3******************************/ Wire.beginTransmission(0x22); //delay(100); //After the sending the config-frame from the master to the slave, one needs to wait for a period for the command to be processed RequestData(); Wire.requestFrom(0x22, 9); // request 9 bytes from slave device @ fixed address while (Wire.available()) { // slave may send less than requested if(Wire.read() == HEADER) { //assess data package frame header 0x59 uart[0]=HEADER; if (Wire.read() == HEADER) { //assess data package frame header 0x59 uart[1] = HEADER; for (i = 2; i < 9; i++) { //save data in array uart[i] = Wire.read(); } check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7]; if (uart[8] == (check & 0xff)) { //verify the received data as per protocol dist3 = uart[2] + uart[3] * 256; //calculate distance value } } } } error = Wire.endTransmission(); //delay(10); if (error != 0) { dist3=0; //Serial.println("I2C device not found"); } /********************************************************/ /**************************SENSOR 4******************************/ Wire.beginTransmission(0x23); //delay(100); //After the sending the config-frame from the master to the slave, one needs to wait for a period for the command to be processed RequestData(); Wire.requestFrom(0x23, 9); // request 9 bytes from slave device @ fixed address while (Wire.available()) { // slave may send less than requested if(Wire.read() == HEADER) { //assess data package frame header 0x59 uart[0]=HEADER; if (Wire.read() == HEADER) { //assess data package frame header 0x59 uart[1] = HEADER; for (i = 2; i < 9; i++) { //save data in array uart[i] = Wire.read(); } check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7]; if (uart[8] == (check & 0xff)) { //verify the received data as per protocol dist4 = uart[2] + uart[3] * 256 - 83; //calculate distance value } } } } error = Wire.endTransmission(); if (error != 0) { dist4=0; //Serial.println("I2C device not found"); } //delay(10); /********************************************************/ /**************************SENSOR 5******************************/ Wire.beginTransmission(0x24); //delay(100); //After the sending the config-frame from the master to the slave, one needs to wait for a period for the command to be processed RequestData(); Wire.requestFrom(0x24, 9); // request 9 bytes from slave device @ fixed address while (Wire.available()) { // slave may send less than requested if(Wire.read() == HEADER) { //assess data package frame header 0x59 uart[0]=HEADER; if (Wire.read() == HEADER) { //assess data package frame header 0x59 uart[1] = HEADER; for (i = 2; i < 9; i++) { //save data in array uart[i] = Wire.read(); } check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7]; if (uart[8] == (check & 0xff)) { //verify the received data as per protocol dist5 = uart[2] + uart[3] * 256; //calculate distance value } } } } error = Wire.endTransmission(); if (error != 0) { dist5=0; //Serial.println("I2C device not found"); } //delay(10); /********************************************************/ Serial.print(dist1);Serial.print(",");Serial.print(dist2);Serial.print(",");Serial.print(dist3);Serial.print(",");Serial.print(dist4);Serial.print(",");Serial.println(dist5); //delay(50); // wait 5 seconds for next scan } /***************LOOP END ********************/ /************************FUNCTIONS********************************/ void RequestData(){ Wire.write(0x5A); Wire.write(0x05); Wire.write(0x00); Wire.write(0x01); Wire.write(0x60); }