Chapter 7

Using the Mbed With Simple Projects

Abstract

This chapter describes in detail the development of simple projects using the Nucleo-F411RE development board with the Mbed integrated development environment. Downloading and registering to use the Mbed environment is described in detail with reference to the Nucleo-F411RE development board. The projects in this chapter range from very simple applications such as flashing one or more LEDs, and using the on-board and external push-button switches to more complex projects using a single digit 7-segment LED and powering and control of loads requiring large currents. The description, aim, block diagram, circuit diagram, and full program listing of all the projects are given with a summary of the chapter and some exercises.

Keywords

Mbed; LED control; Push-button; RGB; 7-Segment LED; DC motor control; Relay; Random number

7.1 Overview

In this chapter we shall be seeing how to use the Mbed integrated development environment (IDE) to write programs, to compile them, and then how to upload them to the program memory of the Nucleo-F411RE development board.

Additionally, we shall be developing simple projects using Mbed, our development board, and some external components. Each project is described in detail under the following subheadings:

  •  Project title
  •  Project description
  •  Aim of the project
  •  Project block diagram
  •  Project circuit diagram (optional)
  •  Project construction (optional)
  •  Project algorithm in PDL (optional)
  •  Project program listing with its description
  •  Suggestions for additional work (optional)

The algorithm (operational steps) of each program in every project is described using the program description language (PDL), explained in detail in Chapter 2.

7.2 Registering to Use Mbed

Mbed is a free online ARM compiler that can be used over an Internet link. It is an IDE platform and operating system based on 32-bit ARM Cortex-M microcontrollers. Mbed is supported by over 60 partners and a community of 200,000 developers. Mbed is a free online IDE consisting of an online code editor, a compiler, and a program upload tool. Only a web browser is required to access Mbed and develop ARM-based programs. Programs are compiled on the Cloud using the ARM C/C ++ compiler. Developing a project using Mbed is very easy since all the user needs is to pick an Mbed supported development board, write the application program, and then upload the program to the board.

The Mbed compiler is easy to use and it supports a large number of ARM processors and software libraries. Using Mbed, the compiled code can easily be uploaded to the program memory of the target ARM microcontroller as a simple drag-and-drop (or copy) operation.

First time Mbed users must register before they can use the Mbed and the target ARM-based development board must be added to the Mbed working environment. The steps to register and add the Nucleo-F411RE development board to the Mbed working environment are given in the following:

  •  Open the following link to create an Mbed login account
    https://os.mbed.com/account/login
  •  Click Signup at the right-hand side and enter your details (Fig. 7.1)
    f07-01-9780081029695
    Fig. 7.1 Mbed registration screen.
  •  Click Hardware -> Boards and then click on picture Nucleo-F411RE (see Fig. 7.2)
    f07-02-9780081029695
    Fig. 7.2 Click on picture Nucleo-F411RE.
  •  Click on Add to your Mbed Compiler at the right-hand side to add the board to your working environment (Fig. 7.3)
    f07-03-9780081029695
    Fig. 7.3 Add Nucleo-F411RE to your working environment.
  •  Click Compiler to start the compiler (Fig. 7.4)
    f07-04-9780081029695
    Fig. 7.4 Start the Mbed compiler.

7.3 Nucleo-F411RE Development Board Pin Names

When working with Mbed we need to know the names of the GPIO pins. These names can be obtained by clicking at the top right-hand side of the Mbed environment where it says NUCLEO-F411RE, or alternatively we can get the pin names from the following site:https://os.mbed.com/platforms/NUCLEO-F411RE/

The Arduino connector top left and top right pin names are shown in Figs. 7.5 and 7.6, respectively. Similarly, the ST morpho connector top left and top right pin names are shown in Figs. 7.7 and 7.8, respectively.

Fig. 7.5
Fig. 7.5 Arduino connector top left pin names.
Fig. 7.6
Fig. 7.6 Arduino connector top right pin names.
Fig. 7.7
Fig. 7.7 ST morpho connector top left pin names.
Fig. 7.8
Fig. 7.8 ST morpho connector top right pin names.

7.4 Project 1—Flashing LED

7.4.1 Description

In this project the user LED on the Nucleo-F411RE board is flashed every 2 s.

7.4.2 Aim

The aim of this project is to show how the Mbed can be used to write a program, compile it, and then upload the program to the program memory of the Nucleo development board. Additionally, the program shows how a GPIO port can be used in output mode and also how the user LED on the board can be flashed at regular intervals.

7.4.3 Block Diagram

Fig. 7.9 shows the block diagram of the project.

Fig. 7.9
Fig. 7.9 Block diagram of the project.

7.4.4 PDL of the Project

The PDL of the project is shown in Fig. 7.10

Fig. 7.10
Fig. 7.10 PDL of the project.

7.4.5 Program Listing

The steps to create and compile the program are given in the following:

  •  Login to Mbed site
  •  Click Compiler
  •  Click New -> New Program. Make sure that the Platform is set to NUCLEO-F411RE. Click at Template and select Blinky LED test for the ST Nucleo boards. Give a name to your program, for example, Nucleo_blink_led as shown in Fig. 7.11.
    f07-11-9780081029695
    Fig. 7.11 Create a new program called Nucleo_blink_led.
  •  Expand the Nucleo_blink_led listing in the Program Workspace (at the left-hand side of the screen). Click on main.cpp and then modify the template as shown in Fig. 7.12.
    f07-12-9780081029695
    Fig. 7.12 Program listing.
  •  Click Compile to compile the program. After a successful compilation you should see the message Success! As shown in Fig. 7.13
    f07-13-9780081029695
    Fig. 7.13 Successful compilation.
  •  You should also see a pop-up window asking you to Open or Save the compiled code (see Fig. 7.14). Click Save File to save the compiled binary file in the download area.
    f07-14-9780081029695
    Fig. 7.14 Save the compiled binary file.
  •  We are now ready to upload the compiled binary file to the program memory for our Nucleo board. Connect the Nucleo-F411RE board to the USB port of your PC. You should see a new device called NODE_F411RE on your PC (see Fig. 7.15)
    f07-15-9780081029695
    Fig. 7.15 New device NODE_F411RE.
  •  Drag the compiled binary file (the file is called: Nucleo-blink_led_NUCLEO_F411RE.bin in this example) from the download area and drop it on device NODE_F411RE. You should see the communications LED on the Nucleo board flashing red and green as the upload process continues. Wait until uploading is complete when the communications LED is steady green color.
  •  You should now see the program working and the user LED flashing at 2 s intervals

Note that you can export the program (or the main.cpp file) by right clicking on the program in the Program Workspace.

We can now look at this simple program in some detail. The comment lines at the beginning of the program starts with characters /⁎ and end with ⁎/. The header file mbed.h must be included at the beginning of all our programs so that the various statements used in the program can be recognized by the compiler.

The statement DigitalOut name(pin) configures the specified pin with the specified name as an output. For example, DigitalOut myled(LED1) specifies LED1 to be a digital output with the name myled. Inside the main program an infinite loop is created using the while statement. Inside this loop the user led is turned ON and OFF by sending 1 and 0 to the LED, respectively. The wait(2.0) statement is used to create 2 s of delay between each output.

7.4.6 Other Useful Mbed Functions

Some other related useful Mbed functions are given in the following:

FunctionDescriptionExample
waitwait in seconds (floating point)wait (2.5)
wait_mswait in milliseconds (integer)wait_ms (10)
wait_uswait in microseconds (integer)wait_us (100)
DigitalOutconfigure specified pin as digital outputDigitalOut myled(LED1)
DigitalOutConfigure Port PA_5 as digital outputDigitalOut myled(PA_5)

7.4.7 Suggestions for Additional Work

Modify the program in Fig. 7.12 so that the LED ON time is 2 s, and the OFF time is 0.5 s.

The User LED is connected to bit 5 of Port A (PA_5). Modify the program in Fig. 7.12 by replacing LED1 with PA_5.

7.5 Project 2—LED Flashing as Morse Code SOS

7.5.1 Description

In this project the user LED on the Nucleo-F411RE board is flashed as Morse code SOS. The SOS Morse code is "...---...". In this project, a dot is represented with the LED being ON for 0.25 s (Dot time) and a dash is represented with the LED being ON for 1 s (Dash time). The delay between the dots and dashes is set to 0.2 s (GAP time). This process is repeated continuously after 2 s of delay.

7.5.2 Aim

The aim of this project is to show how for loops can be used in a program. Additionally, the program shows how to use the #define statements in Mbed programs.

7.5.3 Block Diagram

The block diagram of the project is shown in Fig. 7.9.

7.5.4 PDL of the Project

The PDL of the project is shown in Fig. 7.16.

Fig. 7.16
Fig. 7.16 PDL of the project.

7.5.5 Program Listing

The program listing is shown in Fig. 7.17 (program: sos). At the beginning of the program comment lines are used to describe the operation of the program. Then mbed.h is included and user LED is configured as output and is assigned to name LED. The Dot, Dash, and Gap times and also ON and OFF are defined at the beginning of the program. The remainder of the program is executed in an endless loop created using a while statement. Inside this loop, two for loops are used, each looping 3 times. The first for loop flashes the LED 3 times where the ON time is set to Dot. The second for loop flashes the LED 3 times where the ON time is set to Dash. The end result is that the LED flashes quickly 3 times and then flashes slowly 3 times. This process is repeated forever after 2 s of delay.

Fig. 7.17
Fig. 7.17 Program listing.

7.5.6 Suggestions for Additional Work

Modify the program in Fig. 7.17 so that the flashing stops after sending 10 SOS messages.

7.6 Project 3—Flashing an External LED

7.6.1 Description

In this project an external LED is connected to pin 10 of Port C (PC_10) through a current limiting resistor. The project flashes the LED such that the ON time is 0.1 s and the OFF time is 1 s.

7.6.2 Aim

The aim of this project is to show how an external LED can be connected to the Nucleo-F411RE board.

7.6.3 Block Diagram

The block diagram of the project is shown in Fig. 7.18.

Fig. 7.18
Fig. 7.18 Block diagram of the project.

7.6.4 Circuit Diagram

An external LED can be connected in two ways: in current source mode and in current sink mode. In both modes the current drawn or supplied by any Nucleo GPIO pin must not exceed 20 mA.

In current source mode the GPIO pin is connected to the LED through a current limiting resistor. The LED turns ON when logic 1 is applied to the corresponding GPIO pin. As shown in Fig. 7.19, the GPIO pin drives the anode pin of the LED through a current limiting resistor and the cathode pin is connected to the ground. Assuming that the voltage drop across the LED is about 1.8 V and the forward current through the LED is 4 mA, we can calculate the value of the required current limiting resistor as follows:

R=V/I

si1_e

Fig. 7.19
Fig. 7.19 Connecting the LED in current source mode.

Therefore,

R=3.31.8V/4mA=0.375K

si2_e

where 3.3 V is the output voltage of a GPIO pin when the pin is at logic 1. The nearest physical resistor that we can use 390 ohm (we can use a lower resistor value for higher brightness) which gives a current of

I=3.31.8V/390=3.8mA

si3_e

In current sink mode the GPIO pin is connected to the LED as shown in Fig. 7.20, where the anode pin of the LED is connected to the supply voltage (+ 3.3 V) and its cathode is connected to the GPIO pin through a current limiting resistor. The LED turns ON when logic 0 is applied to the corresponding GPIO pin. The value of the required resistor can be calculated as follows:

R=3.311.8V/4mA=0.125K

si4_e

Fig. 7.20
Fig. 7.20 Connecting the LED in current sink mode.

where 1 V is the maximum output voltage when the GPIO pin is at logic 0. The nearest physical resistor that we can use 120 ohm (we can use a lower resistor value for higher brightness) which gives a current of

I=3.311.8V/120=4.1mA

si5_e

In this project the LED is connected in current source mode with a 390 ohm resistor as shown in Fig. 7.21. GPIO pin PC_10 is at ST morpho connector CN7, and the GND pin is at pin 8 of the same connector (see Fig. 5.8).

Fig. 7.21
Fig. 7.21 Circuit diagram of the project.

7.6.5 Project Construction

The project constructed on a breadboard is shown in Fig. 7.22. The LED is connected to the Nucleo board using jumper wires.

Fig. 7.22
Fig. 7.22 Project constructed on a breadboard.

7.6.6 Program Listing

Fig. 7.23 shows the program listing (program: ExtLED). At the beginning of the program header file mbed.h is included. Port PC_10 is configured as output and name LED is assigned to this port pin. Then, the ON and OFF times of the LED are defined, and also ON and OFF are defined. The remainder of the program runs in an endless loop. Inside this loop the LED is flashed with an ON time of 0.1 s and an OFF time of 1 s.

Fig. 7.23
Fig. 7.23 Program listing.

7.6.7 Suggestions for Additional Work

Try using different resistor values and see how the brightness of the LED varies with the resistor values.

7.7 Project 4—Rotating LEDs

7.7.1 Description

In this project 8 external LEDs are connected to PORT C the Nucleo-F411RE development board. The LEDs turn ON/OFF in a rotating manner every second where only one LED is ON at any time. Fig. 7.24 shows the LED pattern.

Fig. 7.24
Fig. 7.24 The LED pattern.

7.7.2 Aim

The aim of this project is to show how external LEDs can be connected to the Nucleo-F411RE board.

7.7.3 Block Diagram

The block diagram of the project is shown in Fig. 7.25. The 8 LEDs are connected to PORT C pins of the Nucleo-F411RE development board.

Fig. 7.25
Fig. 7.25 Block diagram of the project.

7.7.4 Circuit Diagram

Fig. 7.26 shows the circuit diagram of the project. The LEDs are connected to lower byte of PORT A (GPIO pins PC_0 to PC_7). Current limiting 390 ohm resistors are connected in series with each LED in current sourcing mode.

Fig. 7.26
Fig. 7.26 Circuit diagram of the project.

PORT C pins PC_0 to PC_7 are available at the following ST morpho connector pins (see also Fig. 5.8):

GPIO PinST Morpho Connector
PC_0CN7, pin 38
PC_1CN7, pin 36
PC_2CN7, pin 35
PC_3CN7, pin 37
PC_4CN10, pin 34
PC_5CN10, pin 6
PC_6CN10, pin 4
PC_7CN10, pin 19

7.7.5 The Construction

The project was built on a breadboard as shown in Fig. 7.27. Jumper wires are used to make connections between the LEDs and the development board

Fig. 7.27
Fig. 7.27 The project constructed on a breadboard.

7.7.6 The PDL

Fig. 7.28 shows the project PDL where the LEDs turn ON in sequence starting from the LSB bit.

Fig. 7.28
Fig. 7.28 PDL of the project.

7.7.7 Program Listing

The program listing (program: LED8) is shown in Fig. 7.29. At the beginning of the program mbed.h is included in the program. Then the DigitalOut statement is used to configure PORT C pins PC_0 to PC_7 as digital outputs. A while statement is used to create an indefinite loop. Inside this loop a for statement is used to loop 8 times. The loop counter (variable k) is sent to PORT A to turn LEDs ON and OFF as required. One second delay is used between each output.

Fig. 7.29
Fig. 7.29 Program listing.

7.7.8 Modified Program

The program given in Fig. 7.29 can be written using the PortOut statement. This statement groups the bits of the given port. The first parameter of this statement is the port name, while the second parameter when set to 1 specifies which bits of the port will be used. In the modified program listing (program: LED8-2) shown in Fig. 7.30, the second parameter of PortOut is set to 0x00FF so that all 8 lower byte of PORT C are used in the program. Array State defines the values to be sent to the port so that the required LEDs are turned ON. The program runs indefinitely inside a while loop. A for statement is used to extract bits from array State and send these to the port. In this modified version of the program the delay is set to 250 ms.

Fig. 7.30
Fig. 7.30 Modified program.

7.7.9 Another Modified Program

The program given in this project can be modified by using the BusOut statement as shown in Fig. 7.31 (program: LED8-3). The BusOut statement is used to create a group of GPIO pins which can be addressed by a single value. In Fig. 7.31, PORT C pins are grouped as an 8-bit port called LEDS. This port is then accessed by sending a value to it (e.g., LEDS = 1 turns ON the first LED in the port group). 0.5 s delay is inserted between each output.

Fig. 7.31
Fig. 7.31 Another modified program.

7.8 Project 5—Binary Counting LEDs

7.8.1 Description

In this project 8 external LEDs are connected to PORT C the Nucleo-F411RE development board as in the previous project. The LEDs count up in binary every second as shown in Fig. 7.32.

Fig. 7.32
Fig. 7.32 LEDs counting up in binary.

7.8.2 Aim

The aim of this project is to show how external LEDs can be connected to the Nucleo-F411RE board and how a program can be written so that the LEDs count up in binary.

7.8.3 Block Diagram

The block diagram of the project is shown in Fig. 7.25, where 8 LEDs are connected to PORT C pins of the Nucleo-F411RE development board.

7.8.4 Circuit Diagram

The circuit diagram of the project is shown in Fig. 7.26 where the LEDs are connected using current limiting resistors.

7.8.5 The PDL

Fig. 7.33 shows the PDL of the program.

Fig. 7.33
Fig. 7.33 PDL of the program.

7.8.6 Program Listing

The program listing is shown in Fig. 7.34 (program: Counter). At the beginning of the program BusOut statement is used to group the PORT C lower byte into a variable called LEDS, and variable CNT is initialized to 0. The main program is executed in an endless loop using a while statement. Inside this loop the value of CNT is sent to PORT C and CNT is then incremented by 1. This process is repeated after 1 s delay until CNT is greater than 255 (i.e., all LEDS are ON) at which point CNT is reset back to 0.

Fig. 7.34
Fig. 7.34 Program listing.

7.9 Project 6—Random Flashing LEDs

7.9.1 Description

In this project 8 external LEDs are connected to PORT C the Nucleo-F411RE development board as in the previous project. The LEDs turn ON and OFF randomly so that they look like Christmas light. 250 ms delay is inserted in every output.

7.9.2 Aim

The aim of this project is to show random numbers can be generated in Mbed programs.

7.9.3 Block Diagram

The block diagram of the project is shown in Fig. 7.25, where 8 LEDs are connected to PORT C pins of the Nucleo-F411RE development board.

7.9.4 Circuit Diagram

The circuit diagram of the project is shown in Fig. 7.26 where the LEDs are connected using current limiting resistors.

7.9.5 The PDL

Fig. 7.35 shows the PDL of the program.

Fig. 7.35
Fig. 7.35 PDL of the program.

7.9.6 Program Listing

The program listing is shown in Fig. 7.36 (program: Christmas). At the beginning of the program BusOut statement is used to group the PORT C lower byte into a variable called LEDS as in the previous project. An endless loop is created using a while statement. Inside this loop, random numbers are generated between 0 and 255 using function rand(), and these numbers are then sent to PORT C every 250 ms The net effect is that the Les turn ON and OFF as Christmas lights.

Fig. 7.36
Fig. 7.36 Program listing.

7.9.7 Modified Program

The program given in Fig. 7.36 can be modified by making the delay between the outputs to change randomly. This gives the effect of randomly flashing lights with random delays. In the modified program in Fig. 7.37 (program: Christmas-2) the delay between the outputs changes randomly between 0 and 300 ms.

Fig. 7.37
Fig. 7.37 Modified program.

7.10 Project 7—LED Control With Push-button

7.10.1 Description

In this project the user push-button and the user LED on the Nucleo-F411RE development board are used. The LED flashes 3 times when the button is pressed.

7.10.2 Aim

The aim of this project is to show how the user push-button on the Nucleo board can be used.

7.10.3 Block Diagram

The block diagram of the project is shown in Fig. 7.38. The User LED (at PB_13) is named LED1 and the User Button (at PC_13) is named BUTTON1 (see Fig. 5.8). The User Button is normally at logic 1 and it goes to logic 0 when pressed. The User LED turns ON when logic 1 is applied to it.

Fig. 7.38
Fig. 7.38 Block diagram of the project.

7.10.4 The PDL

The PDL of the program is very simple and is shown in Fig. 7.39.

Fig. 7.39
Fig. 7.39 PDL of the program.

7.10.5 Program Listing

The program listing (program: Push) is shown in Fig. 7.40. At the beginning of the program the header file mbed.h is included in the program. The User LED (LED1) is configured as an output using the DigitalOut statement. Similarly, the User Button (BUTTON1) is configured as an input using the DigitalIn statement. The remainder of the program runs in an endless loop setup using a while statement. The program checks if the button is pressed (i.e., button is at logic 0) and if so a for loop is used to flash the LED 3 times.

Fig. 7.40
Fig. 7.40 Program listing.

7.11 Project 8—Changing LED Flashing Rate

7.11.1 Description

In this project the user push-button and the user LED on the Nucleo-F411RE development board are used. The LED flashing rate is changed every time the button is pressed. This is done by changing the delay between the flashes.

7.11.2 Aim

The aim of this project is to show how the user push-button on the Nucleo board can be used.

7.11.3 Block Diagram

The block diagram of the project is shown in Fig. 7.38.

7.11.4 The PDL

The PDL of the program is very simple and is shown in Fig. 7.41.

Fig. 7.41
Fig. 7.41 PDL of the program.

7.11.5 Program Listing

The program listing (program: LEDrate) is shown in Fig. 7.42. At the beginning of the program the header file mbed.h is included in the program. The User LED (LED1) is configured as an output using the DigitalOut statement. Similarly, the User Button is configured as an input using the DigitalIn statement. The delay between the flashes (variable dely) is initially set to 1 s. Every time the button is pressed this delay is decremented by 200 ms and as a result the LED flashes quicker. When the delay is less than 0, it is reset back to 1 s.

Fig. 7.42
Fig. 7.42 Program listing.

7.12 Project 9—Binary Event Counting With LEDs

7.12.1 Description

In this project 8 external LEDs are connected to PORT C the Nucleo-F411RE development board as in Project 4. The LEDs count up by one in binary every time the User button is pressed.

7.12.2 Aim

The aim of this project is to show how the User button can be used to simulate external events and also how the count of such events can be displayed in binary on 8 LEDs.

7.12.3 Block Diagram

The block diagram of the project is shown in Fig. 7.43, where 8 LEDs are connected to PORT C pins of the Nucleo-F411RE development board.

Fig. 7.43
Fig. 7.43 Block diagram of the project.

7.12.4 Circuit Diagram

The circuit diagram of the project is shown in Fig. 7.44 where the LEDs are connected using current limiting resistors. The on-board User button is used to create external events.

Fig. 7.44
Fig. 7.44 Circuit diagram of the project.

7.12.5 The PDL

Fig. 7.45 shows the PDL of the program.

Fig. 7.45
Fig. 7.45 PDL of the program.

7.12.6 Program Listing

The program listing is shown in Fig. 7.46 (program: Events). At the beginning of the program BusOut statement is used to group the PORT C lower byte into a variable called LEDS and variable Cnt is initialized to 0 and all the LEDs are turned OFF to start with. The remainder of the program runs in an endless loop. Inside this loop the state of the button is checked. If the button is pressed (button is 0) then variable Cnt is incremented by one and the total count is displayed in binary on the LEDs. The program then waits until the button is released and the above process is repeated.

Fig. 7.46
Fig. 7.46 Program listing.

7.13 Project 10—Using an External Button

7.13.1 Description

In this project an external button is connected to port pin PC_0 (connector CN7, pin 38) of the 8 Nucleo-F411RE development board. The User LED (LED1) is turned ON whenever the button is pressed.

7.13.2 Aim

The aim of this project is to show how an external button can be connected to the Nucleo-F411RE development board.

7.13.3 Block Diagram

The block diagram of the project is shown in Fig. 7.47.

Fig. 7.47
Fig. 7.47 Block diagram of the project.

7.13.4 Circuit Diagram

Buttons can be connected in two different ways to a microcontroller input port. In Fig. 7.48 the button state is normally held at logic 1 with the help of an external pull-up resistor and it goes to logic 0 when the button is pressed. In Fig. 7.49 the button state is normally at logic 0 and goes to logic 1 when the button is pressed. It is possible to pull-up a GPIO input pin internally via a pull-up resistor. In such applications there is no need to use an external pull-up resistor.

Fig. 7.48
Fig. 7.48 Button state is normally at logic 1.
Fig. 7.49
Fig. 7.49 Button state is normally at logic 0.

In this project the first method is used, that is, the button state is normally at logic 1 and goes to logic 0 when the button is pressed. The circuit diagram of the project is shown in Fig. 7.50. The button is connected to the following pins of the Nucleo board:

  • + 3.3 V to connector CN7, pin 16
  • GND to connector CN7, pin 8
  • PC_0 to connector CN7, pin 38
Fig. 7.50
Fig. 7.50 Circuit diagram of the project.

7.13.5 The Construction

Fig. 7.51 shows the button and the resistor mounted on a breadboard. Connection to the Nucleo-F411RE board is made using jumper wires.

Fig. 7.51
Fig. 7.51 Project built on a breadboard.

7.13.6 The PDL

Fig. 7.52 shows the PDL of the program.

Fig. 7.52
Fig. 7.52 PDL of the program.

7.13.7 Program Listing

The program listing is shown in Fig. 7.53 (program: EXTbutton). At the beginning of the program DigitalOut and DigitalIn statements are used to configure the LED and the button as digital output and input, respectively. The remainder of the program runs in an endless loop where the state of the button is checked continuously. Whenever the button is pressed the LED is turned ON, otherwise the LED is turned OFF.

Fig. 7.53
Fig. 7.53 Program listing.

7.13.8 DigitalIn Modes

The DigitalIn statement supports the following modes:

  • PullUp: Pull-up the GPIO pin internally (no need for external resistor)
  • PullDown: Pull-down the GPIO pin
  • PullNone: No pull-up/pull-down resistors
  • OpenDrain: GPIO pin on open-drain mode

The earlier modes can either be specified during the declaration of the DigitalIn statement,or later as shown in the following:

DigitalIn button(PC_0, PullUp);

or as:

DigitalIn button(PC_0);

button.mode(PullUp);

7.13.9 Suggestions for Additional Work

Modify the circuit in Fig. 7.50 by removing the external pull-up resistor and enable the internal pull-up resistor on port pin PC_0 in the program as shown in Fig. 7.53.

7.14 Project 11—RGB LED Control

7.14.1 Description

In this project an RGB LED is connected to the Nucleo-F411RE development board. Additionally, three push-buttons named as R, G, and B are connected to the board corresponding to three colors Red, Green, and Blue. Pressing a button toggles the state of the corresponding color. For example, if the Red color is OFF, then pressing button R will turn ON this color. If on the other hand the Red color is ON, then pressing button R will turn OFF this color.

7.14.2 Aim

The aim of this project is to show how more than one external button can be connected to the Nucleo board. Additionally, the project shows how an RGB LED can be used in a project.

7.14.3 Block Diagram

The block diagram of the project is shown in Fig. 7.54.

Fig. 7.54
Fig. 7.54 Block diagram of the project.

7.14.4 Circuit Diagram

The RGB LED (see Fig. 7.55) is a 4-pin LED which can display the mixture of the Red, Green, and Blue colors depending on the pin that has been activated. These LEDs can be either in common-anode or in common-cathode configurations. The one that will be used in this project is a common-cathode RGB LED where the common pin is connected to ground and the individual colors are displayed when the corresponding pins are set to logic 1. As shown in Fig. 7.55, the common pin is longer than the other pins for identification.

Fig. 7.55
Fig. 7.55 Common-cathode RGB LED.

Fig. 7.56 shows the circuit diagram of the project. The three buttons R, G, and B are connected to GPIO pins PC_0, PC_1, and PC_2, respectively. The Red, Green, and Blue pins of the LED are connected to GPIO pins PC_3, PC_4, and PC_5 through 390 ohm current limiting resistors, respectively.

Fig. 7.56
Fig. 7.56 Circuit diagram of the project.

7.14.5 The Construction

Fig. 7.57 shows the project build on a breadboard. The RGB LED is connected to the Nucleo-F411RE board using jumper wires.

Fig. 7.57
Fig. 7.57 The project constructed on a breadboard.

7.14.6 The PDL

Fig. 7.58 shows the program PDL.

Fig. 7.58
Fig. 7.58 PDL of the project.

7.14.7 Program Listing

The program listing (program: RGB) is shown in Fig. 7.59. At the beginning of the program BusOut statement is used to group the RGB control pins, and DigitalIn statements are used to configure the three buttons R,G, and B as inputs. All these three colors are turned OFF at the beginning of the program. The program then checks if a button is pressed and if so checks to see if the color corresponding to that button is already displayed. If the color is already displayed then it is removed, otherwise it is displayed. This is repeated for all the three buttons.

Fig. 7.59
Fig. 7.59
Fig. 7.59 Program listing.

The Red, Green, and Blue LEDs are connected to PORT C in the following order:

  • Blue Green Red

The relationship between the RGB value and the colors that turn ON are as follows:

RGBAction
1Turn ON Red
2Turn ON Green
4Turn ON Blue
6Turn OFF Red
5Turn OFF Green
3Turn OFF Blue

7.15 Project 12—Random Colored LED Display

7.15.1 Description

In this project an RGB LED is connected to the Nucleo-F411RE development board. The three colors Red, Green, and Blue of the RGB are activated randomly every 250 ms. The net effect is that the LED displays different colors randomly.

7.15.2 Aim

The aim of this project is to show how an RGB LED can be activated randomly.

7.15.3 Block Diagram

The block diagram of the project is shown in Fig. 7.60.

Fig. 7.60
Fig. 7.60 Block diagram of the project.

7.15.4 Circuit Diagram

Fig. 7.61 shows the circuit diagram of the project. The Red, Green, and Blue pins of the RGB LED are connected to GPIO pins PC_3, PC_4, and PC_5, respectively.

Fig. 7.61
Fig. 7.61 Circuit diagram of the project.

7.15.5 The Construction

The project is constructed on a breadboard as shown in Fig. 7.62.

Fig. 7.62
Fig. 7.62 Project constructed on a breadboard.

7.15.6 The PDL

Fig. 7.63 shows the PDL of the program.

Fig. 7.63
Fig. 7.63 PDL of the program.

7.15.7 Program Listing

The program listing (program: RGB-2) is shown in Fig. 7.64. At the beginning of the program the BusOut statement is used to group the LED pins into a variable called RGB. Then, an endless loop is formed where a random number is generated between 1 and 4 and this number is sent to the LED to display different colors every 250 ms.

Fig. 7.64
Fig. 7.64 Program listing.

7.15.8 Suggestions for Additional Work

Modify the program given in Fig. 7.64 by randomly varying the delay between 50 and 300 ms. What are the effects of modifying this delay?

7.16 Project 13—7-Segment LED Display

7.16.1 Description

In this project a single-digit 7-segment display is interfaced to the Nucleo-F411RE development board. The display counts up every second from 0 to 9.

7.16.2 Aim

The aim of this project is to show how a 7-segment display can be interfaced to the Nucleo-F411RE development board. Additionally, the project shows how to control such a display by programming using Mbed.

7.16.3 Block Diagram

The block diagram of the project is shown in Fig. 7.65.

Fig. 7.65
Fig. 7.65 Block diagram of the project.

7-segment displays (see Fig. 7.66) are used in microcontroller-based applications to display numbers and some letters. A 7-segment display consists of 7 LEDs connected such that the numbers 0–9 and some letters can be displayed by turning the appropriate LED segments ON or OFF.

Fig. 7.66
Fig. 7.66 Some 7-Segment LEDs.

The segments of a 7-segment LED are identified by letters a–g (or A–G) s shown in Fig. 7.67.

Fig. 7.67
Fig. 7.67
Fig. 7.67 Segments of a 7-Segment LED.

7-Segment displays are available as either common anode or common cathode configurations. In common anode displays all the anodes of the segments are connected together and this common pin is connected to a power supply. A segment is turned ON when its corresponding cathode pin is at logic 0. In a common cathode type display all the cathode pins of the segments are connected together and this common pin is usually connected to ground. A segment is turned ON when its corresponding anode pin is at logic 1. In addition to displaying numeric values, a 7-segment LED can also display a decimal point.

In this project the SMA42056 type common cathode display is used. This is a 0.56 in. (14.20 mm) red color display. Fig. 7.68 shows the connection diagram of this display. Pin 1 starts from the bottom left corner of the display. Bottom right-hand corner is pin 5, and the top left corner is pin 10.

Fig. 7.68
Fig. 7.68 SMA42056 7-Segment display pins.

7.16.4 Circuit Diagram

The circuit diagram of the project is shown in Fig. 7.69. PORT C lower byte pins (PC_0 to PC_6) are connected to the 7-segment pins via 390 ohm current limiting resistors as follows:

Fig. 7.69
Fig. 7.69 Circuit diagram of the project.
SegmentGPIO PinST Morpho Connector
aPC_0CN7, pin 38
bPC_1CN7, pin 36
cPC_2CN7, pin 35
dPC_3CN7, pin 37
ePC_4CN10, pin 34
fPC_5CN10, pin 6
gPC_6CN10, pin 4

The relationship between the numbers to be displayed and the segments that should be turned ON are given in the following:

Number to be DisplayedSegments to be ON
0a b c d e f
1b c
2a b g e d
3a b g c d
4f g c b
5a f g c d
6a f g e d c
7a b c
8a b c d e f g
9a b g f c d

Before writing the program we need to know the relationship between the numbers to be displayed and the data that should be sent to PORT C to display the requested numbers. This is shown in Table 7.1. Note that PC_7 is not used in the 7-segment display and it is set to 0 in this table for convenience.

Table 7.1

Number to be Displayed ad PORT C Value
NumberPC_7PC_6PC_5PC_4PC_3PC_2PC_1PC_0Hex
0001111110x3F
1000001100x06
2010110110x5B
3010011110x4F
4011001100x66
5011011010x6D
6011111010x7D
7000001110x07
8011111110x7F
9011011110x6F

Table 7.1

As we can see from Table 7.1, the following hexadecimal number must be sent to the lower byte of PORT C in order to display a number:

NumberPORT C Value
00x3F
10x06
20x5B
30x4F
40x66
50x6D
60x7D
70x07
80x7F
90x6F

7.16.5 The Construction

Fig. 7.70 shows the project built on a breadboard.

Fig. 7.70
Fig. 7.70 Project built on a breadboard.

7.16.6 The PDL

Fig. 7.71 shows the program PDL.

Fig. 7.71
Fig. 7.71 Program PDL.

7.16.7 Program Listing

The program listing (program: SevenSeg) is shown in Fig. 7.72. At the beginning of the program PORT C lower byte is grouped into variable Segments using the PortOut statement. The segments to be turned ON for a required number to be displayed are given in array LEDS. For example, to display number 0 we have to send the hexadecimal number 0x3F to the LED. Similarly, to display number 1 we have to send the hexadecimal number 0x06 to the LED and so on. Inside the program loop array LEDS is indexed by variable CNT and the resulting segment pattern is sent to PORT C. Variable CNT is then incremented by 1 and the program waits for 1 s. When CNT reaches to 10 it is reset back to 0. The above process repeats forever displaying numbers 0–9 on the 7-segment LED.

Fig. 7.72
Fig. 7.72 Program listing.

7.16.8 Modified Program

The program given in Fig. 7.72 can be written differently using the BusOut statement. This modified program (program: SevenSeg-2) is shown in Fig. 7.73. Used PORT C pin names are defined in statement BusOut. The remainder of the program is same as in Fig. 7.72.

Fig. 7.73
Fig. 7.73 Modified program.

7.16.9 Another Modified Program

The program given in Fig. 7.72 can be modified using a function as shown in Fig. 7.74. In the modified program (program: SevenSeg-3) function Display receives the number to be displayed as its argument and then sends this number to the LED to display it.

Fig. 7.74
Fig. 7.74 Another modified program.

7.16.10 Using Switch Statement

The program given in Fig. 7.74 can be modified using a switch statement. The modified program (program: SevenSeg-4) is shown in Fig. 7.75. Here, a switch statement is used inside function Display to display the required number on the 7-segment LED.

Fig. 7.75
Fig. 7.75
Fig. 7.75 Program using switch statement.

7.16.11 Suggestions for Additional Work

Modify the program given in Fig. 7.72 to display letters L and E alternately on the 7-segment display.

7.17 Project 14—Powering Large Loads—DC Motor Control

7.17.1 Description

The current capacity of a GPIO pin is limited to 20 mA. There are some applications however where larger currents are needed then the port can supply, such as motors, relays, actuators, etc. In this project, we shall see how a DC motor can be controlled from a GPIO pin of the Nucleo-F411RE development board. This is a very simple project where a relay is used to activate the motor for 10 s, then the motor stops for 5 s, and is then reactivated again for 20 s.

7.17.2 Aim

The aim of this project is to show how loads requiring large currents can be connected to a GPIO pin. Additionally, the project shows how a DC motor can be controlled from a relay.

7.17.3 Block Diagram

The block diagram of the project is shown in Fig. 7.76.

Fig. 7.76
Fig. 7.76 Block diagram of the project.

A load requiring large currents can be connected to a microcontroller output pin in one of three ways: using a bipolar transistor, using a MOSFET (Metal Oxide Semiconductor Field Effect Transistor), or using a relay.

Using a Bipolar Transistor

Fig. 7.77 shows how a bipolar transistor can be connected to a load. Here, the transistor is operated as a switch where the load is connected to the collector pin, the emitter pin is connected to ground, and the base is driven from the GPIO pin through a resistor. When a bipolar transistor is operated as a switch, the base current IB is chosen such that the transistor saturates when a base current is applied. If β is the minimum DC current gain of the transistor then the base current should be chosen such that:

IB>=IL/β

si6_e

Fig. 7.77
Fig. 7.77 Using a bipolar transistor.

The base resistor is then chosen using the following formula:

RB=Vo0.7/IB

si7_e

where Vo is the output voltage of the GPIO pin when it is at logic 1, and 0.7 is the Base-Emitter DC bias voltage of a bipolar silicon transistor.

As an example, assume that the minimum β = 400, Vo = 3.3 V, and the load current IL = 50 mA. The value of the base resistor should then be:

IB50/400=0.125mA

si8_e

Choose IB = 0.2 mA, then, RB = (3.3 − 0.7)/0.2 = 13 K, choose 12 K as the nearest physical resistor. It is important to check the data sheets to make sure that the chosen transistor maximum collector current is well above the maximum required load current. It may also be necessary to use heatsinks to protect the transistor.

It is recommended to use a freewheel diode in parallel with the load, especially with inductive loads to protect the transistor when it is switched off. This is shown in Fig. 7.78.

Fig. 7.78
Fig. 7.78 Using a diode to protect the transistor.

Using a MOSFET

MOSFETs are used when it is required to power loads requiring larger currents. When choosing a MOSFET we should make sure that the maximum gate-source threshold voltage is lower than the minimum output voltage of the GPIO pin when the pin is at logic 1. Since the output high voltage is + 3.3 V minimum, the gate-source threshold voltage should be around 3 V. Fig. 7.79 shows how a MOSFET can be used to switch high current loads. Note here that a freewheel diode will be required as in Fig. 7.78 when driving inductive loads. An example MOSFET that can be used in microcontroller-based applications is the IRL540N. The maximum gate-source threshold voltage of this device is 2 V, and the device can handle load currents up to 36 A.

Fig. 7.79
Fig. 7.79 Using a MOSFET.

Using a Relay

Relays can be used when it is required to switch loads requiring very large currents. Relays are normally used as loads in bipolar transistor or in MOSFET circuits. Fig. 7.80 shows how a relay can be used in a bipolar transistor circuit. Note that a freewheel diode will be required in this circuit since the relay winding is inductive.

Fig. 7.80
Fig. 7.80 Using a relay.

7.17.4 Circuit Diagram

Fig. 7.81 shows the block diagram of the project. A small 12 V DC motor is connected to a relay through a bipolar transistor switch. The relay is controlled from GPIO pin PC_0 of the Nucleo-F411RE development board.

Fig. 7.81
Fig. 7.81 Circuit diagram of the project.

7.17.5 The PDL

The PDL of the program is shown in Fig. 7.82.

Fig. 7.82
Fig. 7.82 PDL of the program.

7.17.6 Program Listing

The program listing (program: Relay) is shown in Fig. 7.83. At the beginning of the program PC_0 is configured as digital output. Then, the relay is activated for 10 s, deactivated for 5 s, and then reactivated for 20 s.

Fig. 7.83
Fig. 7.83 Program listing.

7.17.7 Suggestions for Additional Work

Modify the circuit given in Fig. 7.81 so that a MOSFET is used instead of a bipolar transistor.

7.18 Summary

In this chapter we have learned to develop simple LED-based projects, including the following:

  •  using the on-board User LED and User button
  •  using external LEDs and external buttons
  •  using digital input and output statements
  •  using the random number generator
  •  using an RGB LED
  •  using a single-digit 7-segment Display
  •  switching loads requiring large power

7.19 Exercises

  1. 1. Write a program to flash the User LED 10 times with 0.25 s delay between each flashing
  2. 2. It is required to connect two external LEDs to a Nucleo-F411RE development board. Draw a possible circuit diagram for this project.
  3. 3. Write a program to flash the LEDs alternately with 1 s delay in Exercise (2)
  4. 4. It is required to design a project having an external button and a 7-segment display. Draw a possible circuit diagram for this project.
  5. 5. Write a program to count up on the 7-segment display in Exercise (4) above each time the button is pressed. Assume that the number of presses will be less than 10.
  6. 6. It is required to connect an RGB LED to the Nucleo board. Draw the circuit diagram of this project assuming that the PORT C will be used to control the display.
  7. 7. Write a program to display the three colors of the RGB as follows: Red for 5 s, Green for 3 s, and Blue for 2 s. Then, display all the colors (i.e., white color) for 10 s.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.239.76.25