Thursday, May 2, 2024
HomePythonEntry the REPL and add MicroPython libraries utilizing rshell

Entry the REPL and add MicroPython libraries utilizing rshell


MicroPython is an implementation of the Python 3 programming language, optimized to run microcontrollers. It is one of many choices out there for programming your Raspberry Pi Pico and a pleasant pleasant strategy to get began with microcontrollers.

MicroPython will be put in simply in your Pico, by following the directions on the Raspberry Pi web site (click on the Getting Began with MicroPython tab and observe the directions).

After that time you would possibly get a bit caught. The Pico documentation covers connecting to the Pico from a Pi, so when you’re eager to code from your individual pc you may want one thing else. One choice is the Thonny IDE which you employ to put in writing and add code to your Pico. It is acquired a pleasant pleasant interface for working with Python.

However when you do not wish to change your IDE, or desire a strategy to talk along with your Pico from the command line? You are in luck: there’s a easy instrument for accessing the MicroPython REPL in your Pico and importing customized Python scripts or libraries you could want to use: rshell

On this tutorial I am going to take you thru working with MicroPython in rshell, coding reside and importing customized scripts to your Pico.

Putting in rshell

Rshell itself is constructed on Python (not MicroPython) and will be put in and run regionally in your important machine. You possibly can set up it like some other Python library.

bash

python -m pip set up rshell

Sadly, the present model of rshell doesn’t at all times play properly with the Raspberry Pico. You probably have issues you possibly can set up a set model within the pico department from the rshell repository. You possibly can set up this straight from Github with the next —

bash

python -m pip set up https://github.com/dhylands/rshell/archive/pico.zip

This may obtain the newest model of the pico department (as a .zip) and set up this in your Python surroundings.

As soon as put in, you should have entry to a brand new command line instrument rshell.

The rshell interface

To make use of rshell from the command line, enter rshell at your command immediate. You will notice a welcome message and the immediate will flip inexperienced, to point you are in rshell mode.

rshell on Windows 10The rshell interface on Home windows 10

rshell on macOSThe rshell interface on macOS

If earlier pip set up labored however the rshell command would not work, then you will have an issue along with your Python paths.

To see the instructions out there in rshell, enter assist and press Enter.

python

assist

Documented instructions (sort assist <subject>):
========================================
args    cat  join  date  edit  filesize  assist  mkdir  rm     shell
boards  cd   cp       echo  exit  filetype  ls    repl   rsync

Use the exit command to exit rshell.

You possibly can exit rshell at any time by getting into exit or urgent Ctrl-C. As soon as exited the immediate will flip white.

The essential file operation instructions are proven under.

  • cd <dirname> change listing
  • cp <from> <to> copy a file
  • ls record present listing
  • rm <filename> take away (delete) a file
  • filesize <filename> give the dimensions of a file in bytes

If you happen to sort ls and press enter you will note a list of your present folder in your host pc. The identical goes
for any of the opposite file operations, till we have join a board and opened it is file storage — so watch out!
We’ll have a look at how to hook up with a MicroPython board and work with the recordsdata on it subsequent.

Connecting to your Pico with rshell

Enter boards to see an inventory of MicroPython boards related to your pc. If you happen to do not
have any related boards, you may see the message No boards related.

In case your board is not related plug your Pico in now. You can use the join command to hook up with the board,
however for that you’re going to must know which port it’s on. Save your self some effort and simply restart rshell to attach
robotically. To do that, sort exit and press Enter (or press Ctrl-C) to exit after which restart rshell by getting into rshell once more on the immediate.

If a board is related while you begin rshell you will note one thing like the next…

python

C:UsersGebruiker>rshell
Connecting to COM4 (buffer-size 128)...
Attempting to hook up with REPL  related

Or an equal on macOS…

python

Martins-Mac: ~ mfitzp$ rshell
Connecting to /dev/cu.usbmodem0000000000001 (buffer-size 128)
Attempting to hook up with REPL  related

…which reveals you’ve got related to the MicroPython REPL on the Raspberry Pi Pico. As soon as related the boards command
will return some details about the related board, like the next.

python

pyboard @ COM4 related Epoch: 1970 Dirs:

The title on the left is the sort of board (Pico seems as pyboard) and related port (right here COM4). The label on the finish Dirs: will record any recordsdata
on the Pico — at present none.

Beginning a REPL

With the board related, you possibly can enter the Pico’s REPL by getting into the repl command. This may return one thing like the next

python

repl
Coming into REPL. Use Management-X to exit.
>
MicroPython v1.14 on 2021-02-14; Raspberry Pi Pico with RP2040
Sort "assist()" for extra data.
>>>
>>>

You are actually writing Python on the Pico! Attempt getting into print("Hi there!") on the REPL immediate.

python

MicroPython v1.14 on 2021-02-14; Raspberry Pi Pico with RP2040
Sort "assist()" for extra data.
>>>
>>> print("Hi there!")
Hi there!

As you possibly can see, MicroPython works identical to regular Python. If you happen to enter assist() and press Enter,
you may get some fundamental assist details about MicroPython on the Pico. Helpfully, you additionally get a
small reference to how the pins on the Pico are numbered and the other ways it’s a must to management them.

python

Sort "assist()" for extra data.
>>> assist()
Welcome to MicroPython!

For on-line assist please go to https://micropython.org/assist/.

For entry to the {hardware} use the 'machine' module.  RP2 particular instructions
are within the 'rp2' module.

Fast overview of some objects:
  machine.Pin(pin) -- get a pin, eg machine.Pin(0)
  machine.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p
    strategies: init(..), worth([v]), excessive(), low(), irq(handler)
  machine.ADC(pin) -- make an analog object from a pin
    strategies: read_u16()
  machine.PWM(pin) -- make a PWM object from a pin
    strategies: deinit(), freq([f]), duty_u16([d]), duty_ns([d])
  machine.I2C(id) -- create an I2C object (id=0,1)
    strategies: readfrom(addr, buf, cease=True), writeto(addr, buf, cease=True)
             readfrom_mem(addr, memaddr, arg), writeto_mem(addr, memaddr, arg)
  machine.SPI(id, baudrate=1000000) -- create an SPI object (id=0,1)
    strategies: learn(nbytes, write=0x00), write(buf), write_readinto(wr_buf, rd_buf)
  machine.Timer(freq, callback) -- create a software program timer object
    eg: machine.Timer(freq=1, callback=lambda t:print(t))

Pins are numbered 0-29, and 26-29 have ADC capabilities
Pin IO modes are: Pin.IN, Pin.OUT, Pin.ALT
Pin pull modes are: Pin.PULL_UP, Pin.PULL_DOWN

Helpful management instructions:
  CTRL-C -- interrupt a working program
  CTRL-D -- on a clean line, do a smooth reset of the board
  CTRL-E -- on a clean line, enter paste mode

For additional assistance on a selected object, sort assist(obj)
For an inventory of accessible modules, sort assist('modules')

You possibly can run assist() within the REPL any time you want a reminder.

Whereas we’re right here, lets flash the LED on the Pico board.

Enter the next on the REPL immediate…

python

from machine import Pin
led = Pin(25, Pin.OUT)

led.toggle()

Each time you name led.toggle() the LED will toggle from ON to OFF or OFF to ON.

To exit the REPL at any time press Ctrl-X

Importing a file

MicroPython comes with a whole lot of built-in assist for easy gadgets and communication protocols — sufficient to construct some fairly enjoyable issues simply by hacking within the REPL. However there are additionally a whole lot of libraries out there for working with extra complicated {hardware}. To make use of these, you want to have the ability to add them to your Pico! As soon as you possibly can add recordsdata, you can even edit your individual code regionally by yourself pc and add it from there.

To maintain issues easy, lets create our personal “library” that adjusts the brightness of the LED on the Pico board — thrilling I do know. This library incorporates a single perform ledon which accepts a single parameter brightness between 0 and 65535.

python

from machine import Pin, PWM

led = PWM(Pin(25))

def ledon(brightness=65535):
    led.duty_u16(brightness)

Don’t fret when you do not perceive it, we’ll cowl how this works later. The vital bit now’s getting this in your Pico.

Take the code above and reserve it in a file named picoled.py in your important pc, in the identical folder you are executing rshell from. We’ll add this file to the Pico subsequent.

Begin rshell in case you are not already in it — search for the inexperienced immediate. Enter boards on the immediate to get an inventory of related boards.

bash

pyboard @ COM4 related Epoch: 1970 Dirs:

To see the listing contents of the pyboard system, you possibly can enter:

You ought to see nothing listed. The trail /pyboard works like a digital folder that means you possibly can copy recordsdata to this
location to have the uploaded to your Pico. It’s only out there by a pyboard is related. To add a file, we
copy it to this location. Enter the next on the immediate.

bash

cp picoled.py /pyboard/picoled.py

After you press Enter you may see a message confirming the copy is going down

python

C:UsersGebruiker> cp picoled.py /pyboard
Copying 'C:UsersGebruiker/picoled.py' to '/pyboard/picoled.py' ...

As soon as the copy is full, run boards once more on the immediate and you may see the file listed after the Dirs: part, displaying that it is on the board.

bash

C:UsersGebruiker> boards
pyboard @ COM4 related Epoch: 1970 Dirs: /picoled.py /pyboard/picoled.py

You can even enter ls /pyboard to see the itemizing straight.

bash

C:UsersGebruiker> ls /pyboard
picoled.py

If you happen to ever must add a number of recordsdata, simply repeat the add steps till all the things is the place it must be. You possibly can at all times drop out and in of the REPL to verify issues work.

Utilizing uploaded libraries

Now we have uploaded our library, we are able to use it from the REPL. To get to the MicroPython REPL enter the repl command in rshell as earlier than.
To make use of the library we uploaded, we are able to import it, identical to some other Python library.

python

MicroPython v1.14 on 2021-02-14; Raspberry Pi Pico with RP2040
Sort "assist()" for extra data.
>>>
>>> import picoled
>>> picoled.ledon(65535)
>>> picoled.ledon(30000)
>>> picoled.ledon(20000)
>>> picoled.ledon(10000)

Or to pulse the brightness of the LED…

python

>>> import picoled
>>> import time
>>> whereas True:
...     for a in vary(0, 65536, 10000):
...         picoled.ledon(a)
...         time.sleep(0.1)

Auto-running Python

To date we have been importing code and working it manually, however when you begin constructing tasks you may need your code
to run robotically.

When it begins, MicroPython runs two scripts by default: boot.py and important.py, in that order. By importing your individual
script with the title important.py it’s going to run robotically each time the Raspberry Pico begins.

Let’s replace our “library” to turn out to be an auto script that runs at startup. Save the next code to a script named important.py.

python

from machine import Pin, PWM
from time import sleep

led = PWM(Pin(25))

def ledon(brightness=65535):
    led.duty_u16(brightness)


whereas True:
    for a in vary(0, 65536, 10000):
        ledon(a)
        sleep(0.1)

In rshell run the command to repeat the file to important.py on the board.

bash

cp important.py /pyboard/important.py

Do not copy this file to boot.py — the loop will block the REPL startup and you will not be capable to hook up with your Pico to delete it once more!
If you happen to do this, use the Resetting Flash reminiscence directions to clear your Pico. You will have to re-install MicroPython afterwards.

As soon as the important.py file is uploaded, restart your Pico — both unplug and re-plug it, or press Ctrl-D within the REPL — and the LED will begin pulsing robotically.
The script will proceed working till it finishes, or the Pico is reset. You possibly can change the important.py script at any time to
change the habits, or delete it with.

What’s subsequent?

Now you possibly can add libraries to your Pico you may get experimenting with the various MicroPython libraries which are out there.

If you happen to’re on the lookout for some extra issues to do with MicroPython in your Pico, there are some MicroPython examples out there from Raspberry Pi themselves, and likewise the MicroPython documentation for language/API references.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments