Program 2: Reading Pushbutton Input and Controlling LED - 4.2 | EXPERIMENT NO. 9 TITLE: Introduction to ARM Microcontrollers - Basic I/O and Peripherals | Microcontroller Lab
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

GPIO Configuration for LED

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Today, we will learn how to control an LED based on a pushbutton's state using GPIO in an STM32 microcontroller. First, let's understand how to configure a GPIO pin for output.

Student 1
Student 1

What does GPIO mean, and how do we set it up?

Teacher
Teacher

Great question! GPIO stands for General Purpose Input/Output. To set up an LED, we need to enable the clock for the GPIO port where our LED is connected, which is commonly done through the RCC register. Does anyone remember what that stands for?

Student 2
Student 2

Is it the Reset and Clock Control?

Teacher
Teacher

Exactly! We enable the clock for GPIOA to make it operational. We then configure the LED pin, typically PA5, to output mode by writing to the MODER register. Remember: 'Mode and Output go hand in hand!' How do you think we determine the mode settings?

Student 3
Student 3

We look at the MODER bits, right? To set it to '01' for output?

Teacher
Teacher

That's correct! After enabling the clock and setting the mode, we can control the LED through the Output Data Register. Let's summarize what we've learned: enable the clock, configure the pin mode, and write to the output register.

Reading Pushbutton Input

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Now let's shift focus to our pushbutton. We will configure PC13 as an input. Why do we need to ensure we use an internal pull-up resistor?

Student 4
Student 4

To ensure the pin reads HIGH when the button isn't pressed, right?

Teacher
Teacher

Correct! When the button is pressed, the pin connects to ground, making it LOW or '0'. We configure the pull-up by setting the PUPDR register. What's the next crucial step after setting this up?

Student 1
Student 1

We need to continuously check if the button state is LOW to turn the LED ON.

Teacher
Teacher

Exactly! We can do this in an infinite loop, reading the IDR register to check the button state. If it reads '0', we can turn the LED ON; otherwise, we turn it OFF.

Student 2
Student 2

So, in the loop, we keep checking the state of PC13?

Teacher
Teacher

That's right! The loop continually checks the button state and controls the LED based on it. Summarize this concept: Configure PC13 as input with pull-up, consistently check its state, and toggle PA5 accordingly.

Putting It All Together

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Let's put together what we've learned into code. Who can summarize the important steps needed to develop this program?

Student 3
Student 3

First, we enable the clocks for GPIOA and GPIOC, then configure PA5 and PC13.

Student 4
Student 4

Afterward, we set the appropriate GPIO modes and the pull-up for PC13!

Teacher
Teacher

Perfect! Now, can someone provide the loop logic for controlling the LED based on button input?

Student 1
Student 1

We check if the button is pressed using the IDR register and toggle the LED state.

Teacher
Teacher

Excellent! You all have adequately grasped the concept of input/output in ARM microcontrollers. Always remember to break the task down into smaller steps. Let's wrap up: Configure clocks, set GPIO modes, and use a loop to monitor and respond to inputs.

Introduction & Overview

Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

This section covers the implementation of reading a pushbutton input and controlling an LED using an ARM Cortex-M microcontroller.

Standard

In this section, students learn to interface a pushbutton and an LED using GPIO pins of an ARM Cortex-M microcontroller. The program reads the state of the button and toggles an LED accordingly, providing a practical application of microcontroller programming.

Detailed

In this section, students will explore how to control an LED based on the input from a pushbutton using an ARM Cortex-M based microcontroller, specifically the STM32 series. It walks through the systematic steps of enabling clocks for GPIO ports, configuring the LED as an output, and the button as an input with an internal pull-up resistor for correct operation. A demonstration program is provided which reads the button's state: when the button is pressed, the LED is turned ON; when released, the LED is OFF. This simple yet effective program exemplifies fundamental concepts of microcontroller input/output operations and serves as a baseline for more complex projects involving user interaction.

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Objective of Program 2

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Objective: To read the state of the on-board User Button (PC13) and control the on-board LED (PA5). When the button is pressed, the LED should turn ON; when released, it should turn OFF. (Assuming the button is active-low: connected to GND when pressed, pulled high when released).

Detailed Explanation

The main aim of Program 2 is to demonstrate how to read input from a pushbutton connected to the microcontroller and use that input to control an LED's state. This is a common task in embedded systems where user interaction is needed. In this case, the button is connected so that when it is pressed, it completes the circuit to ground (GND), turning the button's state to low (0). When the button is not pressed, it will be in a high state (1) due to the internal pull-up resistor.

Examples & Analogies

Think of the pushbutton as a light switch in your home. When you press the switch down (button pressed), the light turns on (LED ON). When you release the switch (button released), the light turns off (LED OFF). Just like that switch, the pushbutton is used to control the power to the LED.

Setting Up the GPIO

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

// 1. Enable Clock for GPIOA (for LED) and GPIOC (for Button)
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Enable GPIOA clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable GPIOC clock

// 2. Configure PA5 as General Purpose Output (for LED) - Same as Program 1
GPIOA->MODER &= ~(GPIO_MODER_MODER5_Msk);
GPIOA->MODER |= GPIO_MODER_MODER5_0;
GPIOA->OTYPER &= ~GPIO_OTYPER_OT_5;
// Set initial LED state to OFF
GPIOA->ODR &= ~(1U << LED_PIN_NUM);

// 3. Configure PC13 as General Purpose Input (for Button)
GPIOC->MODER &= ~(GPIO_MODER_MODER13_Msk); // Clear existing mode bits for PC13 (sets to 00 -> Input)

// 4. Configure PC13 with Pull-up resistor (User button is usually active-low with internal pull-up)
GPIOC->PUPDR &= ~(GPIO_PUPDR_PUPDR13_Msk); // Clear existing PUPDR bits
GPIOC->PUPDR |= GPIO_PUPDR_PUPDR13_0; // Set to 01 (Pull-up)

Detailed Explanation

In this chunk, we prepare the GPIO pins for use. First, we enable the clock for GPIOA and GPIOC, allowing the microcontroller to control those pins. We then configure PA5 as an output pin where the LED is connected, and PC13 as an input pin for the button. By setting PC13 to use an internal pull-up resistor, we ensure that the pin reads HIGH when the button is not pressed, providing a stable logic level. When the button is pressed, it connects the pin directly to ground, making the pin read LOW.

Examples & Analogies

Consider configuring your desk setup for working. Enabling the clocks is like plugging in your electronics to power. Setting PA5 for output is like deciding where to place your desk lamp (LED), while PC13 for input is like setting up a sensor (button) to detect if you've pressed it to turn on your lamp. The pull-up resistor is akin to having a constant source of energy (battery) that keeps the lamp off until you physically press the button (turning it to GND).

Reading Button State and Controlling LED

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

while (1) {
// Read the state of the button pin (PC13)
if ((GPIOC->IDR & (1U << BUTTON_PIN_NUM)) == 0) { // Check if button is pressed (active low)
// Button is pressed, turn LED ON
GPIOA->ODR |= (1U << LED_PIN_NUM); // Set PA5 high
} else {
// Button is released, turn LED OFF
GPIOA->ODR &= ~(1U << LED_PIN_NUM); // Set PA5 low
}
}

Detailed Explanation

This section is where the main functionality of the program occurs. Inside an infinite loop, we constantly check the state of the button connected to PC13. If the button is pressed (when the state reads LOW), we turn the LED on by setting PA5 to HIGH. If the button is not pressed (state is HIGH), we turn the LED off by clearing PA5. This loop continuously checks the button state and updates the LED accordingly, allowing for real-time interaction.

Examples & Analogies

Imagine a light switch in your home that you keep checking. You can think of the infinite loop as you repeatedly checking if the switch is on or off. When you press the switch (button pressed), you tell your smart home system to turn on the lights (LED ON). Conversely, when you let go of the switch (button released), the system turns off the lights (LED OFF). This real-time control over the LED based on the button's state reflects how immediate responses can be programmed in embedded systems.

Definitions & Key Concepts

Learn essential terms and foundational ideas that form the basis of the topic.

Key Concepts

  • GPIO Configuration: Setting up GPIO pins for input and output.

  • RCC: The Reset and Clock Control register to enable clocks for GPIO ports.

  • Input Reading: How to read the state of a pushbutton using the IDR register.

  • Output Control: Toggling an LED based on the state of an input pin.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • When the pushbutton connected to PC13 is pressed, the LED on PA5 lights up. Conversely, when the button is released, the LED turns off.

  • By configuring the internal pull-up resistor for PC13, we ensure the input pin reads HIGH when unpressed and LOW when pressed.

Memory Aids

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎵 Rhymes Time

  • For GPIO pins, remember this tune: Input or output, they can bloom!

📖 Fascinating Stories

  • Imagine an LED that waits for a knock on the door, the pushbutton being the doorbell! When pressed, the LED lights up, signalling it's time to play.

🧠 Other Memory Gems

  • To remember the steps: 'Clock and Mode, Yes, in Load!' - Enable the clock and set the mode for output.

🎯 Super Acronyms

RCC stands for Reset and Clock Control.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: GPIO

    Definition:

    General Purpose Input/Output, a type of pin on a microcontroller that can be configured as either input or output.

  • Term: RCC

    Definition:

    Reset and Clock Control, a peripheral able to enable the clock for other peripherals.

  • Term: MODER

    Definition:

    Mode Register responsible for configuring the operational mode of GPIO pins.

  • Term: IDR

    Definition:

    Input Data Register, used for reading the current state of input pins.

  • Term: PUPDR

    Definition:

    Pull-Up/Pull-Down Register that enables internal pull-up or pull-down resistors on GPIO pins.