I built a signal light controller to switch these relays that will control higher amperage lamps. The Nano works great and operates the LEDs from the digital pins, but refuses to close the relays.
When the digital pins go LOW they will not go low enough to close the relay. The relay will close if I jump straight to ground.
The LEDs are common anode and are switched on the cathode side.
Is the problem with the LED circuits on the digital pins?
Please provide a full schematic including all power connections.
Please provide clickable links (to datasheets) for all components that are connected to the Arduino.
How do you know that they don't go low enough? Did you measure? If pins don't go low enough, it indicates that you're overloading the pin (max. safe current is 20 mA).
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPinred = 11; // the pin that the LED is attached to
const int ledPinwhite = 10; // added
const int ledPinyellow = 9; // added
const int ledPingreen = 8;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPinred, OUTPUT);
pinMode(ledPinwhite, OUTPUT); //added
pinMode(ledPinyellow, OUTPUT); //added
pinMode(ledPingreen, OUTPUT); //added
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
//added
if (buttonPushCounter == 1) {
digitalWrite(ledPinred, HIGH);
digitalWrite(ledPinwhite, HIGH);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPingreen, LOW);
} else if (buttonPushCounter == 2) {
digitalWrite(ledPinred, HIGH);
digitalWrite(ledPinwhite, HIGH);
digitalWrite(ledPinyellow, LOW);
digitalWrite(ledPingreen, HIGH);
} else if (buttonPushCounter == 3) {
digitalWrite(ledPinred, HIGH);
digitalWrite(ledPinwhite, LOW);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPingreen, HIGH);
} else if (buttonPushCounter == 4) {
digitalWrite(ledPinred, LOW);
digitalWrite(ledPinwhite, HIGH);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPingreen, HIGH);
} else if (buttonPushCounter == 5) {
digitalWrite(ledPinred, LOW);
digitalWrite(ledPinwhite, LOW);
digitalWrite(ledPinyellow, LOW);
digitalWrite(ledPingreen, LOW);
} else if (buttonPushCounter == 6) {
digitalWrite(ledPinred, LOW);
digitalWrite(ledPinwhite, HIGH);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPingreen, LOW);
} else if (buttonPushCounter == 7) {
digitalWrite(ledPinred, LOW);
digitalWrite(ledPinwhite, HIGH);
digitalWrite(ledPinyellow, LOW);
digitalWrite(ledPingreen, HIGH);
}else if (buttonPushCounter == 8) {
digitalWrite(ledPinred, LOW);
digitalWrite(ledPinwhite, LOW);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPingreen, HIGH);
}
else {
digitalWrite(ledPinred, HIGH);
digitalWrite(ledPinwhite, HIGH);
digitalWrite(ledPinyellow, HIGH);
digitalWrite(ledPingreen, HIGH);
buttonPushCounter =0;
}
}
Hi,
Fine, can you please post a picture of your project so we can see your component layout?
Especially a picture of the relay assembly so that we can identify it from the many relay modules on the market.
See attached Picture. I am using a very large power supply that puts out 12V and apppears to be regulated. It is overkill at the moment but the 12v lights it will be switching on the high side of the relay will be using this power supply also.
The relay is one of those cheapo amazon 4 bank relays.
So far, the relay modules I have used with Opto-isolation have had, on those modules, LED's to indicate power and relay energized. Because, I'd guess, the OP is using the proper relay modules with opto-isolation, the LED's would be redundant, w the 1K resistors.
Oh, just saw the posted pic, I noticed the relay modules power LED is not illuminated.
That is true it does have a LEDs built-in that illuminate when the relay is activated. I could remove the LEDs on the Arduino side for simplicity.
That picture does not show it illuminated but when I connect the low side of the relay to the Arduino they automatically activate. Regardless if the pin is higher low.
Ah ha! That was it! I hooked the low side of the relay to the Arduino 5V and ground and it actuated when the pin went low.
I got confused because on the Amazon reviews people said you need to power it off a separate power supply and that the Arduino would not supply enough power to switch the relay.
I will work on it tonight and get the whole thing built and see if it works as a whole, then I will post the project.
If it is a 5 V relay module, you need a 5 V power supply. You connect "JD-VCC" and "GND" on the relay module directly to the 5 V power supply by a pair of wires; when I say "pair", I mean they travel together, such as using a sort of "figure 8" cable.
You connect from the power supply to your Nano using a separate pair of wires, to ground and the "5V" pin. You do not use "Vin" - it is useless.
You connect your Arduino outputs and 5 V to the relay board "IN" pins and "VCC" with ribbon cable (5 wires together). This separates the relay power supply from the Arduino power supply in order to avoid the switching current of the relays interfering with the Arduino, making use of the opto-isolators.