Arduino freezes when using power on my relay board

Hey everyone,

I’m working on a project where I use midi notes to switch 8 old lamps using the Arduino Uno. I’ve build a case with 8 wall sockets that are linked up to a relay board for the Arduino. I use the Hairless midi serial bridge to send midi notes via USB to the Arduino. This serial data is used to send digital outputs to the relay board. This board has eight relays that can be switched with 5 volts.

This all works good and fast until I put power on to the sockets.
Then, after about 5~10 seconds the Arduino freezes. The relay shield stays in it’s current state and the indication lights for serial communication stop flashing. The Hairless serial midi bridge does still show activity. When there isn’t 220 volts going through the relays it all works great. The relay board I’m using had 8 LED’s for indication and they respond as intended without the power switched on.

I tried to make a simple schematic to clarify my setup. The Arduino is powered via USB. I also tried powering the arduino with an additional adapter of 5V and 500mA but that didn’t make a difference.

I’m really confused as to why this happens and I really hope someone can help me. I’m a novice so I might be doing something stupid here (highly probable).

Materials:

This is the code that is running on my Arduino. It’s based on code I’ve found for processing serial midi.

#include <digitalWriteFast.h>

byte incomingByte=0;
byte notebyte=0;
byte velocitybyte=0;
byte statusbuffer=0;
byte NOTE_ON = 144;
byte NOTE_OFF = 128;
boolean arp_triggernext=false;
boolean firstbyte;
void MIDI_Poll(){
  if (Serial.available() > 0) {
    do {
      // read the incoming byte:
      incomingByte = Serial.read();
      if (incomingByte>247) {
        // this is where MIDI clock stuff is done
        switch (incomingByte){
        }
      }
      else if (incomingByte>240) {
        statusbuffer = 0;
        //sysex stuff done here
      }
      else if (incomingByte>127) {
        statusbuffer = incomingByte;
        firstbyte = true;
        notebyte = 0;
        velocitybyte = 0;
      }
      else if (statusbuffer!=0) {
        if (firstbyte == true) {
          // must be first byte
          notebyte = incomingByte;
          firstbyte = false;
        }
        else {
          // so must be second byte then
          velocitybyte = incomingByte;
          //process the message here
          if (statusbuffer == NOTE_ON && velocitybyte != 0) {
            switch (notebyte) {
               case 60:
               digitalWriteFast2(2, HIGH); 
               break;
               case 61:
               digitalWriteFast2(3, HIGH); 
               break;
               case 62:
               digitalWriteFast2(4, HIGH); 
               break;
               case 63:
               digitalWriteFast2(5, HIGH); 
               break;
               case 64:
               digitalWriteFast2(6, HIGH); 
               break;
               case 65:
               digitalWriteFast2(7, HIGH); 
               break;
               case 66:
               digitalWriteFast2(8, HIGH); 
               break;
               case 67:
               digitalWriteFast2(9, HIGH); 
               break;
            }
          }
          else if (statusbuffer == NOTE_OFF || (statusbuffer == NOTE_ON && velocitybyte == 0)) {
            switch (notebyte){
               case 60:
               digitalWriteFast2(2, LOW); 
               break;
               case 61:
               digitalWriteFast2(3, LOW); 
               break;
               case 62:
               digitalWriteFast2(4, LOW); 
               break;
               case 63:
               digitalWriteFast2(5, LOW); 
               break;
               case 64:
               digitalWriteFast2(6, LOW); 
               break;
               case 65:
               digitalWriteFast2(7, LOW); 
               break;
               case 66:
               digitalWriteFast2(8, LOW); 
               break;
               case 67:
               digitalWriteFast2(9, LOW); 
               break;
            } 
          }
          //now clear them for next note
          notebyte = 0;
          velocitybyte = 0;
          firstbyte = true;        
        }
      }
    } while (Serial.available() > 0);
  }
}

void setup() {      
  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  MIDI_Poll();
}

Thanks in advance!

  • Jwktje

I/O pins can deliver MAX 40mA (You need more to drive the reay coil)
Use transistors (and resitores) ... Are they present already?

Supply 5V from other source than Arduino

you may be also getting a spike from the relay when it de-energises.. you may want to use a blocking diode on the relay coil.
this is usually always a must with electronics..it stops spike backfeeding into the electronics.

What knut_nysaid.

SWITCH the relays from the Arduino. POWER the relays from somewhere else.

knut_ny:
I/O pins can deliver MAX 40mA (You need more to drive the reay coil)
Use transistors (and resitores) … Are they present already?

Supply 5V from other source than Arduino

I’ll look into that, thanks! I wouldn’t think that additional components would be necessary as the description of the board states that it is made for direct use with the pins of the Arduino. The demo on the site shows it hooked up exactly the same as me.

And to make things weirder, If I upload a sketch to the Arduino that just alternates all the Relays (and I don’t use the midi serial data) it can run without a problem. That makes me think that:

  • Either the Midi > serial stuff is causing problems
  • Switching multiple relays at once is causing problems

Sorry for being a noob. I’ve done loads of programming but not much with embedded systems.

According to the Schema the board already has the transistors, kick back diode, etc, built in.

What are you using to power the Relais bord?

dannable:
According to the Schema the board already has the transistors, kick back diode, etc, built in.

What are you using to power the Relais bord?

I initially used the 5 volt from the Arduino because that was suggested in the demo on the webshop I got the board from.
But from suggestions in this topic I've just tried powering the relay board with a 5 volt 2A adapter I had lying around.
At first It looked like it didn't work because the first try caused the Arduino to crash in the same manner. But after a reboot everything seemed to function okay!

I did some trial and error and found out that the system would stop working when I tried switching more than 5 relays at the same time for a while. This makes me think that each relay needs about 400 mA to function. I figure ,since the adapter was 2A, that this is the reason I couldn't switch more than 5 relays just now.

Am I right in thinking that I now need a 5 volt 3,2A adapter to get all the relays to switch simultaneously?
Because 5 relays worked fine on my 2A adapter.
5 * 400mA = 2A

The specification states that Spoel vermogen = 0.36W, so I wouldn't expect them to draw 400mA.

Try measuring the current draw with a multimeter?

all the relays to switch simultaneously

If you are controlling each relay with a different Arduino pin, you are actually turning them on sequentially, just very quickly sequentially. So fast sequentially it might as well be simultaneously, but still.

I'll show myself out.

I really doubt you need 2 amps but here is a 5v 2A supply:

Nit pickers: Something about port manipulation, assuming no set up where everything runs through one transistor or something, other nitpicky things- you get the point.