{"id":5754,"date":"2019-03-30T12:50:56","date_gmt":"2019-03-30T16:50:56","guid":{"rendered":"https:\/\/dronebotworkshop.com\/?p=5754"},"modified":"2023-04-11T17:56:32","modified_gmt":"2023-04-11T21:56:32","slug":"i2c-arduino-arduino","status":"publish","type":"post","link":"https:\/\/dronebotworkshop.com\/i2c-arduino-arduino\/","title":{"rendered":"I2C Communications Part 1 – Arduino to Arduino"},"content":{"rendered":"\n
<\/a> <\/a> <\/a><\/p>\n I2C communications have become the de facto method of communicating between microcontrollers, microcomputers and a variety of integrated circuits and sensors. It has been around since 1982 and was originally developed for use in television receivers.<\/span><\/p>\n Although we have used many I2C sensors and displays in previous articles we have not actually looked into how I2C works and how it can be used to communicate between microcontrollers.<\/span><\/p>\n Today we will correct that and learn more about I2C. We\u2019ll also see how it can be used to exchange information between two Arduinos and how it can be used to allows one Arduino to control another one.<\/span><\/p>\n <\/p>\n This will be the first of four articles on I2C. In future articles we will see how we can build our own I2C devices, how to interface a Raspberry Pi and an Arduino using I2C and how to do some advanced I2C configurations, including using multiple masters on one I2C bus.<\/span><\/p>\n Let\u2019s get started!<\/span><\/p>\n I2C is a serial protocol used on a low-speed 2-wire interface. It was originally developed by Phillips in 1982 to allow integrated circuits within television receivers to communicate with one another.<\/span><\/p>\n Times have changed, Phillips is now NXP and I2C has become a communication standard that is supported by virtually every major semiconductor manufacturer.<\/span><\/p>\n I2C is an abbreviation for \u201cInter-Integrated Circuit\u201d. It is also called \u201cIIC\u201d or \u2018I squared C\u201d. <\/span><\/p>\n I2C is used with microcontrollers like the Arduino and with microcomputers like the Raspberry Pi. Many displays and sensors interface to their host controller using I2C.<\/span><\/p>\n I2C does have several limitations however. It is not particularly fast, although for most of its intended uses it is plenty fast enough. <\/span><\/p>\n I2C can only be used over short distances, after all, it was originally meant to communicate between integrated circuits on the same printed circuit board. The maximum distance of reliable transmission decreases as the speed increases, at the slowest speed (100 Kbaud or a clock rate of 100 KHz) the maximum distance is about a metre.<\/span><\/p>\n The original I2C bus had a maximum speed of 100 KHz. Most common applications still use this speed, as it is quite sufficient for transferring data from sensors and to simple displays.<\/span><\/p>\n I2C and has some higher speed modes. Not all I2C devices support these modes:<\/span><\/p>\n On an I2C bus it is the master that determines the clock speed.<\/span><\/p>\n An I2C bus has two signals, along with a power and ground connection.<\/span><\/p>\n <\/p>\n The two signal lines are as follows:<\/span><\/p>\n There are two pull-up resistors attached to each signal line, they pull the bus up to the supply voltage when it is inactive.<\/span><\/p>\n Note that the supply voltage is not standard, it can be either 3.3 or 5-volts. It can also be a lower voltage for some high-speed I2C implementations.<\/span><\/p>\n This difference in supply voltages can cause issues when you are interfacing I2C devices that use different logic levels. We will discuss this more in a future article when I show you how to interface a Raspberry Pi (3.3-volt logic) with an Arduino Uno (5-volt logic).<\/span><\/p>\n There are two types of devices that can be interfaced to the I2C bus – Masters and Slaves.<\/span><\/p>\n The Master device controls the bus and supplies the clock signal. It requests data from the slaves individually. \u00a0There can be more than one master device on the bus but only one can be the active master at any given moment.<\/span><\/p>\n The master devices do not have an address assigned to them.<\/span><\/p>\n Slave devices do have an address, and this address needs to be unique on the bus. They use a 7-bit addressing scheme, so up to 128 slaves can be on one I2C bus. In real life this large collection of devices is never used, it is rare to see over a dozen I2C devices on one bus.<\/span><\/p>\n A newer, 10-bit addressing scheme has been implemented, it is backward-compatible with the existing 7-bit addressing method.<\/span><\/p>\n Commercial I2C devices are allocated I2C address by NXP, who maintain the bus specifications. Although I2C has been open source since 2006 there is a fee charged for obtaining a slave address from NXP. \u00a0No fee is required for master devices, or for devices that are not meant for commercial manufacture.<\/span><\/p>\n Some I2C devices are assigned multiple addresses, usually variances in the lower address bits. These devices can be manually configured for different addresses, allowing multiple devices of the same type to be used on a single I2C bus.<\/span><\/p>\n There are other buses that have been derived from the I2C bus, and which are in many ways compatible with I2C.<\/span><\/p>\n In a future article I will explain how the data on the I2C bus is structured. But now we have some basic I2C information, enough to start experimenting.<\/span><\/p>\n The Arduino has a built-in library for working with I2C called the <\/span>Wire Library<\/span><\/a>. It makes it very easy to communicate on the I2C bus, and it can configure the Arduino to become either a master or a slave.<\/span><\/p>\n The Wire library has several useful functions for working with I2C.<\/span><\/p>\n We will use some of these functions in our sketches.<\/span><\/p>\n The SDA and SCL connections for I2C are different between Arduino models. The experiments I\u2019m about to show you were done using two Arduino Unos, but you can use other models of the Arduino providing you change the pins accordingly.<\/span><\/p>\n I\u2019ve put together a chart to help you get it figured out. It includes some common Arduino boards, as well as a few of the discrete chips. \u00a0The pinouts for the chips I list (ATTiny and ATmega328P) are with the DIP package, not the surface-mount ones.<\/span><\/p>\n Some Arduino Uno clones have separate SDA and SCL pins and you can use them instead of the two analog pins if you wish. They are internally connected to the same place.<\/span><\/p>\n Note that the Arduino Due actually has two I2C ports.<\/span><\/p>\n Also, be aware that there are some incorrect hookup diagrams on the internet for the Pro Mini. Use the two analog pins, A4 and A5, as shown in the table above.<\/span><\/p>\n For our first experiment we will hoo two Arduinos together and exchange data between them. One Arduino will be the master, the other will be the slave.<\/span><\/p>\n I\u2019m using two Arduino Unos, but you can substitute other Arduino’s if you don\u2019t have two Unos. Use the previous chart for the connections.<\/span><\/p>\n Here is how I connected my two Arduino Unos together:<\/span><\/p>\n <\/p>\n It is quite a simple hookup, essentially you just tie the ground and the two I2C pins together.<\/span><\/p>\n One thing to be aware of is that my diagram does not show the use of pull-up resistors, I found that everything seemed to work correctly without them. \u00a0However, you might want to include them, especially if you experience errors or intermittent operation.<\/span><\/p>\n To hook up some pull-up resistors attache a couple of 10k resistors to the SDA and SCL lines. Attach the other end to the 5-volt output on one of the Arduino’s.<\/span><\/p>\n Here is the sketch that will be used on the Arduino that you have designated as being the master.<\/span><\/p>\nIntroduction<\/span><\/h2>\n
I2C Communications<\/span><\/h2>\n
Uses and Limitations<\/span><\/h3>\n
I2C Speeds<\/span><\/h3>\n
\n
How I2C Works<\/span><\/h3>\n
\n
Other I2C Derivatives<\/span><\/h3>\n
\n
Arduino Wire Library<\/span><\/h2>\n
\n
Arduino I2C Connections<\/span><\/h3>\n
\n\n
\n Arduino Board or Chip<\/span><\/td>\n SDA<\/span><\/td>\n SCL<\/span><\/td>\n<\/tr>\n \n Uno<\/span><\/td>\n A4<\/span><\/td>\n A5<\/span><\/td>\n<\/tr>\n \n Mega2560<\/span><\/td>\n 20<\/span><\/td>\n 21<\/span><\/td>\n<\/tr>\n \n Nano<\/span><\/td>\n A4<\/span><\/td>\n A5<\/span><\/td>\n<\/tr>\n \n Pro Mini<\/span><\/td>\n A4<\/span><\/td>\n A5<\/span><\/td>\n<\/tr>\n \n Leonardo<\/span><\/td>\n 2<\/span><\/td>\n 3<\/span><\/td>\n<\/tr>\n \n Due (has two I2C)<\/span><\/td>\n 20 + SDA1<\/span><\/td>\n 20 + SCL1<\/span><\/td>\n<\/tr>\n \n ATTiny85 & ATTiny45<\/span><\/td>\n 5<\/span><\/td>\n 7<\/span><\/td>\n<\/tr>\n \n ATmega328P<\/span><\/td>\n 27<\/span><\/td>\n 28<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n I2C Between 2 Arduino’s<\/span><\/h2>\n
Hooking up 2 Arduino’s<\/span><\/h3>\n
Master Demo Sketch<\/span><\/h3>\n
\/*\r\n I2C Master Demo\r\n i2c-master-demo.ino\r\n Demonstrate use of I2C bus\r\n Master sends character and gets reply from Slave\r\n DroneBot Workshop 2019\r\n
Welcome to the Workshop!<\/a><\/blockquote>