andyx
Posts: 6
Joined: Tue Jun 05, 2018 3:57 pm

MCP23017 Help

Tue Aug 27, 2019 1:08 pm

Trying to control 128 devices with Raspberry PI, one device at a time.

To this end I bought an already made 128 I2C GPIO Expander 8*MCP23017, and 8 * 16 Channel Relay Board.

The expander works as it should, the relays not so much, so I'm actually testing now using an 8 Channel Relay Board.

My problems come in programming the MCP23017, more exactly how to use just one specific pin.

I've managed to activate all the pins, but when it comes to just 1 - this eludes me completely.

Keep in mind that the relay board activates on LOW and deactivates on HIGH.

Code: Select all

import smbus
import sys
import getopt
import time
bus = smbus.SMBus(1)
address = 0x20
bus.write_byte_data(0x20, 0x00, 0x00) #set Port-A as output
#Relay turns ON
time.sleep(1)
bus.write_byte_data(0x20, 0x12, 0xFF) #set Port-A to High
#Relay turns OFF
I've checked the specs on the MCP23017 several times, but still I cannot seem to find the specific addresses for each pin..

Thanks for reading this :)

Edit: http://ww1.microchip.com/downloads/en/d ... 01952c.pdf MCP23017 Datasheet
Last edited by andyx on Tue Aug 27, 2019 2:05 pm, edited 1 time in total.

drgeoff
Posts: 9922
Joined: Wed Jan 25, 2012 6:39 pm

Re: MCP23017 Help

Tue Aug 27, 2019 1:38 pm

Yuu didn't think to help those trying to help you by including a link to the datasheet you have looked at?

andyx
Posts: 6
Joined: Tue Jun 05, 2018 3:57 pm

Re: MCP23017 Help

Tue Aug 27, 2019 2:06 pm

Good point :geek:
http://ww1.microchip.com/downloads/en/d ... 01952c.pdf

I've edited the main post also.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5196
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: MCP23017 Help

Tue Aug 27, 2019 2:43 pm

andyx wrote:
Tue Aug 27, 2019 1:08 pm
Trying to control 128 devices with Raspberry PI, one device at a time.

To this end I bought an already made 128 I2C GPIO Expander 8*MCP23017, and 8 * 16 Channel Relay Board.

The expander works as it should, the relays not so much, so I'm actually testing now using an 8 Channel Relay Board.

My problems come in programming the MCP23017, more exactly how to use just one specific pin.

I've managed to activate all the pins, but when it comes to just 1 - this eludes me completely.

....

I've checked the specs on the MCP23017 several times, but still I cannot seem to find the specific addresses for each pin..
There is no "address" used to control a single pin. You have to write a byte to the A or B register (as you have been doing) and use bit masking:

Code: Select all

byte    bits     "action"
0xFF    0b11111111 All pins  high
0x00    0b00000000 All pins low
0xFE    0b11111110 pin 0 low
0xFD    0b11111101 pin 1 low
0xFC    0b11111100 pin 1 and pin 0 both low
etc ....
[Edit]
I program in 'C' (not python) so, FWIW, here are examples of other "masks" used within the 'C' code for this MCP23017-based circuit:
https://www.cpmspectrepi.uk/raspberry_p ... alLCD.html

Code: Select all

/* Parallel 4-bit interface based upon (port B of)
 * a MCP23017. Data GPB4-7 --> D4-D7.
 * Control GPB0-3:
 * GPB0 --> ¬BackLightA (GPA0-7 used for an 8-bit // interface)
 * GPB1 --> ¬RegisterStrobeB (echoed to RPi GPIO #22)
 * GPB2 --> EnableB (echoed to RPi GPIO #18)
 * GPB3 --> ¬BackLightB
 */
/* "Standby mask" */
#define DUAL_LCD_B4_STANDBY 0x00 /* 4-bit data Low, BLA ON, RSB LOW,  EB LOW, BLB ON */
/* "and" masks: */
#define DUAL_LCD_BLA_ON   0xfe
#define DUAL_LCD_RSB_LOW  0xfd
#define DUAL_LCD_EB_LOW   0xfb
#define DUAL_LCD_BLB_ON   0xf7
#define DUAL_LCD_BLAB_ON  0xf6
/* "or" masks: */
#define DUAL_LCD_BLA_OFF  0x01
#define DUAL_LCD_RSB_HIGH 0x02
#define DUAL_LCD_EB_HIGH  0x04
#define DUAL_LCD_BLB_OFF  0x08
#define DUAL_LCD_BLAB_OFF 0x09
/* "additive" nibble "masks" */
#define DUAL_LCD_B4_RMASK 0x01 /* 4-bit data Low, BLA OFF, RSB LOW,  EB LOW, BLB ON */
#define DUAL_LCD_B4_DMASK 0x03 /* 4-bit data Low, BLA OFF, RSB HIGH, EB LOW, BLB ON */
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, A+ and a B2) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

andyx
Posts: 6
Joined: Tue Jun 05, 2018 3:57 pm

Re: MCP23017 Help

Tue Aug 27, 2019 4:06 pm

Thanks for all the help.
I've found an easy fix using adafruit library :D

Code: Select all

import time
import board
import busio
import digitalio

from adafruit_mcp230xx.mcp23017 import MCP23017

i2c = busio.I2C(board.SCL, board.SDA)
mcp = MCP23017(i2c)

pin0 = mcp.get_pin(0)

pin0.switch_to_output(value=True)

pin0.value = False
time.sleep(1)
pin0.value = True
You can do this for all the pins 0-15.

I'm so happy I finally found a fix for this :mrgreen:

Internet is a wonderful thing, many thanks to you all :)

LTolledo
Posts: 2150
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: MCP23017 Help

Wed Aug 28, 2019 9:14 pm

The adafruit library is the only solution
as the MCP23017 and MCP23008 are still not (yet) integrated to gpiozero
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

Return to “Advanced users”