To isolate the lower end of the address, we use another operator &, known as bitwise and. This will take the higher end of the byte and drop off the lower end, leaving us with the first 8 bits. this means that 'high' = off, 'low' = on.įor (int y=1 y>, known as bitshift right. remember that the IC "sinks" current, that is current runs fro +5v through the LED and then to I/O pin #define greenchip 0x21 // you will need to change addresses if you vary from the schematicĪllOff() // the PCF8574N defaults to high, so this functions turns all outputs off #define yellowchip 0x22 // addresses in this example match the published schematic in the tutorial #define redchip 0x20 // device addresses for PCF8547Ns on each LED colour bank If for some reason you are wiring your PCF8574s differently, you will need to recalculate your device addresses: #include "Wire.h" Note that the device addresses in the sketch match the schematic above.
Normally (for example with a 74HC595) current would run from the IC, through the resistor, LED and then to earth. For example, an LED would have the anode on the +5V, and the cathode connected to an I/O pin.
This means that current runs from +5v, through into the I/O pins. The reason is that the PCF8574 is a current sink. Hopefully you noticed that those last two values seemed backwards – why would we send a zero to turn all the pins on? And the same again to set the output pins, for example to turn them all on we send binary 0 in hexadecimal which is 0 or to have the first four on and the second four off, use 00001111 which is Ox0F. So if we set pins A0 to A2 to GND, our device address in binary will be 0100000, or 0x20 in hexadecimal. To address the PCF8574 we need two things, the device address, and a byte of data which represents the required output pin state. A2~A0 are used to select the last three bits of the device address, P0~P7 are the I/O pins, and INT is an interrupt output which we will not use. There should not be any surprises for you there. So how do we do this? First, let’s consult the pinout: In fact, if you were to use more than one 74HC595 this IC might be preferable, as you can individually address each chip instead of having to readdress every IC in line as you would with shift registers. This gives us another eight outputs, in a very similar method to the 74HC595 or can be used as eight extra inputs. Speaking of which, let’s do that now…Ī very useful and inexpensive part is the PCF8574 I/O expander ( data sheet.pdf). Several applications come to mind with an extended I2C bus, for example remote temperature monitoring using the the ST Microelectronics CN75 temperature sensor from part one or controlling several I/O ports using an I2C expander without the expense or worry of using a wireless system. Below is a good example of this, from the aforementioned NXP data sheet: Using a pair of these ICs, you can have cable runs of 20 to 30 metres, using shielded twisted-pair cable. An example of such an IC is the NXP P82B715 ( data sheet).
These ICs reduce electrical noise over the extended-length bus runs and buffer the I2C signals to reduce signal degradation and chance of errors in the data. If all your devices are on the same PCB, then there is nothing to worry about, however if your I2C bus is longer than around one metre, it is recommended that you use an I2C bus extender IC. One of these is the physical length of the SDA and SCL lines. If you have not already, please read and understand the first I2C article before continuing.įirst of all, there are some limitations of I2C to take into account when designing your projects. / #include //include Wire.In this article we are going to continue learning about the I2C bus and how it can work for us.
/ // Arduino I2C Scanner // Re-writed by Arbi Abdul Jabbaar // Using Arduino IDE 1.8.7 // Using GY-87 module for the target // Tested on 10 September 2019 // This sketch tests the standard 7-bit addresses // Devices with higher bit address might not be seen properly.