Topic: Added a MCP23017 port expander. (I2C)

Just to see how it works I added a MCP23017 port extender and that was working quite well.

I used a small test bash script with a led on GPA0

#!/bin/bash
#
#
# MCP23017 test
#

GPA=0x14 # MCP23017 use GPA
GPB=0x15 # MCP23017 use GPB
I2C=0x20 # I2C address of MCP23017 A0 A1 A2 grounded
COUNT=0
SLEEPER=0.2
LOWEST=0
if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi

echo
echo "Press 'q' to quit 's' for slower blinking and 'f' for faster."
echo

while :
do
      read -t 0.01 -n 1 key
      /usr/sbin/i2cset -y 1 $I2C $GPA 0x01  # MCP23017 led on (pin 21 if GPA)
      sleep $SLEEPER
      /usr/sbin/i2cset -y 1 $I2C $GPA 0x00  # MCP23017 led off (pin 21 if GPA)
      sleep $SLEEPER
      let count+=1
      if [[ $key = q ]]
      then
          if [ -t 0 ]; then stty sane; fi
          echo "Led blinked" $count "times."
#         TEST=($SLEEPER | bc | awk '{printf "%0.3d", $1}') 
          echo "Last blinking delay $SLEEPER sec."
          echo
          break
      fi
      if [[ $key = s ]]
      then
       SLEEPER=$(echo "$SLEEPER + 0.01" |bc)
      fi
      if [[ $key = f ]]
      then
       if (( $(echo "$LOWEST < $SLEEPER" | bc -l ) ));
       then
         SLEEPER=$(echo "$SLEEPER - 0.01" |bc)
       fi
      fi
done

exit 0

Or a smile script:
You need 2 * 10 Segment Digital Red LED Bar Graph Display Ultra Bright (from ebay at $1,57)

http://pi.gids.nl:81/images/10leddisp.png

#!/bin/bash
#
#
i2cset -y 1 0x20 0x00 0x00 # configure Port A pins GPA0-6 as outputs
i2cset -y 1 0x20 0x01 0x00 # configure Port B pins GPA0-6 as outputs
#
RIGHT="/usr/sbin/i2cset -y 1 0x20 0x14"
LEFT="/usr/sbin/i2cset -y 1 0x20 0x15"
x=1
T=0.01

if [ -z "$1" ];
then
  COUNT=20
else
  COUNT=$1
fi

while [ $x -le $COUNT ]
do
   $RIGHT 1
   $LEFT 128
   sleep $T
   $RIGHT 2
   $LEFT 64
   sleep $T
   $RIGHT 4
   $LEFT 32
   sleep $T
   $RIGHT 8
   $LEFT 16
   sleep $T
   $RIGHT 16
   $LEFT 8
   sleep $T
   $RIGHT 32
   $LEFT 4
   sleep $T
   $RIGHT 64
   $LEFT 2
   sleep $T
   $RIGHT 128
   $LEFT 1
   sleep $T
   $RIGHT 64
   $LEFT 2
   sleep $T
   $LEFT 4
   $RIGHT 32
   sleep $T
   $LEFT 8
   $RIGHT 16
   sleep $T
   $LEFT 16
   $RIGHT 8
   sleep $T
   $LEFT 32
   $RIGHT 4
   sleep $T
   $LEFT 64
   $RIGHT 2
   sleep $T
   $LEFT 128
   $RIGHT 1
   sleep $T
   let "x++"
done
$LEFT 0
$RIGHT 0
#
#
# End

Use with ./{scriptname] x where x is number of walks

Or for some C coding see also http://wiringpi.com/extensions/i2c-mcp23008-mcp23017/ this page.
He build a Wiring Pi solution

Any other good examples, please let me know...

Using WebIOPI one get a nice web besed interface to use this device (and others)

My WebIOPI config [DEVICES] part:

[DEVICES]
# Device configuration syntax:
# name = device [args...]
#   name   : used in the URL mapping
#   device : device name
#   args   : (optional) see device driver doc
# If enabled, devices configured here are mapped on REST API /device/name
# Devices are also accessible in custom scripts using deviceInstance(name)
# See device driver doc for methods and URI scheme available

# Raspberry native UART on GPIO, uncomment to enable
# Don't forget to remove console on ttyAMA0 in /boot/cmdline.txt
# And also disable getty on ttyAMA0 in /etc/inittab
#serial0 = Serial device:ttyAMA0 baudrate:9600

# USB serial adapters
#usb0 = Serial device:ttyUSB0 baudrate:9600
#usb1 = Serial device:ttyACM0 baudrate:9600

#temp0 = TMP102
#temp1 = TMP102 slave:0x49
temp2 = DS18B20
#temp3 = DS18B20 slave:28-0000049bc218

bmp = BMP085

#gpio0 = PCF8574
#gpio1 = PCF8574 slave:0x21

#light0 = TSL2561T
#light1 = TSL2561T slave:0b0101001

gpio0 = MCP23017
#gpio1 = MCP23017 slave:0x21
#gpio2 = MCP23017 slave:0x22

#pwm0 = PCA9685
#pwm1 = PCA9685 slave:0x41

adc = MCP3208

http://pi.gids.nl:81/images/webiopi1.png

Chrs

Post's attachments

mcp23017.zip 7.66 kb, file has never been downloaded. 

You don't have the permssions to download the attachments of this post.