Project 03- STM32F4xx PID controller

Posted on November 22, 2014 at 10:57 PM

CMSIS files from ARM provides ARM Math functions. There are also PID controller functions in different formats for f32, q31 and q7. This tutorial/project will talk about how to implement PID controller on STM32F4xx using PID functions from ARM.

PID Controller

Fast about PID controller. PID stands for Proportional-Integral-Derivative controller. This is a control loop feedback mechanism widely used in industrial control systems. It calculates the error between measured value and the desired setpoint value. According to the error, it then calculates output value to minimize this error.

PID Controller block diagram

PID Controller block diagram

Continue reading

Project 02- Snake game for STM32F429-Discovery board

Posted on November 16, 2014 at 1:08 PM

After first FFT project, I’ve decided to make a simple snake game. Basically I’ve made it in Labview first for my partial exam at the university. It looked great so why not port it also to STM32F429-Discovery board which has everything already prepared for us.

You just need USB OTG cable and external keyboard to drive keyboard and you are ready to work.

Snake game


  • Works on STM32F429-Discovery board
  • Supports external keyboard using USB HID Host stack on STM32
  • Supports selecting different speeds
    • More speed, more points
  • Standard snake game mode
    • Your goal is to make snake larger as possible
  • Supports 2 game modes
    • Mode 0: If you hit wall, game is stopped
    • Mode 1: If you hit wall, snake will come out at the other side of game area
  • Supports pause mode

Continue reading

Library 43- MPU-6050 6-axes gyro and accelerometer for STM32F4

Posted on October 28, 2014 at 9:51 PM
MPU-6050 6-axes gyroscope and accelerometer

MPU-6050 6-axes gyroscope and accelerometer

MPU-6050 is a 3-axes accelerometer and 3-axes gyroscope MEMS sensor in one piece. It can also measure temperature. It is very cheap device but also very powerful. It can measure simultaneously three (X, Y and Z) channels for accelerometer and gyroscope at the same time with 16-bit resolution. This chip is also compatible with MPU9150, except that MPU9150 has 3axes magnetometer (or compass) included.

MPU-6050 Features

  • Accelerometer
    • 4 selectable full scales (2G, 4G, 8G and 16G)
  • Gyroscope
    • 4 selectable full scales (250°, 500°, 1000° and 2000°)
  • Temperature sensor
    • 1°C accuracy
  • I2C driven
    • Selectable LSB bit for I2C address, used with AD0 pin
      • AD0 low: I2C address is 0xD0
      • AD0 high: I2C address is 0xD2
    • Max SCL speed is 400kHz



  • Read accelerometer data
  • Read gyroscope data
  • Read temperature
  • Read everything above in one single function or separatelly
  • Use 2 independent MPU-6050 devices on the same I2C bus
    • Devices must have different AD0 pin state

Continue reading

Library 42- Control RC servo with STM32F4

Posted on October 26, 2014 at 11:28 PM

Library 42 allows you to simple connect and interface RC servo motors with STM32F4. They are small, cheap motors, commonly used for RC (Radio Control), small scale robotics and more.

Servos are really simple to interface. They expect 50Hz signal on the input. According to the pulse length (duty cycle) they rotate between 0 and 180 degrees. If signal has 1ms pulse length, motor rotation will be 0 degrees and if signal has 2ms length, then it will have 180degrees rotation. Any pulse length between will set degrees linear to the value.

Equation for pulse length to get specific rotation is:

Pulse length (degrees) = (MAX – MIN) * degrees / 180 + MIN


  • MAX: maximum pulse length for servo, 2000us
  • MIN: minimum pulse length for servo, 1000us

If you want to get rotation in degrees from know pulse length:

Degrees (pulse_length) = (pulse_length – MIN) * 180 / (MAX – MIN)



  • Control RC servo motors
  • Number of available servos depends on STM32F4xx chip you will use
    • More timers means more servos possible

Continue reading

STM32F4 FFT example

Posted on October 23, 2014 at 9:22 PM

As you maybe know, STM32F4 is Cortex M4 with DSP instructions. This allows you to make a FFT with a few simple steps. For that purpose, I have made an example, on how to create FFT with STM32F4.

When the ARM company issued Cortex-M4 core, it also published DSP libraries for mathematics and other stuff. And there are also FFT functions. When you’ve downloaded ST’s standard peripheral drivers, you also downloaded CMSIS (Cortex Microcontroller Software Interface Standard), which are designed for all Cortex-M4 processors from every company.

Note: Tutorial below for Keil DSP does not work anymore with my project. For that purpose I’ve update my project and include all DSP libraries inside. All other libraries are also included in project.

CMSIS libraries are also included in Keil uVision (5 and newest), you just need to enable them. Under “Manager Run-Time Environment” -> CMSIS select DSP. DSP or Digital Signal Processing is a library for “high mathematics” instructions, which are supported by Cortex-M4 with floating point unit.

Enable DSP library in Keil uVision

Enable DSP library in Keil uVision

Fast Fourier Transform – FFT

Very fast about FFT. FFT or Fast Fourier Transform is an algorithm to convert time based signal into frequency domain. In other words, you are able to know from which sinus components is some signal created.

Everything about FFT is described on Wikipedia.

Continue reading