Skip to main content

Subsection 1.4.3 Part 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.
Figure 1.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.
To build the LED circuit, we’ll use a red or green LED and a 220 Ω resistor, wired as shown in Figure 1.4.5: the D13 (PA5) pin connects to the LED’s anode, which is its longer lead (drawn with a bend in Fritzing), and the LED’s cathode, the shorter lead, connects through the resistor to the ground rail.
Figure 1.4.5. External LED wired on the breadboard and driven from D13 (PA5). Green arrows show current flow: from the D13 header pin, through the LED (anode to cathode), through the 220 Ω resistor, and into the ground rail. The resistor limits the current to about 5 mA. The red and black wires connect the board’s 3.3 V and GND headers to the breadboard rails.
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.
Figure 1.4.6. The digital multimeter (DMM) from your lab kit. Turn the dial to the 20 V DC position for 3.3 V circuits. Black probe in COM, red probe in VΩmA. Turn to OFF when done.

Activity 1.4.1. Wire and Measure.

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.

(a)

Flash your updated program. Confirm that both the on-board LED and the external LED blink at that slow rate.

(b)

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.

(c)

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{.}\)

(d)

Replace the 220 Ω resistor with a 1 kΩ resistor. Repeat the measurements. Calculate the new current. Is the LED brighter or dimmer? Why?
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.
Figure 1.4.7. The breadboard circuit after moving the LED’s anode wire from D13 to D4. Only one jumper wire is rerouted.
Figure 1.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.

Activity 1.4.2. Move to a New Pin.

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.

(a)

Using the Nucleo-64 pinout, find which STM32 port and bit number D4 corresponds to.

(b)

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?

(c)

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?

(d)

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.