I am using an Arduino Leonardo to control the on/off of a 8ch relay to control a light. In principle the code works fine with the relays, but when I connect the light to the relay the Arduino sometimes stops working and the L yellow light turns on. After a few seconds comes back again, to later turn off after a while once more.
At first I thought it was the power, so I used an external 12v power supply, didn't solve the problem. I tried switching the Arduino to and Arduino UNO, and the problem persists.
Any help will be highly appreciated.
thank!!
Here is my code:
// Constants
const int numRelays = 8; // Number of relays
const int potPins[numRelays] = {A0, A1, A2, A3, A4, A5 }; // Potentiometer analog pins
const int relayPins[numRelays] = {2, 3, 4, 5, 6, 7 }; // Relay digital pins
// Variables
int potValues[numRelays] = {0}; // Current potentiometer values
int frequencies[numRelays] = {0}; // Calculated frequencies based on potentiometer values
int delayTimes[numRelays] = {0}; // Delay times for on/off cycle
unsigned long previousMillis[numRelays] = {0}; // Previous millis for timing
void setup() {
for (int i = 0; i < numRelays; i++) {
pinMode(relayPins[i], OUTPUT); // Set relay pins as OUTPUT
}
}
void loop() {
for (int i = 0; i < numRelays; i++) {
// Read the potentiometer value
potValues[i] = analogRead(potPins[I]);
// Map the potentiometer value to the desired frequency range
frequencies[i] = map(potValues[i], 0, 1023, 1, 100);
// Calculate the delay time for the on/off cycle
delayTimes[i] = 1000 / (2 * frequencies[i]); // 2 times delay for on/off cycle
// Check if it's time to toggle the relay
if (millis() - previousMillis[i] >= delayTimes[i]) {
previousMillis[i] = millis(); // Update the previous millis
// Toggle the relay
digitalWrite(relayPins[i], !digitalRead(relayPins[i]));
}
}
}
Hint when the action changes when you change something the change is probably the culprit. I will take a SWAG and say per your non posted schematic the relay is drawing an arc which in turn creates EMI ( Electromagnetic interference) which in turn messes with your processor. I am assuming the relay is wired correctly. Check this link: Electromagnetic interference - Wikipedia It is also a big help if the logic and power wires are not run parallel and cross at right angles. Assuming the lights are mains voltage consider using electronic relays with zero cross.
Thanks a lot Stefan and Gils! changed the code to 6 but doesn't seem to be the problem. I was indeed wondering if it could be some kind of interference, I separated now the Arduino and the relay and the problem only occurs when the speed is at max, with most relays working. The funny thing is that it only happens when the light is connected to the output of the relays, there is no problem at all if there is nothing connected to the output of the relay. I am using a neon light btw.
Thanks again!
Check post #3 you are defining EMI. How fast are you cycling the relays? Check the CPM (Cycles Per Minute) assuming it is rated at 6 that is its design limit exceeding that will cause premature wear and failure. This link should help: Understand Relay Specifications to Get the Most Out of Your Switching System
No idea the link does not open. I think the best solution for you is to use the appropriate solid state relays and be careful about your lead dress. It is always better to eliminate the interference then trying to tolerate it.
Would like to see a connection diagram.
8-channel relay module, Leonardo, and 12volt supply raises some red flags here.
Is this a relay module with (optional) opto isolation, and/or are the relays 12volt. Weblink please.
Did you use a single supply for both devices, or the recommended setup with two separate supplies and no ground connection between the Arduino and relay module.
I don't think the relay is isolated, but they are 5v and I am just driving it with the 5v coming from the Arduino, for which I am using 12v power supply. I wasn't aware that it is recommended to have a separate power supply for the Arduino and the relay. Would that help?
A weblink or a picture of that relay module would have been better than words.
With a 12volt supply on the DC socket you can only safely have one relay 'on' at the time.
Two or more relays actively 'on' will overheat the 5volt regulator.
Something to consider:
My Crispy Critter Rules:
Rule #1. A Power Supply the Arduino is NOT!
Rule #2. Never Connect Anything Inductive to an Arduino!
Violating these rules tends to make crispy critters out of Arduinos.
Use a separate power supply for the relays and I believe your problem will go away.