RELAY8 with MCP23017 - toggle outputs

Got comments, questions, or tips about any of the shields listed on ShieldList.org? Post them here!
Post Reply
ilium007
Posts: 8
Joined: Sat Jun 16, 2012 8:32 am

RELAY8 with MCP23017 - toggle outputs

Post by ilium007 » Thu Oct 26, 2017 7:05 am

Hi - looking at the sample sketch for the RELAY8 bard it looks as though only one really output can be enabled at once as the MCP23017 IC requires an 8bit value to be sent. How would you go about redesigning the sketch to allow a command to send an output HIGH or LOW on the MCP23017 but leave all the other outputs in their current state ?

Would this work (Python code below, but you get the idea).

Start with a known bit state, 00000000 (all bits off). I'm using decimal values in the example below but imagine their binary equivalents.

Then use a bitwise XOR to toggle a bit value at a known placeholder. You would reset the state variable with this new state and then send to the MCP21037. If this were to be done we could then toggle the relay outputs whilst easily being able to send a new 8-bit value to the MCP21037 leaving all other bits untouched.

The first operation toggles bit 0 on and off, the second operation toggles bit 7 on and off:

Code: Select all

>>> current_state = 0
>>> current_state ^= 1 << 0
>>> print current_state
1
>>> current_state ^= 1 << 0
>>> print current_state
0
>>> current_state ^= 1 << 7
>>> print current_state
128
>>> current_state ^= 1 << 7
>>> print current_state
0
>>>

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests