![]() In this example void loop() sends the GPIOB address (0x13) to the IC. Serial.println(inputs, BIN) // display the contents of the GPIOB register in binary pins 15~17 to GND, I2C bus address is 0x20įor (byte a=0 a0) // if a button was pressed Here is the sketch: /*Įxample 41.1 - Microchip MCP23017 with Arduino From a hardware perspective we are using a circuit as described above, with the addition of a 560 ohm resistor followed by an LED thence to ground from on each of the sixteen outputs. ![]() Now let’s put all of this output knowledge into a more detailed example. … with the results as such (port B on the left, port A on the right): If you had some LEDs via resistors connected to the outputs, you would have this as a result of sending 0x82:įor example, we want port A to be 11001100 and port B to be 10001000 – so we send the following (note we converted the binary values to decimal): Wire.beginTransmission(0x20) (Using decimals is convenient if you want to display values from an incrementing value or function result). In binary that would be 10000010, in hexadecimal that is 0x82, or 130 decimal. So for example, you want pins 7 and 1 on. Or if binary does your head in, convert it to hexadecimal. So you can insert a binary number representing the status of each output pin. To calculate the required number, consider each I/O pin from 7 to 0 matches one bit of a binary number – 1 for on, 0 for off. … replacing ? with the binary or equivalent hexadecimal or decimal value to send to the register. To control port B, we use: Wire.beginTransmission(0x20) So now we are in void loop() or a function of your own creation and want to control some output pins. To control port A, we use: Wire.beginTransmission(0x20) Wire.write(0x00) // set all of port B to outputs Then to set port B to outputs, we use: Wire.beginTransmission(0x20) Wire.write(0x00) // set all of port A to outputs So to set port A to outputs, we use: Wire.beginTransmission(0x20) First, we’ll examine setting them as outputs. So before using we need to set whether each port is an input or an output. Each address holds one byte of data that determines various options. Now to examine how to use the IC in our sketches.Īs you should know by now most I2C devices have several registers that can be addressed. It contains the minimum to use the IC, without any sensors or components on the I/O pins: Next, here is a basic schematic illustrating how to connect an MCP23017 to a typical Arduino board. For example, if you connect 15~17 all to 5V, the control byte becomes 0100111 in binary, or 0x27 in hexadecimal. You can alter the address by connecting a combination of pins 15~17 to 5V (1) or GND (0). If you have other devices with that address or need to use multiple MCP23017s, see figure 1-2 in the datasheet. If you connect them all to GND, the address is 0x20. These are used to determine the I2C bus address for the chip. ![]() So connect it to 5V!įinally we have the three hardware address pins 15~17. Pin 18 is the reset pin, which is normally high – therefore you ground it to reset the IC. Pin 14 is unused, and we won’t be looking at interrupts, so ignore pins 19 and 20. Pin 9 connects to 5V, 10 to GND, 11 isn’t used, 12 is the I2C bus clock line (Arduino Uno/Duemilanove analogue pin 5, Mega pin 21), and 13 is the I2C bus data line (Arduino Uno/Duemailnove analogue pin 4, Mega pin 20).Įxternal pull-up resistors should be used on the I2C bus – in our examples we use 4.7k ohm values. The sixteen I/O ports are separated into two ‘ports’ – A (on the right) and B (on the left. After reading this you should have the knowledge and confidence to move forward with using a MCP23017.įirst, let’s look at the hardware basics of this IC. The MCP23017 can be quite simple or complex to understand, so the goal of this article is to try and make it as simple as possible. You can order these in through-hole, surface-mount and also mounted on a breakout board. At this point you should also download yourself a copy of data sheet – it will be referred to several times, and very useful for reference and further reading.įurthermore if you are not familiar with Arduino and the I2C bus, please familiarise yourself with the I2C tutorials parts one and two. Here is our subject of the article in DIP form: ![]() Plus we think the I2C bus is underappreciated! So let’s get started… However you may have a distance between the Arduino and the end-point of the I/O pins – so with these ICs you can run just four wires instead of a lot more save board space with custom designs, and preserve precious digital I/O pins for other uses. This 28-pin IC offers sixteen inputs or outputs – and up to eight of the ICs can be used on one I2C bus… offering a maximum of 128 extra I/O ports.Ī few people may be thinking “Why not just get an Arduino Mega2560?” – a good question. In this article we discuss how to use the Microchip MCP23017 16-bit serial expander with I2C serial interface.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |