MOCKSTACKS
EN
Questions And Answers

More Tutorials









Raspberry Control LED On and Off with Button


In this project, we use a button to turn the LED on and off. When the button is pressed, the LED light is switched on. When the button is released, the LED light is switched off.

Hardware Connection

In this project, the electronic hardware we need to use is as follows:

• Raspberry Pi Pico

• Grove Shield for Pi Pico

• Grove - LED Pack

• Grove - Button


The Grove - Button is a simple module. When the button is pressed, its return value is 1. When the button is released, its return value is 0.Connect Pico and the Shield. Use Grove data cables to connect the button to D16, and the LED to D18.

Write a Program

At the beginning of the program, we need to import the machine library and use the Pin function in the library to set the pins for the two electronic modules.

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

In addition to the previous program, when we introduce the function in the library, apart from using “Pin.OUT" to set Pin 18 as the output pin, we also need to use “Pin.IN" to set Pin 16 as the input pin to receive the data returned by the button.

After setting the pins, we can use the following code to obtain the value returned by the button:

1 val = BUTTON.value( )

In this line of code, we use the value() function to read the return value of the button. If you still remember, we have also used the value() function to write the value for the LED in the previous program.The difference between them is that when a value is written in brackets of the value() function, it will output the value to the pin, for example:

1 LED.value(1)

And when there is no value in brackets, it will read the input value of the pin. Finally, we use the conditional statement "if" to evaluate the following condition. When the button is pressed, the return value is 1 and the LED is switched on; otherwise, the LED remains off:

1 while True:
2 val = BUTTON.value) 
3 if val == 1:
4 LED.value(1) 
5 else:
6 LED.value(0)

Use a USB cable to connect Pico to the computer, click the “run" button to save the program to any location. Now, press the Grove button to see the program in action!

Conclusion

In this page (written and validated by ) you learned about Raspberry Control LED On and Off with Button . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Button Light.



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: mockstacks@gmail.com.


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 Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.