Programming an Arduino using Python, rather than C/C++

  • I am not very skilled with the C Language and I was wondering if there is a way in which python could be used to program an Arduino. This would most likely require a different IDE in order to be able to debug the scripts them self.

    There are couple alternatives for programming Arduino, one of them is BitLash and there is a basic interpreter. There are couple options when you search the Internet, but learning C/C++ will pay off in the end.

    Thanks, I am actually getting a bit better at C, just not as good as I am with Python.

    Is a raspberry PI an option?

  • It's going to be extremely difficult to get any kind of Python script running directly on the Arduino. The reason is that it's an interpreted language, so you would need the interpreter on-board in addition to the plain text script. There's probably not going to be enough memory for all of that.

    Your best bet would probably be finding a way to compile a Python script to native machine code (which is how C/C++ works). I believe there are projects around to do something like that for other platforms, but (as far as I know) none which does it successfully for Arduino yet.

    You might find some more useful information on this question at Stack Overflow: Is there a way to "compile" Python code onto an Arduino (Uno).

  • Yes, it is (somewhat) possible to program the Arduino using Python. One such project on Github is the Python Arduino Prototyping API v2. It provides very basic functionality such as digital I/O and analog I/O.

    This can be used for very simple projects.

    *This project is a bit of a hack at "programming" the board using the serial connection. It passes the commands over the serial connection to a sketch running on the board which then "executes" the Python command.

    So the board has to be plugged into a computer in order to actually run the code?

    @JVarhol Oddly, yes.

    Well, that pretty crappy. It would be easier to use a Raspberry Pi.

  • There is a project which brings a Python virtual machine to micro-controllers, including the Arduino Mega.

    Here's a quote from the Ardunio Mega README, which gives a feel for what this could be like (though, I've not tested this!):

    The following is an example session using ipm::

    ipm> import avr, sys
    ipm> avr.ddrA(0xff)
    ipm> avr.portA(0)       # Pins 22-29 all at 0 V
    ipm> avr.portA(0xa5)
    ipm> sys.heap()
    (2622, 7424)
  • I have used pyserial with great success in controlling the pins on the arduino. Your workstation (laptop, etc) runs a python script that then communicates in real time to the arduino.

    It's not quite the same as running the script directly on the arduino but I was doing quite a bit more than I thought the arduino could handle (some music analysis, hitting a REST api, etc).

  • Another option to consider is the Arduino Yun. In addition to the AVR chip which most Arduinos have, it also includes a MIPS chip running Linux. Since it's linux, you can easily run python code, and any pure-python package can be easily installed.

    However, all the interesting input/output capability is on the AVR chip, not on the MIPS chip. Projects such as Autobahn and LininoIO solve this problem by running a special program on the AVR chip which performs I/O operations under instruction from the MIPS chip.

  • We know this is an open issue in Arduino programming and in the embedded world in general. There are various solutions available on the market like MicroPython but most of them are dedicated to specific boards and don't allow to keep the system real-time. We are working on a solution! VIPER is a Python Virtual Machine for Arduino DUE and all ARM 32 bit architectures that runs on top of a real-time operating system. This means that you can develop Python scripts that can be exexuted by Arduino DUE, UDOO, Spark (all boards) and also on the new ST nucleo boards. Moreover if you need high performances and you re skilled in C you can develop real-time modules for VIPER that can be called and managed by the Python scripts.

    You can see VIPER in action here

    It’s not an open issue. python is a horrible indentation language.

  • Yes. Python can be used to program an Arduino, simply by importing pyfirmata, which can interface the arduino with Python.

    True. But that isn't what the OP is asking for.

    I disagree. Using firmata is _effectively_ "programming an Arduino" using python. In fact I was surprised to see nobody else suggested it. From a beginner's point of view it's pretty much the same. I'm thinking of using it to help teach my son programming as python is a bit easier than C.

  • You could learn from C (AVR) how sensors work but it needs much more time. Arduino IDE Code and Python Code for Arduino use C Code functions to make things simple. Python and Arduino IDE if use Firmata they use serial communication and you think you code self.

  • Yes,its possible with the recent new ArduinoPro H7 which has abilities to upload a written programme in Python and JavaScript and its a dual core,32bit.

    Java Script? really? did you try it?

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM