Rangefinder working with Arduino IDE but not VS Code

userHead Matthew.Cooper 2024-09-05 18:36:21 26 Views1 Replies

Hello there,

I'm trying to get a rangefinder sensor (link below) working with a Raspberry Pi Pico.

Sensor:
https://wiki.dfrobot.com/Infrared_Laser_Distance_Sensor_50m_80m_SKU_SEN0366?fbclid=IwZXh0bgNhZW0CMTAAAR01PcccLQxQ4WpxkyFXRAe8iviofnQn2KjtlCe5k-s5er6s0ondmOBm1eI_aem_8c3iMxLCBBxabE5iRlSWcA#target_0

At the moment using the below code, the sensor works perfectly when programmed with Arduino IDE.

For my project however, I need to get it working using VS Code and CMake. When I use the VS Code code it does not work, the raw data that the sensor receives is completely different. The code uploads but continuously reaches the printf("Invalid Data!\n"); line.

I've put code for the two IDEs below, as well as my CMakeLists.txt, any help/advice would be greatly appreciated,

Cheers.



Arduino IDE code:

// Command to set measuring range to 80 meters
char setRange[] = {0xFA, 0x04, 0x09, 0x50, 0xA9};
// Command to set frequency to 5 Hz
char setFrequency[] = {0xFA, 0x04, 0x0A, 0x20, 0xF3};
// Command to set resolution to 1 mm
char setResolution[] = {0xFA, 0x04, 0x0C, 0x01, 0xF5};

void setup() {
  Serial.begin(115200);              // Start USB serial communication
  Serial1.begin(9600);               // Start UART1 with baud rate 9600
  pinMode(LED_BUILTIN, OUTPUT);

  // Send commands to configure the sensor
  Serial1.write(setRange, sizeof(setRange));
  delay(100);  // Small delay to ensure the command is processed
  Serial1.write(setFrequency, sizeof(setFrequency));
  delay(100);  // Small delay to ensure the command is processed
  Serial1.write(setResolution, sizeof(setResolution));
  delay(100);  // Small delay to ensure the command is processed
}

void loop() {
  char buff[4] = {0x80, 0x06, 0x03, 0x77};  // Example command to request distance measurement
  unsigned char data[11] = {0};

  Serial1.write(buff, sizeof(buff));

  while (1) {
    digitalWrite(LED_BUILTIN, LOW);

    if (Serial1.available() > 0) {  // Check if there's data to read
      delay(50);
      for (int i = 0; i < 11; i++) {
        data = Serial1.read();
      }

      unsigned char Check = 0;
      for (int i = 0; i < 10; i++) {
        Check = Check + data;
      }
      Check = ~Check + 1;

    // Print the raw data
      Serial.print("Raw Data: ");
      for (int i = 0; i < 11; i++) {
        Serial.print(data, HEX);  // Print each byte in hexadecimal format
        Serial.print(" ");
      }
      Serial.println();

      if (data[10] == Check) {
        if (data[3] == 'E' && data[4] == 'R' && data[5] == 'R') {
          Serial.println("Out of range");
        } else {
          float distance = 0;
          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;
          Serial.print("Distance = ");
          Serial.print(distance, 3);
          Serial.println(" M");
          digitalWrite(LED_BUILTIN, HIGH);
        }
      } else {
        Serial.println("Invalid Data!");
      }
    }
    delay(20);
  }
}



VS Code:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"

const uint LED_PIN = PICO_DEFAULT_LED_PIN;

void setup() {
    stdio_init_all();
    uart_init(uart0, 9600);
    gpio_set_function(0, GPIO_FUNC_UART);  // TX
    gpio_set_function(1, GPIO_FUNC_UART);  // RX

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
}

int main() {
    setup();

    // Commands to send to the rangefinder
    char setRange[] = {0xFA, 0x04, 0x09, 0x50, 0xA9};
    char setFrequency[] = {0xFA, 0x04, 0x0A, 0x05, 0xF3};
    char setResolution[] = {0xFA, 0x04, 0x0C, 0x01, 0xF5};

    // Send commands
    uart_write_blocking(uart0, (uint8_t *)setRange, sizeof(setRange));
    sleep_ms(100);
    uart_write_blocking(uart0, (uint8_t *)setFrequency, sizeof(setFrequency));
    sleep_ms(100);
    uart_write_blocking(uart0, (uint8_t *)setResolution, sizeof(setResolution));
    sleep_ms(100);

    while (true) {
        char buff[] = {0x80, 0x06, 0x03, 0x77}; // Command to request distance
        uint8_t data[11] = {0};

        uart_write_blocking(uart0, (uint8_t *)buff, sizeof(buff));

        if (uart_is_readable_within_us(uart0, 50000)) {
            for (int i = 0; i < 11; i++) {
                data = uart_getc(uart0);
            }

            uint8_t Check = 0;
            for (int i = 0; i < 10; i++) {
                Check += data;
            }
            Check = ~Check + 1;

            printf("Raw Data: ");
            for (int i = 0; i < 11; i++) {
                printf("%02X ", data);
            }
            printf("\n");


            if (data[10] == Check) {
                if (data[3] == 'E' && data[4] == 'R' && data[5] == 'R') {
                    printf("Out of range\n");
                } else {
                    float distance = 0;
                    distance = (data[3] - '0') * 100 + (data[4] - '0') * 10 + (data[5] - '0') +
                               (data[7] - '0') * 0.1f + (data[8] - '0') * 0.01f + (data[9] - '0') * 0.001f;
                    printf("Distance = %.3f M\n", distance);
                    gpio_put(LED_PIN, 1);  // Turn on LED
                }
            } else {
                printf("Invalid Data!\n");
            }
        }
        sleep_ms(20);
    }
}


 
CMakeLists.txt:


cmake_minimum_required(VERSION 3.12)

include(pico_sdk_import.cmake)

project(RangefinderProject)

pico_sdk_init()

add_executable(rangefindertest
    rangefindertest.c
)

# Link the Pico SDK libraries
target_link_libraries(rangefindertest
    pico_stdlib
    hardware_uart
)

pico_enable_stdio_usb(rangefindertest 1)
pico_enable_stdio_uart(rangefindertest 1)

# Create map/bin/hex/uf2 files
pico_add_extra_outputs(rangefindertest)

2024-09-06 16:44:00

  “if (uart_is_readable_within_us(uart0, 50000)) ”  wondering if “50000” is the baudrate of uart?  if it is, please set to 9600 baudrate.  

userHeadPic R2D2C3PO