Questions And Answers

More Tutorials

Raspberry Button Light

By observing the implementation of the program in Project 1, we find that the LED only lights up when the button is pressed. When we release it, the LED will turn off immediately. This is not the same as a typical button light in our daily life, which turns on when pressed, and off when pressed again. Let’s now try to achieve this.

Write a Program

First, import the required machine library and utime library, and configure the pins for the button and the LED.

1 import machine 
2 import utime 
4 BUTTON = machine.Pin(16, machine. Pin.IN) 
5 LED = machine.Pin(18, machine. Pin.OUT)

Previously, we directly used the value returned by the button to control the LED, and the button returns 1 only when it is pressed and returns 0 when it is released – producing the outcome that we observed.Therefore, we cannot directly use the button to control the LED, but will instead need to create a variable to indirectly control the LED state.

First, we need to create a new variable in the program and set its initial value to 0

1 val = 0

Then, write a program to run in an indefinite “while True" loop. Each time the button is pressed, the value of this variable will be increased by 1:

1 while True:
2 if BUTTON.value() == 1:
3 val = val+1 
4 sleep(1)

We just need to switch the value of the variable between 0 and 1. Thus, when the value of the variable is increased to 2, we will reset it to 0. Here, we can use "elif" to add the additional condition to the “if" statement:

1 while True:
2 if BUTTON.value() == 1:
3 val = val+1
4 sleep(1) 
5 elif val == 2:
6 val = 0 
7 sleep(1)

In this way, the value of the variable “val" that we set can be switched between 0 and 1 every time we press the button. Finally, we can use this variable to control the LED. The complete program is as follows:

1 import machine 
2 import utime 
4 BUTTON = machine.Pin(16,machine. Pin.IN) 
5 LED = machine. Pin(18, machine. Pin.OUT) 
7 val = 0 
9 while True: 
10 if BUTTON.value() == 1:
11 val = val+1
12 utime. sleep(1) 
13 elif val == 2:
14 val = 0
15 utime. sleep(1) 
16 LED.value (val)


In this page (written and validated by ) you learned about Raspberry Button Light . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Analog Signal in Circuit 1 Rotary Angle Sensor.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.