2
votes

I'm working on a project where I use midi notes to switch 8 old lamps using the Arduino Uno. I've built a case with 8 wall sockets that are linked up to a relay board for the Arduino. I am using the Hairless midi serial bridge to send midi notes via USB to the Arduino.

This all works until I put power on the sockets with my Uno. 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. When there isn't 220 volts going through the relays it all works great.

(My schematics are below.) 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.

Code:

#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();
}

It's based on code I've found for processing serial midi.

I'm really confused as to why this happens. I want to know why my Arduino and relay board freeze when there is 220 volts going through the relays.

Materials:

1
When you mean "it works great when there's not power going through it," do you mean it freezes while power is supposed to be flowing (regardless if they're connected) or that when they're physically connected?Anonymous Penguin
Just FYI. There is now a stack dedicated to Arduino arduino.stackexchange.comNick Alexeev
make ensure the all the ground under same potentialAMPS
Annonomus Person: I mean that the relays switch perfectly when there isn't 220 volts going through them. So basically the switches work until they actually switch a live circuit.jwktje
This question appears to be off-topic because it is about electronics design and not code, so it shall be redirected to EE stackexchange!zmo

1 Answers

0
votes

As I am seeing here is some circuit issues so i am going to explain them 1 by 1 1. you should not run relay directly from Arduino pin so use appropriate circuit with transistor to run a relay and use arduino pin as a sink not a source will be better. 2.use a freewheeling diode with relay coil. 3.use optoisolator at Rx and Tx.