Go Down

Topic: Shift Register and Relays (Read 3706 times) previous topic - next topic

Existence

I have been trying to use a shift register (74HC595) to control 8 relays. The Relays are Active Low as per the following info:
Quote
IMPORTANT NOTE: There is a issue with start-up of Arduino programs that control these relays. These relays input controls are Active LOW, meaning that setting a pin LOW turns them ON. To assure that no relays activate at Reset or Power-On until you want them to, the initialization sequence in SETUP should be:
1.digitalWrite(Relay, HIGH);
2.pinMode(Relay, OUTPUT);


So that all works fine and dandy when you're using direct pins but I'm trying to use a switch register to save pins. So the problem is when you use the shift register with it the relay goes bat-shit crazy for about a second, turning the relays on and off as fast as it can. After that though it functions perfectly normal. I have managed to reduce it a bit to just a couple on\off cycles with the following code in Setup, but its still way to much:
Code: [Select]

void setup() {
  bitsToSend = 0;
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
 
  digitalWrite(latchPin, LOW);
  bitWrite(bitsToSend, 0, HIGH);
  bitWrite(bitsToSend, 1, HIGH);
  bitWrite(bitsToSend, 2, HIGH);
  bitWrite(bitsToSend, 3, HIGH);
  bitWrite(bitsToSend, 4, HIGH);
  bitWrite(bitsToSend, 5, HIGH);
  bitWrite(bitsToSend, 6, HIGH);
  bitWrite(bitsToSend, 7, HIGH);
  bitWrite(bitsToSend, 8, HIGH);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
  digitalWrite(latchPin, HIGH);
}

I have also been trying different combination of the Master Reset and Output Enabled Pins to see if I can force the register to HIGH on load. High impedance might also work but I'm having a hard time translating the Function Table:
http://arduino.cc/en/uploads/Tutorial/595datasheet.pdf

Any ideas would be much appreciated!

cr0sh

1. I assume your relays are being driven in some manner via transistors (ie, discrete, or maybe a ULN200x darlington array)?
2. Do you have isolation caps on the power supply pins of the shift register (and any other ICs involved)?

Since you are getting some action, the above are minor - what I suspect is happening is that you are clocking the data out while the pins are toggled to latch the incoming data over; when the IC is powered up, it is in an "unknown" state (pins could be set to anything), so you might need to flush it first, then send the data (without the latch enabled), then latch the data over to set the output pins.

I think the proper steps would be:

1. Bring the output enable pin (OE - pin 13) HIGH to keep it from activating the output pins (and attached relays).
2. Bring the master-reset pin (MR - pin 10) LOW to clear the 595 shift register.
3. Clock in the data via the SHcp, STcp, and Ds pins (pins 11, 12, and 14 respectively).
4. Bring the output enable pin (OE - pin 13) LOW to latch the data over to the output pins.

You'd probably want to run these steps once in a "clear stage" where your data you clock out on step 3 is all LOWs (make this part of an initialization routine); then clock your outputs normally as needed.

Note - this is all a "guess" on my part, looking at the datasheet - I've never played with the 595, personally. It seems like your code is already doing most of the above steps, except you have your latch settings backwards I think (the line over the OE means "active low" - meaning it active when LOW, not HIGH).

Hope this helps...

:)
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Existence

Quote
1. I assume your relays are being driven in some manner via transistors (ie, discrete, or maybe a ULN200x darlington array)?

I believe they are a Darlington array behind opto-isolation. Here is a link:
http://arduino-direct.com/sunshop/index.php?l=product_detail&p=156

Quote
2. Do you have isolation caps on the power supply pins of the shift register (and any other ICs involved)?

I added a .1uF to the latch pin (ST_CP) as I read on a forum it might smooth out any initial flickering. For isolation I'm guessing you mean adding a 1uF across VCC and ground?

I think I tried the steps you stated but I will give it another shot just to be sure.

I'm wondering on power up if the Switch Registers initial state is LOW to all the pins. If that's the case then I'm thinking the Relays (Active LOW) are always going to turn on even before I can issue the OE to HIGH? But then again I'm still getting my head around the intricacies of shift registers.

Existence

Now I'm confused, I was testing and I seem to be getting different results depending on the Arduino Pins I connect Master Reset (MR) and Output Enabled (OE) to. The PWM pins seem to make the relays fire the most at start. The Digital PINS try to fire them put there seems to be a power issue preventing it, and lastly when I just hard wire OE and MR to VCC and ground it works like I would expect it too (No Relays Fire or flicker). This also has the same effect if I add or remove the .1uf capacitor off VCC\GND.

Test 1:
OE -> PWM PIN 6
MR -> PWM PIN 7
Code: [Select]
 
pinMode(OEPin, OUTPUT);
pinMode(MRPin, OUTPUT);
digitalWrite(OEPin, HIGH);
digitalWrite(MRPin, LOW);

Relays Flicker like crazy.

Test 2:
OE -> Digital PIN 22
MR -> Digital PIN 23
Code: [Select]
 
pinMode(OEPin, OUTPUT);
pinMode(MRPin, OUTPUT);
digitalWrite(OEPin, HIGH);
digitalWrite(MRPin, LOW);

Status lights on relays flicker a bit but relays rarely fire. Looks like they are trying to fire but can't.

Test 3:
OE -> Connected to VCC
MR -> Connected to Ground
Code: [Select]
 

Works correctly, power light on the Relay board turns on and the relays never fire or flicker. Only problem is OE and MR are hard connected to Ground and VCC now so I can't turn them off!

The fact test three worked has be hopeful, if I can solve this mystery why Arduino isn't correctly sending MR and OE HIGH and LOW signals then this should solve the larger problem.
I have triple checked and my wiring is an exact match to this tutorial, with the exception of adding MR and OE:
http://www.arduino.cc/en/Tutorial/ShiftOut



Grumpy_Mike

Quote
I added a .1uF to the latch pin (ST_CP) as I read on a forum it might smooth out any initial flickering.

This is wrong. Never connect a signal pin to a capacitor.
The use of pull up or pull down resistors will stablise things during start up. This happens because on reset the pins are set to be inputs and so are left floating.

Existence

Good to know, capacitor removed. I think it was the Shift Register totorial page that set me astray.

HOLY *&#$(*# it worked. The pull-up and pull-down resistors did the trick, I can't believe something so simple caused so many hours of pain!

Just to make sure I'm not doing anything incorrectly I wired the resistors up like so:

Output Enabled (Active Low) - Need it to default to HIGH
IC OE --------+-------- Arduino Pin (Output)
                    |
               10K Ohms
                    |
           VCC (Arduino 5V)

Master Reset (Active Low) - Need it to default to LOW
IC MR --------+-------- Arduino Pin (Output)
                     |
              10K Ohms
                     |
                   GND



dc42

#6
Aug 29, 2011, 10:26 am Last Edit: Aug 29, 2011, 10:31 am by dc42 Reason: 1
You should also digitalWrite a HIGH to the OE pin before you set the OE pin to be an output, to avoid a glitch at startup - just like the Important Note at the start of the thread.

You don't need to use the MR pin at all if you always write the full 8 bits to the shift register each time before you write a LOW to the OE pin (and before you set the OE pin to be an output in the initialization); so you could save an output pin and tie MR to Vcc instead.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Grumpy_Mike

Quote
I can't believe something so simple caused so many hours of pain!

Just to let you know this is always true no matter what level in electronics you get to.  ;)

Existence

Appreciate the help guys! I will try removing MR in the morning to see if I can get it to work without. I could definitely use the extra pin.

It's also worth noting I threw a multimeter inline just to see how much current it was pulling. Low and behold when all relays were on it was pulling down almost 500ma. Way over the limit, so part of the earlier testing may have been compromised by Arduino overloading and resetting.

dc42

500mA is at the limit rather than over it if you are powering from USB. If you are providing external power, the limit is higher (typically 800mA), however the regulator may overheat, depending on the input voltage you are supplying.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

floresta

Quote
So the problem is when you use the shift register with it the relay goes bat-shit crazy for about a second, turning the relays on and off as fast as it can.

You can haul compost in a SUV but a pickup truck is a better choice.  You can provide more I/O pins by using a shift register but an I/O expander would be a better choice. 

Don

Existence

As per Mikes recommendation I removed the Capacitor off the latch pin, and placed it across VCC and Ground. That's working great, however I'm curious if I add another Shift register for something else completely (not linked to the first) would I use another decoupling capacitor for it as well? I guess what I'm asking is the decoupling capacitor shared for everything on the circuit or would you have one for each? My gut says it's only decoupling everything after the Capacitors placement in the circuit so I would use another...

retrolefty

Generally speaking it's best that every IC package has it's own bypass .1 ufd cap mounted as close to the device as possible. Then each module should have a larger say 10-100ufd cap across the Vcc and ground wires powering a module.

Lefty

Grumpy_Mike

Note that this is only "normal" decoupling, if things get heavy you have to resort to inductors as well:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Existence

Oh wow, excellent article Mike, it makes perfect sense now. That's for the info!

Go Up