Example 1:
The Arduino IDE and STM32F4-DISCOVERY LEDs

In this first code example I will demonstrate how to light up the four “user” LEDs on the STM32F4-DISCOVERY board.  This example not only serves as a how-to for interfacing with the on-board LEDs, but also as an introduction to using the Arduino development environment.  Subsequent examples will revolve only around the tasks outlined in that particular example.  If you need help on compiling and uploading the code in other examples, please fall back to this article.

The STM32F4-DISCOVERY has four “user” LEDs connected to the STM32 microcontroller (MCU).  The LEDs are located just below the main chip.  Starting at the top and going in a clockwise motion, the colors are orange, red, blue, green and the connections are PD13, PD14, PD15, PD12, respectively.  (See the LEDs table below.)  The LEDs can be illuminated with a active high (“1”) signal on the corresponding MCU’s pin.

LEDs
MCU PINNAMEVALID STATECOLORPOSITION
PD13LD3HIGHORANGEUP
PD14LD5HIGHREDRIGHT
PD15LD6HIGHBLUEDOWN
PD12LD4HIGHGREENLEFT

As with most of the STM32 examples, we will be using the Arduino development environment.  This is a very easy to use system which will allow us to code and build our projects quickly and easily.  For more information on setting up the Arduino software for the STM32F4-DISCOVERY, see the STM32duino Setup page.

In this first example, we will enter, compile and upload code that will make the four LEDs flash once a second in a clockwise motion.

  1. Connect your STM32F4-DISCOVERY board to your computer using the Mini USB port at the top of the board.  (Do not use the Micro USB port at the bottom of the board.  That port is reserved for other functions, not programming.)
    STM32F4-DISCOVERY_ProgramPort
  2. Start up the Arduino application.
    ArduinoIcon
  3. Configure the Arduino application to use the STM32F4-DISCOVERY board.
    – Click on the “Tools” menu and then “Board:”.
    ArduinoToolBoardMenu
  4. Select the entry “STM32 Discovery F407”.  (It will probably be located near the bottom of the list.)
    ArduinoSTMF32BoardSelect
    – Verify the correct communication port for the board has been selected by going to the “Tools” and then “Port:” and selecting the appropriate entry.  (My board shows up as “COM1” but can vary depending on you computer’s hardware configuration.)
    ArduinoComSelect
  5. Enter the following code into the Arduino window.
    /*
     * STM32F4-DISCOVERY
     * Example 1:  LEDs
     */
     
    void setup() {
      // put your setup code here, to run once:
      
      // Set the MCU's pin data direction.
      pinMode(PD13, OUTPUT);
      pinMode(PD14, OUTPUT);
      pinMode(PD15, OUTPUT);
      pinMode(PD12, OUTPUT);
    
      // Set all outputs LOW to have all LED's initially turned off.
      digitalWrite(PD13, LOW);
      digitalWrite(PD14, LOW);
      digitalWrite(PD15, LOW);
      digitalWrite(PD12, LOW);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
      // Turn off the GREEN LED, turn on the ORANGE LED and wait for 1000ms.
      digitalWrite(PD12, LOW);
      digitalWrite(PD13, HIGH);
      delay(1000);
      
      // Turn off the ORANGE LED, turn on the RED LED and wait for 1000ms.
      digitalWrite(PD13, LOW);
      digitalWrite(PD14, HIGH);
      delay(1000);
    
      // Turn off the RED LED, turn on the BLUE LED and wait for 1000ms.
      digitalWrite(PD14, LOW);
      digitalWrite(PD15, HIGH);
      delay(1000);
    
      // Turn off the BLUE LED, turn on the GREEN LED and wait for 1000ms.
      digitalWrite(PD15, LOW);
      digitalWrite(PD12, HIGH);
      delay(1000);
    
      // The code in the "loop()" statement will automatically repeat.
    }
  6. Click on the ArduinoVerifyToolIcon(check mark) icon in the toolbar to compile and verify the code.  (If the compile was successful, you should see a message “Done compiling” in the green bar intermediately below the code.)
    ArduinoDoneCompiling
  7. Click on the ArduinoUploadToolIcon (right arrow) icon in the toolbar to upload the code to the board.  (If the upload was successful, you should see a message “Done uploading” in the green bar intermediately below the code.)
    ArduinoDoneUploading

After the code is successfully uploaded to the board, it should automatically start executing and you should see the four LEDs blinking in a clockwise manner.