0
0
Raspberry Piprogramming~30 mins

Why LED and button projects build hardware confidence in Raspberry Pi - See It in Action

Choose your learning style9 modes available
Why LED and button projects build hardware confidence
📖 Scenario: You have a Raspberry Pi and want to learn how to control simple hardware parts like LEDs and buttons. This helps you understand how software talks to the real world.
🎯 Goal: Build a small program that turns an LED on or off when you press a button. This shows how code can control hardware and how hardware can send signals back to code.
📋 What You'll Learn
Create a variable to represent the LED pin number
Create a variable to represent the button pin number
Write code to set up the LED pin as output and the button pin as input
Write code to read the button state and turn the LED on or off accordingly
Print the LED state each time it changes
💡 Why This Matters
🌍 Real World
Controlling LEDs and reading buttons is the first step to building interactive devices like alarms, lights, and simple robots.
💼 Career
Understanding how software interacts with hardware is key for roles in embedded systems, IoT development, and hardware prototyping.
Progress0 / 4 steps
1
Set up LED and button pins
Create two variables: led_pin set to 17 and button_pin set to 27 to represent the GPIO pins for the LED and button.
Raspberry Pi
Need a hint?

Use simple assignment like led_pin = 17.

2
Configure GPIO pins
Import the RPi.GPIO module as GPIO. Set the GPIO mode to GPIO.BCM. Set up led_pin as an output pin and button_pin as an input pin with a pull-down resistor.
Raspberry Pi
Need a hint?

Use GPIO.setup(pin, GPIO.OUT) for output and GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) for input with pull-down.

3
Read button and control LED
Write a loop that reads the button state using GPIO.input(button_pin). If the button is pressed (value 1), turn the LED on with GPIO.output(led_pin, GPIO.HIGH). Otherwise, turn the LED off with GPIO.output(led_pin, GPIO.LOW). Use a variable led_state to track the LED status.
Raspberry Pi
Need a hint?

Use a while True loop and GPIO.input(button_pin) to read the button.

4
Print LED state changes
Inside the loop, print "LED is ON" when the LED turns on and "LED is OFF" when it turns off. Only print when the LED state changes to avoid repeated messages.
Raspberry Pi
Need a hint?

Use conditions to check if led_state changed before printing.