Questions And Answers

More Tutorials

Raspberry Realize Blink with While loop

In this project, we will use the while-loop to achieve the effect of a continuously flashing LED.

Write a Program

Using the while loop, we can easily make the program repeat indefinitely. We only need to change the “for i in range(10)" in the program to “while True":

1 while True
2 LED.value(1) 
3 utime. sleep (1) 
4 LED.value(0) 
5 utime. sleep (1)

The “while True" is a use of while-loop statement. Unlike a general while-loop that only executes the loop when a certain condition is met, this “while True" is an indefinite loop statement, which means that the program will be executed repeatedly and continuously until it is artificially terminated.

The complete code is as follows:

1 import machine 
2 import utime 
3 LED = machine. Pin(16, machine. Pin.OUT)
5 while True:
6 LED.value(1) 
7 utime. sleep (1) 
8 LED.value(0) 
9 utime. sleep (1)

Click the “run" button again, and the LED connected to the Shield starts flashing. Unless the process is artificially stopped, the LED will continue to flash.


In this page (written and validated by ) you learned about Raspberry Realize Blink with While loop . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Digital Signal in Circuit Playing with LED.

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.