Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MCP23017 plugin
#1
Hi,

I just wanted to put up a short note that I have started developing a plugin to control a relay board that is interfaced to a Raspberry Pi using a MCP23017 GPIO expander.
The code is very much in its infancy at the moment, and is really just a hack of the relayboard plugin.

My approach has been to abstract the hardware interfacing elements out of the SIP plugin and rather have it call 2 C programs which respectively initialise the MCP23017, and control valves.  This has the side effect that the plugin could be repurposed to interface to a different hardware layer by purely replacing the 2 C programs.

The code still needs tidying, but it looks like it is working so far.

I would be interested to know if anyone else has a use for this plugin.  (That would motivate me more to get it to a point where it can be redistributed.)

Regards,
Geoff.
Reply
#2
Photo 
That would be very useful.

I made an expansion hat for the Pi using an MCP23017 a while back but have not written a plugin for it.


   

   


Dan
Reply
#3
Cool.

I have found a couple of challenges while working on building my controller which uses the MCP23017. Firstly, it seems to be very sensitive to electromagnetic interference. That led me to some work on putting in snubbers on my relay circuits (combination of a MOV and RC snubber on each solenoid). Transients generated by my relays are dealt with via the flyback diodes in the ULN2803A Darlington transistor array that I use to drive the relays.
A second challenge is that if the Pi reboots, the MCP23017 does not automatically reset. That means, if you have an irrigation program running, and for whatever reason the Pi crashes and reboots, the GPIO output pin on the MCP23017 that is driving your relay will stay high and your solenoid will stay open. That is bad. That led me to using a GPIO pin on the Pi to control the Reset line on the MCP23017. That means that in order to start using the MCP23017, I first have to bring the Reset line (which is active low) high to bring the MCP23017 into normal operation. That works well as a fail-safe and also allows me to software-control the reset line on the MCP23017.
A second MCP23017 on my controller provides the interface to a flow sensor. Rather than directly connecting the open drain output of the Hall Effect sensor to a GPIO on the Pi, I am bringing it into a pair of cascaded 4 bit binary counters, which are connected to the GPIO inputs of a MCP23017. I then have the MCP23017 only generate inputs when bit 7 changes, meaning that my interrupt rate is reduced drastically. If I want a more precise reading, I can read the counter any time I want to. It makes the interface more complex to build, but drastically reduces my interrupt rate.
Reply
#4
Hi,
      I was using an i2c i/O extension, based on a PCA9555, to drive my 16 Stations SIP installation. I had plenty of problems with it, to the point that I rebuild my HW using only SN74HC595N shift registers to control my relays. It's very simple, rock solid and works with SIP code without modification if you follow Dan cabling:

   
I actually coded a plugin Infrastructure to enable sip to use different HW setups to control the stations, without using the Relay_plugin hacks. I now think that is not worth the effort for my installation, the shift register works perfectly. I saved the code if someone is interested.

https://github.com/Pelado-Mat/SIP/tree/Control-Plugin
Reply
#5
There is now a page on the SIP wiki that describes the shift register interface:
https://github.com/Dan-in-CA/SIP/wiki/Relay-interface
Reply
#6
(2016 Nov 17, 11:11 PM)dan Wrote: There is now a page on the SIP wiki that describes the shift register interface:
https://github.com/Dan-in-CA/SIP/wiki/Relay-interface

I have to admit it is interesting comparing the two hardware approaches.  I wasn't aware of the shift register approach until I started looking at the available software solutions for irrigation systems.
The shift register approach is cheaper, and probably also quite easy to lay out on a board. - That said, a layout using a MCP23017 is also easy.  The MCP23017 is a more expensive chip however.  Each MCP23017 also only gives you 16 GPIOs.
Using an I2C GPIO controller, you don't need to worry about timing in your software.
There is a gotcha with chips like the MCP23017 in that if you have a relay on, and the Pi reboots, the outputs on the MCP23017 stay as is. - You need a way of switching them off when the Pi reboots to ensure that stations don't accidentally stay on if your system crashes or reboots.  I have addressed this by tying the !Reset pin of the MCP23017 to one of the GPIOs.  As soon as the GPIO goes low (which it will do when the Pi shuts down) the MCP23017 goes into a reset state.
Neither solution provides the ability to reliably drive enough current for a relay - the 74HC595 can drive 35mA on the the parallel outputs.  The relays I am using would require a current of 35.93mA (hold current that is).  Realistically both solutions still need something like a Darlington transistor array to drive the relays. - Arrays like the ULN2803A also provide integrated flyback diodes which are needed when driving an inductive load (like a relay).
With 20/20 hindsight, I would probably now look at using solid state relays with a zero crossing function to minimise transients generated when relays are switched off.
Reply
#7
The shift register layout we describe is intended or use with a relay board like the Keyes 5v board.
http://www.dx.com/p/8-channel-5v-relay-m...DDi-32HPqg

Those boards include opto-isolators and the other necessary circuitry to be driven by a Raspberry Pi or Arduino. The relays can handle up to 10A AC or DC.

The reset pin of the shift registers is connected to a GPIO pin on the Pi if you check the diagram. There are users who have been running this type of setup for years without a problem.

Dan
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)