Arduino Communication with I2C Protocol | Ashraf Madhoun | Skillshare

Arduino Communication with I2C Protocol

Ashraf Madhoun, An Engineer with +10 Year Experience

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
7 Lessons (51m)
    • 1. 1 Introduction

    • 2. 2 Where to use i2c communication

    • 3. 3 I2C Pins in Arduino

    • 4. 4 Arduino Wire Library I2C Functions

    • 5. 5 Arduino to Arduino I2C Communication Circuit Design

    • 6. 6 Arduino to Arduino I2C Communication coding

    • 7. 7 Arduino to DS1307 Time and Date Module Circuit Design


About This Class

I²C (pronounced I-squared-C)

Arduino  Communication with I2C Bus: A step by step guide to Master I2C Protocol and Start using it in your projects

I²C Allows communication of data between I2C devices over two wires. which makes it easier for anyone to exchange information without too much wiring.

What You Will Learn in This Course:

  • What is I²C and how it works

  • How to implement I²C in your project

  • What is the difference between I²C and other communication methods

  • Why using I²C will make your life easier

  • The advantages and disadvantages of using I²C

  • How to code I²C the right way and how to talk to multiple devices easily

  • A practical example of I²C enabled DS1307 Data and Time IC and how to read data from it using I²C

What you’ll learn

  • What is I²C and how it works
  • Advantages of I²C
  • Disadvantages of I²C
  • I²C Operation
  • Main I²C Bus Library Functions
  • Interface different devices with Arduino using I²C
  • How to implement I²C in your project
  • What is the difference between I²C and other communication methods
  • Why using I²C will make your life easier
  • How to code I²C the right way and how to talk to multiple devices easily
  • A practical example of I²C enabled DS1307 Data and Time IC and how to read data from it using I²C

Are there any course requirements or prerequisites?

  • A computer to run the software
  • A basic knowledge in C Programming language
  • No Material is required for this Course

Who this course is for:

  • Anyone interested in talking to multiple devices easily via I²C the right way
  • Anyone Interested in Interfacing Arduino with other ICs or devices via I²C
  • Anyone Interested in Controlling different devices using I²C Protocol
  • Arduino Lovers
  • Microcontrollers Geeks


1. 1 Introduction: Hello and we come. That's a new course in which you are going to learn I square see or I two c communication protocol in Arduino on how to use it effectively to communicate between two Arduino boards or between Arduino on other modules that support I square. See, this is educational engineering team Now what you will learn in this course Since the score stocks about our square See, we will learn about this great serial communication protocol which is called I square. See Onda, we are going toe talk about how this time for communication protocol works, how many wires it needs. Andi, what's the difference between this communication protocol? On other communication protocol, I square see communication is generally used to communicate with gyroscope accelerometer barometer Pressure sensor Alexandre Blais seven Sigmund display on others. So you will also learn how to communicate it with, uh, some eye sees or modules on. We will take a practical example. You learn where to use ice square. See communication, Andi, when it's effective to use it and when you shouldn't use it. So we are going to cover all of these areas. Then we'll talk about I square c programming in Arduino. This is a very simple step. Andi are very easy to do. Step. We're going to show you exactly where to connect. I square. See enabled modules on how many wears it requires for Arduino. Communicate using ice core, see with other artery knows or other modules. So and after connecting the right wires, we're going to show you how to program your art. We know so that it can be read undersea. Send and receive data via the ice core seemed You'll Onda. We will use a quick example off how to connect to Arduino boards on to send that to and from these bores. Then we'll tow take a more advanced example, which is how to read time Date on other times data from the very famous time I see which is called the S 1307 it's on. I see that people use when they want to make lock. Using a micro controller on, we are going toe interface it using I square. See, I'm sure that you will enjoy this course content. After this course, you will be ableto use I square, see communication in any application and you'll learn how to use it effectively. How to connect any thing toe are Do you know Andi Read that from that thing or that? Would you? Using ice core. See Andi, you'll learn where to use ice Percy. And we're not to use ice Coursey because you can't use everywhere you have to meet certain specifications, but for the most, you'll use it in any serial communication project that you have because only requires two wears, so it will make your life away much easier. Thanks for watching this quick introduction. If you have any question, you can ask in the Q and A ball or send us a message. I'll be more than happy to address your concern before joining the course. That's it. This is Ashraf from educational engineering team. 2. 2 Where to use i2c communication: where to use I square. See communication. Our square. See communication is used only for short distance communication. It is certainly repairable toe on extent, since it has a synchronized look. Paul's toe make it smart, but you have to make sure that you don't use it for long distance communication. This political is mainly used to communicate with sensors or other devices or microcontrollers, which has to send information Tomas start device. In this case, it's Arduino Oh no or on our green omega, or are doing land depending on the board that you are using. It's very handy when a microcontroller has to communicate with money. Other slave modules using a moment a minimum off only to wear so in other communication protocols. When you have multiple devices that you want to communicate with to send and receive that at all and from let's say, multiple sensors, multiple Meyer controllers, you have to connect alert off for hours on. When I say a lot of fires, I mean a lot. There are us to 32 module takes up to nine wires, and any other manual won't take less than four wires. But in our case using, I square. See using two wires only you can connect upto 100 I think 28 device. So if you are looking for a long grains communication, you should use R S to 32 on. If you are looking for more reliable communication, you should try the SP I protocol. But for short distance communication, you should go with Ice Square. See, it has less wearing easy after program, a very fast communication protocol, so it will make your life away much easier. I recommend it all the time when trying to communicate with other artery knows or other microcontrollers or other sensors. It's very handy, Andi. It has a lot of capabilities that you can easily manage using a symbol line off court. That's it for where to use I square. See communication. I hope that this covers everything. There is toe. I hope that this did cover everything there is to say about that. Where to use it? If you have any questions or if you don't know without to use new project or not, you can ask in the Q and a ball. Thanks for watching. This is a shot from educational engineering team 3. 3 I2C Pins in Arduino: now, when dealing with any type of microcontrollers or any type of device that has I square see , you need first to identify the pins that are I square see enabled or that are there for you to use in your eye square. See communication An Arduino board. There's two pens called a four on a five you will find as the A and A C L. As the label of these two pins. This means that these two pins can be used in the serial communication using ice core. See, would you? We already mentioned that you only need two wires, so one of these wells is called serial data. On the other is called Serial Clock. The data is the line that's responsible for sending and receiving the data itself, while the clock is responsible for, let's say, synchronizing the communication sort with generous policies. Both of these lines are important because they are the men lines that I square see communication used to send and receive that no, as you can see in this image below, these are the two opens and are you know all about that support I square see again you can find as the A and A. C L ah, which is the nickname for the two lines that are used in I Square. See communication. They are very easy to find. You can simply right are green owner or Abdul Omega. I square sea pens on Google on. You'll find the right plans, but for the owner, they are therefore on a five. So what? If you want to connect toe module, let's say a sensor temperature sense or time and date since or another sensor, you will find that this module has bins to off the spins. If it is, I square See enabled will have as the A and a C. L A. Written after the pin name, so you'll find one on. After that, you will find as the A or a C L. So we will connect these two pens and Arduino a four on a five toe. The other pains in the sensor module, and you will talk about that in the circuit design section. That's it for this lesson. Thanks for watching. This is Usher from educational Engineering team 4. 4 Arduino Wire Library I2C Functions: before we get started in tow. Programming I square see using to agree no or connecting an artery. Noto, a module We need to learn about the yr library Used an Arduino i ve software the library. Why are the text or the etch is included in the program for using the following functions For I square see communication. The first function Toby used is called yr dot Begin and from its name, this library is used for making communication with ice square. See devices on it. Initiate the wire library on Joined the I Square C bus as a master or a slave, the address would shrug exists here. Between these two prophesies, it's ah, seven bit slave address and is optional. If the address is not specified, it joins the bus as, um a start like this. Why aren't begin with the two burning theses without anything written inside them? This means that this device is the master device. Now the second function is called yr. Don't read. This function is used to read a bite that was received from master or slave device either that was transmitted from a slave device toe a master device after a cool or was transmitted from a master to slave so it can go both ways. It can be used by master or a sleeve, and as you can see from its name, you can really know what talks about or what it does. So when we say I read, this means that it will read a bite When we say began, It means that it will initiate the Ice Square. See protocol now. The third function, it's called Why are Detroit? This function is used to write data toe a slave or my star device. When it does that from slave to Master Slave writes that almost are when the right or the request fall is used on, we'll talk about that later while when it comes toe master to slave communication for transmission from a master to slave device that wired to try it is used in between calls off. Begin transmission on in transmission lines. We will talk about these two functions as well in the next slide. Now what you need to know about why are that right is that it seals to write that after a slave or master device, mainly now, when you want to write something, Use us. Try this function. Where to try on do you write your value here, So if you wrote a single value, it will be sent as a single bite. You can write a string to send a series of ICTs all you can like that and length which will send an array of data as bites. Now we will talk about this in the coding section, but for now, what you need to know is that this function is used to write data. The fourth function is called yr dot begin transmission on Dhere. You have to add the address. This function is used to begin a transmission to the eye square. See device with the given slave address. You will have to write the address here and from its name. It will begin the transmission directly. Usually when you use this function, begin transmission. You will use the end transmission function that we will talk about in the next slide on the right function will be in the middle, so you will begin the transmission to assert an address. Then you will send the debtor that you want to send using the right function. Then you'll in the transmission using the end function. It's like a circle now to make it more clear that in transmission function is used toe end a transmission toe. A slave device that was begun by begin transmission on transmits the bite that were cued by YR Detroit. So these three functional work together fares. You will begin transmission and set the other. So that's lived vice thin. You will write the data using while that right. Then he will end the transmission using this function So they work together on again. We are going toe show you this in action in the practical section Now, why are the on request This function gets called when a master requests that using wire that request for from the slave device. As you can see, this function is called all request because it requires the master to request a certain type of data from any off the slave devices connected to it. Andi, why are dot will receive is a function that gets called when a slave device receives a data from a master device on Dhere. We can include why are to treat function to read the letter sent from the Master so receive will indicate that this device will receive data on in orderto actually the value that was sent We need to use Why are the freed? No, we also have Why are the request for from And as you can see, it will take an address on a quantity. This function is used in the master request bites from a slave device. The function why are the treed is used to read the data sent from the slave device. And as you can see, the address is a seven bit address off the device to request bites from the quantity is the number of boys that we want to request or that we want toe receive from the slave device. So all of these works on the same principle which is sending on receiving that a toe and from the device on managing this process with easiest possible way. Now these are the eight main functions in the ice core. See communication using Arduino, they are included in the wild. Oh, takes library. Onda, we can go on check this library. I will show you this library in action. As you can see, this is the Y Our library and our agree No, you can find its reference. As you can see, these are the supported boards Ono Ethernet, Mega Leonardo on Do On the other. There's others that are not supported. This library allows you to communicate with ice square C T W I devices on the Arduino boards with the other three lay out and these are the different time flee out that are available You will find that there is us the ape in that we talked about earlier And there is a c l like Look line on These two friends are included in the head Our pens Ah, OK, These are some notes about this And as you can see, these are the main functions Begin request from begins transmission and transmission, Right, Available, read, sit cloak on request and receive and or request again. This is the available function. It retains the number of fights available for retriever with reads. So there is a lot functions that you can take a look at. There is examples here for Tuesday potential meter master leader, slave writer, master writer, slave reader on other Sierra communication examples and you can also see of our communication protocols again. This is like documentation for the library. You can, ah, go ahead on and see these functions or simply check these examples. But that's it for the main functions that we need you to know for. Start according our are doing. If you have any question regarding any of these functions, please ask that you aren't able. We're going to explain them in more details in the practical coding section, Thanks for watching. This is actually from educational engineering team. 5. 5 Arduino to Arduino I2C Communication Circuit Design: Now let's start by showing you the circuit design or second diagram for connecting to pick Arduino together using the I Square See communication protocol before going on to the practical. You can see from here that we're connected. We have connected a four to the A four pen on day five to the A five men. We already mentioned that these two pins or thes to head our pens are the pens required for I square. See communication. They are named as the A and A C L Andi. Since you are connecting two devices you have toe make the ground connection between them so that there will be no noise on. They will use the same ground level. Now if you want to make this connection, you can simply go and open up Freising software. Now go now. Go to the bridge board area here. Let's zoom out. Go and look for our greenoe board. Now What you need to do is simply drug are doing owner. So let's stick. Okay, this is the organ on board and we need another one. Connect air fall to a four on a five to a five. You can't change the well color by going here. Then connect the two grounds together on list. Give it a black color since it's a ground. Okay, that's it. Now these two are you know, balls can use I square see communication to send on received that toe and from each other. And we'll talk about this in more details in the according section. That's it for the circuit diagram for our first example in the scores. Thanks for watching this quick lesson. If you have any questions, please ask. Thank you on a board. 6. 6 Arduino to Arduino I2C Communication coding: Hello And we come to this annual Asan in which I'm going to teach you how to program. Are we Noto Arduino communication using I square See political. So in order to Bagram, these two are green knows we need fares to identify, which is the Master Arduino and which is the slave Fears of all winning toe include the wire library that we talked about earlier for using I sick wear see communication functions on If you want to use the LCD library we have to include this as well. We will have two main courts and voids it up on in void lube And we will talk about that in a minute. But this is the master side. While the slave Arduino programming will also included the wire library to enable the i square see communication on. We will add a court and voice it up on void Lube on start sending and receiving data. So let's see this in action. The first thing that you need to do is go on, open up our do you know? No, we will have tried to codes. The 1st 1 is for the master albarino on the 2nd 1 is for the sleeve. It can be helpful to sit upto or more out. We know to share information with each other in this example that we're going to talk about here. Two boars are programmed to communicate with one another in a master writer or slave receiver configuration via the Eye Square C Synch Ross Serial Protocol. Several functions off our green wire library are used to accomplish this are doing one, which is the master is programmed to send six bite off data every half second toe are uniquely address slave. Once that message is received, it can bend the be viewed in the Slave Board Cereal Monitor window, which is opened on the USB connected computers. The Ice Acquirer C Protocol involve using two lines or two wires to send and receive data. We already talked about that and cover it. They are labeled a C L on as the A for serial clock pin on for Celia serial data pill. As Dr Klein changes from low to high or the rising edge of the clock balls a single bit off information that will form in sequence. The others of a specific device is transferred so clock controls that sending on receiving process off that each slave device has to have its own unique address, and we're going to sit a unique address for each slave device that we add. Both master and slave devices needs to take tens communicating over a stem that align in this way, it's possible for Arduino to communicate with money, device or other bores using just two wires. Now we know that there is a unique address on there is a master code on slave court. Let's start by opening up opening up new sketch. As you know, we have voice it up. Envoy Group. The first tip is including the wire library. So let's go here and right include with hashtag on this light. Why are the techs now inside? The voice it up. We need to start or join the I square c bus on. If we are writing the court for the master controller for the Master Arduino we have on option toe assign on address or aside it, it's up to us now. You just need to write why are due to begin now If you if you want to assign an address, you can right between these two parent theses, so you can't write one on that is for this Master will be one on. We will leave it for default since we only have one master. No, After that, let's initialize a bite variable that we want to send. X equals zero. No, we have a bite called X with an initial value off here inside the void. Lou Well, right the rest of the cords. First, we already mentioned that we have more than one function responsible for sending on receiving that toe on from master and slave. We will start by writing that all using that begin transmission function basically begin scrunch mention on in transmission are functions that, uh, are functions that you have to use at the beginning on the end of each transmission. So let's write these two functions. Why are they began? Andi, Why are dot and drone's mission now begin Transmission will take the address off the slave that we want toe Transmit that at all so we can let's say eight or six, depending on the address off the device that we are going to sit in the second are Green Accord. This won't take anything on the court that we want to transmit must be written between these two lines. So we really go and use the right function. Why are the right and as you can see here, we felt like anything we can send a text X is on leave a space. We can light another sentence where the trite on let's send the X value Now, if you want. Since this is on infinite loop, we can increase the value of X by simply writing. Explosive blasts will increase the value of X by one and we can add a delay so that we can monitor the change in the X value. So this is the master code on. You can simply verify it to make sure that there is no errors here on. Let me go on, save it. Okay. In the project folder so that we can come back on, get it without want Let's name it codes on this name the file Master. Okay, now it's combining done combining without errors. So now our master code is ready. It will use the wire library for I square. See communication here we have inside the set up the wire begin. You should initialize the icicle mercy. Here we defined a bite that we will send its called X with an initial value of zero. We will send this value toe the master to the slave device on Dhere. We added the begin on end of transmission lines. On between them, we did the transmission. Are we transmit? X is on the value of X. We increase the value by one a day so that it will be a continuous stream off information starting from zero then plus one plus one plus one. So it will Ah, get us on the screen. 01234 etcetera. This will be the master code. Now, let's open up a new project for the sleeve. Now, since we already finished the master now we need to talk about the slave court. This slave court will join the I Square C bus with an address because it's a slave device on each slave device needs an address. But before this, we need to include the YR library. Okay. Now what we need to do is right. Why are dot Begin on between these two fantasies? We have to set the address. Let's say that we want to name or toe Give this slave Algerian on others off sex. We have to use that same address here in the master code. So here is the address of the slave device on and we need to use the same thing here, which is six. So when the master since that that will send it to this are do you know, because we might have other artery knows with different addresses? No, we want to reject star an event which is on received. So why am dot on receive this function will just start on event on. Let's call it receive event? No, we will know if we did get something in the ice square. See line. Using this function will receive will assign a value for receive event when this slave device start receiving data. Now, if you want to display our data in the serial interface, we must use the serial dot Begin. We thought on this it beautiful poetry toe 9600. No, After doing this, we original that there is another function that we need to use. But before moving on, we need toe. Take a quick look at the on. Receive function that we already defined here. Now the only see function dysfunction takes handlers as un in boat. Andi returns. Nothing on did. In its description, it registers a function Toby called when a slave device received a transmission from a master. So what will? What this method will do is basically call a function called handler when there is data coming to the slave device from a master device. So our court must be written inside the function called Handler, not inside. The loop function handler is a function to be called when the slave receives data. This should take a single in orange into your barometer. The number off bite street from the master and returns nothing. So, for example, you cannot avoid my handler, which is the name here and number of ICTs going. Now, let's see what we are going to do here inside the void loop just at a deli, because we are not going to write anything on. The same thing is done with any other slave device. Now, we already named the handler or the function to be used with an M receive event. So what you need to do now is go here on write a function that's called receive event. Andi, give it on in boat, which is and let's see how money, which is the number off bites. Toby received this function that executes whenever data is received from master, it is registered as an event. So as you can see here, we didn't just started here as received event. Now, while this function will do, it will check if there is cereal or I square see communication happening on. Then it will read the incoming data. So first, if you want to check, we need to use on if statement or while statement. Let's use on if statement now inside these two care LeBron theses. Let's write while If we use an if statement, it will only check ones. But while statements usually loop through all but the last Andi, it will check everything on Keep moving, you know. No, this is basic See thing, so I won't go into programming basics right wine if the function called wire available if it has a value larger than one, this means that we are receiving bite as a character or we are receiving that engineer now in order to receive the incoming data. We must solve it in something. Let's define a character. Andi, Let's call the wire the treated function. Why are they treat? Were read the incoming data on return it which will then be stored in the sea character. Now, if you want to print what we have received, we need to use the serial monitor. You can write serial dot print on drive the sea, which is the variable in which we store the incoming data. Now, if you don't receive a bite as integer, you must go outside the wire loop on right and x equal. Why are dot feed? Then you can simply print the received data, which is cereal using serial dot plant Oh, um and inside that and we need to write X, which is the value off the by that we received. Now we know for sure that we have transmitted. We have transmitted characters. X is with the space on. We have transmitted a bite, which is X. It has an initial value of zero. So this part of the court will check if there is Syria communication or not. Andi. It will read the characters on store them in the sea variable. Then it will print it in the serial monitor. Now what about the bite that we have sent here? We will read it using the same method wired it, read and store it in the X variable, which is an integer. Then we are going toe. Print it in the serial monitor so this line will check if there is Syria Communication or not, this line will read. The character on this line will read the bite interior. No, the whole function. It's called when there is something received by the slave organ Amassed are since that us live on the name of the function is identified here. You can't change it to any other name on. You have to change it here as well. So this works in a different way that the master. Now, instead of having the court inside the lube, you will have it in a separated function which is a handler that will handle data. Once master sends that data on the slaves way, That's it for this lesson. I know that it waas too much to take in one lesson, but I have to explain it in just one take because It's like a connected lesson. If you have any question regarding anything in these cords, you can simply asking that you aren't able. I'll be more than happy to help. But what you need to know, basically, is that in the master device, what we did that may save this and the muscle device. What we did is we use the wire library for ice. Percy, we started. I score, seek indication to sit up under initialized variable point bite on, give it initial value of zero. Then we started the transmission with a certain enslaved device number six and we have sent character on a number. Then we ended. The transmission increased the value that we have transmitted by one, then added a delay. Here we identified. We used the library or identified the slave number or address, which is six. Then we initialized on all receive event Ah, that will take action once the slave starts receiving that and it will direct the action to the received event function which is here that receive event function, will receive the incoming character, store it in the sea variable on printed on the serial line, and it will also read the incoming number sort in the X variable, then print it on the serial line. That's it for this lesson. If you have any questions, please ask and unable. This is Ashraf from educational engineering team. 7. 7 Arduino to DS1307 Time and Date Module Circuit Design: I know and will come to this annual Asan in which I'm going to show you with a circuit design for Aren't we know toe ds 13 All seven RTC time and date module. Let's start by adding that, aren't we know board? We will use Arduino on a board. This is it. Let's rotate it now. Let's have the D us 13 or seven module. This is our endure. Let's rotate it. I was one of okay. They okay, This is it. Now let's add LCD module. You can use any of these other cities. I don't try to use the most common one. Okay. Not this one. No, the most common one is this one LCD, and we need some resistors. Okay, these are the resisters. Now let's start connecting Grillo Dimension that we have. And as the A as you can see and us Seattle bins for I square Siebel, you'll so connect the a c l a pin to the US See, it had been which is a five. It's also called a C l In the album data sheet on the SDA will be connected toe a four, which is also the as the A pin. Let's share the caroling. Now You have to connect power on ground to this board. Sold. Connect the five. Alter the five volt on the ground to the ground. That's it. This showed the coloring for reds on black. These are the pens that we have to connect for the our school RTC module. Now it comes to our Arduino board. You can connect it whatever you want with the other city. Let's start by looking at the pen name. Now we have two pens that are stoop ins for all city leads. We won't use them. Let's start by these pins. Number one on number three on number five. Let's connect these three together 13 and five. These three plans must be connected to ground, so we'll take a ground yr from here and connect it. No Number two must be connected to five volts. So take this, Andi, Let's go. So this side Onda, let's drag it here to make it more clear that this is five little pin. The real life you will do the very same thing. Now this is five volts for the second. Been now number 5123 number four on number. The are a spin on the enable pen must be connected to argue, you know, let's connect our esto. This been okay? Spin. Let's connected to Bend number seven enable it's connected. Tobin number six. Andi. After that, we need to connect before that happens, starting from D four. So let's go the before 56 and seven. So starting from the before connected to open number four, all this is going to be number five. It will be easier on the 2nd 1 The wonderful The last plane watches db seven must be connected to being number two. You can change the been connection depending on your needs, but for me, this is the best connection. I'm going to use Thes spins in, according so make sure that you memorize where everything is connected again. 13 and five are connected to ground to is connected to five volts for on. Six are connected. Toe seven and six in the arena. Ball on pens from 11 toe 14 which are the before toe db seven are connected toe bends number 234 and five. This is the LCD connection. We're going toe. Read that I using our score, see communication protocol from this RTC module. And after reading this that I were going to display it on this all city. We'll talk more about this in the practical coding section, but this is the circuit design that you need to make sure, ah, to do it in your lab or in your simulation software. We can also covered this in protest simulations off. Where? If you wish. But for now, this is how to connect your circuit on. Get it ready for our example. Thanks for watching this. Quick. Listen, you can take a look at the connection. I will attach this image in their sources of this lecture. This is actually from educational engineering team.