Saturday, 11 October 2014

PyFirmata summary


For using a firmata (Respberry <-> Arduino) you must install it on:


Statement of connection types:

device/connection type get_pin(...) string available values
relay shield d:pin_no:o 0 and 1
DC motor d:pin_no:p 0.0 - 1.0
servo d:pin_no:s 0 - 255
analog input (*) a:pin_no:i 0.0 - 1.0

(*) you must start reading analog input:
from pyfirmata.util import Iterator
...
board = ArduinoMega('/dev/ttyUSB0')
iterator = Iterator(board)
iterator.start()
...

Example use servo:
from pyfirmata import ArduinoMega
board = ArduinoMega('/dev/ttyUSB0') # Arduino usb port
servo = board.get_pin('d:2:s')      # pin PWM no 2
servo.write(50)                     # set servo position 0-255

Temperature sensor LM35


We need:

Sensor LM35:



Connections schema:


We can connect LM35 to 5V or 3.3V, it doesn't matter.


Photo from my LM35 connections:



Example python app lm35_test.py:
from pyfirmata import ArduinoMega
from pyfirmata.util import Iterator
import time

board = ArduinoMega('/dev/ttyUSB0') # connect to arduino usb port

iterator = Iterator(board) # start reading analog input
iterator.start()

pinTemp = board.get_pin('a:0:i') # set valid in analog pin

while True:
    voltage = pinTemp.read() # read voltage input
    if voltage is not None: # first read after startup is somtimes None
        temp = 5.0*100*voltage # convert voltage to temperature
        print "{0} Celsius".format(temp)
        time.sleep(1) # 1 second waiting 


Run python environment (see Firmata - Raspberry Pi (part 2)), for example:
# cd robot/python_firmata_test/
# source bin/activate
and run app:
# python2.7 lm35_test.py
Screenshoot form app:


LM35 is theoretically appropriately calibrated but in my opinion, given the temperature is overestimated by about 2-4 degrees CelsiusI had a reference (room thermometer;) ) thermometer.


links:
http://raspberrypi-aa.github.io/session3/firmata.html
http://www.ladyada.net/learn/sensors/temp36.html
http://www.ti.com.cn/cn/lit/ds/symlink/lm35.pdf

Sunday, 5 October 2014

Relay shield control

We need:

Relay shield:

Connections schema:


Photo from my relay shield connections:




Simple app to blinking relay connect to 22 pin:
from pyfirmata import ArduinoMega, time
board = ArduinoMega('/dev/ttyUSB0')
servoA = board.get_pin('d:22:o')

while True:
   servoA.write(1) 
   time.sleep(1)
   servoA.write(0) 
   time.sleep(1)


Command line completely app relay_example.py:
from pyfirmata import ArduinoMega, time
board = ArduinoMega('/dev/ttyUSB0')
firstPin = 22 # number first pin, app use 8 pins

__copyright__ = "Copyright 2014, http://letsmakearobot.blogspot.com/"
__version__ = "0.1.0"
__license__ = "GPL"
__email__ = "sebastian.dziak@gmail.com"

def info():
    print "------------------------------------------------"
    print "Command parameters:"
    print "  1-8 - relay no"
    print "  0-1 - relay status (0 - off, 1 - on)"
    print "  ...and exit from app: exit"
    print 
    print "Examples:"
    print "  2 1 #set on relay no 2"
    print "  2 0 #set off relay no 2"
    print "  5 1 #set off realy no 5 "
    print "  exit"
    print "------------------------------------------------"

def validate(position):
   if not position is None and (float(position)<0 or float(position)>255):
      return "Invalid parameter!"

info()

pins = []
for n in range (0, 8):
   pinNo = firstPin + n
   pins.append( board.get_pin('d:'+str(pinNo)+':o') )

while True:
   cmdLine = raw_input('Relay_no(1-8) set_status(0-1):')
   if cmdLine == 'exit': break 
   args = cmdLine.split()
   pins[int(args[0])-1].write(int(args[1]))

print "goodbye"

Run python environment (see Firmata - Raspberry Pi (part 2)), for example:

# cd robot/python_firmata_test/
# source bin/activate

and run app:
# python2.7 relay_example.py


Screenshoot form app: