Skip to main content

Subsection 2.2.3 Part 3: Wiring and Reading a Digital Input

Now weโ€™ll use a pin for input rather than output. Weโ€™ll add a pushbutton to the breadboard (Figureย 2.2.10). Use D5 (PB4) specifically, since the code weโ€™ll start with, button.c, is written for PB4. Wire one side of the button to D5 and the other side to GND.
Figure 2.2.10. Breadboard wiring for a pushbutton connected to a GPIO input. The button bridges two rows of the breadboard; one terminal goes to the GPIO pin, the other to GND. The Nucleoโ€™s internal pull-up resistor keeps the pin HIGH when the button is open.
A button by itself does not produce a 1 or a 0: it only closes a connection from the pin to ground. With nothing else connected the pin floats, its voltage drifts with electrical noise, and reading it produces random 0s and 1s. The fix is to pull the pin up, so that it reads HIGH while the switch is unpressed. A pull-up resistor between the pin and 3.3 V does that: while the button is open the resistor holds the pin at 3.3 V and GPIOB->IDR reads 1, and pressing the button connects the pin to ground, which overrides the pull-up so the pin reads 0. That is the active-low convention, pressed = 0 and released = 1, and it is the standard arrangement for buttons. There are two ways to supply the resistor: an external one of about 10 kฮฉ, or the STM32C031C6โ€™s own internal pull-up of about 40 kฮฉ, which we enable in software. Weโ€™ll use the internal one, so nothing extra goes on the breadboard. If something else is actively driving the pin, the pull-up has no effect.
Figure 2.2.11. Alternative wiring using an external pull-up resistor between the power rail and the button. The resistor holds the pin HIGH when the button is open; pressing the button pulls it LOW. On the STM32C031C6 we use the built-in internal pull-up instead, so no external resistor is needed.
Figure 2.2.12. One GPIO bit: read path (pin โ†’ Schmitt trigger โ†’ IDR) and write path (ODR โ†’ output control โ†’ pin).
Figure 2.2.13. The same bit wired to a button: the internal pull-up holds the pin HIGH when the switch is open; closing it pulls the pin LOW (blue path into IDR).
Three registers control GPIO input behavior on the STM32C031C6. GPIOx->MODER, the Mode Register for GPIO port x (where x is the port letter: A, B, C, D or F), sets the pin mode: input, output, alternate function or analog. GPIOx->PUPDR turns the internal pull-up or pull-down on or off. GPIOx->IDR is read-only, and each of its bits reflects the current logic level on the corresponding pin. The specific bit values for each setting are in the reference manual (RM0490), and weโ€™ll look them up there rather than take them from a slide.
Figure 2.2.14. Registers for a GPIO input: MODER (mode), PUPDR (pull-up/down), IDR (live level). Blue path: button โ†’ pull-up โ†’ Schmitt trigger โ†’ IDR.

Activity 2.2.4. Finding Configuration Details in the Reference Manual.

You know what MODER, PUPDR, and IDR do, but not yet the specific bit values to write. Open the reference manual (RM0490), navigate to the General-purpose I/Os (GPIO) section via the table of contents, then go to RM0490 ยง6.4, GPIO registers. Looking up register details in the reference manual is something you will keep doing in this course.
One note on naming before you start. GPIOx_MODER is the registerโ€™s name in the reference manual. GPIOx->MODER is a dereferenced pointer to the memory location that register is mapped to, so to change GPIOx_MODER we write to GPIOx->MODER, and that is the form weโ€™ll always use in code.

(a)

Find GPIOx_MODER (RM0490 ยง6.4.1). What two-bit value selects input mode? What is the power-on default for all pins?

(b)

Find GPIOx_PUPDR. Which two-bit value enables the internal pull-up? What C statements enable the pull-up on PB4 (pin 4 of Port B), using bit masks?

Activity 2.2.5. Reading a Button in Code.

In STM32CubeIDE, make a copy of your TemplateProject and name the copy Buttons. Then download button.c from the bookโ€™s starter files and place it in the Src folder of your new project. Read through the code, then answer the questions below before flashing it.

(a)

The code calls delay_ms(50) immediately after enabling the pull-up resistor. Why? What do you think would happen if you removed that line?

(b)

The main loop uses (GPIOB->IDR & BUTTON_PIN) == 0 to detect a button press. Why == 0 rather than != 0?

(c)

Flash the code and verify: pressing the button turns on the LED; releasing turns it off. Then modify blinkySEQ.c so that pressing the button interrupts the sequence and lights all three LEDs; releasing returns to the sequence.
Figure 2.2.16. Oscilloscope trace of the pin voltage rising after the internal pull-up is enabled. The pin reaches the HIGH threshold in about 2.5 ยตs; reading IDR before it settles returns a spurious 0.
The general problem is that code executes far faster than a pin can change state. The pins on the chip start out at 0, and enabling the pull-up does not make the pin read 1 until the pinโ€™s stray capacitance has charged; the debounce capacitor weโ€™ll add tomorrow makes that take longer still. There are several ways to deal with it. The easiest is the one button.c uses: wait 50 ms with delay_ms() after initialization, before sampling any pin. You can also loop until the pin reads 1; or start the pin as an output, drive it to 1, and then switch it to input and enable the pull-up; or fit an external pull-up instead of using the internal one, which helps only because an external one can be far smaller than the internal 40 kฮฉ and so recharges the pin faster.
None of those removes the problem, because it is not only a start-up problem. It happens any time the pin is pulled down, so your code has to account for it every time it reads a button. A smaller external pull-up only shortens the wait, and once the debounce capacitor is fitted it works against the debouncing. The reliable way to do that in ordinary code is a state machine that acts only on a transition: make sure the program has gone from PRESSED back to UNPRESSED before it takes another action. That is the pattern weโ€™ll build on Thursday.