Forum

How to turn on an L...
 
Notifications
Clear all

[Solved] How to turn on an LED with a push button on Raspberry Pi 4

3 Posts
2 Users
0 Likes
388 Views
Posts: 1
Guest
Topic starter
(@Haley)
New Member
Joined: 2 years ago

Hi

Can you tell me how to make the connections, and the code, to light an LED using a push button? I have a Raspberry Pi 4

2 Replies
Posts: 13
(@raspi)
Active Member
Joined: 2 years ago

Using a push button to turn on an LED

1 Raspberry Pi 4
1 normally open button
1 LED
1k ohm resistor

Wiring

Insert the LED into the Breadboard paying attention in which row you insert the longest leg (positive pole). The shorter leg connects to the blue rail using a male-male cable.

Insert the resistor so that one of its ends is in the row of the positive pole of the LED (see image). Connect the other end to GPIO pin 24 using a male-female cable.

Insert the button in the middle of the breadboard. Then connect one of the legs to GPIO pin 18. The other to the blue rail.

Connect the blue rail to the GND pin.

Script

from gpiozero import LED, Button
from signal import pause
led = led(24)
button=Button(18)
button.when_pressed=led.on
button.when_released=led.off
pause()

Save the script by clicking Save, and then run it by clicking Run. Finally press the button and check that the LED lights up.

This image has an empty alt attribute; its file name is script-pushbutton.webp

Reply
1 Reply
 Bill
Guest
(@Bill)
Joined: 1 second ago

New Member
Posts: 0

@raspi Thanks

Reply

Leave a reply

Author Name

Author Email

Title *

 
Preview 0 Revisions Saved
Share:
Scroll to Top