embeddedmicropythonraspberry-pi-pico

IndentationError from downloaded example code


For a Raspberry Pi Pico using Thonny IDE and MicroPython I downloaded code:

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
    led.toggle()
    time.sleep(0.5)

I receive:

Traceback (most recent call last):
  File "<stdin>", line 10
IndentationError: unindent doesn't match any outer indent level

Solution

  • You need to indent your code properly:

    from machine import Pin
    import time
    led = Pin(15, Pin.OUT)
    button = Pin(14, Pin.IN, Pin.PULL_DOWN)
    while True:
        if button.value():
            led.toggle()
            time.sleep(0.5)