diy solar

diy solar

Raspberry Pi Pico (microcontroller)

Hedges

I See Electromagnetic Fields!
Joined
Mar 28, 2020
Messages
20,514
Raspberry Pico, a microcontroller to handle overhead without using a massive computer like the Raspberry Pi itself.
Programmable in Python, it seems.

1611273881640.png


"Raspberry Pi on its own does not support analogue input; and while it is possible to run “bare metal” software on a Raspberry Pi, software running under a general-purpose operating system like Linux is not well suited to low-latency control of individual I/O pins.

Many hobbyist and industrial applications pair a Raspberry Pi with a microcontroller. The Raspberry Pi takes care of heavyweight computation, network access, and storage, while the microcontroller handles analogue input and low-latency I/O and, sometimes, provides a very low-power standby mode."

"For power users, we provide a complete C SDK, a GCC-based toolchain, and Visual Studio Code integration."

"For beginners, and other users who prefer high-level languages, we’ve worked with Damien George, creator of MicroPython, to build a polished port for RP2040; it exposes all of the chip’s hardware features, including our innovative PIO subsystem."
 
I really like Micro Python and have been using it for commercial products recently. It is a Python emulator that is totally independent of CPython, but to the programmer you hardly notice. I was very skeptical at first, fully expecting a very resource heavy interpreted language experience but it is rather efficient all things considered.

If you get into a performance bind, you can use inline C with much more direct access to the hardware system. So far, I have not needed to do that but rather glad the option is there if I need it.
 
The Raspberry Pi foundation is flipping blue?
Anyhow, a microcontroller is only as good as it libraries support. It will be hard to beat the Arduino.
That depends on what you are doing with the uC. I never use Arduino because the libraries are generally too limited or fat. All of my work with Atmel architectures has been in C. Even my one-off tests/experiments.

So far, I have been really happy with the Micro Python libraries for STM32. What is really nice is when a library does not exist for your application - it is WAY easier to roll your own in Python compared to C/C++
 
Python... Aaaargh! That stupid thing where complicated is easy and simple things complicated?
Using a language without declared variables in a hardware-close environment is a sin that cannot be pardoned.
 
Love the concept! It's exactly the link who's been missing between the arduino (or any other MCU) and the RPi ?

The Raspberry Pi foundation is flipping blue?
Anyhow, a microcontroller is only as good as it libraries support. It will be hard to beat the Arduino.

It will not really be hard to beat... maybe not in quantity but definitely in quality :) the number of times I had to correct bugs in arduino libraries (or even the core code)... Now, most of the time, I don't even try to use the existing libs anymore and I directly write my own libs --> a lot less bugs and edge cases, lower memory usage, higher perfs, ...

I will never use python for any embedded stuff but that's just an option, not an obligation, so it's fine ;)
 
Python... Aaaargh! That stupid thing where complicated is easy and simple things complicated?
Using a language without declared variables in a hardware-close environment is a sin that cannot be pardoned.
lol....

You code in C
I code in Python

Let's see who finishes faster.

The primary reason Python was developed and is as popular as it is today is because it is easy and line for line efficient. The majority of my code writing experience has been in C (although I have covered a lot of terrirory/languages since I wrote my first line in 1983. Python is now my goto language for a broad range of things. Why? Because I can deliver a solution in a fraction of the time compared to most other languages. When things get more complicated, you can use outboard libraries that were developed in C++ for raw performance, but with the easy interface of Python.

I designed a microcontroller based product last year that had about 10k lines of C to make it work. The next version of that product is on my bench now. I moved to an ESP32 microcontroller that supports C and Micro Python so I decided to give the Python a try.

I was able to do the exact same thing in about 500 lines of code that took 10,000 in C world. The finished production code will probably be longer, but not by much. Obviously, the total time spent coding Python was just a fraction of the time even if I included the learning curve specific to MicroPython. Hard to call that stupid.
 
lua is cool too

teensy 4.0 and 4.1 are decent microcontrollers, i’ve used both and they perform well
 
I am rather pragmatic as a general rule.
If I have a resource limited project that needs carefully controlled and optimized performance - C is usually the choice.
If I have a project that is over resourced - I go with the option that allows the fastest development - Micro Python can fit in that scenario.

The modern microcontrollers are almost always more than I need these days. If I run out of a resource/performance - the next step up is usually cheaper than the time required to tightly optimize code. Now with Micro Python increasing in popularity, I just go with an SoC that has a ton of RAM/FLASH along with a zippy 32bit CPU.
 
You code in C
I code in Python

Let's see who finishes faster.

Actually in C++ but not really important I guess.

And let's see which code run faster and which one fail first ?


I designed a microcontroller based product last year that had about 10k lines of C to make it work. The next version of that product is on my bench now. I moved to an ESP32 microcontroller that supports C and Micro Python so I decided to give the Python a try.

I was able to do the exact same thing in about 500 lines of code that took 10,000 in C world. The finished production code will probably be longer, but not by much. Obviously, the total time spent coding Python was just a fraction of the time even if I included the learning curve specific to MicroPython. Hard to call that stupid.

I'm willing to bet you don't include the libs code lines in those numbers... I can do it in 500 lines of C too (well, ok, let's say 600 or 700 lines to be real) if I don't count what is in the libs... 20:1 ratio is totally unrealistic if you have equivalent libs, it should be around 1.2 to 1.5:1.


If I run out of a resource/performance - the next step up is usually cheaper than the time required to tightly optimize code. Now with Micro Python increasing in popularity, I just go with an SoC that has a ton of RAM/FLASH along with a zippy 32bit CPU.

I hate that mentality. That's because of that we have bloatware instead of software for 99 % of what we use today...

Also, you forgot power usage, it can be very important.

Is care and attention to detail totally lost today or what?
 
[...] has garbage collection

Usually that's the last thing you want in code running on a MCU. Long story short RAM size is small so you don't want to alloc/free constantly as you'l fragment your memory like crazy, can't make new allocs as a result and then crash. The usual way is to avoid using dynamic allocation, and if you really need it then you do it so you don't free the chunck but you re-use it.

Also lua runs in a VM, making it a bad idea to use because of the perfs, memory usage, reliability, ...
 
Last edited:
Actually in C++ but not really important I guess.

And let's see which code run faster and which one fail first ?

Like I said, if raw performance is in the requirements - C/C++ wins every time.
I'm willing to bet you don't include the libs code lines in those numbers... I can do it in 500 lines of C too (well, ok, let's say 600 or 700 lines to be real) if I don't count what is in the libs... 20:1 ratio is totally unrealistic if you have equivalent libs, it should be around 1.2 to 1.5:1.

No, of course not. I am not counting the libs in C/C++ either though. Just what I have to write myself.
I have no idea what a more realistic ratio is, but it does not really matter. If MicroPython has a library that insulates me from the verbose underlying code - GREAT! Winning!

I hate that mentality. That's because of that we have bloatware instead of software for 99 % of what we use today...

Also, you forgot power usage, it can be very important.

Is care and attention to detail totally lost today or what?

Like I said, I am pragmatic. My customers don't care and won't pay more if I have an ultra-optimized platform that justifies every clock-cycle. I have one current project that needs that kind of performance and it is all C. All my other projects are time-to-market sensitive so being able to knock weeks off the design cycle is how I pay attention to the most significant detail - staying in business.
I did not forget power usage either. Almost any chip these days has sophisticated power management that is easily accessible regardless of what you use to develop the code. Again - for ultra-critical designs, C will win but MicroPython is very very close in power sensitive applications.

It is always a balance of resources, and I include my time as part of the resource evaluation. That becomes especially relevant when the product will only see low-volume or even as a one-off. In the past year, I have made a lot of one-off projects that would have been impractical if C was the only choice.
 
I really like Micro Python and have been using it for commercial products recently. It is a Python emulator that is totally independent of CPython, but to the programmer you hardly notice. I was very skeptical at first, fully expecting a very resource heavy interpreted language experience but it is rather efficient all things considered.

If you get into a performance bind, you can use inline C with much more direct access to the hardware system. So far, I have not needed to do that but rather glad the option is there if I need it.
I am looking into this. The fact it is running on an ARM Cortex M4 aroused my interest. If I run into any time critical functionality that needs the speed of C I can always create a library and expose it to python.

I am going to order one of their development setups. I love how cheap embedded development has gotten. It used to be ridiculously expensive to do anything in this world, now it is virtually free.

If you are looking for an excellent, inexpensive C arm development environment that is not tied one one particular manufacturer's chips, I recommend you check out Rowley Crossworks ARM. I have been using it for 12 years now and love it.

Commercial licensing is reasonable (a named developer license is $1500 a seat plus $400 a year for maintenance). They have a personal, non-commercial license for $150. It is the full thing, they just ask that you be honest. I retired in 2012 (for a while) and I bought a copy of the non-commercial version for my own use. When I returned to the workforce, I bought a couple of seats of the commercial version for my team to use. The text editor is their own and it is every bit as good as Microsoft's Visual Studio (which is fantastic in my opinion).

Rowley Crossworks ARM uses GNU compilers except they make it work seamlessly, without frustrations. The result is every bit as good as Keil's MDK-ARM or IAR's Embedded Workbench for ARM (I have licenses for both and use them when I have to). Crossworks supports everybody's ARM processors and development boards and their technical support is fast and excellent. It is worth paying for if messing about with ARM processors pays your salary.


I think that Silicon Labs toolchain is one of the better GNU based vendor locked toolchains. If they have the chips you need this is a painless and free option. Really no complaints. I like their radio technology and they make fantastic galvanic isolation ICs.

I have used NXP's MCUExpresso and the main thing holding it back is it still uses Eclipse as a editor. The chip configuration tools are fantastic. I just wish they would switch to a better text editor. At least its not VI, I guess that is something. The example code in the SDK is excellent. Written by people who understand how to get stuff done. An amazing, mature ecosystem. I have been using NXP's processors since the 90's. 8051, 51XA, ARM-7, Cortext M0, M3, M4 and now M33. Absolutely some of the best IC's you can use.

Nordic Semiconductor's is a mixed bag, very best in some ways, but the SDK is infuriatingly bood/gad (good and bad?). The silicon is fine. Best in class power consumption. The crosspoint switch is wonderfully flexible, you can configure every digital function to any GPIO (I like that a lot).

The Nordic Semi SDK is the 800 pound turd floating in the bathtub. All of the radio related stuff is really excellent. Everything else is cumbersome, inefficient and opaque: [Nordic Semiconductor Diatribe/Rant]
  • Every non-radio specific code example is almost unusable because way too much of the magic is hidden behind the scenes which makes comprehending what you need to do to use the hardware resources is entirely up to you (pore over the data manual and figure it out for yourself). Basically there is no point to even providing this code, it is that bad.
  • It is very difficult to merge tutorials together in order to do real work because the SDK uses a configuration wizard that disables access to everything except what that particular tutorial needs to use and they don't give you the configuration wizard. "Oh you want to use the Radio AND the SPI port in the same program. Why would you ever want to do that?"
  • Everything non-radio specific is painfully slow and unoptimized. Just flipping a GPIO bit high or low took 6 nested function calls if done through the SDK. I traced it painful step, by painful step one day to see why it was so flipping slow.
  • An example: I needed to read a 24 bit ADC at 5000 Hz (read a sample every 200 microseconds). Their SPI routine took so long to return data that it was was falling behind the 200 microsecond timer interrupt function. It was 3 times faster to just bit-bang the SPI data in (not using the SDK GPIO routines of course) than it was to use the SDK to access the SPI hardware. Arggg.
  • Great silicon. Terrible SDK. Real shame that.
[/Nordic Semiconductor Diatribe/Rant]

Dallas and STMicro I really haven't even looked at their stuff since the 8051 days. The MicroPython development board is going to get me to buy an STMicro development board, so I may turn into a fan.

In case you can't tell I am a major NXP fanboy. I think their asymmetrical dual core parts are amazingly powerful. I have used the LPC4357 and LPC55S69 now and they both knocked my socks off.

The LPC4357 primary core is an M4 with floating point coprocessor. The second core is an M0 that you can dedicate to whatever tickles your fancy. I used the M0 to handle communication duties with the high speed sensor input and to service the high speed analog outputs. The M4 core did all the DSP and host communication functions. Both processors were essentially loafing (worst case bandwidth utilization peaked at 25%).

The LPC55S69 has two M33 cores. The primary M33 core has the floating point/DSP coprocessor and the second M33 core is again available for any special time sensitive task you might need done right now. This part has 9 flexible serial ports and all ports are available to either core. Each port can be a USART, SPI, I2C or I2S port. There is one additional dedicated high speed SPI port that supports 50 MHz clock speed. I am using the second M33 core as a data pump dedicated to running the high speed SPI port. Let just say, I am never waiting on the SPI port now.
 
Last edited:
What makes "best" in terms of a programming language to me is code I can read and really great debugger and other tools. It might be art to some, but I just want it to work and not be so jumbled that I can come back in a few years and not struggle with it. That said, I will struggle to minimize needs for watts on a solar app.

Remember back when I wrote Foghorn in Android Studio? Couldn't get it to start on my cell phone the other day so I went to rebuild it, figured I'd just reload the APK. Except it won't build anymore... INSTALL_PARSE_FAILED_NO_CERTIFICATES... Tools clearly indicate what's wrong, but not the steps needed to resolve (a clean/rebuild should always get you to where you were, you should be able to click the error message to figure out what you need to do - especially something as route as certificates). I was going to look into it more, then remembered I had turned the WiFi off on the cellphone and that fixed the problem.

...lua runs in a VM, making it a bad idea to use because of the perfs, memory usage, reliability, ...
Not an expert, but as I understand it... Lua can run in a VM, but can also be compiled to ANSI C and run as a native exe on anything that can compile ANSI C.
 
Not an expert, but as I understand it... Lua can run in a VM, but can also be compiled to ANSI C and run as a native exe on anything that can compile ANSI C.

I checked and it only runs in the VM, it's the VM who is coded in C ("only" 24 000 lines... see why I don't like abstraction layers in embedded software?), it's basically the same concept as java (hopefully with a better VM...).
 
Here is a data point on the power efficiency that is possible with C.

We did a battery powered wireless load cell. It has a 700 ohm bridge sensor, plus a Nordic Semiconductor nRF52832 processor. This device measures force at a 100 Hz sample rate and communicates data wirelessly to a base station. The operating conditions include winter on the north shore of Alaska (-40 degrees) and summer in the middle east (60 degree C when in direct sunlight). This system has a one year battery life operating from a single 3.7V, 19 AH primary lithium cell.
 
I checked and it only runs in the VM, it's the VM who is coded in C ("only" 24 000 lines... see why I don't like abstraction layers in embedded software?), it's basically the same concept as java (hopefully with a better VM...).
Lua is generally regarded as more efficient than Java

Disclaimer, every tool has pros and cons and the reader's situation is the one that is important to optimize for.
 
Usually that's the last thing you want in code running on a MCU. Long story short RAM size is small so you don't want to alloc/free constantly as you'l fragment your memory like crazy, can't make new allocs as a result and then crash. The usual way is to avoid using dynamic allocation, and if you really need it then you do it so you don't free the chunck but you re-use it.

Also lua runs in a VM, making it a bad idea to use because of the perfs, memory usage, reliability, ...
There are systems I designed that haven't been rebooted in over a decade. Malloc, New? Not in my embedded projects.

I use a C++ compiler (because every embedded C tool chain is C++), but my embedded code is pure C and assembler. And yes, I use the dreaded pointers. Even worse, tables of function pointers! Gasp.

That is a great way to write table driven code though which really improves productivity when you are called upon to support a lot of custom protocols or user interfaces. At a previous job, I built a C# emulator that let the marketing people play around with the user interface on their PC until they get exactly what they want. Then they give me the emulator output header files and I load them into the product and turn the crank. No longer really software development at that point. Every body wins. The most anal retentive guys can tweek to their hearts content and if it turns out they don't like the results, the finger points right back at them

I used to burn a lot of time maintaining state machine logic until I realized I could automate quite a bit of it. It wasn't that hard a sell to convince management that the PC tool was worth the time investment.
 
Back
Top