Continuing from the “Getting Started” post that didn’t really get to anything exciting, we’re going to play with the REPL and blink a light!

Getting to the REPL

On linux, you can install a program called Picocom that will get you the REPL over the serial port.

sudo apt-get install picocom

To use it call the program and give it your device ID and the baud rate:

sudo picocom /dev/ttyUSB0 -b115200

And you should now be at the triple chevron of the MicroPython REPL. MicroPython is based on Python 3, so remember to enclose your print statements!

micropython REPL

When you’ve had your fill of 2 + 2 = 4, you can escape the REPL with CTRL – A, and then quit Picocom with CTRL-Q.

Blinkenlights!

There is an on-board LED on the Wemos (and most ESP8266s) that we’ll start with to get used to using the GPIO (General Purpose Input Output) pins. The LED is hooked up to GPIO 2, to use this we’ll import Machine and assign pin 2 as an output:

>>> from machine import Pin
>>> pin2 = Pin(2, Pin.OUT)

You should immediately see a blue LED light up on the board, which is weird and unexpected. MicroPython explains in the docs that depending on how the board is wired, led LED might be active-on or active-off. When we set our pin2 as a output, it defaulted to off, but we have an active-low LED so it turns on. To prove this, we can try turning it “off” and “on”

>>>pin2.off()

Nothing happens, because MicroPython already has pin2 set to off.

>>>pin2.on()

The LED now turns off.

>>>pin2.off()

And it’s back. Clearly our LED is active-off, so what can we do about this? That’s covered in the docs too. Using Signal from the machine library, we can specify inverted logic for our pin.

>>>from machine import Pin, Signal
>>>pin2 = Signal(2, Pin.OUT, inverted=True)

Now our LED is still on from our last command to turn it off, which was still inverted. Now we can try actually turning it off.

>>>pin2.off()

Ta-Da! The LED turned off

>>>pin2.on()

And it’s back, working as we expected it to the first time around. That’s it for this post, next time I’ll be working on getting the D1 mini pro connected to WiFi and hopefully actually doing something slightly more interesting.

1 Comment

  1. Fred

    Reply

    Hi in the line

    pin2 = Signal(2, Pin.OUT, inverted=True)

    I think it should be “invert” rather than “inverted”

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.