My personal blog around GNU/Linux

Posts tagged ‘arduino’

Python & Arduino – 2

This is a continuation of my previous post, where I just had my hands on with Python-Arduino-Proto-API-V2.

As I have mentioned at the end of my last post, I wanted to take a look into the prototype.pde file and file to understand what exactly is happening and what data are being transmitted.

From my understanding this is how it works.

All the Arduino boards have Serial interface which communicates via pin 0 (Rx – pin for receiving data) and pin 1 (Tx – pin for transmitting data). Arduino IDE has with it a built-in Serial Monitor. Now taking advantage of this built-in Serial library of Arduino IDE, the Prototype.pde file has been written.

Taking a look at setup method in the prototype.pde file, which initializes the Serial interface on the board & taking a look at loop method where switch case is used with options ranging from 0 to 4 used for Reading and writing to Digital and Analog pins. Now that this pde file which is also called as Sketch is uploaded to the board using Arduino IDE will now be listening for the inputs on the Serial interface pin 0 (Rx) mentioned above, based on the choice supplied to the switch case the board will serve the purpose.

Let us now take a look into the file which serves as an Python API that communicates with the prototype.pde file in the board. Python also has a Serial interface library called as PySerial. uses this python library to talk with the serial interface on the board.

setLow, setHigh, getState, analogWrite, analogRead are the methods defined in the Arduino class which are mapped to Switch case 0,1,2 & 3 on the prototype.pde file which in turn uses the built-in Serial functions of Arduino to achieve the same.

So the approach is pretty straight forward, we can also write our own sketch taking advantage of arduino’s built in serial library and upload to the board. This API looks decent to do basic operations on the board.

Python & Arduino -1

Since I have got my Freeduino (a cloned version of Arduino Duemilanove w/ ATmega328), I wanted to control it using my defacto programming language (Python).

I am an electronics novice / beginner. Electronics had always attracted me and continue to do so. My love towards playing with Software + Hardware is growing infinitely. They give us a physical sense, unlike Software that runs into an virtual environment.

I tried few basic examples of controlling the board using the Arduino IDE and their C like programming structure. These examples include, blinking the LED (Literally the Hello World), controlling the LED using a switch and then controlling the Color changing LEDs.

Arduino’s playground site has a list of Python interfacing libraries. I chose the Python Arduino Proto API-V2.


  • pyserial (sudo pip install pyserial)

The Procedure:

  1. Went to the Github repo.
  2. Cloned the repo using git.
  3. Connected the Freeduino board using USB interface.
  4. Started the Arduino IDE.
  5. Selected my board category and Serial Port.
  6. File -> Open -> (Navigate to the cloned repo and select the prototype.pde file).
  7. Verify and Upload this Prototype.pde sketch to the board.

Done. The device is now ready to accept instructions via Python serial interface.

Open the terminal and navigate to the cloned repository and cd into arduino directory where you will find file. Take advantage of the python interpreter. I prefer using IPython, so I have invoked the interpreter (you can also carry this with default python interpreter). Try the below code to control the LED on pin 13 on the board.

from arduino import Arduino
b = Arduino(‘/dev/ttyUSB0’)

pin = 13  # set the pin number
b.output([pin])  # set the output on this pin number

b.setHigh(pin) # This is power on the LED
b.setLow(pin) # This is power off the LED

b.close() # closes the connection and the port.

Voila. It feels good. Really, really good. My next attempt is to understand the file which actually uses the pyserial library to talk with the device and the prototype.pde file, which makes the device to serve python instructions.