As a note to others: 5v 8 channel relay boards have 2 GND pins and one of them should be connected to the Arduino while other is connected to the external source. I think the current which comes from Arduino's digital pin should be come back with its GND pin.
[Solved] But 5v 1000ma wall type adapter which is brand new couldn't run the relay. I couldn't understand the problem.
Answer: The problem was the polarity of the adapter jack.
Sample Relay code:
/* Used Arduino Mega pins and Relay ones
- D46 > In1, D47 > In2, D48 > In3, D49 > In4, D50 > In5, D51 > In6, D52 > In7, D53 > In8
- VCC > 5V
- I GND on Relay board > Arduino's GND
- II GND on Relay board > External Power source's GND (If you use external power source) */
void setup(){
for(int i=46; i<=53; i++){
pinMode(i, OUTPUT);
}
}
void loop(){
for(int i=46; i<=53; i++){
int z = i-45;
digitalWrite(i, LOW);
delay(z*20);
digitalWrite(i, HIGH);
delay(z*20);
}
}
I have the same problem.
Please check the adaptor. You must use the right polaritation.
Pull out jumper at JD VCC then connect the positif to JD VCC and the other to ground.
Be sure to measure the voltage. If there is minus sign, change the polarisation then it become positif.
I solve the problem by changing the adaptor because when I measure the voltage, the result are - 5V and + 2,5V. Relay need +5V to make it work.
/* 8 Relay Module configured for Opto-isolation
Arduino UNO pins 3 to 10 connected to IN1 to IN8
Connections: http://i.imgur.com/MDNQGeC.png */
void setup() {
for (int i = 3; i <= 10; i++) {
pinMode(i, INPUT_PULLUP);
pinMode(i, OUTPUT); // defaults HIGH (relays off)
}
}
void loop() {
for (int i = 3; i <= 10; i++) {
digitalWrite(i, LOW); // energize relays until all on
delay(1000);
}
for (int i = 3; i <= 10; i++) {
digitalWrite(i, HIGH); // de-energize relays until all off
delay(1000);
}
}
The circuit is equivalent to having a LED + resistor in series, where the LED's anode is connected to 5V. The LED (IRLED) is illuminated when the controlled end of the resistor (IN1-IN8) goes LOW. The Arduino outputs (when LOW) provide a path to Arduino's GND.
LarryD ... you're quick! (really though, I'm a bit sluggish).
void setup() {
for (int i = 3; i <= 10; i++) {
pinMode(i, INPUT_PULLUP);
pinMode(i, OUTPUT); // defaults HIGH (relays off)
}
}
void loop() {
for (int i = 3; i <= 10; i++) {
digitalWrite(i, LOW); // energize relays until all on
delay(1000);
}
for (int i = 3; i <= 10; i++) {
digitalWrite(i, HIGH); // de-energize relays until all off
delay(1000);
}
}
Sorry for digging up an out dated topic, but this thread was the reason I was able to identify that I had initially connected my 8 channel relay module incorrectly and have since updated my circuits. My question now is in the above example sketch, "i" is first assigned to be an INPUT_PULLUP() in pinMode, than switched to be an OUTPUT(). Can you explain how INPUT_PULLUP() is being used in this application? I understand that the +5V is sourcing current to the LED circuit on the relay board, and when the OUTPUT is sent LOW, the voltage finds its ground and flows through the LED and opto, thus telling the relay to latch and the VAC~ flows. However, I am not able to grasp how INPUT_PULLUP() is being used, and most/all other threads/tutorials only show this being used to monitor a switches position. Any insight?
myggle:
My question now is in the above example sketch, "i" is first assigned to be an INPUT_PULLUP() in pinMode, than switched to be an OUTPUT(). Can you explain how INPUT_PULLUP() is being used in this application?
This "trick" of making the pin HIGH first (enabling the internal pull up resistors), before anything else, stops the relays from "chattering" during bootup of the Arduino.
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
does the same thing.
Leo..
Thanks for the reply Wawa! Would this also apply during future pin state changes? Like if LOW is on and HIGH is off, would the pin revert back to the input pullup when off?
Regarding pinMode, they show 3 possible ways the pin can behave, but actually there are 4.
When using pinMode(OUTPUT); the output will default LOW. As you can see, there are 2 possible ways to make the output default HIGH, but both ways do the same thing - they enable the pullup resistor before the pin is changed to OUTPUT mode.
Perhaps a future version of of the IDE would make all possible modes available like this:
Not sure, but I think the pull up resistor is disabled once the pin has changed to an output.
Doesn't matter anyway if a ~20k resistor stays between pin and VCC.
It's a fraction more current that the pin has to sink compared to the ~2mA the opto requires.
Leo..