Solving I2C Address Conflicts - TCA9548A I2C Multiplexer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop were resolving i2c address conflicts I'll show you how to hook up to oled displays that use the same i2c address and send them different data we're playing mediator today so welcome to the workshop [Music] well hello and welcome to the workshop and today we're back working with the i2c bus now we've done a number of videos about this bus we started off by learning what the i2c bus is how it goes under a number of different names such as I squared C III C Twi bus and we learned how it works with a series of slave devices communicating with a master in the second video we learned how we could take an Arduino and turn it into a slave device and we also built our own eye to see sensor and the third video we covered how we could deal with different logic levels on the bus by interfacing or 3.3 volt Raspberry Pi with a 5 volt Arduino well today we are going to discuss a problem that you can run into when using this bus it's a very common problem and that is of address conflicts what happens when you have two slave devices that need to use the same address so let's go and take a detailed look at what this problem is and then I'll show you how we can solve it now as you recall the i2c bus is a two wire bus one wire is the clock signal and the other wire is the data signal the bus has a master device the master device controls all of the devices on the bus and it is the only one that initiates communication the master does not have an address assigned to it we can begin by adding a slave device to our bus now this slave device has an address of hexadecimal to eight so the master can communicate with that device by addressing it directly we can add another slave device to the bus this device has another address hexadecimal one nine and the master can communicate with the two devices individually however now we're going to add a third device another slave device that also has an address of hexadecimal to eight this is an address conflict and is not acceptable in the i2c bus on the i2c bus all i2c slave devices must have a unique address now some I to see slave devices such as sensors and displays have configurable addresses these devices use jumpers switches solder pads or external address connections to alter their base I to see address however many devices have a fixed eye to see address and these are not alterable unfortunately there's no formal regulation of i2c addresses so it is entirely possible for two manufacturers to make a device that has the same address to resolve this problem we need a second i2c bus the master can connect to this bus and therefore be connected to two independent I to see buses we can now connect one of our conflicting slaves to the second bus and resolve all of our conflicts now the way to resolve the issue of having multiple slaves with the same address is to simply have more than one eye to see bus and put each slave on its own individual bus now some single board computers actually have this arrangement they have multiple eye to see buses and so you can just put your slaves on each of the different buses and you won't run into any address conflicts but with the Arduino there is only one eye to see bus the Raspberry Pi actually has a second i2c bus but the second bus is limited to communicating with the hats the hardware attached on top expansion boards that you put on a Raspberry Pi so it can't be used for additional sensors so with these devices we have to look at other methods of adding additional I to see buses now there is a method with the Arduino of doing this in software and it does have its limitations but it's worth looking at so let's take a look at some software implementations of multiple I to see buses on the Arduino the software solutions to our problem involved the use of alternate I to C libraries these libraries are used in place of the built-in arduino wire library these libraries will allow I to see connections on different i/o pins they will also allow for multiple I to see buses let's look at some libraries soft i2c master has been implemented by todd a curt the last update as of this video was January 26 2017 the library is available on github soft i2c master is a simple i2c software implementation it comes with three example sketches including an i2c scanner soft wire is a library implemented by Steve Marple as of this recording the last update was on doomed the third 2018 soft wire is also available on github soft wires an i2c software implementation that uses basic Arduino functions to allow any pin to be used for i2c this library is dependent upon another library called async delay it comes with two example sketches software I to see has been implemented by seed studio as of this recording the last update was April to 2nd 2018 this is available on seed studios wiki and also on github software I to see is another implementation of i2c that is also documented on the seat studio wiki it comes with additional seat studio libraries to use in an example sketch it comes with two examples an i2c scanner and a multiple OLED display sketch a software implementation of multiple i2c buses might seem to be the ideal solution after all you don't need to buy any extra hardware and it cuts down the wiring complexity but there are some drawbacks one of the drawbacks is that most of these systems only allow the arduino to act as a master but i don't see that as being a huge drawback personally the second drawback is that some of these take a bit more memory again i don't see that as being a real problem but one real problem is that a lot of the libraries we use with i2c sensors are actually dependent upon the wire library the arduino ide s built in i - c library and they call the wire library functions within them now you need to go and edit those libraries and replace them with the calls to your software library and while this isn't that difficult to gob to do it means now you have a library that is dependent upon this other i2c implementation and not the wire library and to me that gets a bit complex however I wouldn't completely eliminate using these software i2c implementations one thing they are good at is using alternate pins for i2c so if you need for example in the Arduino Uno your analog input a 4 and a 5 pins for something you can assign the i2c bus to a couple of the digital i/o pins that you're not using also for things like the 80 tiny which don't actually have an i2c bus this is an ideal implementation but the implementation that I like is a hardware implementation and so we're going to take a look at a component now called an i2c multiplexer that will resolve our problem in hardware the module we'll be using is the TC a 95 48 a module from 8 of this is an 8-channel I to see multiplexer the device operates on three to five volts and all of the pins are 5 volt tolerant the module has internal 10k input pull-up resistors the outputs do not have pull-up resistors the module has a base address of hexadecimal seven zero there are also three address pins to allow you to change the address here are the pin outs of the TCA 95 48 a the VN is the power which can be from 3 to 5 volts DC D and D of course is the ground s da is the input I to see data SC L is the input i2c clock the rst pin is a reset pin and is normally left unconnected a 0 a 1 and a 2 allow you to set the i2c address channel 0 is represented by s d 0 which is the SDA line for channel 0 and SC 0 which is the SCL line there are similar pins for channels 1 through channel 7 the TCA 95 48 a is available in other modules as well some of these modules do have pull-up resistors on the output pins now this is the Adafruit TCA 95 48 a module and it's the one I'll be using in our experiments now you can get this module in other form factors and from other manufacturers as well but I like the Adafruit module it's nicely made as all the way to fruit products are and it's got the connection silk-screened on the board to make it easy to hook up now on the back you'll find a couple of solder pads the ones on the right are ones that you can cut if you want the disabled the internal 10k pull-up resistors now remember those are the pull-up resistors on the input I to see the outputs do not have pull-up resistors so you'll have to add them yourself or use slave devices that already have integrated pull-up resistors the three pads on the Left can be used to alter the i2c base address by itself its hexadecimal seven zero but if that conflicts with something or if you want to use more than one of these modules you can change it with those pads by jumping them over and you can make it any address from hex seven zero to hex seven seven now those connections are also brought to the outside of the module so you don't have to use the pads you can do it externally as well now one other device I want to show you is based on a similar tip this is a shield for the Arduino and it's based on the TC a ninety five forty four which is a 4-channel multiplexer not an 8-channel multiplexer but it's a shield and it has the four outputs over here and it has also an output for the base i2c if you want to use that and so if you want a solution in the form of a shield if that's easier for you you might want to look at a module like this as well so now let's go and start using our multiplexer now those of you who've watched robot workshop videos for a while may recall that about a year or so ago I did a video on using OLED displays organic light-emitting diodes and in that video in one scene I took an OLED display that had a fixed address I took a second one that was identical hooked them both up on a solderless breadboard and tied them to an Arduino I to see bus since both of them had the same address both displays showed exactly the same thing and this might actually have some practical uses for example you might want to build a project which has a display on both the front and the back and you want both displays to be identical however we're going to expand upon that today we're going to use our eye to see multiplexer and connect each display to a different channel of the multiplexer and then we're going to modify one of the other sketches that we use in that same OLED video the temperature and humidity meter and we're going to use it to display the temperature on one LED display and the humidity on the other one so let's go and take a look at the hookup for that and then I'll show you the code that we can use with our multiplexer in order to achieve this for our demonstration we're going to be using an Arduino Uno a TC a 95 48 a I - C multiplexer an AM 2 3 - 0 I - C temperature and humidity sensor and two SSD 1306 or equivalent OLED displays will also require some pull-up resistors I used for 2.2 K resistors but you can use any value from 2.2 to 10 K and that will work fine we'll begin by connecting the 5 volt output of the Arduino to the V in pin on the TC a 95 48 a next we'll connect the Arduino z' ground to the ground pin on the i2c multiplexer will also connect the 5 volts to pin one of the a.m. to 3 to 0 and the ground to pin 3 next we'll connect the 5 volts to the VDD pins on both of the OLED displays and we'll connect the ground to the ground pins on the OLED we'll connect the analog input a four on the Arduino to the SDA input on the TC a 95 48 a analog pin a five will be connected to the SCL input on the i2c multiplexer will also connect the SDA line the pin two on the am to three to zero temperature and humidity sensor and will connect the SCL line to pin four on the sensor will connect the sd1 pin to the SDA connection on one of our OLED displays will connect the SC one pin to the SC l connection on the same OLED will connect the SD two pin to the SDA pin on the other OLED display and the SC two pin will be connected to the SCL pin on that display finally we'll connect the 5 volts to one side of each of the dropping resistors we'll connect the other side of the dropping resistors to the SDA and SC l inputs of the OLED and this completes our wiring now here's the sketch that we're going to be using to driver two OLED displays now this is a modified version of a sketch that I used previously in my OLED video now we're going to start off by including the wire library this is the built-in library in your arduino ide for using i2c devices the next few libraries however are not built-in and you'll need to install them by going into your library manager and searching for them if you haven't got them already the first one is the Adafruit graphics library and you can disturb for Adafruit graphics the second one is the Adafruit OLED library for the SSD 1306 OLED displays the third one is the Adafruit library for the am 2 3 to 0 temperature and humidity sensors so you'll need to install those libraries and include them in your sketch now we're going to start by defining a reset pin for the OLED this is actually not used electrically but it's required for the library so we just define this with a value as or as required by our display next is where the sketch differs from my original sketch and that I need to create an object for each of the independent ole ads so I'm calling the objects display one and display two then I define an object called a.m. - 3 - 0 to represent the temperature and humidity sensor now this function here which is actually provided by Adafruit is a function that is the key to working with the i2c bus multiplexer this is essentially forming a switch and what you do is you pass it the number of the channel that you want to use and what the function does is it goes and it talks to the channel and then passes your data over to it and then ends the transmission in other words this acts as a switch now the 7 0 over here is the address of the TC a 95 48 a you can change the address on this device by altering the a0 a1 and a2 pins and if you do that you'll need to change the address in your sketch accordingly now we have function called display temp and the temperature is going to be displayed on display number 1 and so what we do is we clear display number 1 we set the color to white even if the display is physically a different color we always use white we set the font size I set it to 2 so it would be visible and then we set the cursor to a position on the display I moved it down a bit toward the center then we print out the letter T and a colon with a space after it then we print the value TEM which we passed in the function and this is a float representing the actual temperature and then we do a space and print to see because we're displaying our temperature in Celsius now display humid does a similar thing except we're going to be using display number 2 and passing it the humidity value now let's go into our setup and the setup will start the i2c library with a wire begin and will also initialize a temperature and humidity sensor now we've got to initialize the individual displays but we need to use the multiplexer and switch out to that channel first so we call our function and pass it a one meaning we want to go to channel number one and then we do a display begin and then we do the same thing for channel number two a new display begins so we're switching our channels before we send the command out and that's the key to working with the multiplexer now the loop is actually pretty simple we start off with a two-second delay and this allows the temperature and humidity sensor to stabilize and then we go and read the temperature and the humidity and we assign it to two different floats then we set the multiplexer to channel number one where we want to display the temperature and we call our display temp function and pass it the value of temperature and then after that we refresh our display and after that we go and do the same thing for channel number two we switch the channel number to call display humid and pass it the humidity value and refresh the display and then we go back and do everything over again and so this should result in the temperature being displayed on display number one and the humidity on display number two so let's go and check it out and here we have our demonstration running and as you can see it seems to work pretty well now the top display is display number one and it's displaying the temperature and the bottom one display number two is displaying humidity and remember both of these displays have the same fixed I to C address and yet we're able to drive them independently from the Arduino because of the TC a 95 48 a multiplexer now you can see the multiplex are back over here on the top and down here buried a bit in the wires of the temperature and humidity sensor plus you might be able to note the pull-up resistors back over here so that's really all there is to it the multiplexer seems to work as advertised and it is sending the data independently to both displays now here's another use for the TC a 95 48 a in our example we'll use a 5 volt microcontroller attached to the input thus putting five volts DC on the V in pin on the TCA 95 48 a will attach i2c connections to channel seven of the multiplexer as well as power and ground connections and a five volt power supply this creates a five volt I to see bus we may now attach five volt I to C slaves to our new bus we can also attack another i2c bus to the multiplexer on channel 3 this one using a 3.3 volt power supply and 3.3 volt sensors we can attack as many different buses as we wish with independent power supply voltages just make sure to tie to ground of all the buses together this example also works equally well with a 3.3 volt microcontroller the TCA 95 48 a is always powered by the host microcontroller but you can mix and match bus voltages as you wish to increase the flexibility of your design so having multiple i2c slave devices with the same address is not the end of the world in fact I've shown you two different methods you can resolve at one using software and another one using a piece of hardware and by implementing multiple I to see buses you can also help resolve another i2c issue the i2c bus is limited to approximately one meter of total cable length and that's because of the cable capacitance will eventually degrade the signal now if you have multiple i2c buses each one of those buses is subject to that same limitation so you can have much more total cable and run many more I to see sensors and displays around your project in fact I'm using that same technique in my DB 1 robot to run I to see sensors around the robot chassis now if you'd like to learn a little bit more about what we talked about today you'll find an article accompanying this video on the drone ball workshop comm website and there'll be a link to that article right below the video now if you'd like to learn more about the drone ba workshop you can subscribe to our newsletter the newsletter comes out about every two weeks it's not a sales letter is this my way of letting you know what's going on here in the workshop now I make videos approximately once a week but sometimes I miss out on one so the best way to find out about new videos is to subscribe to the YouTube channel and when you subscribe click on that little Bell notification and that way you'll be notified every time I make new content and finally if you'd like to discuss today's video or if you'd like to discuss electronics with some like-minded people or if you'd like to make suggestions about new videos and articles you'd like to see the place to go is a drone bot workshop forums at forum drama workshop comm and I hope to see you on the forum very soon until next time please take care of yourselves and I hope to see you again very soon here in the drone bot workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 53,097
Rating: undefined out of 5
Keywords:
Id: vV42fCpmCFg
Channel Id: undefined
Length: 24min 29sec (1469 seconds)
Published: Tue Feb 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.