Skip to main content
Associate II
Oct 16, 2024 11:36 AM UTC
Solved

ultrasonic A02YYUW not working in STM32L443VCT6

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.
roystm321Best answer
Associate II
Oct 16, 2024 12:42 PM UTC

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!  

10 replies

Tesla DeLorean
Guru
Oct 16, 2024 11:53 AM UTC

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..

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Super User
Oct 16, 2024 12:24 PM UTC

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

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
roystm321AuthorBest answer
Associate II
Oct 16, 2024 12:42 PM UTC

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!  

Andrew Neil
Super User
Oct 16, 2024 1:48 PM UTC

@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

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
Oct 16, 2024 2:03 PM UTC

@Andrew Neil wrote:

 

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

Maybe this (from stm32f0xx_hal_uart.c, but probably widespread):

 

/**
 * @brief This function handles UART Communication Timeout. It waits
 * until a flag is no longer in the specified status.
 * @param huart UART handle.
 * @param Flag Specifies the UART flag to check
 * @param Status The actual Flag status (SET or RESET)
 * @param Tickstart Tick start value
 * @param Timeout Timeout duration
 * @retval HAL status
 */
HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
 uint32_t Tickstart, uint32_t Timeout)
{
 /* Wait until flag is set */
 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
 {
 /* Check for the Timeout */
 if (Timeout != HAL_MAX_DELAY)
 {
 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))

 

So, if Timeout == HAL_MAX_DELAY, the code does not do any timeout at all.

@STTwo-32 At the very least, the documentation needs to be updated.

Better would be change the name to actually reflect what it does; eg. instead of

 

#define HAL_MAX_DELAY 0xFFFFFFFFU

 

Have something like:

 

#define HAL_NO_DELAY 0xFFFFFFFFU

#define HAL_MAX_DELAY (HAL_NO_DELAY-1)

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
roystm321Author
Associate II
Oct 16, 2024 2:19 PM UTC

Ok i will try it, thanks