Uploading arduino code interactively using python

Hi all,

I work in a psychology lab, and in some of our experiments we use arduino to create some physical simulations in experiments.

usually we create a python script that runs on a computer, and a sketch for the arduino, and they both interface through serial to send the stimuli.
my issue is that after creating quite a large amount of experiments that work with the arduino, the experiments are getting mixed up and we often upload the wrong sketch to the arduino and it does not interact correctly with the script on the computer.

to fix that, and organize this mess, I would like make the script running from the computer to upload the right code to the arduino (since its already connected through serial), but I haven't found any library that can do it.

another option I thought of is writing a batch file that can be run through python, however I haven't found any windows commands to do this.

does anyone know of a solution to my problem? I prefer finding a library for python than write a batch file, however I will accept any solution that will upload the right sketch using an python script.

my only limitation is that I might have to set it up on many computers so it would be best to keep the dependencies and first time setup to a minimum.

Thank you.

You can use Python to call the Arduino IDE and upload a program.

I illustrated this process in this Thread. You don't need to use the Geany editor.

In this more recent Thread I wrote an extended Python program that makes archive copies of the program that is uploaded and writes the name of the archive into the Arduino code so it can the seen in the Serial Monitor. This version also allows a program to be compiled and uploaded without the need for the .ino file to be in a directory of the same name. That could be useful for your situation.

It should be easy to dismember my code and use the parts that interest you.


thanks, this is pretty much what I wanted.

I have a few questions though:

  1. the 'subprocess.call()' command should work the same on windows? should the command given to it be different?

  2. on windows, the path to the arduinoIDE should be the path to the actual executable that runs the ide?

thanks for you response.

I am not sufficiently familiar with Windows to answer your questions.

Python seems to be well constructed to be portable.

/mnt/sda4/Programs/arduino-1.5.6-r2/arduino is a shell script on my Linux laptop. It came as part of the IDE. The Arduino IDE is just a Java program. This is the content of the script - it is regular Java stuff.


APPDIR="$(dirname -- "$(readlink -f -- "${0}")" )"

cd "$APPDIR"

for LIB in \
    java/lib/rt.jar \
    java/lib/tools.jar \
    lib/*.jar \


export PATH="${APPDIR}/java/bin:${PATH}"

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel processing.app.Base --curdir $CURDIR "$@"

What happened when you tried the ideas you have suggested?

Is the JVM installed on your PC independently of the Arduino IDE?
If not it may simplify things to install it and then download the IDE without using the Windows installer.


Hi Robin,

Is there a way of running the code without using Geany? ie solely using python?


Is there a way of running the code without using Geany? ie solely using python?

Yes. You can run the Python program from the command line. Just pass it the name of the .ino file

You seem to have started another Thread about the same problem. Don't ask the same question in 2 places. That just wastes everyone's time.