Forum >SEN0366 and Adafruit ESP32-S3 Reverse TFT
TroubleshootingArduino

SEN0366 and Adafruit ESP32-S3 Reverse TFT

userHead NickW36 2024-08-04 19:32:04 355 Views2 Replies

Hi all, I am trying to use these 2 components together and for the life of me, I can't seem to get it to work. I've used the sample code provided by DFRobot and the sensor works perfectly, but upon trying to make the code work on the Adafruit board with hardware serial instead of software serial all I can get it to do is turn on briefly and then turn off. I'm sure there's something I'm missing but I see no reason why this isn't working. Any help is welcome. I'll post the code I am running below.

 

#include <HardwareSerial.h>

char buff[4] = { 0x80, 0x06, 0x03, 0x77 };  // Send the command 80 6 3 77 to the sensor (Continuous measurement)
unsigned char data[11] = { 0 };             // This defines an array to store the incoming data from the sensor

HardwareSerial MySerial(1); // define a Serial for UART1
const int MySerialRX = 16;
const int MySerialTX = 17;

void setup() 
{

 Serial.begin(115200);
 MySerial.begin(9600, SERIAL_8N1, MySerialRX, MySerialTX); // Initialize MySerial at 9600 baud, RX = GPIO 16, TX = GPIO 17

 // Command to set measuring range to 5 meters
 unsigned char setRange[5] = {0xFA, 0x04, 0x09, 0x05, 0xF4};
 // Send the command
 MySerial.write(setRange, sizeof(setRange));

 // Command to set resolution to 0.1mm
 unsigned char setRes[5] = {0xFA, 0x04, 0x0C, 0x02, 0xF4};
 // Send the command
 MySerial.write(setRes, sizeof(setRes));
}

void loop() 
{
 MySerial.write(buff, sizeof(buff));  // Sends the buff array to the sensor

 while (1) 
 {
   if (MySerial.available() > 0) 
   {     // Determine whether there is data to read on the serial
     delay(50);                      // Waits for 50 milliseconds
     for (int i = 0; i < 11; i++) 
     {  // Reads 11 bytes of data from the serial port into the data array
       data[i] = MySerial.read();
     }

     unsigned char Check = 0;
     for (int i = 0; i < 10; i++) 
     {  // Calculates a checksum by summing the first 10 bytes of the received data
       Check = Check + data[i];
     }

     Check = ~Check + 1;  // Inverts the checksum and adds 1 to it

     if (data[10] == Check) 
     {  // Checks if the calculated checksum matches the checksum in the received data
       if (data[3] == 'E' && data[4] == 'R' && data[5] == 'R') 
       {
         Serial.println("Out of range");
       } 
       else 
       {                                                                                                                                                           // If there is no error, calculates the distance using the received data
         float distance = 0;                                                                                                                                              // Initializes the distance variable
         distance = (data[3] - 0x30) * 100 + (data[4] - 0x30) * 10 + (data[5] - 0x30) * 1 + (data[7] - 0x30) * 0.1 + (data[8] - 0x30) * 0.01 + (data[9] - 0x30) * 0.001;  // Calculates the distance based on the received data
         Serial.print("Distance = ");
         Serial.print(distance, 3);
         Serial.println(" M");
       }
     } 
     else 
     {
       Serial.println("Invalid Data!");  // If the checksum doesn't match, prints "Invalid Data!"
     }
   }
   else{
   Serial.println(" M");
 }
   delay(20);  // Waits for 20 milliseconds
   
 }
 
}
 

2024-08-05 13:07:56

Please check if there is a baud rate mismatch. 

userHeadPic lia.ifat
NickW36 wrote:

I've tested the baud rate and it seems to be okay. Maybe there is some kind of initialization that is being missed because my issue is that the sensor won't stay on. When I got it working with the Arduino Uno there was a nice bright red laser that came out of the sensor, letting me know it was operating correctly, but with the Adafruit board the sensor turns on briefly and turns off with no bright red laser.

2024-08-08 19:21:01
1 Replies