In a previous post, how to blink LED with Raspberry Pi 3 was introduced (Blink LED on Raspberry Pi 3 ). In this work, I just got an “output” from Raspberry Pi. Today, I will send an “input” to Raspberry Pi by using a tact switch.By sending an input, you can get Raspberry Pi to send an output. With this work, you can turn on/off your LED with a tact switch.
|Table of contents|
What you need
You may want a nipper to cut the legs of resister if they are long.
|Red LED||1||Vf = 2.0 V|
|Breadboard jumper wire||4||male-female|
|Breadboard jumper wire||3||male-male|
1. Tact switch
Tact switch physically has four legs, but they are electrically two since each two legs are internally connected. Make sure that you insert it to your breadboard in a right direction. Otherwise it will not work as a switch.
2. Hardware setup
Here is the circuit. There are two parts of circuit. One is for the tact switch and the other is for LED (same one with Blink LED on Raspberry Pi 3). The idea is that you use GPIO 24 as an input. A 10 kΩ resister works as a pull-down resistor so that GPIO 24 does not become unknown-status. Namely it gets 3.3 V when the switch is ON while 0 V when OFF. A pull-down resister is usually large to save power consumption. Thus I selected 10 kΩ . Raspberry Pi can judge if the switch is ON or OFF based the status of GPIO 24.
GPIO 25 is used as an output. Now you can control if GPIO 25 sends an output or not in response to the tact switch because Raspberry Pi can know the status of switch with GPIO 24(input).
This is the code. In Line 16, sleep (0.01) is different from sleep (0.5) to blink LED. This very short sleep time alows Raspberry Pi to do other tasks. Without this, it will use all the resource for the While loop of this program.
import RPi.GPIO as GPIO
from time import sleep
#Use GPIO number instead of Pin number
#Make GPIO 25 an output and GPIO 24 an input
if GPIO.input(24)==GPIO.HIGH:#if S/W ON
GPIO.output(25, GPIO.HIGH)#LED ON
GPIO.output(25, GPIO.LOW)#LED OFF
#No error occurs when Ctrl+C is pushed.
I successfully controlled the LED with the tact switch. At first, I forgot to put the jumper wire (male-male) between 3.3 V and the tact switch. When I run the code, I got an error. Then I checked the circuit and found the mistake. Except for that, I didn’t have any problem.
Also I did not connect my Raspbery Pi with my monitor when I set and run the code. I already installed VNC server on my Raspberry Pi. So I could controlled it with my laptop in which SSH and VNC viewer were installed. It was more easy to work with this technique as I did’t have to prepare not only a monitor but also a keyboard and a mouse for Raspberry Pi. So I could save space on my desk 🙂 You can refer my previous post for this technique (How to use your tablet/smartphone as screen).
For my experiment, I removed the pull-down resister (10 kΩ) to see what would happen. As a result, LED became unstable when I ran the code. This means that once GPIO 24 loses its LOW status, the system gets in unknown status. Consequently, LED repeats ON/OFF in a rapid way despite no tact switch operation. You may slightly find the unstable LED.
Sorry, it’s only written in Japanese….But you can still see the picture of circuit in this source 🙂