STM32 microcontrollers and pcb design

Posted on October 25, 2022 by Zach Lambert ‐ 2 min read

STM32 microcontrollers and pcb design

Alongside developing my robotics framework, I wanted to build a simple mobile robot (similar to turtlebot) to experiment with.

The plan was to use:

  • A raspberry pi (3b+) as the main computer.
  • Use a cheap single channel lidar for observing the environment.
  • Use a differential-drive design, which velocity feedback, torque feedback, a gyroscope and an accelerometer.
  • Design a pcb with a microcontroller to interface with the actuators and sensors, and do low-leve, high frequency control and state estimation.

For the pcb, I wanted to make an improvement over previous electronics projects, so had the following goals:

  • Use stm32 microcontrollers with RTOS
  • Design a 4-layer pcb with smd components, and put more thought into “proper design”.
  • Provide a usb interface for the commands and feedback.

To use with my robotics framework, the interface to the board would be a single “component”, that reads input commands, and outputs commands. Additionally, it could read/write configuration to the board, such as controller gains, etc.

To design the pcb, I used as reference a few youtube videos from Phil’s lab, who has really good videos on designing stm32 pcbs in kicad in particular.

Setting up a workflow for STM32 boards

Initially, I used stm32-cube ide, which had handy code generation tools. However, I had a few problems with it:

  • Everything was tightly coupled with stm32-cube ide. The api was overly complex, such that the only practical solution was to use the code generation.
  • I wanted to use CMake, and it was difficult to setup the compiler pipeline.

Instead, I changed the HAL I used to libopencm3. This also didn’t use CMake, but it was much simpler and easier to work out how to port to CMake.

Following this, I got RTOS working with libopencm3, and ported to CMake.

To make this easier to use, I put all the relevant CMake code in a separate github repo: stm32-collection

The “stm32-collection” repo includes:

  • Relevant CMake code, allowing “find_package(stm32-collection)”
  • Automatically sets up the project for a given stm32 board, for use with C or C++.
  • Automatically sets up RTOS, provided a given include directory is provided for the STM32 config header.
  • Provides a lightweight printf library.

Designing a breakout board

In addition to the main base controller pcb, I designed a breakout board for the particular microcontroller I was using, which would allow me to test things with a breadboard in case the main pcb didn’t work for whatever reason.

Base PCB

Below shows the finished pcb, which on testing, could be programmed and read/write data over usb.

Base pcb