I2C Part 1 - Using 2 Arduinos

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're going to take a look at the i2c bus we'll start by learning the basics of i2c communications and then we'll move on to exchange data between two arduino z' will also control in arduino slave with an arduino master i've got a lot to communicate today so welcome to the workshop [Music] good day and welcome to the workshop today we're going to be working with the i2c bus now we've worked with the i2c bus a number of times we've used i2c sensors we've used i2c displays and we've had them hooked up to our arduino and you can use i2c with microcontrollers like the arduino with micro computers like the Raspberry Pi it's a very important technology was a technology that was originally developed for the television industry for the television receiver industry to allow integrated circuits to talk to each other and that's why it's called the inter integrated circuit bus but it can be used for microcontrollers microcomputers sensors a number of different things so it's a very important subject I'll be using a lot of i2c devices for building the robot that I'm building and my separate series build a real robot so if you're following that you're going to want to pay particular attention to how to use i2c now i2c a such an important topic that I cannot cover it all in one video so I'm going to be doing a number of different videos on AI to see today what we are going to be doing is showing you how to connect to arduino z' together with i2c how to send data between them and how to control one arduino using the other one again via i2c now before we get started I wanted to remind you about some of the i2c devices that we've already used so I've got a few of them down over here on the workbench right now these are a number of i2c devices we've used in other projects now this very tiny one over here is a UV sensor an ultraviolet ray sensor and we use that to build a UV meter it works with i2c these two displays are both Oh LEDs and they also work with i2c you can see there's only four connectors on these devices and that's typical of i2c devices of power and ground a clock and a data signal now this may look like a standard temperature and humidity sensor it's actually also an i2c device in the 4-pin the bottom again are the i2c connections we used our real time clock quite recently this is an i2c device now this display is not an i2c device however there is an adapter for it in order to make it into an i2c device so we can use the LCD display with only a few wires connected to it so I to see is very versatile so what we're going to start off by doing is learning a bit about I 2 C and then we're going to move on to our experiments now in the future videos which you're going to see staggered over the next couple of weeks I'm going to talk about building your own eye to see sensor and taking a regular sensor and i2c off eyeing it if that's actually a word and I don't think it is in other words connecting it so that you can use it via i2c another subject we will be covering and again in a future video we'll be interfacing a Raspberry Pi and Arduino using I to see that's a particular challenge because the Arduino uses 5-volt logic at least most Arduinos do and the Raspberry Pi uses 3.3 volt logic and there you have to take that into consideration when you're tying them both together with i2c there's a number of ways of doing it and finally I will be doing a video with some advanced I to see techniques including using multiple masters on the same i2c bus so there's a lot to cover I didn't want to do it all in one video because it would just be way too much and way too long so today we're just going to focus on learning about i2c and getting our doing those to talk to each other using it so let's start learning about i2c right now so let's take a look at I to see bus communications I to see is a serial protocol for a low-speed two-wire interface it was originally developed by Philips in 1982 for use in the television manufacturing industry it was a method for different integrated circuits in a television to communicate with each other I to see is now used by every major semiconductor manufacture the i2c bus is suitable for use only on short distances approximately one meter at a speed of a hundred kilowatt the original eye to see bus had a maximum clock speed of 100 kilohertz there is an i2c fast mode which allows a clock speed of up to 400 kilohertz there is also a high speed mode which can run the clock up to 3.4 megahertz and an ultra fast mode which will run up to 5 megahertz now the i2c bus actually goes under several different names the full name is the inter integrated circuit bus it's also called the I squared C bus written as I squared C and is also the IIC bus the Twi bus is the twin wire interface and it is a subset of the i2c bus it is virtually the same but it does not allow clocks direction another subset of the i2c bus is the SM bus which was developed by Intel in 1995 now here's how devices are connected on the i2c bus there is a VDD line which is the power supply line this is genuinely either 3.3 or 5 volts DC the SDA line is the bi-directional data line the SDC line is the clock signal a couple of resistors pull the SDA an SDC line up to the VDD level these are two pull-up resistors the first device on the i2c bus is the master device along with the master multiple slaves can be added to the bus it is also possible to run the i2c bus with multiple masters only one device can be the master at any given time the master device on the i2c bus generates the clock signal and it has no eye to see address the master always initiates the communications the slave devices use a unique seven bit address this makes it possible to have up to 128 slave devices on an i2c bus many of the slave addresses are reserved by NXP which is the new name for Philips semiconductor some I to see devices can be configured for multiple I to see addresses this allows the use of several devices of the same type on one i2c bus there is a new 10 bit addressing scheme that will allow up to 1024 devices to be used on the i2c bus so now let's start working with the i2c bus now that we know a little bit more about I to see let's begin our experiments now a couple of notes about hooking up these experiments I'm going to show the experiments using two arduino uno x' you can use other arduino x' if you wish the pin outs however will be a bit different on the arduino uno the SDA line is the same as analog input a4 however if you're using an Arduino mega it is pin 20 the SCL line or the clock line on the uno is analog a5 and it's pin 21 on the Arduino mega so if you'd like to use a mega instead of an O you can go ahead and you can also mix and match Omega and and you know if you wish to they don't have to both be the same now one other note is that you'll notice when we talked about i2c I showed you that the bus needs to pull-up resistors if you're using the Arduino Uno technically you do need to use these resistors although I did not use them however if you experience some problems you'll probably want to put them in resistors of about 10k pulled up to the 5 volt line will do the trick on the Arduino mega there is a built-in pull-up resistor on those 2 pins so you won't need to use it if you're using Omega so with that said let's take a look at the hookup diagram and start her first experiment for our experiment we're going to use two arduino x' i'm using arduino uno x' one of the arduino x' is going to be the master and the other one will be the slave the connection between the two hardly knows is extremely simple first we tie the ground of one arduino to the ground of the other one you can use any of the ground connections on either device next we connect the SDA or data lines between the two arduino x' some arduino s have a separate SDA in SCL connection but if yours does not just use analog pin a4 and tie both of them together and in a similar fashion we need to tie the clock or s CL lines between the two arduino x' together again if your Arduino has a dedicated connection you can use that otherwise use analog pin a5 and this completes the connection now let's take a look at a sketch that will make use of the two Ardley nose with i2c here's the first sketch that we're going to be using for our eye to see demonstration now this is the sketch that will run on the master unit now we start off by including the wire library the wire library is the built in Arduino library for using the i2c bus then we'll define the address of the slave that we want to communicate with in this case the address is 9 we'll also define the size of the answer that we expect to receive from the slave and in our case we're expecting to receive 5 characters in this setup we initialize the i2c communications with a wire dot begin we'll also set up a serial monitor so we can look at what is happening we'll set it up at 9600 baud and write I to see master demonstration to it then we'll go into the loop after a short delay we'll again print the serial monitor then we'll write a character out to the slave now in this case we use the begin transmission with transmits out to the slave that we want to talk to then after that we will write the data we want to send now we're only sending the character zero or actually the number zero in this particular case and then after that we'll end the transmission and then we're going to look for the data back from the slave so we print receive data on the serial monitor and we read back five characters from the slave what we do is do we do a request from the slave address and we're going to request the answer size which in our case is five bytes and then we go through that and add those characters to a string called response and then we simply print response up to the serial monitor so that is the sketch for the master now let's take a look at the sketch that we need to use on the slave unit now here's the sketch we're going to be using on the slave unit we start off once again by including the wire library then we have the address of the slave which is address number nine in this particular case and as we did before we'll define the size of the answer that we are going to send back to the master we also will create a string that has the answer in it so here's our five character string then in the set up we initialize the i2c communications now this is a little bit different than what we did before in the master unit we just simply said wire begin and didn't give it a parameter but in the slave unit we do wire begin and give it the slave address when the wire library sees this it knows we want to run in slave mode and not in master mode there are also two events that we need to define function for we need to define a function for when data is requested from the master and it's going to run a function called request event that will take a look at at a moment and also a function when data is received from the master and this is a receive event so we're going to receive data from the master which is the character 0 in our case and the master is going to request some data which is the five characters we're going to send back to it then once again we'll set up the serial monitor now here's the two functions we just spoke about when we receive data from the master we want to read that data and what we're going to do over here is just assign it to a byte so while dwyer data is available we will do a read and assign it to a byte which we are calling X and then we'll print the serial monitor that we've had a receive event now for the request event when the master requests data we have to send a response out so we set up our response as an array with the same size as our answer so we do that over here it's a byte array and then we format that data as an array by doing this over here and then once we format that as an array we will respond to the master using this line over here a wire right and then we will print to the serial monitor that we have had a request event now if the loop in this sketch there isn't much happening just a short time delay so now that you've seen both of the sketches let's run them together and watch the data being exchanged between the master and the slave so here's my demonstration of the master and slave Arduino now that's not really much to look at I've just got the three connections the ground and the two i2c connections and this is my master unit and this is my slave unit over here the real activity you can see up on the two different serial monitors and on the master unit you can see that I'm writing over to the slave and receiving the data back hello it's coming very quickly but I think if you look you can see that and on the slave side you can see that I'm alternating between receive events and request events receiving data from the master and I'm getting a request to send data from the master and so that's basically all there is to it but we are demonstrating that we can send and receive data between two arduino 'he's using the i2c protocol so now we've seen how we can hook to arduino together and use the i2c bus to exchange data between them and I think from the sketches you've also seen that the wire library itself is really not too difficult to use and so we're going to put it to use again for our next and final experiment today what we're going to do is we're going to control the slave unit from the master unit I'm going to hook a potentiometer on to the master unit and an LED on to the slave unit and use the pot to control the blink rate of the LED on the slave unit and of course we'll send the potentiometer values over the i2c bus to the slave now in some respects this sketch is even easier than the first one we looked at because in this situation I'm not asking the slave to send any data back to acknowledge the receipt of the data and send something back I'm just simply asking it to get the data and to control the LED so let's take a look at the very simple hookup for this experiment and then we'll look at the sketch and run the experiment for our next experiment we're going to start with the same arrangement we already have with to Arduino x' when acting as master in the other is slave we're going to add a few more components first a potentiometer now this can be any value from 5k upwards it could be a linear taper pot but most of them are he'll also add an LED and a dropping resistor for the LED although I'm showing a 220 ohm resistor any value from 150 to 470 ohms would work fine we'll begin by connecting one side of the potentiometer to the 5 volt line on the Arduino we'll connect the other side of the pot to the ground connection the wiper of the pot will be connected to analog input a 0 on our master Arduino on the slave Arduino will connect pin 13 to one side of our dropping resistor the other side of the dropping resistor will be connected to the anode of the LED and finally we will ground the cathode of our LED and this completes our wiring let's take a look at the sketch that we'll be using for the master unit the unit that has the potentiometer on it now we'll start off of course by including the wire library and then we'll define the address of the slave we want to communicate with and once again we're using nine as an address we also need to define a couple of variables through the potentiometer itself the pin that it is connected to analog pin zero and the value of the potentiometer then we'll go into the set up which as you can see is very simple we simply initialize the i2c communications with a wire begin now remember since we didn't provide any parameters after begin the wire library knows that we want to be the master then we go into the loop we have a short delay to begin with and then we read the pot value and map it to a range of 1 to 255 because we're sending a single integer down to the slave unit and it can only represent the value of 0 to 255 we won't use 0 because 0 will turn off the LED indefinitely now we use a map command to take the range of the analog input which is 0 to 1023 because of the Arduinos 10 bit A to D converter and we'll map it to a range of 1 to 255 but notice that we do it backwards and the reason for that is we want the LED to be flashing its lowest when we have the pot turn to the far left side and at its quickest rate when the pot goes to their right and so we reverse it using the map command this is actually a very useful method that you can use with the map command we assign the final value to the Val variable and then we write it out to the slave and as you'll notice it's very simple to do well begin transmission to the slave the slave address and we write the value out to it and end the transmission and that's all there is to it then we go through the loop again and do it over and over reading the pot value and sending that value out to the slave as a value from 1 to 255 so now that we've looked at this sketch let's go and take a look at the sketch that we'll use on the slave side so here's the sketch that we're going to use on the slave side the flash the LED now we'll start off once again by using the wire library the Arduino library for working with i2c and we'll define our slave address as being nine we also define the pin we're using for the LED now I'm using pin number 13 since the built-in LED on the Arduino Uno is also on pin 13 you could eliminate the external LED if you really wanted to then we have a variable that's going to hold the receive data this is going to be the 1 to 255 that's being sent from the master and we're going to call that variable Rd and a variable for the rate that we want the blink the LED at I'm calling that B R now in this setup will set up our LED as an output and we'll initialize I to C communications as a slave so we do a wire begin and pass the slave address remember because we've passed an address to it the wire library knows that we want to run as a slave and not as a master then we're going to define a function that we run every time we receive data from the master now with a master is not going to request data in this case so we only need to define an on receive function and we're calling that once again a receive event and look at that function in a moment then we'll set up our serial monitor and write i2c slave demonstration to the monitor now here's the receive event function the function that's going to run every time we receive data from the master we're going to do a wire read which will be the value from 1 to 255 and assign that to the variable Rd and then we're going to print that to the serial monitor so we can monitor the incoming data now in the loop we start off with a small delay and then we calculate the blink rate and once again we're going to use the map function the blink rate is going to be from 100 to 2000 and this is the delay in milliseconds between the flashes of the LED you can change these values if you'd like to and we know that we're going to be receiving data from values from 1 to 2 fifty-five and then after that we go and flash the LED and this is basically the blink sketch will send the LED on delay for the blink rate and then we'll send the LED off and once again delay for the blink rate and then we repeat the loop and do it again if the value of B RS of our D excuse me should change then B R will change accordingly so now that you have seen the sketch let's take a look at all of it in action now here is our master and slave control demo running I've got the potentiometer connected to this Arduino which is my master and the LED connected to this one which is a slave and as you can see the LED is blinking at a rather slower rate and if you look at the serial monitor you will see that I'm sending a value of 255 which means that the pot is at its far left rotation point so I'm going to bring the pot up and watch the serial monitor over here and you can see now remember the serial monitor is on the receive side so this is what we're receiving from the master and as I bring it up we should see the LED flashing a bit quicker there you go it's left and quicker I'll bring it all the way to the top and it's definitely flashing a lot more rapidly than it was at the other end and so it's a simple demonstration but it shows how I can control an LED on one Arduino with a potentiometer and another one using the i2c bus okay well that wraps it up for today's video on i2c I hope you've enjoyed it and that it's opened your eyes to some of the things that you can do with the i2c bus now the next time we talk about i2c we're going to go further into the data structure on the i2c bus and we'll also look at taking a regular sensor and attaching it to a small Arduino and creating our own eye to see sensor out of it so you're going to want to see that now the best way to find out about new videos is to become a subscriber to the YouTube channel so if you have not subscribed already please do so just click on the subscribe button below the video or you can even click on the little robot in the car of the video and he'll subscribe you as well now if you need more information about i2c or if you want to get the sketches I use today just check out the article in the drone bot workshop comm website the one that accompanies this video you'll find a link to that article right below this video you'll also find a link to subscribe to the newsletter and if you haven't done that please do so as well it's not a sales letter it's my way of keeping in touch with you and I will be very soon announcing a new method for you to suggest topics for future videos and articles I get a lot of emails about that I get a lot of comments and the YouTube videos but I'm coming up with another method so that you can suggest new topics or vote on topics that I am considering for new videos and that way I can create content that is especially done for you so until the next time please take care of yourselves and I hope to see you very soon again here in the workshop good bye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 248,948
Rating: undefined out of 5
Keywords: i2c protocol, i2c arduino, i2c communication between two arduino, i2c communication
Id: PnG4fO5_vU4
Channel Id: undefined
Length: 25min 51sec (1551 seconds)
Published: Sat Mar 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.