Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

download Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

of 10

Transcript of Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    1/10

    I2C the extension of GPIOs

    The Raspberry Pi has 26 ports out of which a handful GPIOs left to dogreat works. But to further impress your nephew with your fancy

    Raspberry Pi here's how you can connect additional micro controllers toextend the GPIOs capability by any numbers !

    Even the clumsy LCD panel which has eaten away half a dozen of GPIOscan straight away be shifted to the additional micro controllers leavingthe Raspberry Pi with only 2 wires to control all these countless GPIOsprecisely yet very economically.

    The new mantra is I2C , just two wires - SDA and SCL (Pin-3 & Pin-5)

    will control all these additional devices. The small help that I'veborrowed from Adfruit.com is in the form of a GPL python script tomanage these extended GPIOs.

    System set up: To use I2C on Raspi you need to enable a few things inWheezy Raspbian as by default it is not enabled. This is a fairly easyprocess. First you need to edit the modules file using :

    $ sudo nano /etc/modules#and add the following two lines

    i2c-bcm2708

    i2c-dev

    Save the file and exit nano and open the next file

    $ sudo nano /etc/modprobe.d/raspi-blacklist.conf

    Put a # symbol at the beginning of the two lines so that they look likethis:

    #blacklist spi-bcm2708

    #blacklist i2c-bcm2708

    save and exit nano.

    Now install the necessary softwares to manipulate the I2C bus.

    $ sudo apt-get update #this updates your Raspberry Pi$ sudo apt-get install python-smbus i2c-tools #instals smbus & i2c tools

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    2/10

    So far so good. Relax now ! As the next step is going to be little messy but berest assured that soon you will have enough knowledge to expand your Raspi

    with as high as 8 16 64 or whatever you like no of GPIOs and thencontrolling these extended GPIOs by only two wires from your Raspi !

    Once the softwares are installed reboot your Raspi so that these effects takeplace permanently.

    $ sudo reboot

    However, my nephew likes to use the Raspi like a TV and he simply switchesit off and then switches on and it works glitch free !

    Connections: Two package MCP23008 ( 18 pins DIL ) and MCP23017 (28

    pin DIL) is taken for our exercise. Together they comes for around Rs:200from kit's n spares. Connect them as shown in the drawing:

    Testing: Once you have connected the hardware double check the wiring.Make sure the 5 volt is going to the correct pins else it will heat up themicroprocessor and damage it eventually. Also ensure the reset pins (6 or 18 )are connected with the +5V rail. Open a shell prompt from a remoteconnection or from the local and issue the following command.

    $ sudo i2cdetect -y 0

    or$ sudo i2cdetect -y 1

    If your Raspi is version 1 then use 0 else 1 and watch for the following screenappears at the terminal prompt.

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    3/10

    Congratulations ! Pat your back , your cute Raspi has successfully detected thehardware at 0x20 address. Now it's only a matter of time to issue commandsto control it. The address is 020 because allthe three address pins (A0,A1,A2

    ) are set low. If you set only A0 high (connected to +5 volt) the address wouldhave become 021.

    This signifies one fundamental issue with the I2C communications - You canconnect any number of devices on the I2C bus (Pin-3 & Pin-5) but all devicesshould have a unique address. So how you set A0, A1 and A2 is going to definedifferent address for the different devices. You may try that feats here to gainmore confidences now.

    Testing: There is a command line testing for I2C on Raspi. Connect a few

    LEDs on the pins as shown in Fig-1 and issue the following commands.

    $ sudo i2cset 0x20 0x00 0x00 #this will switch on the LED on A0

    $ sudo i2cset 0x20 0x00 0x01 #this will switch off the LED on A0

    The command line program is easy ,provided you understand the binary andhex conversion of all the numbers, bits and not talk of the impressivedatasheet that comes with each micro processors. However, Adafruit.com has

    an excellent library by which we can manipulate these GPIOs in a far morelucid way.

    Open a terminal window and write the following python scripts. However, allthe program files including the Adafruit library is available at this location.Download it and then expand them all in a fixed location.

    https://docs.google.com/file/d/0B3E3LcSKoM-6YUZZbndXYVR1VTA/edit?usp=sharing

    $sudo nano mcp23017.py

    from Adafruit_MCP230xx import *

    import time

    mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 16) # for MCP23017

    #mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 8) # for MCP23008

    # Set pins 0, 1 and 2,3,15 to output (you can set all pins 0..15 this way)

    mcp.config(0, mcp.OUTPUT)

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    4/10

    mcp.config(1, mcp.OUTPUT)

    mcp.config(2, mcp.OUTPUT)

    mcp.config(3, mcp.OUTPUT)

    mcp.config(15, mcp.OUTPUT)

    # Set pin 4 to input with the pullup resistor enabled

    mcp.config(4, mcp.INPUT)

    mcp.pullup(4, 1)

    #Make pin-4 internally

    while True:

    # Read input pin and display the results

    print "Pin 4 = %d" % (mcp.input(4) >> 4)

    mcp.output(0, 1) # Pin 4 is High

    time.sleep(0.5);

    mcp.output(0,0)

    time.sleep(0.5)

    mcp.output(1, 1)

    time.sleep(0.5);

    mcp.output(1,0)

    time.sleep(0.5)

    mcp.output(2, 1)

    time.sleep(0.5);

    mcp.output(2,0)

    time.sleep(0.5)

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    5/10

    mcp.output(3, 1)

    time.sleep(0.5);

    mcp.output(3,0)

    time.sleep(0.5)

    mcp.output(15,1)

    time.sleep(0.5)

    mcp.output(15,0)

    time.sleep(0.5)

    Run the program at the command prompt by

    $sudo python mcp23017.py

    You will find the LEDs will change in sequence continuously. The pin-4 is setas input and it's internally made 1 by it's pull up resistor. Join it with theground and you will see the print out changes from 1 to 0.

    My Prototype for Fig-1:

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    6/10

    The same feats can be achieved with the MCP23008 too but because it hasonly 8 GPIOs therefore, the no of outputs to be reduced to 0 to 7 unlike 0 to15 for MCP23017. Also if we connect a number of MCP23017 or acombination of MCP23008 then for each to have a different address we haveto make different combinations of the Address pins (A0,A1,A2) and

    accordingly in the instance of the Adafruit library we have to change theaddress bits.

    mcp1 = Adafruit_MCP230XX(address = 0x20, num_gpios = 16) # MCP23017

    mcp2 = Adafruit_MCP230XX(address = 0x21, num_gpios = 8) # MCP23008

    mcp3 = Adafruit_MCP230XX(address = 0x22, num_gpios = 16) # MCP23017

    ----- like that we have to change in our program --------

    LCD Panel on extended GPIOs: Now lets do something more interestingon the extended GPIOs. We will make our MCP23008 to drive a 4*16common Hitachi LCD panel with a few real time data. Refer to Fig-2 below.

    Connections:Wire up the LCD panel like this:

    LCD Pin Function Pi PinMCP23008Pin

    01 GND PI-6 -

    02 +5V PI-2 -

    03Contrast via5K pot : center

    PI-6PI-2

    -

    04 RS - 0

    05 RW PI-6 -

    06 Enable - 1

    07 Data 0 N/C -

    08 Data 1 N/C -

    09 Data 2 N/C -

    10 Data 3 N/C -11 Data 4 - 2

    12 Data 5 - 3

    13 Data 6 - 4

    14 Data 7 - 5

    15+5V via560ohmresistor

    PI-2 -

    16 GND PI-6 -

    Hook up the DS18B20 temperature indicator like this.

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    7/10

    GPIO4 gets connected with Raspi Pin 7.

    Schematic :

    Fig-1: Simple Control

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    8/10

    Fig-2: LCD Panel

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    9/10

    My prototype:

    The DS18B20, the temperature scanner is connected to the Raspi while theLCD panel is connected on the MCP23008. Apart from Gnd & 5 volt only twomore wires SDA,SCL have gone to the MCP23008 board.

    Softwares:

    Adafruit_MCP230xx.py and Adafruit_I2C.py - these two GPL libraries aretaken from Adafruit.com . They can be downloaded using github but now-a-days the github runs very slow therefore these two small files can be straightaway downloaded from my site here. The other two files lcd23008.py and

    iptalk2mcp.py are for running the 16*4 LCD panel and the date / time / Day /temperature clock. Download the link, extract all 4 files in a directory andthen run.

    [Files: Adafruit_MCP230xx.py , Adafruit_I2C.py, lcd23008.py,iptalk2mpc.py ]

    $sudo python iptalk2mcp.py

    The LCD as connected on the MCP23008 will start showing the IP Address,Day , Date and time along with ambient temperature.

  • 7/29/2019 Extend your GPIOs on Raspberry Pi with MCP23017 & MCP23008.

    10/10

    The files are all available in zip format here. To make it running every time itboots , add the above line some where near the top in /etc/rc.local file.

    https://docs.google.com/file/d/0B3E3LcSKoM-6YUZZbndXYVR1VTA/edit?usp=sharing

    Webiopi: Webiopi has a beautiful interface with MCP230XX.Once you add it in /etc/webiopi/config file in the[Devices] section, MCP230XX will start showing all it'sGPIOs in the explorer. By clicking them in the explorerwindow you can make them on or off.

    Bye

    S. Bera

    Vindhyanagar