Download presentation
Presentation is loading. Please wait.
1
Raspberry Pi <--> Arduino
Paul MacDougal January 11, 2016
2
Motivation We want to communicate between python code running on a Pi and C++ code running on an Arduino Why? Arduino runs realtime. Arduino has analog inputs. Pi talks to the Internet.
3
Hardware The Pi has two pins (8,10) connected to a UART (Universal Asynchronous Receiver/Transmitter) Arduino Uno has two pins (0,1) connected to a UART
4
UART Hardware block for handling serial communication
Different baud rates (e.g , 38400, 9600) Universal Asynchronous Receiver/Transmitter
5
Just hook them up? Pi 5v Uno 8 TXD Tx 1 10 RXD Rx 0 Gnd Gnd
Do not hook up a 5V Arduino Uno pin to a 3.3v Raspberry Pi pin. It is important to connect the grounds Gnd Gnd
6
No!!! Pi 5v Uno 8 TXD Tx 1 10 RXD Rx 0 Gnd Gnd
Do not hook up a 5V Arduino Uno pin to a 3.3v Raspberry Pi pin. It is important to connect the grounds Gnd Gnd
7
Use a 3.3v Arduino Pi 3.3v Arduino 8 TXD Tx 1 10 RXD Rx 0 Gnd Gnd
Yourduino.com sells a UNO clone called RoboRED that can run at 3.3v Arduino Due runs at 3.3v Pro Mini runs at 3.3v Gnd Gnd
8
Use level shifters Pi Uno 8 TXD 3.3 5 Tx 1 10 RXD 3.3 5 Rx 0 Gnd Gnd
3.3 5 Tx 1 10 RXD 3.3 5 Rx 0 I use Adafruit part #735 74LVC245 Not all chips support 5v inputs when running at 3.3v (check the datasheet) The 3.3v logic level on Rx … Gnd Gnd
9
Demo A Setup USB to PC Pi 3.3v DigiX 8 TXD Tx 1 10 RXD Rx 0 Gnd Gnd
DigiX has SerialUSB (== Serial), Serial0, Serial1, Serial2 UARTs Gnd Gnd
10
Demo A Sketch void setup() { SerialUSB.begin(115200);
while(!SerialUSB); // wait for connection Serial0.begin(115200); // pins 0,1 } void loop() while (Serial0.available()) SerialUSB.write(Serial0.read()); while (SerialUSB.available()) Serial0.write(SerialUSB.read()); DigiX has SerialUSB (== Serial), Serial0, Serial1, Serial2 UARTs is chosen because that is the default UART setting in Raspbian Linux
11
Demo A % cd ~/demos % ./demoa.sh % sudo /sbin/shutdown now
<power off Pi> <hook up Demo A Setup> <load demoa.ino onto DigiX> <start the serial monitor> <Power cycle the Pi> <log in and play around> % cat /etc/motd <prepare Pi for demoB> % ./demob.sh
12
Demo A The individual bits take 1/ of a second
13
Serial Ports are useful
Raspbian Linux uses the serial port for console output and for login. /boot/cmdline.txt dwc_otg.lpm_enable=0 console=ttyAMA0, console=tty1 root=/dev/mmcblk0p6 rootfstype=ext4 elevator=deadline rootwait /etc/inittab #T0:23:respawn:/sbin/getty -L ttyAMA vt100 Other distros do similar things, I believe.
14
Uno Pins 0,1 are in use for serial communication over USB
Pins 0,1 are used to communicate with the IDE We cannot do both USB and Pi on the same port When not connected to USB, we can hope that the other end of those 1k resistors is high impedance
15
SoftwareSerial Arduino Library for bit-banging on digital I/O lines to emulate a UART Not as fast as a real UART
16
Demo B Setup USB to PC Pi Uno 8 TXD 3.3 5 Tx 3 0,1 10 RXD Rx 2 Gnd Gnd
17
Demo B Sketch #include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3); // RX, TX void setup() { Serial.begin(38400); // HW UART to PC host mySerial.begin(9600); // bit-banging to Pi } void loop() while (mySerial.available()) Serial.write(mySerial.read()); while (Serial.available()) mySerial.write(Serial.read()); Bit banging a serial port on the digital I/O lines is not as fast as a HW UART 38400 works for a few characters, but falls behind on large bursts
18
DemoB öænuóÿn:—ûøöænuóÿÿ some spew at 115200 baud
Raspbian GNU/Linux 7 rpiB ttyAMA0 rpiB login: pi Password: Last login: Sat Jan 9 10:07:31 EST 2016 on ttyAMA0 Linux rpiB #691 PREEMPT Wed Jun 18 18:29:58 BST 2014 armv6l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. ls crontab.txt [0m[01;34mdemos[0m [01;35mocr_pi.png[0m [01;31mRPi.GPIO tar.gz[0m crontab.txt.save [01;34mDesktop[0m [01;34mpython_games[0m [01;34mrs485[0m [01;32mdatalog.sh[0m foo.log [01;34mRPi.GPIO [0m % cd ~/demos % ./demob.sh <change baud rate to 9600 and turn off console spew> % sudo /sbin/shutdown now <power off Pi> <hook up Demo B config> <load DemoB sketch into Uno> <start Serial Monitor> <power on Pi> <log in> <prepare Pi for demo C> % ./democ.sh % sudo /sbin/shutdown –r now
19
Demo C Same hardware setup as demo B
Turn off the default Rasbian uses of /dev/ttyAMA0 Transmit from Python program Receive on Arduino and echo to PC If you don’t turn off ttyAMA0, your python code will fail to connect to the serial port
20
Demo C Python program import serial import time
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0) ser.open() ser.write("democ.py") value = 0 try: while 1: ser.write(chr(value)) value = (value+1) & 255 time.sleep(0.1) except KeyboardInterrupt: ser.close()
21
Demo C Initialized Pi_talk democ.py
<restart Serial Console> % cd demos % sudo python democ.py
22
Demo D Transmit from Arduino Receive on Pi
23
Demo D Sketch #include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3); // RX, TX void setup() { mySerial.begin(9600); // bit-banging to Pi } uint8_t c = 0; void loop() mySerial.write(c++); delay(1);
24
Demo D Python program import serial import time
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0) ser.open() ser.write("demod.py") try: while 1: # Nonblocking read of serial port response = ser.read() if len(response) > 0: print("got %d" % (ord(response[0]))) except KeyboardInterrupt: ser.close()
25
DemoD % sudo python demod.py … got 163 got 164 got 165 got 166 got 167
% sudo python demodcheck.py Expected 0, got 220 1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 <load DemoD sketch> % sudo python demod.py % ^C % sudo python demodcheck.py
26
Software Roll your own (e.g. ‘a’ means LED on, ‘b’ means LED off)
MOSS (Message Over SoftwareSerial) Start byte for synchronizing to message Length / Data / Checksum Firmata (OpenSource package) Initial impression is quite favorable I developed MOSS for my rs485 home automation use
27
Q/A Questions?
28
Backup Slides
29
5 to 3.3 Level Shifter Adafruit part #735
Check the datasheet to be sure that input pins can accept greater than Vcc (i.e. 5v > 3.3v)
30
3.3 to 5 Level Shifter I have not tried this.
31
Level Shifter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.