Communication Between Raspberry Pi and Arduino Via Usb #firmata #python

8.3K536

Intro: Communication Between Raspberry Pi and Arduino Via Usb #firmata #python

hello, guys, in this Instructable will show how to make communication between raspberry pi & Arduino via USB for this we are using firmata protocol.

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

I believe this is the easiest way to control Arduino via USB.
I found this, method in this old magazine . & this method really works great.

In this method, you have to upload a sketch in Arduino only one time & you get full access on your raspberry pi.

basically raspberry pi act as Master & Arduino act as a slave.

Must watch this video for detail guidance-https://youtu.be/s4o8T6F-zGU

like & subscribe for more such videos.

STEP 1: Upload Sketch to Arduino

connect your Arduino your computer & upload StandardFirmata sketch.you will found this sketch in

Examples / Firmata / StandardFirmata and Upload the sketch.

STEP 2: Installing Firmata to Raspberry Pi

pyFirmata is the magic that allows your
Arduino running Firmata to talk to Python. run this commands in terminal to install required modules.

sudo apt-get install python-serial mercurial
pip install pyfirmata

to check is this install properly on not.
open python idle & run

  import pyfirmata

if you get any error then pyfirmata is not installed properly

try another method:

sudo apt-get install python-serial mercurial
git clone  https://github.com/tino/pyFirmata
cd pyfirmata
sudo python setup.py install

STEP 3: Simple Blinking LED Code

now build the circuit.
connect led to pin D13 of Arduino

connect Arduino to raspberry pi via USB.

by entering this command in terminal you will get a list of all USB devices

ls /dev/tty*

in my case, ACM0 is my Arduino.

now create new python file in idle copy & past this sketch.

you will get properly formatted sketch here Blinking LED code

import pyfirmata
import time board = pyfirmata.Arduino('/dev/ttyACM0')
pin13 = board.get_pin('d:13:o') 
while True:
	time.sleep(1)
        print("on") 

	pin13.write(1)
	time.sleep(1) 
	print("off") 
	pin13.write(0)

important links

Magpi magzin==https://drive.google.com/open?id=0B_7VHGHWspGcMndXNjhHakczRDg
firmata ==https://github.com/tino/pyFirmata


3 Comments

Help me to solve this problem! Thanks all!!!

Traceback (most recent call last):

File "<pyshell#2>", line 1, in <module>

board = pyfirmata.Arduino('/dev/ttyACM0')

AttributeError: 'module' object has no attribute 'Arduino'

2.10.2017

Interesting and the very simple example works.

But if I put a print pin13.read one gets a

<bound method Pin.read of <pyfirmata.pyfirmata.Pin object at 0x76a1afd0>>

Which I assume is an error saying this is a read-only pin.

More examples would be most helpful: such a read a pin, Analogue input, PWM output.

You need to change 'o' to 'i' in 'get_pin('d:13:o')

d - digital pin

a - analog pin

o - output

i - input

s - servo

p - PWM

I think that should help you a little, if you still need it :)