Skip to main content
Explorer
October 16, 2024
Solved

ultrasonic A02YYUW not working in STM32L443VCT6

  • October 16, 2024
  • 4 replies
  • 1919 views

Hi,

I try to comuunicate between STM32L443VCT6  and ultrasonic A02YYUW (By UART1) 

Just to make sure that the ultrasonic is working ok, i tested it by UART1 with model: stm32 neucler f441 and everthing works ok.

 

This is the code for the STM32L443VCT6:

 

 

 

#include "main.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include <stdbool.h>

UART_HandleTypeDef huart1;

uint8_t data[4] = {0}; // Buffer to hold the 4 bytes from the sensor
float distance = 0.0;

void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_USART2_UART_Init(void);

int main(void) {
 HAL_Init();
 SystemClock_Config(); // Use internal oscillator (MSI)
 MX_GPIO_Init();
 USART1_UART_Init();




 /* Infinite loop */
 while (1) {
 /* Wait for the start corrector (0xFF) */
 do {
 HAL_UART_Receive(&huart1, &data[0], 1, HAL_MAX_DELAY);
 } while (data[0] != 0xFF);

 /* Clear the data array before receiving new data */
 //clearDataArray();

 /* Start receiving 3 more bytes of data */
 for (int i = 1; i < 4; i++) {
 HAL_UART_Receive(&huart1, &data[i], 1, HAL_MAX_DELAY); // Blocking receive
 }

 /* Process the sensor data */
 processSensorData();

 HAL_Delay(100); // Delay for sensor refresh (similar to Arduino delay)
 }

 /* Infinite loop */
 while (1) {
 // Keep running if needed, or you can add additional functionality
 }
}



void processSensorData(void) {
 /* Validate the checksum */
 int sum = (data[0] + data[1] + data[2]) & 0x00FF;
 // if (sum == data[3]) {
 /* Calculate the distance */
 distance = (data[1] << + data[2]; // Combine the two bytes into a 16-bit value
 distance = distance / 10.0; // Convert distance to cm

 /* Blink the LED to indicate valid data */

 if(distance<10){

 	HAL_Delay(500);
 }
 // } else {
 // Handle checksum error (optional)
 //}
}



/* GPIO Initialization Function */
 void MX_GPIO_Init(void) {
 GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* Enable GPIO clock for PA9 and PA10 (USART1 TX/RX pins) */
 __HAL_RCC_GPIOA_CLK_ENABLE();

 /* Configure PA9 (USART1 TX) */
 GPIO_InitStruct.Pin = GPIO_PIN_9;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF7_USART1; // AF7 for USART1
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 /* Configure PA10 (USART1 RX) */
 GPIO_InitStruct.Pin = GPIO_PIN_10;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF7_USART1; // AF7 for USART1
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}


 void USART1_UART_Init(void) {
 /* Enable USART1 clock */

 	 /* GPIO Ports Clock Enable */
 	 __HAL_RCC_GPIOC_CLK_ENABLE();
 	 __HAL_RCC_GPIOA_CLK_ENABLE();
 	 __HAL_RCC_GPIOB_CLK_ENABLE();
 	 __HAL_RCC_GPIOE_CLK_ENABLE(); // Enable clock for Port E
 	 __HAL_RCC_GPIOD_CLK_ENABLE();

 __HAL_RCC_USART1_CLK_ENABLE();

 /* Initialize USART1 */
 huart1.Instance = USART1;
 huart1.Init.BaudRate = 9600;
 huart1.Init.WordLength = UART_WORDLENGTH_8B;
 huart1.Init.StopBits = UART_STOPBITS_1;
 huart1.Init.Parity = UART_PARITY_NONE;
 huart1.Init.Mode = UART_MODE_TX_RX;
 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart1.Init.OverSampling = UART_OVERSAMPLING_16;

 if (HAL_UART_Init(&huart1) != HAL_OK) {
 Error_Handler(); // Handle initialization error
 }
 }


 /* System Clock Configuration */
 void SystemClock_Config(void) {
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 // Configure the main internal regulator output voltage
 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) {
 Error_Handler();
 }

 // Initializes the RCC Oscillators according to the specified parameters
 // Here we use the internal Multi-Speed Internal (MSI) oscillator
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
 RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; // MSI range 6 corresponds to 4 MHz
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
 RCC_OscInitStruct.PLL.PLLM = 1;
 RCC_OscInitStruct.PLL.PLLN = 40;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
 Error_Handler();
 }

 // Initializes the CPU, AHB, and APB buses clocks
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
 Error_Handler();
 }

 // Enable MSI Auto-calibration
 HAL_RCCEx_EnableMSIPLLMode();
 }



/* Error Handler */
void Error_Handler(void) {
 __disable_irq();
 while (1) {
 // Blink the LED to indicate an error

 HAL_Delay(500);
 }
}

 

 

    This topic has been closed for replies.
    Best answer by roystm321

    Thank Everyone for you fast response,

    I Solved it, i do not understand why..., but the "HAL_MAX_DELAY" blocked the receiveing data,

     

    So i modify it to 300ms and now i get response from the ultrasonic, 

    There is a tiny glitch, that seldom. the sensor not detect the correct value.

     

    Is there any settings in the code i sent, that i need to pay attention?

     

    Thanks!  

    4 replies

    Graduate II
    October 16, 2024

    Post not terribly enlightening..

    How's this all wired up, show a schematic or wiring diagram.

    Describe expected behaviour. Show salient portion of datasheet. Link to data sheet.

    What do you actually observe?

    Any data? The wrong data? Looked at signals with a scope or logic analyzer?

    Any helpful observations you can share?

    "Not Working" is an unhelpful diagnosis / symptom.  What's happening vs what's not..

    Super User
    October 16, 2024

    As @Tesla DeLorean said, your post is not very clear.

    Are you saying that you have this working on an ST Neucleo-F441 board, but not on your own custom board, with an STM32L443VCT6?

    If so, you'll need to

    1. Confirm that this is the "ultrasonic A02YYUW": 
      https://wiki.dfrobot.com/_A02YYUW_Waterproof_Ultrasonic_Sensor_SKU_SEN0311 
    2. Show how you had this connected-up on the Nucleo
    3. Post the schematic of your custom board - including how the "ultrasonic" is connected
    4. Describe what testing you have done on your custom board:
      1. Is the board working at all?
      2. Is the UART transmitting and/or receiving?
      3. etc, ...

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

     

     

    roystm321AuthorAnswer
    Explorer
    October 16, 2024

    Thank Everyone for you fast response,

    I Solved it, i do not understand why..., but the "HAL_MAX_DELAY" blocked the receiveing data,

     

    So i modify it to 300ms and now i get response from the ultrasonic, 

    There is a tiny glitch, that seldom. the sensor not detect the correct value.

     

    Is there any settings in the code i sent, that i need to pay attention?

     

    Thanks!  

    Super User
    October 16, 2024

    @roystm321 wrote:

    the "HAL_MAX_DELAY" blocked the receiveing data,


    Hmmm...,  I've seen that somewhere before but can't quite remember where or what the issue was.

    I think the maximum actual wait is HAL_MAX_DELAY - 1

    Anyhow, as it's solved your problem, please mark the solution:

    https://community.st.com/t5/community-guidelines/help-others-to-solve-their-issues/ta-p/575256

     

    roystm321Author
    Explorer
    October 16, 2024

    It looks like that sometime not all the data in the array comes:

     HAL_UART_Receive(&huart1, &data[i], 1, HAL_MAX_DELAY); 

    and then this prevent from the code continue to run..

    roystm321Author
    Explorer
    October 16, 2024

    Ok i will try it, thanks