1

I have compiled code to transmit data from an STM32 board to the Arduino Uno and print it on the serial terminal. I have previously written code, similar to this and it worked as expected.

However, when I try transmitting a string from the STM32 this time, the TX-Led of the Arduino blinks, which is strange.

I have checked the baud rate of both devices and they match. On the serial terminal of the Arduino, backward question marks are printed when I am transmitting a short string "Test".

Please could someone help me identify any potential problems with what I have done.

The code for the STM32 UART settings was generated by cubeMX:

    static void MX_USART1_UART_Init(void)
    {
      huart1.Instance = USART1;
      huart1.Init.BaudRate = 115200;
      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();
      }
    }

The string and HAL_UART instruction to transmit the data:

    char let[30] = "Test\r\n";

    HAL_UART_Transmit(&huart1, (uint8_t*)let, strlen(let), 1000);

All necessary functions (MX_USART1_UART_Init()) have been called and the HAL_UART_Transmit() is in the while loop, situated in the main loop, as I have cross-checked that.

My arduino program:

void setup() {
  Serial.begin(115200);
  
  // Define the LED pin as Output
  pinMode (13, OUTPUT);
      
  Serial.println("Arduino UART Receiver");
  Serial.println("-----------------------------");
}
    
void loop() {
  digitalWrite(13, LOW); 
  //wait until something is received
  while(! Serial.available());
  digitalWrite(13, HIGH); 
  //read the data
  char in_read=Serial.read();
  //print the data
  Serial.print(in_read);
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
S23
  • 119
  • 12
  • How did you connect them together? Is it STM32 Board -> Arduino Uno -> PC ? – Tagli Jan 14 '21 at 09:39
  • @Tagli I have my stm32 board connected to the pc via a STM32F407 Discovery board for debugging. The arduino is connected directly to the pc. Between both devices, I have a single jumper from STM32-TX to arduino-RX. – S23 Jan 14 '21 at 09:56
  • Did you do anything to make sure that the 3V3 stm32 can communicate with the 5V arduino? – Tarick Welling Jan 14 '21 at 13:06
  • @TarickWelling I have tried to use the 5v and GND of the arduino, to power the stm32 board. I created a voltage divider between the tx pin of the stm32 and rx pin of the arduino, holding the rx pin at approximately 3.3V. No data was transmitted at all. – S23 Jan 14 '21 at 19:48
  • If you have powered the STM32 at 5V, then it is broken. TX is output, RX is input. If you divide the 3v3 it will become lower and thus most definitely not trigger the 5V high level – Tarick Welling Jan 15 '21 at 10:23

1 Answers1

3

Arduino Uno has only one USART hardware, and it's connected to a USART-USB converter chip. This bus is point-to-point and can't be shared by another device when there is an active connection between Arduino and PC.

The RX pin of the Arduino is driven by two devices: STM32 and the converter chip. Converter tries to keep that line at logic high (because the TX coming from the PC is idle) while STM32 tries to drive that line with its own TX. Anything can happen there... Arduino is probably getting junk from its RX line.

If you can somehow disconnect Arduino RX from the converter chip (cutting the trace for example) it will probably work. But then you won't be able to program Arduino again.

You may use a software serial pin instead of the hardware RX pin. I don't have much experience with Arduino but I'm sure there must be software serial libraries out there.

Tagli
  • 2,412
  • 2
  • 11
  • 14
  • 2
    +1 for `RX pin of the Arduino is driven by two devices`. Even the author fixed that, I think he also needs a 5V-3.3V converter to connect TX pin of STM32 to RX pin of Arduino. The high logic of Arduino is 5V, while the high logic of STM32 is 3.3V. – dustin2022 Jan 14 '21 at 10:29