Relay switch between 2 power source

Hi, I'm doing my project relay switch between 2 power source but im not sure about the connection
And this is my code


// Pin definitions
const int relayPin = 7; // Relay control pin for switching between municipality and solar
const int primaryVoltagePin = A0; // Primary voltage sensor pin (Municipality/Eskom)
const int solarVoltagePin = A1; // Solar panel voltage sensor pin

// Voltage thresholds
const float primaryThreshold = 4.9; // Threshold to confirm if the primary power is "ON" (slightly below 5V)
const float offThreshold = 0.1; // Threshold to detect when power is "OFF" (considered near zero)
const float solarLowThreshold = 2.0; // Solar panel low voltage threshold (too low to be used)
const float solarHighThreshold = 3.0;// Solar panel high voltage threshold (sufficient to be used)

void setup() {
pinMode(relayPin, OUTPUT); // Configure relay pin as output

// Start with primary source assumed available at startup
digitalWrite(relayPin, LOW); // Activate primary power

// Initialize serial communication for monitoring
Serial.begin(9600);
}

void loop() {
// Read and convert voltage readings
float primaryVoltage = analogRead(primaryVoltagePin) * (5.0 / 1023.0) ; // Voltage divider for primary
float solarVoltage = analogRead(solarVoltagePin) * (5.0 / 1023.0); // No divider for solar

// Display voltage readings on the Serial Monitor
Serial.print("Primary Voltage: ");
Serial.print(primaryVoltage);
Serial.print(" V, Solar Voltage: ");
Serial.print(solarVoltage);
Serial.println(" V");

// Decision-making based on voltage levels
if (primaryVoltage > primaryThreshold) {
// Primary power is available, switch to primary
switchToPrimary();
} else if (primaryVoltage <= offThreshold && solarVoltage > solarHighThreshold) {
// Primary power is off, but solar is sufficient, switch to solar
switchToSolar();
} else if (primaryVoltage <= offThreshold && solarVoltage <= solarLowThreshold) {
// Both sources are off, power off the system
powerOff();
}

delay(1000); // Wait 1 second before the next reading
}

// Function to switch to primary power
void switchToPrimary() {
digitalWrite(relayPin, HIGH); // Switch to municipality power
Serial.println("Switched to Primary Power (Municipality)");
}

// Function to switch to solar power
void switchToSolar() {
digitalWrite(relayPin, HIGH); // Switch to solar power
Serial.println("Switched to Backup Power (Solar)");
}

// Function to power off the system
void powerOff() {
digitalWrite(relayPin, LOW); // Turn off the relay
Serial.println("All Power Sources are OFF");
}

By looking at picture, i am not sure either. But you have the actual system there and can see it.
Please draw an actual schematic of your system so we can easily follow the connections.

Have you taken into consideration that during the relay switching time there will be NO power to your device?

1 Like

I moved your topic to an appropriate forum category as this is not related to the IDE…

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an « About the _____ category » topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the « How to get the best out of this forum » guide. The guide contains a lot of other useful information. Please read it to know how to add code tags to your post…

Thanks in advance for your cooperation.

You cannot drive that relay directly from an Uno output pin. You will need a transistor driver with a flyback diode

No, your wiring is not correct. You hav not connected the negative terminal of either power source to the GND terminal of th Uno. Without these connections the Uno cannot make an accurate measurement of the voltages.

Both functions turn the relay on.

By spending too much time researching, I found that your relay coil consumes 0.2 watts. Which is 40 mA at 5 volts. That is the absolute maximum current for 1 output pin. A proper drive transistor and flyback suppression diode are required as @jim-p has said.

There are likely more errors in your connections but you must make a proper schematic drawing of your circuit in order for us to make a determination of that. Your picture is called a wiring diagram and is very hard to determine what is being connected to what else.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.