Laser sensor tw10sp not working correctly

hello everyone, am working with the laser sensor tw10sp to mesure distance and interfacing it with UART. It require commands to work. i send the HEX command via UART and it response always with the same HEX number (clearly something wrong).
Any help please !!
image

1 Like

Did you forget something?

the command of distance mesurement sent to the sensor is 100% correct, but always it response with HEX values showed in the picture below ; which i can't understand

Perhaps there's a problem with the code you didn't post.
Or the schematic.

I purchased the same laser modules. I can't figure out how to get them to work. If you were successful, would you mind sharing your knowledge/code?

THANK YOU!!

Use 9600 in the baud rate configuration.

Would you mind sharing your code? I've tried multiple times and can't get it to activate the laser or read returned data properly.

Thank you very much!!

I've tried multiple baud rates. I can't figure out how to send and receive data. If you'd share your Arduino code I would be very much thankful.

try this code :

#include <SoftwareSerial.h>

String sensorReadings;
uint8_t sensorReadingsArr[6];

uint8_t byte6_1,byte5_1,byte6_2,byte5_2,byte5_1_hex,byte5_2_hex,byte6_1_hex,byte6_2_hex;

uint8_t command[8] = {0x01,0x03,0x00,0x0F,0x00,0x02,0xF4,0x08};
uint8_t mesure[9];
uint32_t distance;

uint8_t convert_hex_dec( uint8_t a)
{
uint8_t decimal;

switch(a)
{ case 0 :
decimal = 0;
break;

case 1 :
  decimal = 1;
break;
case 2 :
  decimal = 2;
break;
case 3 :
  decimal = 3;
break;
case 4 :
  decimal = 4;
break;
case 5 :
  decimal = 5;
break;
case 6 :
  decimal = 6;
break;
case 7 :
  decimal = 7;
break;
case 8 :
  decimal = 8;
break;
case 9 :
  decimal = 9;
break;
case 'A' :
  decimal = 10;
break;
case  11:
  decimal = 11;
break;
case 'C' :
  decimal = 12;
break;
case 'D':
  decimal = 13;
break;
case 'E' :
  decimal = 14;
break;
case 'F' :
  decimal = 15;
break;

}
return decimal;

}

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myserial.begin(9600);

}

uint32_t calcul_distance(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
{
uint8_t byte5_1_hex = convert_hex_dec(a);
uint8_t byte5_2_hex = convert_hex_dec(b);
uint8_t byte6_1_hex = convert_hex_dec(c);
uint8_t byte6_2_hex = convert_hex_dec(d);

    distance = (byte6_2*1 + byte6_1*16 + byte5_2*16*16 + byte5_1*16*16*16)-50;

return distance;
}

void loop() {

/*****************************************Command Sensor*********************************************/

myserial.write(command, 8);
delay(150);

myserial.read(mesure, 9);
delay(150);

byte5_1 = mesure[5] /16;
byte5_2 = mesure[5] - byte5_116;
byte6_1 = mesure[6] /16;
byte6_2 = mesure[6] - byte6_1
16;

    distance = calcul_distance(byte5_1,byte5_2,byte6_1,byte6_2);

}

If you'd used code tags, you'd have avoided the italics.

The compiler hates italics

That code doesn't work properly. It complains that 'myserial.write....' was not declared in this scope in the LOOP section of the code.

I'm using a Feather 32u4 and is supported by the SoftwareSerial library so it should work, but something's not quite right.

It's not declared in the setup section either

Isn't this a declaration for it?
image

No, of course not, any more than that's a declaration of Serial.

Hmmm, but this is how I do it in other programs and it works just fine. The code below uses the altSoftSerial library, but the declarations are the same. I'm confused if you're telling me it's incorrect. How should it be programmed?

image

You haven't shown lines 1 through 375.

Thank you. My code for that other program is too sensitive to share, so I'll work with the example that bhiri_med provided. I've cleaned up the formatting a bit to my liking.

I can't figure out what is wrong and why the myserial in the LOOP thinks it's not declared.

Thank you for your help!!!

//*****************************************************************************
#include <SoftwareSerial.h>

String sensorReadings;
uint8_t sensorReadingsArr[6];

uint8_t byte6_1,byte5_1,byte6_2,byte5_2,byte5_1_hex,byte5_2_hex,byte6_1_hex,byte6_2_hex;

uint8_t command[8] = {0x01,0x03,0x00,0x0F,0x00,0x02,0xF4,0x08};
uint8_t data[9];
uint32_t distance;

//*****************************************************************************
uint8_t convert_hex_dec( uint8_t a) {
  uint8_t decimal;
  
  switch(a) { 
  case 0 :
    decimal = 0;
    break;
  case 1 :
    decimal = 1;
    break;
 <code removed for simplicity here>
  }
  return decimal;
}

//*****************************************************************************
uint32_t calcul_distance(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  uint8_t byte5_1_hex = convert_hex_dec(a);
  uint8_t byte5_2_hex = convert_hex_dec(b);
  uint8_t byte6_1_hex = convert_hex_dec(c);
  uint8_t byte6_2_hex = convert_hex_dec(d);  
  distance = (byte6_2*1 + byte6_1*16 + byte5_2*16*16 + byte5_1*16*16*16)-50;
  return distance;
}

//*****************************************************************************
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  myserial.begin(9600);
}

//*****************************************************************************
void loop() {
  myserial.write(command, 8);
  delay(150);
  myserial.read(data, 9);
  delay(150);  
  byte5_1 = data[5] /16;
  byte5_2 = data[5] - byte5_116;
  byte6_1 = data[6] /16;
  byte6_2 = data[6] - byte6_116;
  distance = calcul_distance(byte5_1,byte5_2,byte6_1,byte6_2);
}`

Because you have no object of type SoftwareSerial called myserial declared in global scope.

Try Nivea?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.