Raspberry Pi and Arduino communications using SPI with Python and CPP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to show you how you  can use spi to communicate between a raspberry   pi and an arduino i'll be showing a practical  example using python on the raspberry pi and C++   on the arduino with an example test environment  with code that you can use in your next project   i'm going to start by explaining what spi is why  you may want to use it instead of the traditional   serial protocol and i'll cover how you can  handle the difference in voltage between the   raspberry pi gpio and the arduino and a simple  data protocol that i've created for this example so i'm going to start with the theory about spi   if you're already familiar with that and would  like to move straight onto the practical section   then look for the chapters on the  timeline or in the description   but first i'm going to talk a little about  the language and terminology i'm going to use traditionally spi has been explained  in terms of a master slave relationship   this is now considered by many to be inappropriate  as it is trivializing the suffering of slaves   both historically and through modern day slavery  affecting vulnerable people around the world also the terminology isn't really correct whilst  spi is often used to connect to basic devices   which just respond to requests we'll be using an  arduino which is programmable whilst the raspberry   pi will be controlling the communication  bus it's equally possible for the arduino   to instruct the raspberry pi to do something it  doesn't need to be just one way so on that basis   referring to a master slave doesn't really make  sense i'm therefore going to refer to raspberry   pi as the main device or main controller and the  arduino used in this example as a secondary device   there's other terminology that could  be used but using words that begin with   M and S allow it to fit in with the  usual acronyms which you will see later   with that out of the way i can now explain  how spi works it's a serial protocol similar   to the uart usb serial communications which  i used in a previous video however spi is a   synchronous communication protocol which means  that it needs a clock signal it's also normally   used in a full duplex bi-directional mode  whereas in the uart based serial communication   previously the data was sent in a half  duplex sending only one direction at a time   first i'll look at the uart serial communications  then show how this differs for how spi works   the uart is actually the name of the hardware  rather than the protocol stands for universal   asynchronous receiver and transmitter but to make  it easier to understand i'll use uart when talking   about the protocol normally used by usb on the  raspberry pi the uart is also available on the   gpio ports 14 and 15 physical pins eight and ten  and on the arduino uno on physical pins zero one   excluding the ground connection which is needed  for all communications there are two connections   between the raspberry pi and the arduino the  transmit from the raspberry pi goes to the   receive of the arduino and the transmit from the  arduino goes to the receive of the raspberry pi   the information is sent from the transmitter of  one device to the receiver of the other shown here   going from the raspberry pi to the arduino if the  arduino would like to send information back then   it takes its turn to send data on its transmit  port which is received at the raspberry pi   this happens asynchronously which means  there is no need for an external clock   and is normally half duplex meaning that one  side takes in turns at transmitting information   this is simplified as in reality there will be  flow control signals between the devices as well in the code you can send information and then wait  on the other side to respond or if no response is   needed then you can continue to send information  the communication can be initiated from either   side and both need to be configured with the same  parameters to be able to understand what the other   is saying also you can normally only have two  devices connected between a pair of uart ports   now to look at spi we still need an equivalent  of the transmit this time called M O S I mosi   main out secondary in and M I S O miso main in  secondary out unlike the uart where these cross   these are referred to the same name at both sides  the transmission is synchronous so this also needs   an additional clock signal as well which is an  additional connection whereas with the uart it   is normally half duplex spi is full duplex so  we'll send data in both directions simultaneously there are pros and cons between these different  protocols while spi can transmit data faster   that's not so important for the example i'm  using important factors would be simplicity   when programming the uart serial is easier or  the ability to communicate with multiple devices   which spi does if you only need one device then it  may be easy to stick with you out but if you want   multiple devices then spi is a good choice you may  also want to consider that spi uses four digital   ports the serial only uses two but of the serial  ports those are also used to update the code   so it's usually better not to use  them except for serial communications   another option is i squared c which uses the two  ports that are normally used for analog inputs   i won't be covering those in this video but  hopefully i had a video on that in future this diagram shows how you can connect multiple  secondary devices to a single main device we'll   only be using one secondary device in this  example but this shows three as you can see   the three lines mosi miso and s-clock are shared  but each of the secondary devices uses a separate   secondary select or chip select pin the next thing  to consider is the voltage difference between the   raspberry pi and the arduino the raspberry pi gpio  works at 3.3 volts whereas the arduino uno is at   five volts in the case of signals going from the  raspberry pi to the arduino they should be okay   the minimum high voltage into the arduino is three  volts however in the reverse direction on the mosi   port then the output were between four point two  and 5 volts which is too high for the raspberry pi   some people have successfully connected the  raspberry pi directory you know but i would advise   you to use a level shifter between the raspberry  pi and the arduino this can safely convert the   voltage between 3.3 volts and 5 volts i've used  the adafruit 4 channel bi-directional logic level   converter although it's designed for I2C it can  be used for spi up to 2 megahertz an alternative   is to run the arduino or the ATmega328p processor  at a lower voltage of 3.3 volts. The freeduino i   have used for the demonstration it has an option  to switch to 3.3 volts or you can create your own   arduino like project using the atmega328p and  i've shown examples of this in an earlier video   here i'm showing the test setup i used this has  the logic level converter to convert voltages   i've connected led outputs to some of the digital  pins to test the digital outputs i've actually   used an led bar graph and resistor array for  convenience but they could just be standard leds   i've also used the other spare digital port as an  input and i've used a variable resistor for the   analog input to measure different voltages this  setup is going to allow me to demonstrate digital   inputs and outputs as well as an analog input the  spi protocol only covers the transmission of the   data of the wire it doesn't provide a protocol  explaining about the data that is transmitted   i've therefore created my own protocol or set  of rules which define how to pass information as   the data is transmitted to bite at a time i've  used the individual bits for different parts   looking at the outgoing byte from the raspberry  pi to the arduino looking at the least significant   bit which is on the right hand side first  the first four bits will be the address of   the digital or analog port the next bit determines  if the pin being referenced is digital or analog   then whether to read the request or write to  the pin next value to write out which applies   for digital on and off i haven't included pwm in  this finally the most significant bit changes how   the rest information is used that is set to one  then the rest of the bits are handled differently   and that's used so that we can send null codes or  just to check whether the spi device is responding   to get the relevant information from  the byte then i'll be using bit masks   on this diagram i've added the bit masks used  as hexadecimal values for example to get the   address of the pin number then the mask 0x0f  is used and that gives the value of the pin   see if it's a digital or analog pin then use  the mask 0x10 and if the output is zero then   it's a digital otherwise it's analog a similar  thing is used for the other pins i'll now show   some parts of the code to get the full  code then there's a link in the description   and on the penguin tutor website on the  arduino then i've used the spi library the setup code needs the miso port as an  output which sets it as a secondary device   then it sets the appropriate bits  on the spi control register SPCR in this case i'm polling the spi  registers to see if there's been an update   the alternative is used interrupt-based messaging   there's pros and cons to each but i think  polling works better for this example this is the main code  included in the loop function   note there is a big chunk of code in the  middle which is just shown as a comment   this is where the logic for handling the protocol  is added the spi status registers are checked   this if condition will be met when the full byte  is received at that point the data is a byte in   the spi data register to send data back then that  same register needs to be updated with the reply   the arduino will receive and send data at the same  time but because the protocol works by responding   to a request from the raspberry pi then as long  as the response is included before the next byte   is received then it can treat this as if it was  being sent one at a time the raspberry pi has   the job of starting communications and handling  the values that are sent back to it many of the   examples you will find using the c programming  language but i've instead created this in python   it uses the spidev library this is the code used  to set up the communications the spi bus 0 refers   to the sbi connections available on the gpio  and the device 0 refers to chip enable port 0.   if you wanted to use an additional secondary  device then you could use device one to refer   to chip enable one it then creates an spi  instance and sets its maximum speed to one megahertz this code then sets up the communication  with the arduino it first sends the byte 0x80   which the arduino should echo back then uses the  spi xfer2 method which is the transfer method   that works with the arduino as the data is full  duplex it will receive data from the arduino at   the same time as it sends data at this stage the  arduino hasn't put anything into the data register   so that byte is discarded it then resends the same  code so that that gets a response back this way it   can check the data has been received you can then  send code to and from the arduino using the spi   xfer2 method the demonstration code is available  to download which shows how the arduino pins can   be turned on and off and data read from  the analog on digital pins on the arduino   so as you've seen spi can be used to  communicate between the raspberry pi and arduino   it has high bandwidth and supports full duplex  messaging but it does come with a trade-off in   terms of ease of use it does also use quite a few  of the digital pins that you may otherwise want to   use for other purposes it's worth considering for  communication between a raspberry pi and arduino   particularly if you wanted to add multiple devices  i hope you found this useful if so please like   and share the video if you haven't already so  please subscribe to my channel for future videos   on maker projects including the  raspberry pi and the arduino
Info
Channel: Penguin Tutor
Views: 19,548
Rating: undefined out of 5
Keywords: Raspberry pi, Arduino, spi, serial, communications, networking, primary, secondary, main, master, slave, MISO, MOSI, SS, cs, chip, select, raspberry, pi, rpi, atmega328, atmega328p, atmel, microcontroller, microprocessor, freeduino, python, cpp, c++, linux, data, transfer, asynchronous, synchronous, duplex, half-duplex, full-duplex, digital, pin, analog, digitalread, digitalwrite, analogread, computers, computing, somputer, science, stem, electronics, level-shifter, logic, level, convertor, led
Id: dzVLRjH5i78
Channel Id: undefined
Length: 13min 21sec (801 seconds)
Published: Mon Jan 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.