Raspberry Pi Arduino Paul Mac Dougal January 11
Raspberry Pi <--> Arduino Paul Mac. Dougal January 11, 2016
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.
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
UART • Hardware block for handling serial communication • Different baud rates (e. g. 115200, 38400, 9600)
Just hook them up? 8 TXD Tx 1 10 RXD Rx 0 5 v Uno Pi Gnd
No!!! 8 TXD Tx 1 10 RXD Rx 0 5 v Uno Pi Gnd
Use a 3. 3 v Arduino 8 TXD Tx 1 10 RXD Rx 0 3. 3 v Arduino Pi Gnd
Use level shifters 8 TXD 3. 3 5 Tx 1 10 RXD 3. 3 5 Rx 0 Uno Pi Gnd
Demo A Setup 8 TXD Tx 1 10 RXD Rx 0 3. 3 v Digi. X Pi Gnd USB to PC
Demo A Sketch void setup() { Serial. USB. begin(115200); while(!Serial. USB); // wait for connection Serial 0. begin(115200); // pins 0, 1 } void loop() { while (Serial 0. available()) Serial. USB. write(Serial 0. read()); while (Serial. USB. available()) Serial 0. write(Serial. USB. read()); }
Demo A
Demo A
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=tty. AMA 0, 115200 console=tty 1 root=/dev/mmcblk 0 p 6 rootfstype=ext 4 elevator=deadline rootwait • /etc/inittab #T 0: 23: respawn: /sbin/getty -L tty. AMA 0 115200 vt 100
Uno Pins 0, 1 are in use for serial communication over USB
Software. Serial • Arduino Library for bit-banging on digital I/O lines to emulate a UART • Not as fast as a real UART
Demo B Setup 8 TXD 10 RXD 3. 3 5 Tx 3 USB to PC 0, 1 Rx 2 Uno Pi Gnd
Demo B Sketch #include <Software. Serial. h> Software. Serial my. Serial(2, 3); // RX, TX void setup() { Serial. begin(38400); // HW UART to PC host my. Serial. begin(9600); // bit-banging to Pi } void loop() { while (my. Serial. available()) Serial. write(my. Serial. read()); while (Serial. available()) my. Serial. write(Serial. read()); }
Demo. B öænuóÿn: —ûøöænuóÿÿ some spew at 115200 baud Raspbian GNU/Linux 7 rpi. B tty. AMA 0 rpi. B login: pi Password: Last login: Sat Jan 9 10: 07: 31 EST 2016 on tty. AMA 0 Linux rpi. B 3. 12. 22+ #691 PREEMPT Wed Jun 18 18: 29: 58 BST 2014 armv 6 l 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. pi@rpi. B: ~$ ls crontab. txt �[0 m�[01; 34 mdemos�[0 m �[01; 35 mocr_pi. png�[0 m �[01; 31 m. RPi. GPIO-0. 5. 11. tar. gz�[0 m crontab. txt. save �[01; 34 m. Desktop�[0 m �[01; 34 mpython_games�[0 m �[01; 34 mrs 485�[0 m �[01; 32 mdatalog. sh�[0 m foo. log �[01; 34 m. RPi. GPIO-0. 5. 11�[0 m
Demo C • Same hardware setup as demo B • Turn off the default Rasbian uses of /dev/tty. AMA 0 • Transmit from Python program • Receive on Arduino and echo to PC
Demo C Python program import serial import time ser = serial. Serial('/dev/tty. AMA 0', 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 Keyboard. Interrupt: ser. close()
Demo C Initialized Pi_talk democ. py !"#$%&'()*+, -. /0123456789: ; <=>? @ABCDEFGHIJKLMNOPQRSTUVWXYZ
Demo D • Transmit from Arduino • Receive on Pi
Demo D Sketch #include <Software. Serial. h> Software. Serial my. Serial(2, 3); // RX, TX void setup() { my. Serial. begin(9600); // bit-banging to Pi } uint 8_t c = 0; void loop() { my. Serial. write(c++); delay(1); }
Demo D Python program import serial import time ser = serial. Serial('/dev/tty. AMA 0', 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 Keyboard. Interrupt: ser. close()
Demo. D % sudo python demod. py … got 163 got 164 got 165 got 166 got 167 got 168 got 169 got 170 got 171 got 172 got 173 … % sudo python demodcheck. py Expected 0, got 220 1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8
Software • Roll your own (e. g. ‘a’ means LED on, ‘b’ means LED off) • MOSS (Message Over Software. Serial) – Start byte for synchronizing to message – Length / Data / Checksum • Firmata (Open. Source package) – Initial impression is quite favorable
Q/A • Questions?
Backup Slides
5 to 3. 3 Level Shifter
3. 3 to 5 Level Shifter
Level Shifter
- Slides: 31