Skip to content

Reza-shojaei/stm32-serial-debug-without-ttl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

🛠️ STM32 Serial Debug via ST-Link + Arduino (No USB-TTL Needed)

This project shows how to debug STM32F103 (e.g., Blue Pill) without a USB-to-TTL adapter, using only:

  • ✅ ST-Link V2 (for programming/flashing)
  • ✅ Arduino Uno (as a USB-Serial bridge)
  • ✅ A few jumper wires

🎯 Goal

💡 Redirect printf() or debug messages from your STM32 firmware over UART, and see them on your PC, without needing extra hardware like an FTDI module.


🧠 Why This Is Useful

STM32 boards usually lack built-in USB-to-Serial support. So to view serial debug output (e.g., from printf()), you typically need a USB-TTL adapter.

This method removes that need, using:

  • 🧩 STM32 USART2 (PA2) for serial output
  • 🔌 Arduino Uno to receive and forward data to your PC
  • 🧵 ST-Link V2 only for flashing/debugging (no UART function)

Bonus: You can keep other UARTs (like USART1, USART3) free for modules like GPS, Bluetooth, etc.


🔌 Hardware Connections

STM32F103C8T6 Arduino Uno Function
PA2 (TX) D0 (RX) Serial from STM32
GND GND Common ground
ST-Link (SWD) Flashing / Debug only

⚠️ Do not connect PA3 or Arduino TX — only PA2 to D0 is needed.


📦 STM32 Code (HAL)

Redirect printf() to USART2 like this:

int __io_putchar(int ch) {
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

Then use printf() anywhere in your code:

printf("Hello from STM32\n");

Make sure USART2 (PA2/PA3) is correctly initialized with CubeMX or manually.


📟 Arduino Sketch

Upload this code to Arduino Uno:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    Serial.write(c); // Echo to Serial Monitor
  }
}

🧠 Arduino acts as a Serial "passthrough": receives data from STM32 and echoes it to the Serial Monitor.


🔥 Extra Benefit

You can use other USARTs (e.g., USART1, USART3) for communication with:

  • 🌐 WiFi Modules (ESP8266, ESP01)
  • 📡 GPS Modules (SIM808, NEO-6M)
  • 📲 Bluetooth (HC-05)
  • 📶 LoRa, RF, and more...

While using USART2 (or any you choose) solely for debugging/logging via printf().


🧪 Serial Monitor Output

When STM32 runs:

printf("Temp = %.2f\n", temp);

Arduino Serial Monitor shows:

Temp = 23.45

🧯 Troubleshooting

Issue Solution
Garbage characters Baud rate mismatch or voltage issue
Nothing displayed Check wiring (TX→RX), baud, GND
Sketch upload fails Disconnect Arduino D0 (RX) during upload
STM32 TX not working Check PA2 is in AF Push-Pull mode

🎨 Diagram

STM32F103 (PA2) ---> Arduino Uno (D0/RX) ---> PC via USB
         |
        GND
         |
     Arduino GND

🛠️ Tools Used

  • 🧠 STM32F103C8T6 ("Blue Pill")
  • 🔌 ST-Link V2 (clone)
  • ⚡ Arduino Uno (any version)
  • 💻 STM32CubeIDE or Keil
  • 🛠️ Arduino IDE
  • 🧵 Jumper wires (3 total)

📃 License

This repo is open-sourced under the MIT license.
Feel free to fork, adapt, and improve ✨


💬 Questions?

Feel free to open an Issue or contact me if you need help.

About

Debug STM32 printf() output using Arduino as a serial bridge — no USB-TTL required.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published