I2C with Arduino and Raspberry Pi - Two Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop were working with the i2c bus again I'll show you two ways to interface a 3.3 volt logic Raspberry Pi with a 5-volt logic arduino or changing levels today so welcome to the workshop [Music] hello and welcome to the workshop and we are back working with AI to C or I squared C or III C or Twi the inter integrated circuit bus now the fact that this bus has a number of different names and none of them have actually been standardized except of course for inter integrated circuit bus is just one of the many discrepancies you'll find when you're working with i2c another thing that you will find is that there is no standard connector for i2c unlike things like USB there are a variety of connectors you can use there is a small molex connector that many manufacturers have latched on to but it's by no means a standard a lot of people also simply use dupont pins and even in that case there's no standard as to what the pin note is I have a number of i2c modules and they have the same four pins but they're not necessarily in the same order another thing about the i2c bus that varies is the voltage levels that we can use now the two most common levels are five bolts and 3.3 volts now most of these discrepancies are things that we can get around quite easily obviously the name is really irrelevant we can call it Bill's bus and it'll work just as well and with the pin notes in that as long as we know what four pins are connected to what we can connect everything together and that will work fine but in terms of voltage that's a different story it's a very common application to have interface 5 volt and 3.3 volt logic and since I to see can use both of them you'll find yourself in that situation many times and one of the most common situations is interfacing in Arduino with a Raspberry Pi the Raspberry Pi uses 3.3 volt logic on is GPIO bus whereas most arduino is use 5 volts although there are 3.3 volt Arduinos as well now this is a really common application because it creates quite a powerful combination the Raspberry Pi of course is a full-fledged microcomputer capable of running Python capable of connecting to the internet natively most raspberry PI's of Bluetooth and Wi-Fi on them they're very powerful but one thing that they aren't powerful act is at i/o and this is where the Arduino zyk sell our dwee knows are basically built for i/o they have several digital i/o ports they have analog digital converters they can handle interrupts they can do timing very precisely so by putting the two of them together you can get a very powerful combination and that's what we're going to do today so today we're going to look at two different ways of interfacing a Raspberry Pi to an Arduino and handling the difference between their 3.3 volts and 5 volt logic so what we're going to do right now is take a look at the situation with 3.3 volts and 5 volt logic see what concerns we might have with it and then we'll take a look at the Raspberry Pi in the Arduino and see a couple of different ways to hook them up and run the same test program with both methods so let's get going so let's examine I to see voltage level conversion I to see can be run at different logic voltage levels the most common voltages are 3.3 volts and 5 volts the i2c voltage is determined by the voltage of the master device the SDA and SCL lines need to be pulled up with a pull-up resistor to the master voltage level now here are the rules regarding connecting 3.3 volts to 5 volts a 3.3 volt output can be safely connected to a 5 volt input and because 3.3 volts is above the minimum threshold required it will work correctly a 5 volt output however cannot be connected to a 3.3 volt input it will damage the 3.3 volt logic device the Raspberry Pi uses 3.3 volt logic the Arduino Uno uses 5-volt logic in this scenario the PI can work as a master device and this is the normal case as a master the direct connection is possible because the PI is determining the voltage you need to ensure that no extra pull-ups or 5 volt I to see devices are connected into this arrangement the Arduino cannot be directly connected to the PI as a master in order to do this you require a logic level converter here's an example of a bi-directional logic level converter on one side we have a 3.3 volt power connection and on the other side of 5 volt power connection on the 3.3 volt side we have the logic a input output and this connects through to the 5-volt logic a input output the 3.3 volt logic B input output pin is connected through to the 5-volt logic B input output pin and finally there are two ground pins a bi-directional logic level converter chained is the logic voltage so two devices of different logic levels can be operated and interfaced safely you must use a bi-directional converter for the i2c data line because the data needs to travel in both directions these devices are available in two four or eight channel designs logic level converters are a very inexpensive solution to logic level problems we've already used i2c with the arduino many times and it's a pretty simple matter there are two pins defined as SDA and SCL on the Arduino Uno they use the address pins a4 and a5 on different models of our dwee knows they may use two different pins but once we know what these pins are we can pretty well get going and connecting our i2c devices however there are a few additional considerations when using i2c with a Raspberry Pi when you look at the Raspberry Pi GPIO you might notice there are actually two I to see buses one is labeled I to see zero and one is labeled i2c one now when you first see this you might get the idea that hey I've got two independent buses I can take the same sensor take two of them put them on to the different buses and still read them even though they have the same addresses because it's two independent buses while that is actually not true the i2c zero bus on the Raspberry Pi is exclusively for reading an EE prom that is located on a Raspberry Pi hat our hat is Hardware attached on top it's the expansion cards that you can attach the Raspberry Pi similar to a shield and an Arduino now that expansion card will have a little a EEPROM on it and it identified what the card actually is and so the software can go and configure itself correctly that's the only reason the i2c 0 bus is there you cannot use it for anything else so you need to use the i2c 1 bus which of course is a 3.3 volt i2c bus another consideration is that when you first install raspbian the operating system that most of us use on the PI the i2c bus is actually not enabled and you have to go through and enable it so right now what I'm going to do is show you how we set up the Raspberry Pi and then we can get started with our experiments in order to set up your Raspberry Pi you're going to need to get a copy of the raspbian operating system and install it on a micro SD card now the current version of raspbian as of this recording is raspy and buster and there are three versions of Buster available on their ass berry pie org website I chose raspbian Buster with desktop because it's very easy to work with having a full-fledged desktop and it's probably the best choice for you as well you can also elect to choose raspbian Buster with desktop and recommended software and you'll get some additional software utilities including the Libre Office office suite but this is not necessary for our program so it's up to you which of these two you do wish to download download the zip file and then you're going to need to install that onto a microSD card now the simplest program that I found for doing that is something called echar it's a free program that you can download for any operating system and it makes installing images onto a microSD card very very simple and those of you who've watched my videos have seen me install and use at your several times before so I won't repeat myself here but get your raspbian copy install it on a micro SD and insert the microSD into your Raspberry Pi and we're ready to go when you first install raspbian and booted up you're going to be asked a number of questions in order to set the operating system up these are questions as to the location that you're in the type of keyboard your timezone etc etc you'll also be given the opportunity if you wish to set up a Wi-Fi connection which you can certainly do although it's not required for the experiments that we are going to be doing once you've done that however you're still not finished because we still need to activate the i2c connections on the Raspberry Pi because by default they do not come activated in order to do that you'll need to open a terminal which is this icon over here once you have it opened you're going to need to bring up the configuration utility and you type this following in order to do that sudo su D o space grasp IRAs P I - config conf ID and hit enter and this will bring the configuration utility up now I've already configured my but I'll show you what you need to do in order to configure yours you can use your arrow keys to navigate down into interface options and then hit the enter key and then in the interface options you'll notice a number of the accessories that you can activate for your pie now by default these are all disabled but we want to enable the eye to see in this case so we'll go down over here to i2c and again hit enter now in my case I have already got the interface enabled but in your case it'll come up as no and so you can use your keyboard and your tab key in order to navigate to yes and then press ENTER and then you can say okay and then you can navigate down here using your tab key and your arrow key in order to go finish now in previous versions of raspbian you would have to reboot after this point but it seems that with the current one that is not necessary however you can do that of course if you wish now in order to test to see that the port is enabled there's a simple way of doing that you can use an LS command which is the command that Linux uses in order to list directory contents so if you do LS space / de vie space star i2c oops start and hit enter you should get this result with I - C - 1 and that indicates that the i2c - 1 port is now available and if you've done this you have now successfully set up your raspberry pi for our eye to see experiments so now that we have our Raspberry Pi set up we're ready to begin our experiments now as I said earlier we are going to do the same experiment twice with two different hookup configurations and the first one we are going to look at is hooking the Arduino and Raspberry Pi directly together now we can do this because as you recall it is the i2c bus master that determines the voltage levels and in all our configurations Raspberry Pi is going to be the master and that is indeed the most common configuration that you'll have now the Raspberry Pi has a couple of pull-up resistors on its GPIO and they pull it up to the 3.3 volt level because the Arduino uses open collector outputs its output will also be pulled up to only 3.3 volts so that on the data line when it returns data it will be returning it at 3.3 and not 5 volts now this setup will work however you need to be cautioned if you are going to add additional I to see devices to make certain that there are no devices with pull-up resistors that will pull the lines up to 5 volts because if that's the case then you will harm or possibly destroy your Raspberry Pi what does not something you want to do so let's take a look at the hook up right now for our first experiment and then we'll look at the code for both the Arduino and the PI that will need to communicate now for the first experiment you are going to need a Raspberry Pi and you can use any model of the Raspberry Pi that you wish you'll also need an Arduino Uno optionally you can use an LED and a dropping resistor for the LED I use 220 ohms but any value from 150 to 470 would be sufficient again these two components are optional we'll begin by tying the ground of the Arduino and the ground of the Raspberry Pi together now there are several ground pins on each device I used pin 20 on the Raspberry Pi and tied it to one of our duane O's grounds the SDA line on the Raspberry Pi is pin 3 on the DPI L and this is connected to pin a 4 on the Arduino Uno the SCL line is pin 5 on the Raspberry Pi GPIO and it connects to pin a 5 on the Arduino if you're using the LED connect the anode of the LED through the dropping resistor to pin 13 on the Arduino and then ground the cap fold of the led and this completes the hookup now here's the code that we're going to use on the Arduino side now this is very simple code because all we are doing is accepting signals on the i2c bus from the Raspberry Pi and using them to toggle the built-in LED on pin 13 on and off so we'll start our code out by using the wire library the wire library of course is the i2c library for the Arduino and this is built into your Arduino IDE so you will not need to add an additional library we'll also define a constant for the LED that we have on pin 13 and we call that LED pin now in the setup we are going to join the i2c bus as a slave with address eight so when you do a wire begin and pass an address to it it tells the wire library that we're going to be operating the Arduino as a slave and not as a master we'll also use the on receive to call a function that we call receive event which will take a look at in a moment so every time we receive data we will call the receive event function and then we're going to set the LED pin up as an output and we're going to set it low so that the LED is off now here's the receive event function itself now we are going to receive the event and it has a parameter called how many events we are going to get and while the wire is available we are going to read the wire and then we're simply going to write the value to the LED pin so since we're going to be using values of zero or one that will toggle the LED on and off now in our loop there isn't very much all were doing is running a delay and basically we are really just waiting for data on the i2c line so that we can run the receive event function and so now that you've seen the code let's go and take a look at the raspberry pi code now here's the program we are going to be using on the Raspberry Pi side for our experiments and we're writing this in Python now you can use a number of different editor's for Python I'm using the genie editor which is included in the raspbian distribution and genie just happens to be one of my favorite text editors anyway but you can use a different one if you wish now we're going to start off by importing SM bus from the SM bus library and this is the library we need to work with i2c we're going to set a variable called addr and we're going to set it to the bus address the address of our slave which in our case is 8 remember the 0x indicates its hexadecimal although with the number 8 that really doesn't make a difference because a decimal 8 and a hexadecimal 8 are actually the same thing and then we're going to set up a variable called bus and it's an object using the SM bus number 1 and that indicates that we're going to be using the i2c 1 connections on the Raspberry Pi now remember that the Raspberry Pi actually has an i2c 0 but that's exclusively reserved for using hats I also have a variable that I call num and I set it to 1 it's going to be used in the while loop right below this then I print to the screen some instructions so it's enter 1 for on or 0 for off and that's how our program is going to work when we type in the number 1 it is going to send a 1 down the line and turn our LED on if I type in a 0 it will turn it off and so while num equals 1 which it does in this case I'm going to get an input and assign it to the LED state variable now the input command also has a parameter in which I can have some text and I just chose to use a number of greater than signs as a prompt now if the LED state equals 1 and that's this value over here now notice that the 1 is captured within quotation marks that means the key 1 as opposed to the value 1 there's quite a difference so if we press the key that is labeled 1 then we are going to write to the i2c bus that value and here's the value we're going to write and that will switch the LED on if we enter a 0 and we're going to send a zero down the bus and switch the LED off if we type anything else and we're going to change the number able to a different value other than one and now this while loop will evaluate as false and that will end our program so we can continue to toggle are led by typing ones and zeros and when we're done with that we just type something else and that will end the program and so there we have it the simple Python program that we are going to be using in order to test our eye to see connection now here's our simple demonstration the hookup between the PI and the Arduino of course is quite simple I've tied the SDA and SCL lines to each other make sure you don't reverse these and I've also tied two of the grounds together now there are several ground connections on the Raspberry Pi GPIO pins as well as several in the Arduino and you can use any two you want I've also got my LED and dropping resistor tied to pin 13 on the Arduino of course this is optional you could just elect to use the built-in LED in the Arduino and that would work fine as well so now let's take a look at the Raspberry Pi desktop now I'm going to open a terminal window now I have placed by Python program into the Documents folder and so I'm going to need to change into that first and if I do a listing I will see my Python program there's another one in there that I was using earlier that will just ignore and you just need to type Python 3 and then the name of the program you're using so and this will run our program which is displaying enter 1 for on or 0 for off so I'll enter a 1 and as you can see our LED is indeed illuminated and if I enter 0 the LEDs turned off and I can continue to do this as much as I wish if I enter any other value other than 1 or 0 the program will and so it's a very simple demonstration but remember we are controlling this LED which is on the Arduino using I to see from the Raspberry Pi so as we see hooking an Arduino and a Raspberry Pi directly together will work for communications on the i2c bus providing that you pay attention to pull-up resistors and have nothing that is going to pull it up to 5 volts however it's not the ideal situation for one thing I to see can only really Drive a cable of about a meter so after all it was originally designed as to be used within the confines of a printed circuit board but with the setup that we have right now I think you're going to find you won't even be able to get that meter it's kind of operating on the threshold another issue comes if you're building a product and create an external I to see connection that creates the possibility that someone might connect the device that is 5 volts and has a pull-up resistor and that of course will go and destroy your Raspberry Pi which is not a good thing there is a solution to this and it's called bi-directional data converter a bi-directional data converter simply converts data from one voltage level to another level and because it's bi-directional the data can flow in either direction so we will use a converter that converts 3.3 volt logic into 5-volt logic and can do it in both directions that can also convert the 5 volt 3.3 now bi-directional data converters are very inexpensive devices you can make your own using a MOSFET but since you can get a little one for less than 50 cents it's probably the route you're going to go so let's take a look at a bi-directional data converter first and then we'll see how we wire it into a circuit between the Arduino and Raspberry Pi and we'll repeat the same experiment and hopefully get the same results let's take a look at a couple of these logic converters now these are very simple devices and they're usually sold without the pins on them I've got another one that I've soldered a few pins on too as well and as you can see they're very tiny and they basically have two sets of connections on them they have the connections on one side for the 5-volt stuff and the other side for the 3.3 volt stuff and that's really about all there is to these things now they're very very inexpensive you should be able to pick up a handful of these things for a couple of dollars and for 3.3 volt to 5-volt logic applications these things are great now these are dual channel bi-directional converters you can also get four channel and eight channel ones if you wish just make certain when you're getting a converter you are getting a bi-directional converter a converter that can handle data going in either direction there are unidirectional ones that have a specific input and an output and that won't work on the i2c data line because the i2c data line of course can be sending or receiving data so definitely get the bi-directional sort but otherwise there's really not too much more to the logic converters now for this hookup we're going to start with the same components we used before I'm not showing the LED and dropping resistor but you can leave these connected to the Arduino if you hook them up in the first experiment we're also going to add a two channel bi-directional logic level converter now first we'll hook our Raspberry Pi up to the logic level converter so connect the ground on the Raspberry Pi which I'm using on pin 20 of the GPIO to the ground pin on the 3.3 volt side of the logic level converter connect the SDA line which is pin 3 on the GPIO to the 3a input onto your converter the SCL line which is pin 5 on the GPIO will be connected to the 3b line on the on the converter and the voltage the 3.3 volts which you can get from pin 1 of the GPIO will be connected to the v3 or 3.3 volt VCC on your converter now on the arduino side will make the equivalent connections so we will connect the ground on the 5 volt side to the arduino z' ground the SD a which is pin a4 on the Arduino is connected to the 5a line on the logic level converter the SCL which is pin a5 on the arduino is connected to the 5b line on the converter and finally the converters 5 volt input comes directly from the Arduino 5 volt output and again you can still leave the LED and dropping resistor intact and so now that you've got that hooked up let's go and give it a test and once again we're back with our demonstration this time using the logic level converted device which you can see over here in my solderless breadboard and of course I've got the 5 volt side of this powered by the Arduino Uno and the 3.3 volt side powered by the Raspberry Pi and my LED is still in the same place it was for the last demonstration so I'm running our Python program our very simple demonstration program I'm gonna hit have 1 and press enter and as you can see indeed it does turn the LED on giving it a 0 will turn it off and of course I can continue to toggle the LED as much as I wish and again a very simple demonstration of the i2c capabilities between the Raspberry Pi and the Arduino only this time using a logic level converter so this brings us to the conclusion of today's video and i2c but it is not the last time we're going to look at i2c I'm going to do one more i2c video and you'll see it in a couple of weeks what we're going to cover next time we talk about i2c is some more advanced topics including using multiple I to see masters on the same bus and extending the i2c bus so that you can use devices that are separated by more than a meter so keep your eyes out for that video now the best way to find out about that video and about any new videos that I do here in the drĂ´me bot workshop is to subscribe to the YouTube channel so if you haven't done that please click the little thing just below the video to subscribe you can also click on that little in the corner and he'll do the job of subscribing for you now you will find an article accompanying this video on the drone bot workshop website there's a link to that article below you'll also find a lot of other great tutorials and projects on the website and to keep up to date with that the best way of doing that is to subscribe to the drone bot workshop newsletter it's not a sales letter just an occasional letter I send you out to let you know what's going on in the workshop and you can subscribe to the newsletter when you're on the website and finally if you want to discuss anything that we've talked about today with myself or with other enthusiasts just like you the best way of doing that is to go to the drone bots workshop forum and you will find a link to that below so there are lots of ways that we can dialogue about AI to see and other subjects so until the next time we meet please take good care of yourselves and I hope to see you again very very soon here in the drone bot workshop good bye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 112,914
Rating: 4.9456825 out of 5
Keywords: I2C, Arduino, Raspberry Pi, Logic Level
Id: me7mhrRbspk
Channel Id: undefined
Length: 30min 13sec (1813 seconds)
Published: Mon Sep 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.