Make a 5MM Led Toggle on Off Button
by yvzkaan in Circuits > Raspberry Pi
160 Views, 0 Favorites, 0 Comments
Make a 5MM Led Toggle on Off Button
I've made a 5mm led with resistor pre built in. You can prepare a led like i have did or use a breadboard and resistor seperately if you want (that was too messy for me so i made my own)
Supplies
Raspberry pi (any with header pins except pico)
5MM Led
Resistor suitable for your LED (if you havent made it built in or purchased it with pre built in the led)
A touch sensor
Female to female jumper cable for the sensor
OPTIONAL: Make a 5MM Led With Resistor Built In
First of all get 2 jumper cables (one end must be female). Cut the other end from the near of it so they dont have length differences. Strip the 2 jumper cables equally and determine the + and - Leg of your pin. Calculate the resistance value (formula is Resistance = Voltage / Current). Cut the + Leg so that when the resistor is soldered, it doesnt effect the length of the leg. Solder it, let cool down the LED. Get one of the jumper cable and solder to the end of the resistor and the other jumper cable to the other leg. Use a third hand stand for easier soldering or get help from someone you know.
After soldering, add individual heat shrink tubes to both of the legs for insulation and a main insulation after the leg insulation is completed. Connect the + Jumper cable to GPIO17 and - To GND
Connect Your Led
Connect your led with your accurate resistor, + pin to GPIO17 - pin to GND
Connect Touch Sensor
Connect GND to GND VCC to 5V and IO to GPIO27
Set Up the Software
Install Python3 on your Pi using
sudo apt-get install python3
Install Python IDLE on your Pi using
sudo apt-get install idle3
Open IDLE and click New File
import RPi.GPIO as GPIO
import time
# Set the GPIO mode and the pin numbers for the touch sensor and LED
GPIO.setmode(GPIO.BCM)
touch_pin = 27
led_pin = 17
# Setup the touch sensor pin as input and the LED pin as output
GPIO.setup(touch_pin, GPIO.IN)
GPIO.setup(led_pin, GPIO.OUT)
# Variable to keep track of the LED state (True for ON, False for OFF)
led_state = False
try:
while True:
if GPIO.input(touch_pin) == GPIO.HIGH:
led_state = not led_state # Toggle the LED state
GPIO.output(led_pin, led_state) # Turn on/off the LED based on the state
if led_state:
print("Light: ON")
else:
print("Light: OFF")
time.sleep(0.5) # Debounce delay to avoid multiple toggles on a single touch
else:
# Optional: You can include an LED OFF statement here if desired.
pass
except KeyboardInterrupt:
print("\nExiting...")
finally:
GPIO.cleanup()
Save it as lightbutton.py in Desktop
Open terminal and run the 2 commands
cd Desktop
sudo python3 lightbutton.py
Press the touch sensor and the light will enable, press it again and it will disable.
Ask your questions if you got some! (: