Program STM32F4 with UART

Posted on September 17, 2014 at 11:24 PM

STM32F4 devices have great feature. They can be programed with only USB to UART converter. Each device has bootloader inside, which supports UART programming. This is not very great method for Discovery boards, because they have better and faster solution on board, ST-link. But if you are working own board, then this solution can be quite nice.

In this tutorial, I will go through step-by-step how to program device with USB->UART converter. I will use FTDI’s home made converter.

This will work on all STM32F4 boards (Discovery, Nucleo). Also, if you create own board. Continue reading

Library 37- BMP180 pressure sensor for STM32F4

Posted on September 17, 2014 at 4:11 PM

BMP180 is a digital pressure sensor, with builtin temperature sensor. Its range is between 300 and 1100 hPa (0.3 to 1.1Bar). In relating to the sea level, this is +9000 to -500 meters.

Sensor works with I2C communication at 100kHz.

If you want sensor from ebay, here is one link.

Sensor is quite bad created, because if you want to read pressure, you first need to read current temperature for pressure calculation. To read temperature you have to wait at least 4.5ms. After that, you first have to read temperature and then you can start with pressure measurement. Time for pressure measurement depends on mode you select. After pressure is measured, you can read uncompensated data from device. This is because device has one register for control temperature and pressure, and also one register for data. So both, temperature and  pressure can not be measured at same time.

This takes about at least 10ms for every stuff to be done. At the end a lot of calculations are needed to get valid pressure from device.

Because veeery long time for STM32F4, I split things into several functions, to start temp, read temp, start pressure, read pressure, etc.

I have also added possibility, that you calculate pressure right above the sea from known pressure at known altitude.

Sequency for reading useful data is:

  • Start temperature measurement
    • Wait 4.5ms at least
  • Read temperature
  • Start pressure measurement
    • Delay depends on measurement mode you select
  • Read pressure
  • Use pressure data



  • Read BMP180 temperature sensor
  • Read BMP180 pressure sensor
  • Calculate sea level pressure at from given know pressure and altitude
    • If you are at ex 1000meters and there si pressure 98000Pascals, then you can calculate pressure at sea level from this values

Continue reading

Library 36- DAC Signal generator for STM32F4

Posted on September 16, 2014 at 12:42 PM

For FFT project purpose, I needed simple really simple signal generator. I don’t have separate device at home, so I made on with STM32F4.

DAC Signal library uses timer for output generation and DMA for transferring data from memory to DAC peripheral. So this library does not use processor for controlling. Everything is behind the scenes.

You can use 4 different, really simple signal waves:

  1. Square
  2. Sawtooth
  3. Triangle
  4. Sinus

They are really simple, predefined signals. You can adjust signal frequency for specific wave. I tested some frequencies, and sinus worked just well at about 100kHz, but square works great to about 50kHz. Square wave has 50% duty cycle. If you need variable square wave duty duty cycle, then you should look at my PWM library or PWM tutorial.

This library is not a real signal generator, sinus signal is not going to negative. Signals have fake ground at the middle of DAC max value, so 2047 is fake ground, and sinus is going from 0 to 4095. If you want “negative” effect, then you should add a capacitor in series to the output, this will allow only AC component to go through. Capacitor varies between your signal frequency, but about 680nF should be just OK.



  • Supports 4 different simple predefined waves
  • Supports 2 DAC channels
  • Timer driven DMA
    • TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8 can be used

Continue reading

Library 35- LIS302DL or LIS3DSH accelerometer

Posted on September 15, 2014 at 10:36 PM

Maybe you notice one chip between four leds on STM32F4-Discovery board. This chip is accelerometer. On market, there are 2 versions of STM32F4-Discovery board. First release had LIS302DL (old board) and new release (current) has LIS3DSH device. LIS3DSH has 5 selectables full scales (2/4/6/8/16G), old LIS302DL has only 2 (2.3/9.2G).

I made i simple library which support both devices. Library automatically recognized connected device, if there is any. You just have to check which is on board, because different devices have different possible settings. Both devices uses the same pinout on board. They use SPI for communication with STM32F4.

I have a new version of STM32F4-Discovery board, so LIS3DSH is in use. In that case i had to find someone who will test this for me. I found one, he is also very good with STM32F4 devices. His website is here.



  • Supports LIS302DL accelerometer
    • old STM32F4-Discovery boards
  • Supports LIS3DSH accelerometer
    • new STM32F4-Discovery boards
  • Supports sensor detection

Continue reading

Library 34- STM32F4 as USB HID Device

Posted on September 13, 2014 at 5:13 PM

With USB HID Device library, you can turn STM32F4 to be a keyboard, mouse or gamepad device. It also supports all three settings at the same time. Device is shown to computer as “Keyboard; Mouse; Game controller“. This library allows you to use 2 gamepads at the same time, one keyboard and one mouse.

For mouse you can use left, middle and right buttons, X and Y cursor axes and wheel vertical rotation.

For gamepads, you can implement 16 buttons and 2 joystick, like on PS2 gamepad device.

Also, if you use keyboard, you have enabled all special keys (SHIFT, CTRL, ALT and GUI), in combination with other keys.

You can of cource make a device, which implement everything listed here in one piece. Your imagination is the limit.



  • Interface STM32F4 with computer as keyboard, mouse or gamepads
  • Supports up to 2 gamepads, 1 keyboard and 1 mouse
  • Keyboard supports all special keys
  • Mouse supports 3 main buttons, cursor movement and wheel vertical rotation
  • Each of 2 gamepads supports up to 16 buttons and 2 joysticks
  • Library works in USB FS or USB HS in FS mode
  • Just plug USB in computer and works. No need for drivers

Continue reading