cancel
Showing results for 
Search instead for 
Did you mean: 

UART communication

Swastik_Dey
Associate II

Hello everyone, I am trying to make a UART Communication between STM32 NUCLEO-L476RG and ESP32 S3. The STM must send data to ESP and the ESP must print it on Serial Monitor of Arduino IDE
However, this doesn't happen, even though I have double checked the connections.  The code for the same are given below :- 
STM32 NUCLEO-L476RD board code:-

#include "stm32f4xx_hal.h"

UART_HandleTypeDef huart2;

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

int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();

char data_to_send[] = "Hello, PC!";

while (1)
{
HAL_UART_Transmit(&huart2, (uint8_t *)data_to_send, strlen(data_to_send), HAL_MAX_DELAY);
HAL_Delay(1000); // Delay for 1 second (adjust as needed)
}
}

void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}

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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

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

static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}

static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();

GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void Error_Handler(void)
{
while (1)
{
// Error handling
}
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
// User can add their own implementation to report the file name and line number,
// ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
}
#endif


ESP32 S3 WROOM 1 code:-

void setup() {
Serial.begin(115200, SERIAL_8N1); // Set baud rate, data bits, no parity, and 1 stop bit
}

void loop() {
if (Serial.available() > 0) {
char incomingByte = Serial.read();
Serial.print("Received: ");
Serial.println(incomingByte);
}

delay(1000);
}

8 REPLIES 8

Sounds like a homework assignment.

Draw a schematic of how this is wired up.

Check signals with a scope. Perhaps send "UUUU" pattern continously.

Make sure you have the RX and TX sense correct, and a common ground.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I couldn't make a detailed circuit diagram,sorry for the inconvinience,
Here's a simplified version I made. 

 

AScha.3
Chief III

Hi,

Just what i see here and what I would do : 

on stm32 side :

- the send string always should have an "end mark" : (\n) -> char data_to_send[] = "Hello, PC! \n ";

- dont wait forever ... (HAL_MAX_DELAY) , just some time : 

HAL_UART_Transmit(&huart2, (uint8_t *)data_to_send, strlen(data_to_send), 50);

 

on ESP side :

- Serial.begin() -> you know for sure , on which pins this will work then ? ( i dont...)

- Serial.print("Received: ");  where is this coming out ? Dont send here so much - better nothing for now, comment it out :

// Serial.print("Received: ");

 

+ Now look with a scope, what happens on the rx / tx lines .

If you feel a post has answered your question, please click "Accept as Solution".

The problem is, I am setting the default UART pin (PA2 & PA3) as Tx and Rx, but the data is being sent via the USB port. 

 

You seem to be making the classic mistake of trying to do everything all at once - and then wondering why nothing works.

At the moment, you don't know whether the problem is:

  1. Your STM32 not transmitting properly
  2. Your ESP32 not receiving properly from the STM32
  3. Your ESP32 not sending properly to the Arduino Serial Monitor.
  4. Any combination of some of all of the above!

Instead, the key is to take things one step at a time; eg

  1. Get the transmit from your ESP32 to the Arduino Serial Monitor working reliably
  2. Get the receive on your ESP32 working reliably from a PC terminal
  3. Get the transmit from your STM32 working reliably to a PC terminal
  4. Finally, when you have the parts working separately, connect them all together

See also:

https://community.st.com/t5/stm32cubemx-mcus/usart-configuration/m-p/629770/highlight/true#M26974

 

Ooooo - i didnt know this.   You cannot use the uart pins on the nucleo board, that are fix wired to the st-link onboard !

Use another uart (you have MANY here! ) or define some FREE pins (in the ioc edit ) to be used for rx/tx , then it will work.

If you feel a post has answered your question, please click "Accept as Solution".

@AScha.3 wrote:

You cannot use the uart pins on the nucleo board, that are fix wired to the st-link onboard !


Indeed - much like Arduinos!

It's described in the User Manual for the board - including how to break the connection, if you wish:

AndrewNeil_0-1705658666718.png

 


@AScha.3 wrote:

Use another uart (you have MANY here! ) 


Indeed. :thumbs_up:

 

Use this button to properly post source code:

AndrewNeil_1-1705659077391.png

 

To get that extra row of icons, press this button:

AndrewNeil_2-1705659143400.png