0
0
Embedded Cprogramming~5 mins

Pull-up and pull-down resistor configuration in Embedded C

Choose your learning style9 modes available
Introduction

Pull-up and pull-down resistors help set a clear default voltage level on a microcontroller pin to avoid random signals.

When you want a button input to read HIGH when not pressed and LOW when pressed.
When you need to make sure a pin reads LOW by default to avoid floating values.
When connecting sensors that need a stable reference voltage on their input pins.
When you want to prevent noise from causing unpredictable pin readings.
Syntax
Embedded C
GPIO_InitTypeDef GPIO_InitStruct = {0};

// For pull-up resistor
GPIO_InitStruct.Pin = GPIO_PIN_X;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIO_PORT_X, &GPIO_InitStruct);

// For pull-down resistor
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIO_PORT_X, &GPIO_InitStruct);

GPIO_PULLUP sets the pin to HIGH by default through the resistor.

GPIO_PULLDOWN sets the pin to LOW by default through the resistor.

Examples
This sets pin PA0 to input mode with a pull-up resistor, so it reads HIGH when not connected.
Embedded C
// Configure pin PA0 with pull-up resistor
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
This sets pin PB1 to input mode with a pull-down resistor, so it reads LOW when not connected.
Embedded C
// Configure pin PB1 with pull-down resistor
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
Sample Program

This program sets up pin PA0 with a pull-up resistor and reads its state. Without pressing a button or connecting the pin to ground, it will read HIGH (1).

Embedded C
#include "stm32f1xx_hal.h"

int main(void) {
    HAL_Init();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // Configure PA0 as input with pull-up resistor
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // Read the pin state
    int pin_state = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);

    // Normally prints 1 because of pull-up resistor
    if (pin_state == GPIO_PIN_SET) {
        // Simulate output
        // printf("Pin PA0 reads HIGH\n");
    } else {
        // printf("Pin PA0 reads LOW\n");
    }

    while(1) {}
}
OutputSuccess
Important Notes

Pull-up and pull-down resistors prevent pins from floating, which means they avoid random noise signals.

Many microcontrollers have built-in pull-up and pull-down resistors you can enable in software.

Always check your hardware datasheet to know if internal resistors are available or if you need external ones.

Summary

Pull-up resistors make a pin read HIGH by default.

Pull-down resistors make a pin read LOW by default.

They help keep input pins stable and avoid random signals.