
MicroPython - neilpanchal
https://micropython.org/
======
neilpanchal
MicroPython is stellar. If you're building an embedded system for people to
hack and customize, make the highest level abstraction of the embedded system
in MicroPython so that users can easily modify/patch/contribute/hack your
system with ease.

This is what Arduino and Arduino IDE did for millions of hobbyist, but it is
time to ditch C/C++ for MicroPython.

In this [1] PyCon talk by Matt Trentini, a demo of QR code generation proves
that with plain C and with MicroPython bindings, the performance is identical
- 11.5ms. MicroPython _is_ slow, and if the QR code library was written in
pure MicroPython, it would take 1500ms. Therefore, it is critical to put
performance demanding code in C/C++ modules and compile a MicroPython
bindings.

I think this is one of the best things that's happened to embedded development
in a long time. Lua is nice but IMO Python is even better :)

There is also Async IO library support for MicroPython. So ISRs can trigger
coroutines!

[1]
[https://www.youtube.com/watch?v=437CZBnK8vI](https://www.youtube.com/watch?v=437CZBnK8vI)

~~~
dheera
The one big issue I've had with MicroPython is that it's a PITA to manage
files on it. I use this hacky tool called "ampy" to do it, and wish that the
device could just be mounted as a media device. CircuitPython, however, does
accomplish this, with the microcontroller showing up just like a USB drive.

~~~
dcmorton
Bit of shameless plug, but I put this [0] together to make it easy to build a
simple, flashable MicroPython image with your code included.

[0] - [https://github.com/dcmorton/esp32-micropython-image-
builder](https://github.com/dcmorton/esp32-micropython-image-builder)

------
dang
If curious see also

2014
[https://news.ycombinator.com/item?id=7840566](https://news.ycombinator.com/item?id=7840566)

also 2014
[https://news.ycombinator.com/item?id=6996692](https://news.ycombinator.com/item?id=6996692)

2015
[https://news.ycombinator.com/item?id=9558969](https://news.ycombinator.com/item?id=9558969)

2016
[https://news.ycombinator.com/item?id=11181158](https://news.ycombinator.com/item?id=11181158)

Many submissions but those seem to be the interesting threads.

~~~
albertzeyer
There was also the submission about Pycopy recently, a fork of MicroPython:
[https://news.ycombinator.com/item?id=23446721](https://news.ycombinator.com/item?id=23446721)

------
pansa2
MicroPython seems to be heavily focused on embedded _systems_.

Is there any support for embedding it within an _application_ , i.e. as a
replacement for Lua? Or is there another lightweight Python implementation (or
Python-like language) that supports this?

~~~
toastal
Why would you want to replace Lua in this space? At least why replace with
another dynamically typed, imperative scripting language, but without tail-
call optimization? What would be the gain?

~~~
mottosso
I was wondering the same as the parent, and in my case it's library support
(e.g. NumPy) and compatibility/familiarity in the space (VFX) where Python is
everywhere and Lua is unheard of.

~~~
otterlicious
If you want NumPy support in particular you basically have to use CPython.

The only other implementation that really supports it is PyPy which is not in
any sense more lightweight.

~~~
matt_trentini
Although much smaller in scope than NumPy, MicroPython (and CircuitPython) now
have the ulab library which provides similar fast array manipulations:

[https://github.com/v923z/micropython-
ulab](https://github.com/v923z/micropython-ulab)

------
teddyh
There also exists a Python-like language for when you only have a few kb of
memory, and can’t even run MicroPython. It’s called “Snek”:

[https://sneklang.org/](https://sneklang.org/)

~~~
SV_BubbleTime
That’s cool. Hadn’t seen that one.

What is a “few kb”? Because last I looked Micropython was like 256kB and the
smallest JS interpreter that had foreign function interface was 53kB.

~~~
teddyh
> _What is a “few kb”?_

From the manual:

> […] _could fit on our existing Arduino Duemilanove compatible hardware. This
> machine has:_

> _32kB of Flash_

> _2kB of RAM_

[…]

------
fxj
MaixPy ported Micropython to K210 (a 64-bit dual-core RISC-V CPU with hardware
FPU, FFT, sha256 and convolution accelerator). It includes a general-purpose
neural network processor, which can do convolutional neural network
calculation at low power consumption, for example obtain the size, coordinates
and types of detected objects or detect and classify faces and objects. It can
load TensorFlow Light Neural Nets.

[https://maixpy.sipeed.com/en/](https://maixpy.sipeed.com/en/)

~~~
syntaxing
That is awesome! Unfortunately the K210 link on the site is giving me a
certificate error so I'm a bit afraid clicking into it. Do you know if the
library will work with ([https://www.seeedstudio.com/Sipeed-Maix-
Cube-p-4553.html](https://www.seeedstudio.com/Sipeed-Maix-Cube-p-4553.html))?

~~~
fxj
I think it should. I have the Maix-Go ([https://www.seeedstudio.com/Sipeed-
MAix-GO-Suit-for-RISC-V-A...](https://www.seeedstudio.com/Sipeed-MAix-GO-Suit-
for-RISC-V-AI-IoT-p-2874.html)) and it works pretty well. I also own the
m5stick-v which has the K210 builtin
([https://m5stack.com/collections/m5-core/products/stickv](https://m5stack.com/collections/m5-core/products/stickv)).
I connect to them via screen on linux and get a micropython prompt in an xterm
and can use it interactively. There is also an IDE available. See also the
docs at M5Stack:
([https://docs.m5stack.com/#/en/quick_start/m5stickv/m5stickv_...](https://docs.m5stack.com/#/en/quick_start/m5stickv/m5stickv_quick_start))

------
btgeekboy
I once built a METAR light map like this -
[https://slingtsi.rueker.com/making-a-led-powered-metar-
map-f...](https://slingtsi.rueker.com/making-a-led-powered-metar-map-for-your-
wall/)

However, I did it with an ESP8266 running MicroPython; I was impressed that
such a small, cheap little computer was able to connect to the internet,
retrieve XML, parse it (this step in particular), and update the state of each
of the WS8211 lights with ease, all with some quick, easy-to-write Python.

~~~
ficklepickle
I have an ESP8266 wired into my intercom buzzer for opening the door
remotely[0]. It has been unreasonably stable and reliable. Many months,
possibly into years, of consecutive uptime. I used the arduino IDE and
modified demo code for a web server. The cheapo dev board had it's blue LED
die (mercifully) but is still chugging along. It's crazy what these things can
do.

[0] [https://jeremypoole.ca/posts/put-your-door-on-the-
internet/](https://jeremypoole.ca/posts/put-your-door-on-the-internet/)

------
ak_25
I think this is good for someone getting started with microcontrollers(similar
to the Arduino)

However, one should not be disillusioned — I doubt this can be used to deploy
a mass market product.

However, I can see micro-python’s value in rapid-prototyping to some extent
for someone who is not into embedded/firmware as much.

~~~
analog31
In my view, _nothing_ can be used to deploy a product, without further
consideration.

My rationale for saying this is that in addition to programming in any
language, you need to understand hardware, including safety. And I think that
by the time a person reaches that level of knowledge, they have probably
familiarized themselves with programming at a variety of levels from bare-
metal C up to desktop software.

Now, I've only been playing with MicroPython (actually the CircuitPython fork)
for a few months, and at this point haven't come up with a "scale" of project
that needs more than bare metal C but less than a full blown operating system.
But then again I haven't come up with _any_ good product ideas in that time
period.

As I mentioned to a friend of mine: "Hi, we're programmers, and we're here to
burn your house down."

Between the two real projects that I'm nursing along right now, one uses a FFT
and the other an interrupt running at 170 kHz, so neither of those can be pure
Python projects yet.

~~~
matt_trentini
> one uses a FFT and the other an interrupt running at 170 kHz

Neither of those rule out using MicroPython. Though you are likely to have to
do _some_ C work to achieve higher performance.

~~~
analog31
Yes, definitely. Any application that I can think of off the top of my head
involves adding C code. This is not a hurdle, but would be for someone who is
using Python to get into embedded programming at a beginner level. From the
MicroPython documentation:

>>>Compiling the cmodule into MicroPython

>>>To build such a module, compile MicroPython (see getting started) with an
extra make flag named USER_C_MODULES set to the directory containing all
modules you want included (not to the module itself). For example: ...

This is not precisely for the faint of heart, but should not be prohibitive
for a seasoned embedded developer. ;-)

------
StavrosK
MicroPython is amazing, it runs on very tiny microcontrollers not much else
runs on, and makes development of hobbyist embedded projects orders of
magnitude easier than C.

I backed it way back when, I think for the ESP8266 support, and have loved it
ever since.

~~~
hak8or
I wouldn't say "very tiny". For example, does it run (and can do something
useful) on a 8 KB cortex M0 with 24 KB of flash?

~~~
makapuf
For that, maybe snek[1] would be more suited.

[1] [https://sneklang.org/](https://sneklang.org/)

~~~
wizzwizz4
Snek's documentation has errors.

> _Like Python, Snek doesn’t have an explicit Boolean type._

------
noobermin
Does anyone have any experience beyond short programs/sketches with
micropython? The most I've used it for is playing around with a microbit. How
does it hold up?

~~~
keyme
Had it manage an Ethernet Phy IC via an MDIO interface, and an FPGA over SPI.
The MDIO was bit-banged from micropython, and SPI is a built in feature. This
was running on an ESP32 creating a WiFi to Gigabit Fiber bridge (obviously not
at gigabit speeds).

I never thought it would be that easy, but it converted the task of writing
something that does MDIO, SPI, and WiFi (with a webserver) in C, to basically
a day of work.

Also the "feel" of running an interactive python shell on a tiny 1x3cm board
as part of the initial bring-up process after manufacturing is just awesome.
Controlling every IO "manually" via the interactive shell is so much better
than seeing your prepared C code crash and burn, and only then start debugging
it.

------
not_the_fda
Neat, but not very practical. To be of any use you still need board support to
drive the I2C, SPI, etc which means you have to use C anyhow.

You can buy one of their boards with the BSP but that's over $40, yikes.

To use micropython you need at least an M4 class micro-controller so that a $5
or greater part.

Or you could just stick with C and use a M0 class micro that costs < $1.

A difference of $40 in BOM cost makes it only applicable to the hobbyist.
Which is fine, but its just a toy.

~~~
ChuckMcM
Going to disagree here, i2c and spi work fine in micropython.

Is it as fast as C? No. Does that mean it's useless? No.

The absolute coolest thing is to take a $2 Wemos D1 clone, put micropython on
it, hook it to a sensor (I've been using temp and humidity sensors lately) and
have it periodically fetch "[http://webserver-
ip/temp?t=<value>&p=<value>"](http://webserver-ip/temp?t=<value>&p=<value>")
every 10 seconds and have a little python webserver sitting on an machine on
your network that is stuffing the values into influx or some other DB.

This is trivial to do, and takes little to no embedded coding experience on
the ESP8266. That's a win as far as I am concerned.

~~~
dcmorton
I've done something pretty similar that with my project here [0]. Instead of
using MQTT the ESP32 feeds directly to an InfluxDB database.

[0] -
[https://github.com/dcmorton/esp32-dht22-upython](https://github.com/dcmorton/esp32-dht22-upython)

------
pansa2
> _MicroPython implements the entire Python 3.4 syntax_

Python 3.4 is now over 6 years old. Are there plans to add features from newer
Python releases? Or perhaps the developers feel that Python's newer features
wouldn't "pull their weight"?

~~~
analog31
I'd be interested in knowing how much bigger Python has gotten since 3.4 time
frame. If course it's not unthinkable that it could always get smaller if
there's a lot of refactoring going on, but that might be wishful thinking.

~~~
matt_trentini
Resource usage is carefully considered when introducing new features; but
there are cases where it has becomes smaller. The recent addition of LittleFS
meant that a couple of KB more RAM was available at boot for example.

------
alpineidyll3
In my experience, one should understand the c dependencies to use
micro/circuit python for most of desired uses. Still great stuff. The mere
decrease in linecount for simple algs has real value.

~~~
TeeMassive
For plumbing stuff together it's probably great. Anything lower level that
requires performance or real time constraints will probably not do.

~~~
halbritt
Micropython can do some real-time stuff. It's limited, but a bit easier in my
experience than getting a Preempt-RT running on a Raspberry Pi.

------
VectorLock
The coolest application of Python on uCs that I've seen recently is in the
custom mechanical keyboard space:

[https://github.com/makerdiary/python-
keyboard](https://github.com/makerdiary/python-keyboard)

I'm loving the thought of being able to customize my keyboard's functions by
just dropping python files onto its USB drive. Looking forward to using this
on my Dactyl-CC build.

------
fxj
Check out uiflow from M5Stack. It is a web-IDE which uses micropython for
ESP32. You can use the M5stack grove sensors. The IDE generates python code
which can be used on any MicroPython Implementation (ESP8266, EPS32, Raspi,
CircuitPython, Micro:Bit, you name it) and the python libs are all open
source.

[https://flow.m5stack.com/](https://flow.m5stack.com/)

------
skybrian
I'm more familiar with CircuitPython because I've bought a few boards that
could run it, though I've stuck with Arduino so far. It seems to be a fork
started by AdaFruit, but it looks like there are other vendor boards it works
on like SparkFun, Teensy, and Arduino.

What boards work with MicroPython? On their website, they only list their own?

~~~
jennasys
You have to find the version of MicroPython that has specifically been ported
to your board. In addition to the PyBoard (STM32), there is ESP8266, ESP32,
BBC Micro Bit (ARM Cortex-M0), and WiPy (CC3200)

------
snvzz
Remarkably, it has recently gained (still not mainlined) 68000 support:
[https://twitter.com/micko_mame/status/1256221039308267520](https://twitter.com/micko_mame/status/1256221039308267520)

------
FpUser
I've done quite a few firmware development projects (they're commercial type
and deployed in fitness industry, police vehicles, etc). They type of thing
they do, I have a hard time imagining doing it in Python.

------
butz
For rapid GUI application prototyping LVGL has micropython bindings:
[https://github.com/lvgl/lv_micropython](https://github.com/lvgl/lv_micropython)
.

------
KaiserPro
I've been doing microprocessor stuff since the basic stamp 2 came out. (not
that I'm an expert, I've just been fiddling over a long amount of time)

I know that previous me was very scornful of micropython (the original boards
were pretty expensive and power hungry)

However, the ESP* and SAM21 microprocessors have changed my opinion. Yes
python is still wasteful. _but_ I can now operate in the microamps, and for
less than £7

Not only that, these micro controllers has a wealth of documentation and
addons.

I feel like I've discovered the arduino all over again.

------
oaiey
LEGO ist using it for SPIKE Prime and their related upcoming 51515 robotics
set. There is even an inofficial firmware for their regular Powered UP
electronics enabling MicroPython.

------
halbritt
They really push the pyboard which is cool and all, but imho, the killer
application for Micropython has been the ESP8266 port and now the ESP32.

The original pyboard can be had for around $20 and used to be closer to $30.
The ESP8266 is a couple dollars, and the ESP32 is just north of $5.

Obviously, we're only talking about a handful of dollars, but for embedded
systems, it matters.

------
fxj
Seeedstudio provides ArduPy for their Wio Terminal which can generate
micropython libs from Arduino C Libs. So you can script Arduino Code with
micropython on the device. As IDE they provide plugins for MS VisualStudo
Code.

[https://wiki.seeedstudio.com/ArduPy/](https://wiki.seeedstudio.com/ArduPy/)

------
wilsonfiifi
Another great resource for micropython tutorials is Miguel Grinberg’s blog
[0]. In fact his blog is a gold mine for Flask development as well.

[0]
[https://blog.miguelgrinberg.com/category/MicroPython](https://blog.miguelgrinberg.com/category/MicroPython)

------
ngcc_hk
Fpga version so cover the really open issue?

Single global thread issue still around?

And whilst I like ulisp it is tie down with arduino and hope this will go out.
Trying the js microprocessor implementation and let these two compete at
least.

------
Waterluvian
Can someone help demystify this for me?

If I got out a microscope and looked at cross sections of a pyboard, could an
expert point out the sections of physical gates and other parts that implement
each python bytecode?

~~~
Kliment
No - this is implemented in software on a general-purpose processor. You can't
read flash contents optically with a microscope, so the software part is
invisible. You can certainly identify the bits of the general-purpose
processor though.

------
ausjke
I think Lua is also and already great at embedded usage? Of course the more
the merrier.

tinygo, micropytho, lua, there were a few mini javascripts but I lost track
any of them

------
aitchnyu
Has anybody got requests and/or full HTTPS protection working?

------
geophile
Can anyone comment on what parts of Python are not in MicroPython?

~~~
matt_trentini
The language is surprisingly complete; bigger differences are found in library
support, even standard library support. Libraries that are memory efficient
are easy to port but many Python libraries assume vast amounts of memory so it
can require significant effort to modify them to use few resources.

------
D0zMe
Perhaps it would be useful to package it for mobile phones.

------
foxh0und
Have a few M5's, they're great little gadgets.

------
liquidify
anyway to get wifi?

~~~
IceWreck
Ive used the nodeMCU (ESP8266). Its arduino compatible, costs around $3 and
has WiFi

