Monday, October 14, 2013

Sainsmart 8 Channel Relay

I recently purchased a Sainsmart 8 channel relay for use with some holiday lighting with my Arduino, I had some sketches already typed up for use with another project however I soon discovered that the relay board does not simply turn off with the normal LOW command with the digital output pins.I basically spent half a day writing up a new sketch and trying it until I got it to perform the way that I wanted it to. I have an ITEAD Mp3 shield I am using with it, so I cant guarantee this sketch will work for anyone else, But it’s a starting point since I found hardly any information on this relay board.This page is the best information I found but it only loops the relays so I had to us his example to formulate the code to do as I wished.If you look at your serial output and adjust the sense limit and smoothing you can get it to react to some some degree to the music.

IMG_0033[1]

 

// Sainsmart 8 channel relay v1.0
//



#define NUMREADINGS 70 // raise this number to increase data smoothing

int senseLimit = 400; // raise this number to decrease sensitivity (up to 1023 max)
int probePin = 3; // analog 3
int val = 0; // This is the reading from the probePin

#define RELAY_ON 1
#define RELAY_OFF 0

#define Relay_1 4 // Arduino Digital I/O pin number
#define Relay_2 5
#define Relay_3 6
#define Relay_4 7
#define Relay_5 8
#define Relay_6 9
#define Relay_7 10
#define Relay_8 11


// variables for smoothing

int readings[NUMREADINGS]; // the readings from the current analog input
int index = 0; // the index of the current reading
int total = 8; // the running total (Realys)
int average =0; // final average of the probe reading


void setup() {


pinMode(Relay_8, OUTPUT); // specify Relay Outputs
pinMode(Relay_7, OUTPUT);
pinMode(Relay_6, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_1, OUTPUT);

Serial.begin(9600); // initiate serial connection for debugging/etc

for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
}

void loop() {

val = analogRead(probePin); // take a reading from the probe

if(val >= 1){ // if the reading isn't zero, proceed

val = constrain(val, 1, senseLimit); // turn any reading higher than the senseLimit value into the senseLimit value
val = map(val, 1, senseLimit, 1, 1023); // remap the constrained value within a 1 to 1023 range

total -= readings[index]; // subtract the last reading
readings[index] = val; // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index

if (index >= NUMREADINGS) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning

average = total / NUMREADINGS; // calculate the average


if (average > 250){ // if the average is over 250 ...
digitalWrite(Relay_1, RELAY_ON); // Turn on the first Relay*
}
else{ // and if it's not ...
digitalWrite(Relay_1, RELAY_OFF); // turn that Relay off
}

if (average > 300){ // and so on ...
digitalWrite(Relay_2, RELAY_ON);
}
else{
digitalWrite(Relay_2, RELAY_OFF);
}

if (average > 350){
digitalWrite(Relay_3, RELAY_ON);
}
else{
digitalWrite(Relay_3, RELAY_OFF);
}

if (average > 375){
digitalWrite(Relay_4, RELAY_ON);
}
else{
digitalWrite(Relay_4, RELAY_OFF);
}

if (average > 400){
digitalWrite(Relay_5, RELAY_ON);
}
else{
digitalWrite(Relay_5, RELAY_OFF);
}

if (average > 450){
digitalWrite(Relay_6, RELAY_ON);
}
else{
digitalWrite(Relay_6, RELAY_OFF);
}

if (average > 550){
digitalWrite(Relay_7, RELAY_ON);
}
else{
digitalWrite(Relay_7, RELAY_OFF);
}

if (average > 600){
digitalWrite(Relay_8, RELAY_ON);
}
else{
digitalWrite(Relay_8, RELAY_OFF);
}


Serial.println(val); // use this output to aid in calibrating
}
}