0

So I am designing a simple project where my Arduino reads analog values from a potentiometer, or rotation sensor, as it is called on EBay, and uses that value to determine how many relays to close. I am connecting 4 digital pins to the relay module, and powering it with a constant digital signal from pin 2.

Here is my code:

//set up the pins for activating each relay on the module
#define in1 13
#define in2 11
#define in3 10
#define in4 9
#define power 2      //set up the power pin for the module

void setup(){
 Serial.begin(9600);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);
 pinMode(power, OUTPUT);
 digitalWrite(power, HIGH);        //for power to the relay module
}

void loop(){
 float rotationValue = analogRead(A0);          //read raw value from potentiometer
 float percentValue = rotationValue / 10.23;       //convert the value to 0%-100%
 Serial.println(percentValue);                  //print it out on screen
 if(percentValue >= 0.00 && percentValue <= 1.00){         //all relays off
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
 }
 else if(percentValue > 1.00 && percentValue <= 25.00){        //relay #1 on
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
 }
 else if(percentValue > 25.00 && percentValue <= 50.00){           //relay #1, #2 on
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
 }
 else if(percentValue > 50.00 && percentValue <= 75.00){     //relay #1, #2, #3 on   
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
 }
 else if(percentValue > 75.00){             //all relays on
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, HIGH); 
 }
}

Ok, here's the problematic part. After I upload the program onto the board, when I set the potentiometer to the 0 position, all relays are on. And when I twist it to the 100 position, which is maximum, all relays are off. Although it is usable, what's weird is that when I start from 100 and twist it back toward 0, the relays will activate, but in the opposite order. So relay #4 will activate, then #3, then #2, and then #1, and all four are on when I reach 0. Is it something wrong with my code? I've checked and rechecked, and even checked with a different project where I connect the pins to LED's. And they work normally. But when I connect up the relay module, it's reversed. Any ideas?

Here is the relay module: http://www.miniinthebox.com/4-channel-relay-module-with-optocoupler-5v_p903429.html?currency=USD&litb_from=paid_adwords_shopping&gclid=Cj0KEQiAts-kBRCbgrXc1rnXw7MBEiQAnFqTdph_qI5n7rrpW7gZaxafVVkeWitQlN8FUSUJzdsV2JwaAlBA8P8HAQ

And here is the potentiometer: http://www.ebay.com/itm/Potentiometer-Sensor-module-Rotation-Angle-Sensor-Arduino-competible-/251656942542

I mean, right now I'm even thinking that the relay module may be labeled incorrectly, that the numbers are printed the wrong way. But it could be my fault, most likely.

Thanks for any help.

3

All the relay boards I've come across are 'active low' i.e. as you've discovered bringing the control pin down to earth activates the relay. As an aside just be careful that the arduino has enough juice to power the relays, a separate supply is normally required if these are coil rather than ssr type relays

  • They are mechanical relays. By "active low," do you mean that the relays are closed when voltage is applied to their gates? Or what do you mean by "bringing the control pin down to earth?" Are you saying to ground the pin? – user3211857 Dec 20 '14 at 8:50
  • Oh, I did some more searching and came to this: electronics.stackexchange.com/questions/90777/… So when you pull the control pin to ground, the difference in voltage makes current flow through the relay to whatever you're connecting it to. Ha, I've been using them wrong the whole time. – user3211857 Dec 20 '14 at 8:52
  • Don't worry about, it does seem counter intuitive till you look at the circuit diagram, I've made exactly the same mistake in the past. N.B do check the point I made about the relay power supply I would expect your Arduino's voltage regulator to be getting very hot powering all those relays – Bra1n Dec 20 '14 at 9:16
  • So would it be better to power the relay module from something else? Like having the Arduino close a MOSFET and use a separate 5V power source going from the MOSFET to the relays? – user3211857 Dec 20 '14 at 20:55
  • Ok, so now I have it working. Kind of. As I turn the potentiometer passed 0, relay #1 turns on, and my DC motor spins. But as I continue to turn, although the LED's on the module board indicate, the remaining three relays aren't doing anything. The only thing I've changed in my code is replace all the digitalWrite(inX, HIGH) to LOW whenever I want to turn on the relay. But only the first one works. Is this because there isn't sufficient current from the Arduino connected to USB? – user3211857 Dec 20 '14 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.