Rpi Pico - Sensor working with Arduino IDE but not VS Code - Page 1
Forum section: electronics / general

author: Dogma
author_rank: Newbie
timestamp: September 05, 2024, 10:20:24 am
content: 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_0At 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 theprintf("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:Code:// Command to set measuring range to 80 meterschar setRange[] = {0xFA, 0x04, 0x09, 0x50, 0xA9};// Command to set frequency to 5 Hzchar setFrequency[] = {0xFA, 0x04, 0x0A, 0x20, 0xF3};// Command to set resolution to 1 mmchar setResolution[] = {0xFA, 0x04, 0x0C, 0x01, 0xF5};void setup() {Serial.begin(115200); // Start USB serial communicationSerial1.begin(9600); // Start UART1 with baud rate 9600pinMode(LED_BUILTIN, OUTPUT);// Send commands to configure the sensorSerial1.write(setRange, sizeof(setRange));delay(100); // Small delay to ensure the command is processedSerial1.write(setFrequency, sizeof(setFrequency));delay(100); // Small delay to ensure the command is processedSerial1.write(setResolution, sizeof(setResolution));delay(100); // Small delay to ensure the command is processed}void loop() {char buff = {0x80, 0x06, 0x03, 0x77}; // Example command to request distance measurementunsigned char data = {0};Serial1.write(buff, sizeof(buff));while (1) {digitalWrite(LED_BUILTIN, LOW);if (Serial1.available() > 0) { // Check if there's data to readdelay(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 dataSerial.print("Raw Data: ");for (int i = 0; i < 11; i++) {Serial.print(data, HEX); // Print each byte in hexadecimal formatSerial.print(" ");}Serial.println();if (data == Check) {if (data == 'E' && data == 'R' && data == 'R') {Serial.println("Out of range");} else {float distance = 0;distance = (data - 0x30) * 100 + (data - 0x30) * 10 + (data - 0x30) * 1 +(data - 0x30) * 0.1 + (data - 0x30) * 0.01 + (data - 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: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); // TXgpio_set_function(1, GPIO_FUNC_UART); // RXgpio_init(LED_PIN);gpio_set_dir(LED_PIN, GPIO_OUT);}int main() {setup();// Commands to send to the rangefinderchar setRange[] = {0xFA, 0x04, 0x09, 0x50, 0xA9};char setFrequency[] = {0xFA, 0x04, 0x0A, 0x05, 0xF3};char setResolution[] = {0xFA, 0x04, 0x0C, 0x01, 0xF5};// Send commandsuart_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 distanceuint8_t data = {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 == Check) {if (data == 'E' && data == 'R' && data == 'R') {printf("Out of range\n");} else {float distance = 0;distance = (data - '0') * 100 + (data - '0') * 10 + (data - '0') +(data - '0') * 0.1f + (data - '0') * 0.01f + (data - '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);}}Code:CMakeLists.txt:cmake_minimum_required(VERSION 3.12)include(pico_sdk_import.cmake)project(RangefinderProject)pico_sdk_init()add_executable(rangefindertestrangefindertest.c)# Link the Pico SDK librariestarget_link_libraries(rangefindertestpico_stdlibhardware_uart)pico_enable_stdio_usb(rangefindertest 1)pico_enable_stdio_uart(rangefindertest 1)# Create map/bin/hex/uf2 filespico_add_extra_outputs(rangefindertest)

author: brucehoult
author_rank: Super Contributor
timestamp: September 05, 2024, 10:23:56 am
content: Don't post the same thing in two different places!Put your code in github, or similar, not in the post.

author: pcprogrammer
author_rank: Super Contributor
timestamp: September 05, 2024, 10:39:40 am
content: And if you feel the need to add the code to your post in a readable manner use the code button to mark it as a section so it does not flood the page.