It's been a while since my last post, and since I did anything much with the Arduino too. Unfortunately, I had to go on a 'short' business trip that went wrong and ended up doubling in length. While I was away, I know that two more of my component purchases have turned up from China, so I am hoping that one of them is my long lost LCD display. I'll find out when I get into work in the morning.Talking of the LCD display, I started wondering - what if I end up with both the original AND the replacement arriving, would it be possible to use them both at the same time ? So I started investigating...
The answer is "YES". The IIC (I2C) interface mechanism allows for this by sending instructions out on the I2C interface bus along with the address that is related to the specific device the instruction is meant for. So you can have many different devices attached to the I2C bus - they don't even need to all be the same type - and as long as each has its own 'address', then each will see its own instructions.
Think of a mailman walking down the road, dropping off letters to each house... Dr Jones at number 10 only gets his own post, and not the post for Mr Smith at number 19 (well, as long as the postman is doing his job properly). This analogy is near enough, but not quite accurate - and because I am a pedant, this is how the mailman analogy has to be changed... He walks down the street (I2C interface bus), and goes to house (device) number 1. Mr Andrews from number 1 comes out and checks through the mailbag to see if there is any mail (data instruction) with his address on it. If there is, he takes it, and the mailman moves on to number 2. Mrs Brown from number 2 comes out and checks through all the mail to see if there is any with her address, then the mailman goes to see Mr Connor at number 3, Miss Davies at number 4, Mrs Edwards at number 5, and so on all down the street. Each person has the chance to look and see if there is mail for them, regardless of whether there is or not.
I used the word 'bus' just now - and since I didn't mean a bus to take you to work (though the concept is kind of appropriate), maybe I should explain? In electrical terms, a bus is a single long common electrical contact that several devices or components can be connected to. Your breadboard probably has +ve and -ve strips that run all the way down the edge - these are examples of an electrical bus. A device interface bus is similar in concept (and relies on the same electrical principal). The I2C interface bus consists of just 4 connections (+ve and -ve obviously, a clock line that carries timing signals, called SCL, and then the actual data line, called SDA, that actually carries the address and instructions). These 4 connections go from the Arduino to device one, then to the next device, and the next device, and so on. So, not only could I have 2 LCDs, it seems I could have 8!! In fact, I could have up to 127 different devices, as long as I was able to give each one a unique address - but there are some limiting constraints - read on.
Here's a picture I found during my investigations, of 8 LCD displays all connected to a single microcontroller (not an Arduino family unit though) and all showing something different, and an article from 'Embedded Lab' about it (that explains it all much better than me). You can see the 4 wires that form the bus, visiting each device in turn, but delivering only the instructions relevant to that particular device.
It looks as though every device that has an I2C interface, has a controller that defines a base address BUT... note that each type of device could potentially use a different controller that has been programmed with a different base address, such as the I2C Real Time Clock (RTC) module DS1307, which has a default base address of 0x68..
When you purchase them, by default every device of that type, with that controller, will have the SAME address. When you look at Arduino code samples for an I2C LCD display, for example, you will probably see a line of code that looks something like...
LiquidCrystal_I2C lcd(0x27,16,2);
...where LiquidCrystal_I2C is the name of the external library of commands; lcd is the object reference you will use in your sketch; 0x27 is the address of the LCD device; and 16,2 is the number of characters and lines.
By the way, the 0x prefix indicates the number is in hexadecimal (base 16), so the value 255 in hexadecimal would be 0xFF. I am not really sure why the convention is to use hexadecimal values for the interface addresses - but it is - so get used to it...
Since the 16x2 LCD is such a common device, and they mostly tend to use variants of the PCF8574 controller, then you're on a fairly safe bet that 0x27 will be the correct base address. However, some versions of the PCF8574 - (the AT for example, I think), use different addresses such as 0x3F, or they may even use completely different controllers, which also may not use that address. A quick look at the various Arduino and other electronics forums, will reveal that many many people seem to have problems with addressing, when they get LCD 16x2 units that do NOT have that base address - maybe it is due to cheap clone devices from China, that have to be slightly different so as to avoid copyright infringements? Worry not dear reader, one of the sample sketches that comes with the LiquidCrystal_I2C library, is a scanner that will locate any I2C devices attached to your Arduino, and show you the address they are using.
So it looks relatively easy to use a single LCD device, even if it's base address isn't 0x27... but what about if we want to use 2 (or 8) LCD devices? How do we change that 0x27 base address, to something that is unique per device? Well, you are going to have to get your soldering iron heated up, for a start... If you look carefully at the I2C interface board, you should see 3 pairs of solder pads, or jumper pins, that are labelled A0, A1, A2, or something similar.
If you know anything about binary, you will probably recognise that a 3 bit binary number can hold the values 0 to 7 in decimal - which is 8 different numbers. This is what gives us our potential for 8 different addresses. The number given by these three connections (all open by default - e.g. value = 000) is added to the base address of the controller, and this in turn gives us 8 possible unique addresses that we could use. For example, add a blob of solder across the A0 contacts, and your binary number is now 001 (1), which would give this device an address of 0x28 instead of 0x27. The image above is from an Adafruit article about changing I2C addresses, that you may want to look at.
So - if I do end up with both LCD units arriving, I will certainly be giving this a go... Look out for a sketch with...
LiquidCrystal_I2C lcd1(0x27,20,4);
LiquidCrystal_I2C lcd2(0x28,20,4);
😁
Joke for the day
There are 10 types of people in the world...
Those that understand binary, and those that don't
😁
Joke for the day
There are 10 types of people in the world...
Those that understand binary, and those that don't


No comments:
Post a Comment