Savage///Chats

Hardware Development => Member Projects => Topic started by: granz on Apr 30, 2025, 06:02 PM

Title: MicroPython On A Pi Pico
Post by: granz on Apr 30, 2025, 06:02 PM
While waiting for my PicoCalc (https://savagechats.com/index.php?topic=364.0,) I thought that I should look into MicroPython. It was no trouble to install MicroPython (https://forums.raspberrypi.com/viewtopic.php?t=380089) onto one of my Pico 2Ws. A simple "screen /dev/ttyACM0" got me connected, and my Pico responded with MicroPython signon screen:
(https://savagechats.com/proxy.php?request=http%3A%2F%2Ffiles.granzeier.com%2FDownloads%2FSavageCircuits%2FuPython-Pico.jpg&hash=2f7bc678f603aaca741dce61515f19dd6a73ed5b)

Now, I needed an introduction to MicroPython. The last time that I played with Python was in 2018, before my last back surgery. While Python gets a lot of fanfare and people think that it is the greatest programming language for beginners, I have to disagree. Python may be better for beginners than C, or C++, but it is no match for the ease of teaching BASIC to beginners. So, on to look for a good MicroPython tutorial. Oh yeah, I don't want to use Thonny, of Visual Studio, or any other IDE - I just want to use the serial interface to do my programming.

There is a tutorial-ish  ::) at:https://www.kevsrobots.com/learn/micropython/, but it doesn't really give a good structured, incremental approach to learning. So far, I have been able to find out on which Pico my system is running, turn on, and off, an LED, and a few other simple things. But, when I try to do actual programming, I get errors - it's like the thing is taking immediate commands, but not storing them as a program.

If I remember correctly, there is something to do with tabs (any language that depends on white space should be out-lawed,) but I'm not sure what. The next step is probably to dig into regular Python, and see if writing a program in MicroPython is like regular Python.

Do any of you know of a free, good non-IDE MicroPython course, preferably oriented to the Pico - or better yet the Pico 2W.

Title: Re: MicroPython On A Pi Pico
Post by: Chris Savage on Apr 30, 2025, 09:56 PM
Quote from: granz on Apr 30, 2025, 06:02 PMDo any of you know of a free, good non-IDE MicroPython course, preferably oriented to the Pico - or better yet the Pico 2W.

Jeff_T is the resident Micro-Python wizard here, in my opinion. He has a few threads, including this one (https://savagechats.com/index.php?topic=133.0) about Micro=Python. It may be that he could recommend something.
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 01, 2025, 06:55 AM
Thanks, Chris. I sent him a PM.
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 01, 2025, 08:36 PM
Micropython has a slight edge over c/c++ for me just because I can create considerably quicker, everyone has their preference.

I replied to your pm granz, if you have something you would like to do or need an explanation on something elementary I'll see what I can do to help if you need it.

I don't think it matters too much but I would practice on regular Python 3 and micropython at the same time, the differences are not that great. The standard Python install comes with a minimal editor called IDLE

I did a couple of online classes but I found Codemy a great place to start to learn Python.

https://www.youtube.com/watch?v=a6dTFj8Sr68&list=PLCC34OHNcOtrZTjsC5qtn_X9eRAmGmGOI
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 02, 2025, 08:21 AM
Jeff,

Thank you for your help here. I went to the video that you suggested, but John (the teacher) is using the Sublime IDE. He also starts off with installing Python (I use Linux which already has Python) and then installing BASH (the default shell for Linux Mint.) So, after watching a few minutes of his first video, I jumped to the third video, where he starts work in the Python IDE.

However, this let me find another video (https://www.youtube.com/watch?v=nCDZ1zvoZg0) where the presenter at least starts in the interactive Python shell. Here he writes a Python "program" and then runs the "program." Unfortunately, this is not a program, in the Python Interactive Shell he only enters commands, directly, not a program (a list of commands for the computer to execute on its own.) Before he gets to actually writing a program, he exits the Interactive Python Shell, and loads up an editor. Just to make sure that I understood this, I went into the terminal (command line, in Windows) and used Nano (a standard text editor in Linux) to create the same Python Hello World program. Then I was able to run the program by just entering the "hello.py" command. It worked as expected. But, the thing is, that I want to run this completely, and totally, within the Pico itself - no help from the PC. This way, I can write, and run, MicroPython programs even if I only have one of my old terminals, and no PCs available.

In the old GW-BASIC (or the TRS-80, Sinclair, Atari or Commodore BASICs) the interactive environments allowed you to write an actual program (using line numbers to indicate to the interpreter that you were writing program commands.) You could then save this program to disk (or tape) and run it later on. Is there any way to write an actual program inside the Interactive Python Shell without using an external editor?

Moving on to the Raspberry Pi Pico, running MiicroPython, I tried using a built-in editor, but of course there does not appear to be one installed. In PicoMite BASIC, there is a built-in editor for writing programs, which can then be saved and run later on. Is there an editor in MicroPython (which is supposed to be a Python that is fully contained in a microcontroller) that I didn't find? If not, do you know how to take a MicroPython program, written on a PC, and then download it to the Pico?
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 02, 2025, 11:08 AM
Hi granz, did you try the app I suggested, mpremote, that will do everything you need. If you add Nano to your environment variables mpremote will bring Nano up so that you can write and save sketches to the device, mpremote is a terminal app.

I'll try and add a video or two, I'm having mic problems at the moment so I need to see if I can work around that.
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 02, 2025, 12:29 PM
Quote from: Jeff_T on May 02, 2025, 11:08 AMHi granz, did you try the app I suggested, mpremote, that will do everything you need. If you add Nano to your environment variables mpremote will bring Nano up so that you can write and save sketches to the device, mpremote is a terminal app.

I'll try and add a video or two, I'm having mic problems at the moment so I need to see if I can work around that.
Jeff, no, I got the idea, from your description, that mpremote was an external IDE (running on a PC.) Now, having looked into it, as far as I can tell it is an IDE (or performs the same function as Thonny.) I still want something that runs MicroPython completely, and totally, on the Pico.

I guess that I'm stubborn that way - but I have many tablets, PCs and phones that all have a serial (RS-232, TTL or USB serial) terminal capability. I want to be able to take my Pico (just the chip alone while learning, and then a Pico on a dev system (https://zappbots.altervista.org/forum/viewtopic.php?f=40&t=327 or equivalent,) and connect with whatever terminal (even an old SWTPC CT-82 - https://deramp.com/swtpc.com/CT_82/CT82_Index.htm :P ) is available at the time, and place.

Is MicroPython really unable to create programs (scripts) inside the Interactive Python Shell (REPL)?  :( I have still been unable to figure out any way to edit program files on the Pico itself.

On the other hand, there is a project to run MicroPython on the PicoCalc (https://github.com/zenodante/PicoCalc-micropython-driver), and that has an editor built in (along with a file system for saving programs on the system. Since, I currently have two of those PicoCalcs on order (https://savagechats.com/index.php?topic=364.0) maybe I'll just wait until I get those in, and load up the MicroPython .UF2 file for my Picos - I ordered the Pico 2Ws to use instead of the Pico that is included with the PicoCalc. The 2W will give me more RAM, more Flash and WiFi, so...

In the mean time, just to get a little bit of Python (since MicroPython is supposed to be a complete Python on a microcontroller,) I'll play around with things like https://realpython.com/python-repl/ - unless you know of a way to do what I was hoping for above.
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 02, 2025, 01:40 PM
Here is my demo granz

https://youtu.be/k5hD6wOChyY
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 02, 2025, 04:03 PM
mpremote is widely used by professionals and comes with some really cool features, included is the REPL which allows you to evaluate code snippets that run in memory.

Here is my REPL + mpremote demo

https://youtu.be/aOwNxPq6cjY

I don't see where you could get a smaller footprint, you have to have some means of editing and storing files.
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 02, 2025, 07:41 PM
Jeff,

Please don't think that I am ungrateful, I am thankful for your help. I realize that my requirements are rather unusual, and specific, but that is where I am right now.

Quote from: Jeff_T on May 02, 2025, 04:03 PMmpremote is widely used by professionals and comes with some really cool features, included is the REPL which allows you to evaluate code snippets that run in memory.

Here is my REPL + mpremote demo

https://youtu.be/aOwNxPq6cjY
OK, I tried that - connecting through screen /dev/ttyACM0 gave me the REPL that is actually on the Pico, through the USB serial connection. It worked exactly as you showed in your video (but with the screen program rather than mpremote) - the LED flashed correctly (pin 16, because that is where I had the LED already.) This is what I wanted, however when I used CTRL-C to break the running loop, I could not figure out how to restart the flashing. How do I do that?
Quote from: Jeff_T on May 02, 2025, 04:03 PMI don't see where you could get a smaller footprint, you have to have some means of editing and storing files.
Yes, that is what I am asking - is an external editor/IDE required in MicroPython (other than, apparently, the PicoCalc MicroPython)? And, is there no way (again, other than the PicoCalc version) to store one or more, .PY programs on the Raspberry Pi Pico so that I can load one of those Python programs and run it later on, from the serial USB connection?
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 02, 2025, 09:37 PM
Hi granz, the two videos are different in the fact that video 1 writes the program to flash and it will retain that program through power cycles. Video 2 uses the REPL that evaluates a snippet in RAM and is lost after a reset or power cycle.

The first video is the way to go when you finalize your code, naming the file "main.py" causes the program to automatically run after reset or power cycle. Most microcontrollers have enough flash to store several python files and mpremote is capable of organizing these files and moving them between device and PC. The Picocalc is just a microcontroller plugged into its own interface which acts as an IDE/controller, in the end the objective is to get a .py file written to the flash.

I'm thinking of progressing with the mpremote videos let me know if you think that would be useful.
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 02, 2025, 09:47 PM
Hi granz, I'm just browsing through the mpremote docs and I see that there is an option to save a REPL session to a file, I didn't realize that. There is so much about mpremote I need to learn so forgive my mistakes.  ;D
Title: Re: MicroPython On A Pi Pico
Post by: Chris Savage on May 02, 2025, 09:59 PM
Quote from: Jeff_T on May 02, 2025, 09:47 PMHi granz, I'm just browsing through the mpremote docs and I see that there is an option to save a REPL session to a file, I didn't realize that. There is so much about mpremote I need to learn so forgive my mistakes.  ;D

We're still all learning from you (and your mistakes).  ;)
Title: Re: MicroPython On A Pi Pico
Post by: Chris Savage on May 13, 2025, 11:30 AM
Quote from: Chris Savage on May 02, 2025, 09:59 PMWe're still all learning from you (and your mistakes).  ;)

That is to say, you're still the foremost authority for python on this site.  ;)  I'm just over here watching and learning while I take on my current projects.
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 13, 2025, 05:46 PM
Yes , read and appreciated Chris TY. I will tell you one thing though some of these real Python gurus blow me away with their knowledge. I think providing accurate information or directing to a source that knows better is my angle of attack.

I am waiting for @granz to take delivery of his Picocalc and give us a demo of its capabilities, I know very little about any kind of Pico so I need to buy a couple, probably with wireless.

The past few days I have been programming my version of an infra red transmitter using micropython and the RMT module. I might post more about that later, maybe in the tutorials. Lots of that stuff out there but you can't beat DIY.
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 14, 2025, 06:58 AM
Quote from: Jeff_T on May 13, 2025, 05:46 PMI am waiting for @granz to take delivery of his Picocalc and give us a demo of its capabilities, I know very little about any kind of Pico so I need to buy a couple, probably with wireless.
Yeah, Jeff. The Pico is a pretty great little board. It runs many different languages. The best prices that I have been seeing are $5.49 (non-WiFi, https://www.amazon.com/Pre-Soldered-Raspberry-Microcontroller-Dual-Core-MicroPython/dp/B0CW3BR259) but that is a "limited time only" deal, and no mention of when it expires,) and $9.95 (for the WiFi version, https://www.amazon.com/Vis-Viva-Raspberry-Wireless-Sticker/dp/B0BHM88FCT).
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 14, 2025, 10:40 AM
thanks definitely on this weeks shopping list. Have you ever looked at the regular Python forums they cover all subjects GUI, Games, Networking. Unfortunately it's not Micropython but close enough to get some good tips. https://python-forum.io/

Also in March Sparkfun started to roll out boards loaded with Micropython with tutorials.

https://learn.sparkfun.com/tutorials/tags/micropython
Title: Re: MicroPython On A Pi Pico
Post by: Jeff_T on May 14, 2025, 02:34 PM
@granz I ordered two of the modules with wireless with soldered headers from your link, I got a break for ordering two and ended up paying 14.95 plus the taxes.
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 14, 2025, 04:17 PM
Quote from: Jeff_T on May 14, 2025, 10:40 AMthanks definitely on this weeks shopping list. Have you ever looked at the regular Python forums they cover all subjects GUI, Games, Networking. Unfortunately it's not Micropython but close enough to get some good tips. https://python-forum.io/
Thanks, but Python is just a minor interest to me. So, I'm going to wait for my PicoCalcs, and play with Python in portable format.
Title: Re: MicroPython On A Pi Pico
Post by: granz on May 14, 2025, 04:19 PM
Quote from: Jeff_T on May 14, 2025, 02:34 PM@granz I ordered two of the modules with wireless with soldered headers from your link, I got a break for ordering two and ended up paying 14.95 plus the taxes.
Nice, glad to be able to help. MicroPython installed easily on my Pico 2W, and seemed to work with Thonny (and probably would work with mpremote.)