Accurate Cycle Counting On RP2040 MicroPython

The RP2040 is a gorgeous little chip with a well-defined datasheet and a fantastic price tag. Two SDKs are even offered: one based on C and the other MicroPython. More experienced MCU wranglers will likely reach for the C variant, but Python does bring a certain speed when banging out a quick project or proof of concept. Perhaps that’s why [Jeremy Bentham] ported his RP2040-based vehicle speedometer to MicroPython.

The two things that make that difficult are that MicroPython tries to be pretty generic, which means some hackery is needed to talk to the low-level hardware, and that MicroPython doesn’t have a reputation for accurate cycle counting. In this case, the low-level hardware is the PWM peripheral. He details the underlying mechanism in more detail in the C version. On the RP2040, the PWM module can count pulse edges on an input. However, you must start and stop it accurately to calculate the amount of time captured. From there, it’s just edges divided by time. For this, the DMA system is pulled in. A DMA request can be triggered once the PWM counter rolls over. The other PWM channel acts as a timer, and when the timer expires, the DMA request turns off the counter. This works great for fast signals but is inaccurate for slow signals (below 1kHz). So, a reciprocal or time-interval system is included, where the time between edges is captured instead of counting the number of edges in a period,

What’s interesting here is how the hardware details are wrapped neatly into pico_devices.py. The uctypes module from MicroPython allows access to MMIO devices such as DMA and PWM. The code is available on GitHub. Of course, [Jeremy] is no stranger to hacking around on the RP2040, as he has previously rolled his own WiFi driver for the Pico W.

MicroPython ESP32 IDE Makes Life Simpler

In theory, using MicroPython on the ESP32 is easy —  just flash an image and connect using a serial port. But that leaves a lot of things you still have to do. You need to move files between the two platforms. You’ll want to manage network configurations. You might want better editing and assistance, too. So there are a number of IDEs made to help you and one we recently noticed was MPY-Jama.

The IDE provides source code editing, of course. But it also allows you to do things like pull information about the network using a dashboard or connect to a WiFi network easily. You can even create your own AP with a simple interface.

Although the front part of the README mentions it is for Windows or Mac, if you scroll down you’ll find instructions for installing under Linux. The IDE is extensible using “Jama Funcs” and can handle the flashing operation from inside the IDE.

Of course, there is an IDE from Arduino (but not the Arduino IDE) that handles MicroPython. You can also find a rundown of several similar alternatives online.  If you need some inspiration for a MicroPython project, perhaps you’d like to play a game?

GitHub ESP32 OTA Updates, Now In MicroPython Flavor

Wouldn’t it be great if you could keep all of your small Internet-connected hacks up to date with a single codebase? A couple of weeks ago, we wrote up a project that automagically pulls down OTA updates to an ESP32 from GitHub, using the ESP32 C SDK. [Pascal] asked in the comments, “but what about MicroPython?” Gauntlet thrown, [TURFPTAx] wrote ugit.pya simple library that mirrors all of the code from a public GitHub Python repo straight to your gizmo running Micropython.

[Damped] wrote in about Senko, another library that does something very similar, but by then [TURFPTAx] was already done. Bam! Part of the speed is that MicroPython includes everything you need to get the job done – parsing streamed JSON was the hard part with the original hack. MicroPython makes those sorts of things easy.

This is one of those ideas that’s just brilliant for a hacker with a small flock of independent devices to herd. And because ugit.py itself is fairly simple and readable, if you need to customize it to do your own bidding, that’s no problem either. Just be sure that when you’re storing your WiFi authentication info, it’s not publicly displayed. ([TURFPTAx], could I log into your home WiFi?)

What’s [TURFPTAx] going to be using this for? We’re guessing it’s going to be deploying code to his awesome Open Muscle sensing rigs. What will we be using it for? Blinky Christmas decorations for the in-laws, now remotely updatable without them having to even learn what a “repo” is.

Continue reading “GitHub ESP32 OTA Updates, Now In MicroPython Flavor”

Screenshot of the Arduino Lab for MicroPython

Arduino Brings A MicroPython IDE

Both Arduino and MicroPython are giants when it comes to the electronics education area, and each one of them represents something you can’t pass up on as an educator. Arduino offers you a broad ecosystem of cheap hardware with a beginner-friendly IDE, helped by forum posts explaining every single problem that you could and will stumble upon. MicroPython, on the other hand, offers a powerful programming environment ripe for experimentation, and doesn’t unleash a machine gun fire of triangle brackets if you try to parse JSON slightly incorrectly. They look like a match made in heaven, and today, from heaven descends the Arduino Lab for MicroPython.

This is not an Arduino IDE extension – it’s a separate Arduino IDE-shaped app that does MicroPython editing and uploads code to your board from a friendly environment. It works over a serial port, and as such, the venerable ESP8266-based boards shouldn’t be be left out – it even offers file manager capabilities! Arduino states that this is an experimental effort – it doesn’t yet have syntax checks, for instance, and no promises are made. That said, it already is a wonderful MicroPython IDE for beginner purposes, and absolutely a move in the right direction. Want to try? Download it here, there’s even a Linux build!

High-level languages let you build projects faster – perfect fit for someone getting into microcontrollers. Hopefully, what follows is a MicroPython library manager and repository! We’ve first tried out MicroPython in 2016, and it’s come a long way since then – we’ve seen quite a few beginner-friendly MicroPython intros, from a gaming handheld programming course, to a bipedal robot programming MicroPython exploration. And, of course, you can bring your C libraries with you.

screenshot from the video linked, showing example code that lights up an LED, and in a small window, also shows the LED lit up on a small Pi Pico board connected over USB

Your MicroPython Board Can Be Your Tinkering Peripheral

[Brian Pugh] has shared a cool new project that simultaneously runs on desktop Python and MicroPython – the Belay library. This library lets you control a MicroPython device seamlessly from your Python code – interacting with real-world things like analog/digital trinkets, servos, Neopixels and displays, without having to create your own firmware or APIs.

You need a serial-connected MicroPython board – even an ESP8266 should do. Then, you can intersperse your Python code with MicroPython-written functions, and call them whenever you need your connected device to do something – keeping the entire logic of your project within a single device. [Brian] provides quite a few examples, even for more complex things like displays. No doubt, there are limitations, but this looks to be a powerful tool in a hacker’s arsenal.

Readers might be reminded of an Arduino library called Firmata – an old-time way to do such connectivity. We’ve also previously covered a Pi Pico firmware that does a similar thing, and even features a breakout board for all your experimentation needs!
Continue reading “Your MicroPython Board Can Be Your Tinkering Peripheral”

Tutorial Teaches You To Use Neopixels With Micropython

Addressable LEDs are wonderful things, with products like Neopixels making it easy to create all kinds of vibrant, blinking glowables. However, for those without a lot of electronics experience, using these devices can seem a bit daunting. [Bhavesh Kakwani] is here to help, with his tutorial on getting started with Neopixels using the MicroPython environment. 

The tutorial flows on from [Bhavesh’s] Blink example for MicroPython, and is aimed at beginners who are learning for the first time. It explains the theory behind RGB color mixing that allows one to generate all manner of colors with WS2812B-based LED strings, and how to code for the Raspberry Pi Pico to make these LEDs do one’s bidding.

The guide even covers the use of the Wokwi simulation tool. This is a great way for beginners to test their projects before having to play with actual hardware. This is useful for beginners, because it’s a great way to catch mistakes – is there a software problem, or did they push the soldering iron through the microcontroller? It’s also a technique that pays dividends when working on more complicated projects.

Whether you’re entirely new to the embedded world, or just want to learn the intricacies of talking to addressable LEDs and make sense of color mixing theory, this tutorial will serve you well. Before you know it, you’ll be building glowing projects with the best of them!

 

Simple MicroPython Game Is A 30 Minute Game Dev Course

Sometimes, it’s really useful to watch a project’s parts come together one piece at a time in order to get a complete understanding and mental picture of the whole, and we found that to be the case with this simple, retro-inspired sample game from [ezContents]. (Video, embedded below.) The code is on GitHub but if you’re at all interested in what goes on behind the scenes in a game like that, don’t miss the video.

In the video, each game element and function is illustrated, showing exactly what gets done and why. This part is collision detection (click to enlarge.)

These sprite-based games are mostly about moving a small graphical object (a sprite) around a screen in response to user input, and managing what happens when collisions are detected between the player’s sprite and other sprites like enemies, projectiles, and so forth. The development process is wonderfully documented and demonstrated in a video, as each separate part of functionality gets built and explained one piece at a time.

The simple game is made using ArduPy (which is MicroPython combined with Arduino APIs) using Seeed Studios’ Wio Terminal, a small microcontroller development board with integrated screen, sensors, and button inputs including a little directional clicker that [ezContents] uses as a joystick.

The video of the whole process is embedded below; give it a watch and you’ll maybe come away with inspiration, but you’ll definitely have a much better understanding of how these types of games are developed, even if you’re not using the same hardware or development environment.

Continue reading “Simple MicroPython Game Is A 30 Minute Game Dev Course”