Does anyone explain to me how this code works, to turn on and off a led?


Well this would be the code, unfortunately I'm just watching videos ... I do not have a raspberry, where I live so cheap they are not ... what confuses me here is the conditional While, it says while it's true ... in In any case, when I see that the code is executed, the led is turned on and off every so often and I do not understand why, if it gives a false value, when it ends the while, it should not be executed again or if ... leaves the control structure and in my opinion only once should it turn on and off and nothing else ...

 import RPi.GPIO
 import time

 While true:
       RPi.GPIO.output(2, True)
       RPi.GPIO.output(2, False)

In this case, I see that he uses the Putty program, and not directly from the Raspbian that does it, I still do not know very well about this, but the next semester I will take this and I have to see it, since it will be in groups and economically I can not contribute much to this, so sorry if I miss very obvious mistakes, but for now is the only way I have to learn and your help on this page, thanks in advance to all.

asked by Alvaro Machado 13.12.2017 в 16:25

3 answers


It is an infinite cycle, in reality as you have it is not correct, I guess it is simply a typo because it must be while True , unless true is a previously defined variable.

A cycle while will be iterating while the exit condition that happens to it is evaluated as true ( True ). Therefore, by definition a while True will be running forever under normal conditions unless the cycle is broken internally with a break .

The sequence is therefore:

  • RPi.GPIO.setmode(RPi.GPIO.BCM) : it establishes that the BCM system ("Broadcom SOC channel") is used for the numbering of the pins.
  • RPi.GPIO.setup(2,RPi.GPIO.OUT) : pin 2 is set as output pin.

  • While True : as the condition is met, the cycle is entered.

  • RPi.GPIO.output(2, True) : the channel status is set as GPIO.HIGH, which allows pin 2 to supply voltage and turn on the led.

  • time.sleep(1) : we wait a second.

  • RPi.GPIO.output(2, False) : we set the channel status in GPIO.LOW and pin 2 stops supplying voltage, led turns off.

  • time.sleep(1) : we wait another second.

  • Once the code block contained in the while is executed, the cycle exit condition is evaluated again (step 3), as it is still true, everything is repeated again (steps 4, 5, 6, 7 and 8) and so infinitely.

  • The result will therefore be a led that stays on for a second and then off for another second repeating this cycle infinitely until the program is interrupted by some cause external to the code.

    To understand what the cycle does, you do not need the Raspberry, you just need to replicate the code using print :

    import time
    While True:
        print("Led encendido.")
        print("Led apagado")
    answered by 13.12.2017 / 17:06

    If you can not have a Raspberry Pi, and while foundation does not publish the long-awaited OnLine simulator, there are several emulation options like Quemu (Of which I can not comment since I have not tried it), there is also the raspberry-pi-web-simulator from Microsoft (logically for your system (IoT), of which I can not comment), or the Raspberry Pi Desktop official that you can virtualize with Oracle VM VirtualBox for example. Logically these virtualizations will not allow you to execute any code that tries to access the GPIO because they are simply not present, but you can print outputs or send data to the log to be able to debug your codes. You can test your codes and visualize the desktop as it would look on an original Raspberry Pi.

    answered by 03.03.2018 в 13:32

    That While True will never end because it enters the cycle every time the condition is true (in this case the condition is True, and it will never change, therefore always is going to be true), the translation into "Spanish" of the code would be:

    repetir infinitamente {

    I recommend reading about control structures if you still have doubts.


    answered by 13.12.2017 в 17:06