Using multiple programming languages in Arduino

Can various device programs / programming be in different languages (say one in C, another in an Arduino sketch, another in MicroPython) and all be uploaded / used on a Arduino Nano RP2040 Connect?

I expect that multiple functions in different languages would be written to call each other or utilise API’s, but I’m not a programmer, so technically challenged in this realm. Example - setpoint control function is written in C, takes a DS1820 one wire temperature input using say CircuitPython, with the C program output to operate an on-board relay, and Wifi relies on the Arduino WifiNINA library (uBlox) which communicates via MQTT to a server. The arrangement might also involve I2C on-board comms. It appears that PlatformIO in conjuction with VS Code could address things, but I’m not sure. TIA

5 Likes

There are several different possibilities for what you are describing.

  1. The Nano (or any other MCU isn’t concerned about the language that the code was originally written in. As long as the compiler/linker/uploader sends the correct code to the correct memory locations the program will execute. All the conversion work happens before it gets to the MCU.
  2. For any two or more MCU that are communicating using a common communications protocol (such as I2C) the language in which their respective programs were written is not relevant - it could be any mixture of languages.
  3. Some development environments allow for the use of precompiled libraries. In some cases (depending on the particular environment) those imported modules could be written in a different language. For instance, microPython allows for the import of modules written in C. The Arduino IDE supports precompiled libraries (but it is not clear what other languages can prepare the module library in a suitable form to be included). How the two pieces of code communicate depends on how the language implements the library facility. This is usually through function calls and public variables (the predefined API).
  4. Some development languages allow for the use of in-line language alternatives. For instance some versions of C support including in-line assembler, and microPython supports in-line inclusion of C code (with some restrictions). In this case the communication mechanism is entirely up to the programmer, but typically just involves setting up the memory or register storage, doing some sort of calculation, and putting results back into the storage.
9 Likes

Hi Jeff,

Great list of options! One more that deserves some mention is Viper, a way of writing constrained functions in MicroPython that are a lot faster, as they compile to native machine code. @Brenton has a great explanation in his video on our GlowBit library for MicroPython:

@Pete56180 What sort of stuff would you rather run on C?

4 Likes

Jeff - thanks for taking the time to clearly explain. Very helpful.

My main motivation is to use existing libraries and code as much as possible. @James - below should also help respond to your question.

I will have at least three different operational functions (including setpoint control) which all need to be executable in the application. Each one would be called based on specific conditions and run exclusive of the other operational functions. This is along with fundamental peripheral & IO control - wifi, BT, memory, sensor interface, etc. There is potentially a lot of time and money in doing anything from scratch. Not what I’m looking for at this stage.
It sounds like what I need to do is:

  • List the must have hardware (e.g. DS1820, I2C mux, cellular modem, etc.)
  • Note the libraries and programming language for each of those hardware components
  • Determine which language has the most libraries I need to use
  • Determine if the language(s) with the most libraries has a means to incorporate code from another language (is this effectively “porting”?)
  • Evaluate any perfomance hits from porting and whether or not they’re tolerable
  • Determine if the methods to address perfomance hits are viable for me (@James thanks for your link)
  • On breadboard, run code for various applications (e.g. setpoint control) and see if they work OK

Am I on the right track? Am I missing key considerations?

5 Likes

When you get to step 4 you will know what’s required.to complete the job. I would expect that you will have everything you need at that point in a single development environment. For instance, if you have chosen Arduino you have a large number of development environments available, most of which also support ESP. Similarly for PI. If that device support is available then you are not concerned about the language in which any supporting libraries were originally written.

If it turns out that you can’t access the devices that you are using with existing libraries within that environment then you need to look at other options. It is possible that you might find what you need in a language that is not supported in the development environment you have chosen, and you would need to convert it to something that is supported. That is porting - you take the code that you can get and rewrite it to the language you use. How difficult this is depends on the languages concerned. In some cases you will do a simple syntax conversion. The other extreme is that you use the original as the task definition and start the coding from scratch. It might or might not be a complex job.

But I suspect that it won’t be required. Check the resources available in the major development environments for your chosen hardware and I would be fairly confident that you will find that everything you need is already there.

5 Likes

Pete,
you seem to be jumping into the deep end of analysis. MicroPython and a nano RP2040 is quite capable of handling your example, grab one and have a go!
I would start with https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-openmv-setup. When that’s running, then just look for an example using the DS18B20, the setpoints would just need some logic:
if temp < x do one thing (e.g. turn on Green LED, relay 1 or both)
if temp > x and <y do the next thing (e.g turn on Orange LED and relay 2)
if temp > y do something else (e.g Flash RED led madly and beep loudly).

Come back anytime and ask more questions and someone can help.

Dave

3 Likes

Pete,
Thinking more (that’s dangerous!). Although (Micro)Python and Circuit Python are generally a bit easier to code in, I’m not aware of a site that takes you from concept to complete project in one go. Adafruit (Welcome To CircuitPython | Welcome to CircuitPython! | Adafruit Learning System) does very good tutorials but they generally apply to several of their devices, so could be a bit confusing.

The option I suggest is Rui Santos’ site, have a look at ESP32 MQTT – Publish BME680 Sensor Readings (Arduino IDE) | Random Nerd Tutorials for example. He uses generic ESP32’s but you can get equivalent stuff from core https://core-electronics.com.au/sparkfun-thing-plus-esp32-wroom-47658.html, which also offers Qwicc connectors, like Core’s Piico.

Dave

Hey Pete,

Scott from Adafruit touched on this a while ago in one of his deep dives: https://youtu.be/LXAwW2IYT7o?t=1931
The proposed idea was to run a different language on each core of the RP2040, CircuitPython as the ‘handler’ and C running all of the ‘real-time’ tasks.

Once you start talking about a control system it would also be worth taking a look at threading tasks, making sure that you can execute them in a prioritised order and periodically (note interupts as well).


You could also go the route of using a few microprocessors running different languages to handle the sensors and a main processor for Comms. The Pico (also the heart of the RP2040 Connect) is $6 and a very very good value MCU. I2C would be perfect for this, see Core’s smart modules for some inspiration and Rob’s stackable control units

4 Likes

So that buzzer module has it’s own MCU. That’s good because it means you can provide a custom hardware device with a high-level API. It makes the system more plug-and-play, especially useful for less experienced users.

2 Likes

Hey Rob,

Yeah, I picked one up and its suuuper intuative, feels just like any other implementation of an I2C sensor or output!

There is a hardware and software repo: Core Electronics · GitHub
The API’s are really good imo as well: GitHub - CoreElectronics/CE-PiicoDev-Buzzer-MicroPython-Module

2 Likes