Subsection1.4.3Part 3: External LEDs on the Breadboard
The on-board LED is convenient for software testing, but a microcontroller is there to control devices outside the chip. Let’s make an external LED blink, one on your breadboard rather than on the Nucleo board: we’ll wire it, drive it from a GPIO pin, and use a digital multimeter to measure the voltages and currents in the circuit.
While wiring, unplug your Nucleo from your computer or any other source of power. Your breadboard has power rails running along each long edge. Some kits come with these rails already wired to the Nucleo; if yours does not, wire them now as shown in Figure 1.4.4. Use the Nucleo’s 3.3 V header pin for the positive rails. Also connect all positive rails to each other and all ground rails to each other as shown. This lets you tap 3.3 V or GND from anywhere on the board. Once wired, leave all of these jumpers in place for the entire term.
Power is always a red wire and ground is always a black wire. Use no other color for power or ground, and never use red or black for a signal wire, so that you can tell at a glance what every wire carries.
Figure1.4.4.The Nucleo board connected to a breadboard at the start of Lab 1. Short red and black jumper wires link the Nucleo’s 3.3 V and GND header pins to the breadboard’s positive and negative power rails. Leave these wires in place for the entire term.
The digital multimeter (Figure 1.4.6) is essential for verifying circuits. Set the dial to the 20 V DC range for 3.3 V circuits; turn it off when finished to preserve the battery.
We’ll drive the external LED from the board and measure the voltages around it. First we’ll switch the build back to blinky.c. Right-click blinkySlowToFast.c and choose Resource Configurations → Exclude from Build, then right-click blinky.c and un-exclude it the same way. In blinky.c, change the include to ES28.h and replace its two delay loops with delay_ms(2000), so that the LED stays on and off long enough for the DMM display to settle.
With the LED lit, measure three voltages: \(V_{CC}\) from the power rail to ground (expect ≈ 3.3 V), \(V_F\) across the LED (anode to cathode), and \(V_R\) across the resistor.
Check your three measurements against Kirchhoff’s voltage law: does \(V_{CC} = V_F + V_R\text{?}\) Then calculate the current through the LED, \(I_F = V_R / R\text{.}\)
Now we’ll move the LED’s anode wire from D13 to D4, as shown in Figure 1.4.7. That is the only jumper wire that changes; everything else on the breadboard stays where it is.
Figure1.4.8.Nucleo-64 pinout reference showing each Arduino header label and its corresponding STM32 port and bit number. Use this to look up which STM32C031C6 pin any Arduino header connects to.
We have just moved the LED’s anode wire from D13 to D4, the one jumper wire we rerouted. To make the LED blink from D4 you now need three changes in your program: enable the correct GPIO port clock, configure the pin as an output, and drive it. Before writing any code, answer the following.
In the reference manual, RM0490 §5.4 (RCC registers), find the RCC_IOPENR register description. Which bit enables the clock for the port you identified?
From Day 1: for pin \(n\text{,}\) MODER uses bits \(2n\) and \(2n+1\text{.}\) What are the two MODER bit numbers for the pin you identified? What value encodes output mode?
Update your code with the values you found and verify the external LED blinks. The on-board LED (PA5) will stop blinking, because your program no longer configures or drives Port A.
Connect the D13 (PA5) pin to the LED’s anode, which is its longer lead. In a Fritzing drawing, the longer lead is the one drawn with a bend. Connect the LED’s cathode, the shorter lead, through the resistor to the ground rail.