Raspberry Pi of Goemon

Raspberry Pi3 learning blog

Beginner Electric work

Control LED by tact switch

Post:01/10/2016 Update:

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

  1. Tact switch
  2. Hardware setup
  3. Code
  4. Result
  5. Reference

What you need

You may want a nipper to cut the legs of resister if they are long.

Name Qty
Resistor 1 330 Ω
Resistor 1 10kΩ
Red LED 1 Vf = 2.0 V
Breadboard 1
Breadboard jumper wire 4  male-female
Breadboard jumper wire 3 male-male
Tact switch 1
Nipper 1
Sponsored Link

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.tact-swtitch

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).




3. Code

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.

4. Result

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.



5. Reference

Sorry, it’s only written in Japanese….But you can still see the picture of circuit in this source 🙂


Sponsored Link

Sponsored Link

-Beginner, Electric work
-, ,

Related Posts


Blink LED on Raspberry Pi 3

Blink LED is a very first step for beginners. You can learn how to connect each electric component and control it with minimum numbers of components and a small chunk of code. Table of contents GPIO pin layout Hardware setup Code Result Reference The necessary parts and tools are here. The selection of resistor depends on LED’s Vf (forward voltage). You can usually find the value on its specification/catalog. Name Qty Resistor 1 330 Ω Red LED 1 Vf = 2.0 V Breadboard 1 Breadboard jumper wire (male-female) 2 Nippers 1 Sponsored Link 1. GPIO pin layout GPIO (General Purpose Input/Output) …


Utilize an internal pull-down resister

In the previous post, I checked how a pull-down resister functioned. When it’s there the system worked as expected. However once it’s taken off, then LED became unstable. Actually, Raspberry Pi 3 contains an internal pull-down resister and you can enable it by coding. Today, I’m gonna show you how you can enable an internal resister and how it works. Don’t worry, it’s very simple 🙂


How to deal with switch bounce

Today, I will deal with switch bounce. Then I will toggle the switch to control LED (OFF/ON). Now let’s start! Table of contents Switch bounce Bounce time Code Result Reference Sponsored Link 1. Switch bounce Switch bounce is a phenomenon that occurs when you use a physical contact switch such as a tact switch. When you push a switch, you may think it gets immediately ON. However, that’s not correct in the micro-scale world. When a switch is toggled, metal contacts of a switch come together and close. During this time, metal contacts are bouncing and that causes noise. This …


AD converter: LED responsive to room brightness

If you use a toggle switch, it’s enough  that you just monitor the HIGH/LOW signal. But what about monitoring brightness/darkness? If you allocate “0” to the night as it’s dark and “1” to the noon as it’s bright, what would you allocate to the morning and evening? Maybe you have no solution because brightness/darkness is not digital value, which can be expressed 0/1, but analogue value. Analogue value is infinitely continuous. For example, there is 0.5 between 0 and 1, there is 0.25 between 0 and 0.5……. But if you want to handle those analogue values with computer, what do …


Install Raspbian on Raspberry Pi 3

The very first thing you have to do is to install OS on your Raspberry Pi. Today I install Raspbian on which various programs for learning are available. Table of contents Format SD card Download NOOBS Hardware setup Raspbian installation & setting Connect to Internet OS update What you need Micro SD card (larger than 8GB) *class 10 is recommended PC connected to Internet Mouse Keyboard Micro USB power supply PC monitor (HDMI or DVI-D) Monitor cable (monitor <=> Raspberry Pi 3) Sponsored Link 1. Format SD card If your SD card is new, you can skip this step. Mine …