In this project, we will learn how to turn on and off the LED (Raspberry pi LED flashlight) using the push button. It is very important to know this job to work in electronics circuits. In this project, you will learn more about how to trigger using the push button.
you have to need a component for the Raspberry pi LED flashlight
- Raspberry Pi
- 5 mm LED
- 330 Ω resistor
- Jumper wires
Introduction to switches and buttons
You can see the switch wherever you look Every time you turn on and off the light using the switch Again you control the speed of the fan using the switch You also use the switch for various purposes Stay. In the market, we see different types of switches like a push-button switch, toggle switch, magnetic switch. Figure 1 shows some common types of switches used in electronics.
Here we will use a simple push-button switch. There are two main push button switches, one normally open and the other normally closed. Normally open push-button switch is shown in Figure 2. It is open in normal condition and no current flows through it for the reason of being open. If the push button is pressed then the push button connects the two sites so that the circuit is closed and current flows through it.
NormallyNormally Close The push button has a normally closed circuit so that normal current flows through it. When the switch is pressed, the circuit opens and the current flows through it.
The push-button has two or four legs, the four-leg push button is commonly used in various prototype circuits because it is easily attached to the breadboard.
In the figure above we have shown the four-legged push-button No. 1 and No. 2 legs are always connected. In this project, we will use the Normally open four-leg push button switch.
Current will flow through this push button only when the button is pressed.
Raspberry Pi LED flashlight Circuit
This project made LEDwill light up only when you press the push button and you will be able to know this information through your Raspberry Pi GPIO PIN.
You can easily create a circuit by following the steps below.
- Attach the Raspberry Pi ground to the breadboard.
- The LED needs to be connected to a 330-ohm register series from the positive edge of the LED by placing the breadboard and attaching it to the GPIO 25 pin of the Raspberry Pi.
- Now place the push button in the middle of the breadboard so that the two ends of the push button switch split apart.
- Now one end of the push button switch needs to be connected to Raspberry Pi’s GPIO2 and the other end to the ground as shown in the figure below.
Raspberry Pi LED flashlight Script Writing:
This script will trigger various events depending on whether the push button is pressed or released. Based on this, the program we will create is how the program will work:
- When the push button is pressed, the LED will be one.
- The LED will be off when the push button is in a release state.
Now take out the python IDE of Raspberry Pi and write the following program there and save it as blinking_led.py.
➊ from gpiozero import LED, Button from signal import pause ➋ led = LED(25) button = Button(2) ➌ button.when_pressed = led.on ➍ button.when_released = led.off ➎ pause()
➊ from gpiozero import LED, Button
from signal import pause
LED, Imported from gpiozero library to control Button, Imported button from signal library pause imported.
➋ led = LED(25)
button = Button(2)
Led and button created two objects led data store the data of JPIO 25 and button GPIO 2 stores the data.
➌ button.when_pressed = led.on
➍ button.when_released = led.off
button.when_pressed function Button will call led.on when pressed button so LED is turned on in the same way button.when_released function will call led.off as soon as a button is released and with Becomes LED.
pause () function runs the program after the event is detected In the case of this project, this function always checks the status of the push button switch. When the push button switch creates an event, the program runs according to that event.
Run: In order to run, first, you have to save the program above then save Run ▸ Run current script or press F5 from the keyboard then the program will run. To close the running program, press CTRL + F2.
Create a new project:
When you simple project, you can create more new project ideas from the ideas of the project. Here are some ideas:
- Add more pushbuttons and LEDs.
- Modify the code to change the LED state when you press the push button so that you don’t have to hold the button to keep the LED lit.
- Build a traffic light.
27 total views, 1 views today