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.
Mbed; LED control; Push-button; RGB; 7-Segment LED; DC motor control; Relay; Random number
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:
The algorithm (operational steps) of each program in every project is described using the program description language (PDL), explained in detail in Chapter 2.
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:
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.
In this project the user LED on the Nucleo-F411RE board is flashed every 2 s.
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.
Fig. 7.9 shows the block diagram of the project.
The PDL of the project is shown in Fig. 7.10
The steps to create and compile the program are given in the following:
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.
Some other related useful Mbed functions are given in the following:
Function | Description | Example |
---|---|---|
wait | wait in seconds (floating point) | wait (2.5) |
wait_ms | wait in milliseconds (integer) | wait_ms (10) |
wait_us | wait in microseconds (integer) | wait_us (100) |
DigitalOut | configure specified pin as digital output | DigitalOut myled(LED1) |
DigitalOut | Configure Port PA_5 as digital output | DigitalOut myled(PA_5) |
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.
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.
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.
The block diagram of the project is shown in Fig. 7.9.
The PDL of the project is shown in Fig. 7.16.
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.
Modify the program in Fig. 7.17 so that the flashing stops after sending 10 SOS messages.
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.
The aim of this project is to show how an external LED can be connected to the Nucleo-F411RE board.
The block diagram of the project is shown in Fig. 7.18.
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:
Therefore,
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
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:
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
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).
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.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.
Try using different resistor values and see how the brightness of the LED varies with the resistor values.
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.
The aim of this project is to show how external LEDs can be connected to the Nucleo-F411RE board.
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.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.
PORT C pins PC_0 to PC_7 are available at the following ST morpho connector pins (see also Fig. 5.8):
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.28 shows the project PDL where the LEDs turn ON in sequence starting from the LSB bit.
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.
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.
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.
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.
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.
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.
The circuit diagram of the project is shown in Fig. 7.26 where the LEDs are connected using current limiting resistors.
Fig. 7.33 shows the PDL of the program.
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.
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.
The aim of this project is to show random numbers can be generated in Mbed programs.
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.
The circuit diagram of the project is shown in Fig. 7.26 where the LEDs are connected using current limiting resistors.
Fig. 7.35 shows the PDL of the program.
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.
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.
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.
The aim of this project is to show how the user push-button on the Nucleo board can be used.
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.
The PDL of the program is very simple and is shown in Fig. 7.39.
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.
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.
The aim of this project is to show how the user push-button on the Nucleo board can be used.
The block diagram of the project is shown in Fig. 7.38.
The PDL of the program is very simple and is shown in Fig. 7.41.
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.
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.
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.
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.
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.45 shows the PDL of the program.
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.
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.
The aim of this project is to show how an external button can be connected to the Nucleo-F411RE development board.
The block diagram of the project is shown in Fig. 7.47.
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.
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:
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.52 shows the PDL of the program.
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.
The DigitalIn statement supports the following modes:
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);
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.
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.
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.
The block diagram of the project is shown in Fig. 7.54.
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.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.57 shows the project build on a breadboard. The RGB LED is connected to the Nucleo-F411RE board using jumper wires.
Fig. 7.58 shows the program PDL.
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.
The Red, Green, and Blue LEDs are connected to PORT C in the following order:
The relationship between the RGB value and the colors that turn ON are as follows:
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.
The aim of this project is to show how an RGB LED can be activated randomly.
The block diagram of the project is shown in Fig. 7.60.
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.
The project is constructed on a breadboard as shown in Fig. 7.62.
Fig. 7.63 shows the PDL of the program.
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.
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?
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.
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.
The block diagram of the project is shown in Fig. 7.65.
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.
The segments of a 7-segment LED are identified by letters a–g (or A–G) s shown in Fig. 7.67.
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.
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:
Segment | GPIO Pin | ST Morpho Connector |
---|---|---|
a | PC_0 | CN7, pin 38 |
b | PC_1 | CN7, pin 36 |
c | PC_2 | CN7, pin 35 |
d | PC_3 | CN7, pin 37 |
e | PC_4 | CN10, pin 34 |
f | PC_5 | CN10, pin 6 |
g | PC_6 | CN10, 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 Displayed | Segments to be ON |
---|---|
0 | a b c d e f |
1 | b c |
2 | a b g e d |
3 | a b g c d |
4 | f g c b |
5 | a f g c d |
6 | a f g e d c |
7 | a b c |
8 | a b c d e f g |
9 | a 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 | PC_7 | PC_6 | PC_5 | PC_4 | PC_3 | PC_2 | PC_1 | PC_0 | Hex |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0x3F |
1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0x06 |
2 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0x5B |
3 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 0x4F |
4 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0x66 |
5 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0x6D |
6 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 0x7D |
7 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0x07 |
8 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0x7F |
9 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0x6F |
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:
Fig. 7.70 shows the project built on a breadboard.
Fig. 7.71 shows the program PDL.
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.
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.
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.
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.
Modify the program given in Fig. 7.72 to display letters L and E alternately on the 7-segment display.
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.
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.
The block diagram of the project is shown in Fig. 7.76.
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.
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:
The base resistor is then chosen using the following formula:
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:
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.
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.
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.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.
The PDL of the program is shown in Fig. 7.82.
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.
Modify the circuit given in Fig. 7.81 so that a MOSFET is used instead of a bipolar transistor.
In this chapter we have learned to develop simple LED-based projects, including the following:
3.236.100.210