A few days ago I was experimenting with the firmata library on Arduino uno, the simple experiment I describe here is about a LED semaphore triggered by a Ligh Dependent Resistor (LDR).

Firmata is a software library which implements the firmata protocol, better described in the website http://firmata.org/wiki/Main_Page :

Firmata is a generic protocol for communicating with microcontrollers from software on a host computer.

Basically, it allows to send commands on a serial link to an Arduino board from a host PC.

Of course you could open a serial terminal and issue the commands typing on the keyboard, luckily enough libraries that “speaks firmata” are available for all the main programming languages, here I will use pyfirmata, which is a firmata library for python.

To reproduce this experiment, you will need to:

  • install firmata binary on Arduino board
  • installa pyfirmata on the host
  • assemble the circuit on the breadboard
  • start sending commands from the host

Part list for the circuit:

  • 3 x leds: green red and yellow (it’s a semaphore, after all)
  • 3 x 330 ohm resistors to limit current in the leds
  • 1 x LDR (light dependent resistor)

Install firmata on Arduino

You should find Firmata on you standard Arduino examples, fire your IDE or search for Firmata on the example folder and load it on the board, here is where I found it on my dev machine:

./arduino-1.0/libraries/Firmata/examples/StandardFirmata/StandardFirmata.ino

You can install it as you would do for an ordinary sketch.

Install pyfirmata on your host PC

You can get the sources from the repository below:

cd
hg clone https://bitbucket.org/tino/pyfirmata
cd pyfirmata
sudo python manage.py install

Now you can connect your board and test the library with a simple LED 13 blink program which turns on and off the on-board LED connected on pin 13:

from pyfirmata import Arduino, util
board=Arduino('/dev/ttyACM0')  # adapt to your device address
ledpin = board.get_pin('d:13:o') # d=digital 13=pin number o=output
ledpin.write(1)
ledpin.write(0)

Green light to go!

Semaphore circuit, click to enlarge.Here is the semaphore script, see the picture for the circuit details.

The idea is to control the semaphore through a LDR, when you darken the sensor, the semaphore goes green (after two cycles the program ends).

import time
from pyfirmata import Arduino, util

board = Arduino('/dev/ttyACM0')

"""
rosso: pin D 8  -> red
giallo: pin D 11 -> yellow
verde: pin D 13 -> green
light pin A 0 -> LDR

"""
DELAY=1
GREEN=7

# GYR 13 11 8
g = board.get_pin('d:13:o')
y = board.get_pin('d:11:o')
r = board.get_pin('d:8:o')

l = board.get_pin('a:0:i')

iterator = util.Iterator(board)
iterator.start()

r.write(1)

def go_green():
	y.write(1)
        time.sleep(DELAY)
	r.write(0)
	g.write(1)
	y.write(0)
	time.sleep(GREEN)
	y.write(1)
	time.sleep(DELAY)
	g.write(0)
	y.write(0)
	r.write(1)

cycles = 0

while cycles < 2: 	
       val = l.read() 	
       print "Val %s" % val 	
       time.sleep(1) 	
       if val < 0.8:
		cycles+=1
		go_green()

board.exit()

It's only fair to share...Tweet about this on TwitterShare on FacebookShare on LinkedInPin on PinterestShare on RedditShare on Google+

Trackbacks/Pingbacks

  1.  ItOpen – Open Web Solutions, WebGis Development » Blog Archive » Driving a 7-segments display with arduino CD4511 and pyfirmata