Bonus Chapter: Sending Data Between Raspberry PI And Arduino

Introduction

 
This is a bonus chapter for you, In this chapter, you will be going to learn how we can send the data from Pi to Arduino Board, and Arduino Board to Pi, and see the output on the serial monitor and Arduino (Blinking Led).
 
The chapter will also be going a brief introduction of some components used, to brush up your skills.
 

Data Exchange between Raspberry Pi and Arduino

 
Parts Of Lists
  1. Raspberry Pi
  2. ATMega 328
  3. 1-Stripboard (prototype) 3-3/4*2 (larger)
  4. 1-24 pin wire wrap socket
  5. 1-26 pin stacking header
  6. Hook up wires.
Raspberry Pi
  1. Raspberry Pi is a credit card-sized computer that plugs into your TV and a keyboard.
  2. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games.
  3. It also plays high-definition video. We want to see it being used by kids all over the world to learn to program.
computer
ATMega328
  1. The ATmega328 is a single-chip microcontroller created by Atmel in the megaAVR family.
     
  2. The Atmel 8-bit AVR RISC-based microcontroller combines 32 kB ISP flash memory with read-while-write capabilities, 1 kB EEPROM, 2 KB SRAM, 23 general-purpose I/O lines, 32 general purpose working registers, three flexible timer/counters with compare modes, internal and external interrupts.
     
  3. The serial programmable USART, a byte-oriented 2-wire serial interface, SPI serial port, 6-channel 10-bit A/D converter (8-channels in TQFP and QFN/MLF packages), programmable watchdog timer with internal oscillator, and five software selectable power saving modes. The device operates between 1.8-5.5 volts.
     
  4. The device achieves throughput approaching 1 MIPS per MHz.
     
    computer
Stripboard
  1. Stripboard is the generic name for a widely used type of electronics prototyping board characterized by a 0.1 inch (2.54 mm) regular (rectangular) grid of holes, with wide parallel strips of copper cladding running in one direction all the way across one side of the board.
     
  2. It is commonly also known by the name of the original product Veroboard, which is a trademark, in the UK, of British company Vero Technologies Ltd and Canadian company Pixel Print Ltd.
     
  3. In using the board, breaks are made in the tracks, usually around holes, to divide the strips into multiple electrical nodes. With care, it is possible to break between holes to allow for components that have two-pin rows only one position apart such as twin-row headers for IDCs.
     
  4. Stripboard is not designed for surface-mount components, though it is possible to mount many such components on the trackside, particularly if tracks are cut/shaped with a knife or small cutting disc in a rotary tool.
     
    computer
Wire Wrap Socket
  1. Standard IC Wire Wrap Sockets. Extended leads allow for multiple wire wrap connections for prototyping.
  2. This is a 600mil 40-pin socket. Check the related items for more wire wrap sockets in different widths and pin counts.
     
    computer
Stacking Header
  1. A Digikey.com search for samtec SSQ includes numerous longtail stackable headers (as well as some shorttail, or non-stackable). These headers are made to work with the Arduino Main Board, Arduino Pro, and the Arduino Mega.
  2. They are the perfect height for clearing the USB-B connector and great for stacking multiple shields.
  3. This kit includes 4 headers (2 8-pin and 2 6-pin), enough to connect a shield to an Arduino Main Board. These are the same headers we use in our tutorials and with our own shields.
Step 1 Building The Board
  1. I used a Stripboard to build the Raspberry Pi interface board/hoody. Stripboard has the advantage of design flexibility.
  2. It does not constrain the component layout to any particular configuration.
  3. It's also much cheaper than equivalent-sized solderable protoboards.
  4. The chief disadvantage of Stripboard is the necessity of extensive cutting of copper traces on the solder side.
Explanation Of Connecting
 
It's advisable to have a preliminary idea of the layout before beginning. A rough diagram on a sheet of paper helps.
 
I mounted the 26-pin stacking (long-pin) GPIO header near one edge of the board, with the female (plug-in) part of the header on the copper-trace side. The trick is for the header not to mount flush against the board, but to stand off about 1/4" high, in order to permit soldering the pins to the copper traces. But, first, we need to cut thirteen rows of traces where the GPIO header will mount so that adjacent header pins will not be shorted to each other (see 2nd and 3rd illustrations).
 
Cutting the traces requires going back and forth with a sharp knife blade with a fair amount of pressure (caution!), then follow-up checking with a continuity tester. It's a fair amount of work, and next time I try something like this I'll probably use a Dremel tool with cutting disk, rather than a knife. With the thirteen traces cut, carefully mount the GPIO header approximately 1/4" in elevation.
 
This will give enough room to maneuver the tip of the soldering iron between the pins to solder them. A couple of dabs of poster-mounting putty hold the header in position prior to soldering. Solder one endpin, then carefully straighten out the header so it sits perpendicular to the board. Solder the remaining pins, then use the continuity tester to look for shorts between pins, both sideways and across.
 
A good continuity tester with an audible indicator is especially helpful here. After soldering, the header pins will protrude 1/4" or a bit more on the perf board (non-solder) side. This is convenient, as these will function as posts to attach patch-cords and jumpers. Each post will be an electrical connection to the corresponding GPIO pin underneath when the board is mounted on the Pi.
 
Images Of Connecting
  • Step 1 Image Trace the cut
     
    computer
     
  • Step 2 make the cut where we mark
     
    computer
     
  • Step 3  Fix the Stacking header on the trace cut 
     
    computer
     
  • Step 4  Fix the wire Wrap Socket
     
    computer
Building the Arduino ATMega328
 
computer
 
Connection
  1. Vcc (3.3v) on the Arduino connects to the GPIO Vcc (3.3v) pin: Pin #1.
  2. Ground on the Arduino connects to the GPIO Ground pin: Pin #6.
  3. TxD on the Arduino connects to the GPIO RxD pin: Pin #10.
  4. RxD on the Arduino connects to the GPIO TxD pin: Pin #8.
Explanation
  1. That TxD connects to RxD, and vice-versa. The reason for this is that signal-out on one device goes to signal-in on the other. It's a bit confusing at first, but quite logical when you think about it.
  2. To facilitate tracing and troubleshooting, use a red jumper for Vcc and a black one for ground, and two other colors for TxD/RxD and RxD/TxD.
Programming For Arduino to Pi
  1. //#include <stdlib.h>     
  2. #define DELA 1200 // Time between message sends.  
  3.    
  4. int cnt = 0;    
  5. void setup() {    
  6.     Serial.begin(9600);    
  7. }    
  8. void loop() {    
  9.     Serial.print("Line Number # [");    
  10.     Serial.print(cnt);    
  11.     Serial.println("]");    
  12.     delay(DELA);    
  13.     cnt++;    
  14. }   
Explanation
  1. We're not quite ready to plug the interface hoody into the Pi. First, let's load a program into Arduino Pro that will tell it to broadcast the data on its serial port. Hook up an appropriate serial/tty-to-USB cable or an FTDI breakout board to the 6-pin right-angle programming header. Plug the USB end into your laptop computer running the Arduino IDE, and upload the following sketch into the Arduino. So, this would give the Pi access to analog ports and devices that connect to the same.
     
  2. This is a simple sketch that increments a variable and sends it to serial-out as part of a "Line Number #" string. It will display in the console or on an xterm on the Pi running minicom
Output For Arduino To Pi
 
Figure5: Arduino to pi.
 
PI Sending Data To Arduino
  1. Now, we're finally ready to plug our interface hoody into the Pi's GPIO connector. Carefully orient the pins so that female header Pin 1 plugs into GPIO Pin1, etc. Gently press down, and ...
     
  2. All right, hook up the Pi to power and a video display. Make sure an SD card with Pidora is in the card slot underneath. Now, boot it up and invoke minicom (sudo minicom -s) in the console or in an xterm. You'll have to set minicom to the /dev/ttyAMA0 serial port because this is what rx and tx are hooked to. The default setting of minicom is /dev/modem, so you can cd /dev and sudo ln -s ttyAMA0 modem. And, set the baud rate to 9600, because that's what the sketch has set the Arduino serial transmission speed.
Programming
  1. # define DELA 500 // Long delay.     
  2. # define DSHORT 250 // Short delay.     
  3. const int ledPin = 13;    
  4.     
  5. void setup() {    
  6.     pinMode(ledPin, OUTPUT); // Initialize pin as output.     
  7.     // Pin 13 has an LED hooked to it.     
  8.     Serial.begin(9600);    
  9. }    
  10.     
  11. void loop() {    
  12.     if (Serial.available()) {    
  13.         light(Serial.read() - '0');    
  14.     } // How many positions past ASCII 0?     
  15.     delay(DELA);    
  16. }    
  17.     
  18.     
  19. void light(int n) {    
  20.     for (int i = 0; i < n; i++) {    
  21.         digitalWrite(ledPin, HIGH); // Turn the LED on.     
  22.         delay(DSHORT);    
  23.         digitalWrite(ledPin, LOW); // Turn the LED off.     
  24.         delay(DSHORT); // Wait.     
  25.     }    
  26. }   
Output
 
Figure5: Arduino to pi.
 
That’s all for this chapter. I hope you enjoyed reading!!
 
And hence we have come to the end of IoT learn series. Happy IoTing.
Author
Sr Karthiga
0 12.1k 2m