Raspberry Pi communicates with Arduino

Source: Internet
Author: User

First, install the Raspberry Pi and Arduino development environment

1, Arduino development environment in the previous article has been explained that the need for friends can see "Arduino Control two motor Movement" this blog post.


2. Building a Raspberry Pi serial communication development environment

(1) Install Python:

sudo apt-get update

sudo apt-get install python

(2) Installing a Python gpio module for controlling LEDs, motors, etc.:

wget https://sourceforge.net/projects/raspberry-gpio-python/files/RPi.GPIO-0.6.2.tar.gz (download Gpio library, because update, specific reference website website change )

Tar xvzf rpi.gpio-0.6.2.tar.gz (tar decompression)

cdrpi.gpio-0.6.2 (Enter the unpacked folder)

sudo python setup.py install (installing Gpio library files)

(3) Install serial, for serial communication and USB communication:

sudo apt-get install python-serial

(4) test the first three steps of the environment installation is correct

#用nano编辑器编辑一个test文件

sudo nano test.py

Write the following two lines of code #在test the. py file

Import serial

Import Rpi.gpio

#运行python test.py Statement, if there is no error indicating that the Raspberry Pi development environment is built correctly.

To this Raspberry Pi and Arduino communication development environment is completed, the following explains the specific communication implementation.

second, Raspberry Pi and Arduino communicate via USB

1. Connect the Raspberry Pi with the Arduino via a USB cable


2. In the Raspberry Pi terminal, enter ls/dev/tty* to view the names of the connection ports. See if you have ttyACM0 this file (note that this is only possible with two hardware USB interconnects.) If there is no connection between the two, the latest system will generally be generated automatically. Seeing ttyACM0 shows that they can communicate the next test code.


3, write the Raspberry Pi and Arduino communication code.

Arduino Code:

void Setup ()
{
Serial.begin (9600); 9600 bps
}
void Loop ()
{
if (serial.available ())
{
if (' s ' = = = Serial.read ())
Serial.println ("Hello raspberry,i am Arduino.");
}
}

Download the above code through the Python IDE to the Arduino and then link to the Raspberry Pi USB. Raspberry Pi Code:

Import serial #import serial module
Ser = serial.   Serial ('/dev/ttyacm1 ', 9600,timeout=1); #open named port at 9600,1s Timeot

#try and Exceptstructure are exception handler
Try
While 1:
Ser.write (' s '); #writ a string to port
Response = Ser.readall (); #read a string from port
Print response;
Except
Ser.close ();

Here I save the above code in the Raspberry Pi as a communication.py file.

2. In the Raspberry Pi terminal, enter sudo python communication.py to run the program.

Show Results:


This figure is the result of communication between the two: the Raspberry Pi sends a character ' s ' to the Arduino, and the Arduino replies to the Raspberry Pi string "Hello raspberry,i am Arduino".

third, Raspberry Pi and Arduino communicate via Gpio pin


1. Connection method:

Raspberry Pi RX---Arduino TX

Raspberry Pi TX---Arduino rx

Raspberry Pi Gnd---Arduino GND 2, modify the Raspberry pi configuration file (to see the detailed please go here http://www.eeboard.com/bbs/thread-8574-1-1.html)

(1) Modify Cmdline.txt
Enter sudo nano/boot/cmdline.txt copy code to see dwc_otg.lpm_enable=0 console=ttyama0,115200 kgdboc=ttyama0,115200 console=tty1 ROOT=/DEV/MMCBLK0P2 rootfstype=ext4 elevator=deadline rootwait Copy code remove console=ttyama0,115200 kgdboc=ttyAMA0,115200

Into this dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait Copy Code ctrl+x Press Y to save exit

2. Modify the Inittab and then disable the login from the serial port function,

Enter sudo nano/etc/inittab copy code to find t0:23:respawn:/sbin/getty-l ttyAMA0 115200 vt100 Copy Code This line is preceded by a # comment out this line becomes #T0:23:respawn :/sbin/getty-l ttyAMA0 115200 vt100 Copy Code ctrl+x press Y to save exit
Then restart the Raspberry Pi, then the Raspberry Pi pin8 (GPIO14) pin10 (GPIO15) These two ports can be used as a normal serial port to use now you can use the same as a normal serial port, using/DEV/TTYAMA0.


3. Experiment Code and Effect

Just add the above USB communication to the Raspberry Pi code ser = serial. Serial ('/dev/ttyacm1 ', 9600,timeout=1); The ttyACM1 in the ttyAMA0 can be changed.




to this the Raspberry Pi with Arduino through USB and Ggpio two kinds of communication methods are finished.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.