5V relay keeps cycling on/off

Hey I’m working on a project that will take 12v power and trigger a 5v relay when 0v-5v is applied to AO pin on the Arduino Nano! But currently have everything setup on a breadboard and when I turn on power to the breadboard, the Arduino nano and relay power up but the relay is triggered every 5-7 seconds, on and back off? When the relay should only be triggered when anything more then 0.5v is put into the AO pin, Here is the wiring diagram I’m following! Appreciate any help or input on this project!

Code I’m using:


// Define pin connections
const int relayPin = 2;         // Relay connected to digital pin 2
const int inputSignalPin = A0;  // Input Signal connected to analog pin A0

// Define threshold and voltage conversion constants
const float voltageThreshold = 0.5;  // Threshold voltage to control relay (in volts)
bool relayLogic = LOW;               // Relay Working logic you can set it to HIGH/LOW

const float maxVoltage = 4.8;     // Maximum expected voltage (5V on most Arduino boards)
const int maxAnalogValue = 1023;  // Max analog read value for 10-bit ADC (0 to 1023)


bool relayOn = false;  // Flag to check if relay is on/off

void setup() {
  // Initialize serial monitor for debugging
  Serial.begin(9600);
  // Initialize relay pin as output
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, !relayLogic);  // Start with relay off
}

void loop() {
  // Read the potentiometer value
  int potValue = analogRead(inputSignalPin);
  for (int i = 0; i < 9; i++) {              // Take 9 more readings
    potValue += analogRead(inputSignalPin);  // Read value and add it
    delay(50);                               // wait for 50 milliseconds
  }
  potValue /= 10;  // Take average readingys

  // Convert analog reading to voltage
  float voltage = (potValue * maxVoltage) / maxAnalogValue;

  // Debugging: print the voltage
  Serial.print("Input Voltage: ");
  Serial.println(voltage);

  // Check if voltage is below the threshold
  if ((voltage <= voltageThreshold) && (relayOn == false)) {
    digitalWrite(relayPin, relayLogic);  // Turn on relay
    relayOn = true;               // Set relay flag to show it's on
  } else if ((voltage > voltageThreshold) && (relayOn == true)) {
    digitalWrite(relayPin, !relayLogic);  // Turn off relay
    relayOn = false;               // Set relay flag off
  }

  // Small delay for stability
  delay(100);
}

Provide a link to the particular relay you are using, please.

Disconnect the relay. Just remove it completely and put it in the living room.

If you have a resistor and LED, put it on the output pin where the relay was.

How does your sketch work then?

It is likely that the relay is taking too much power from the place you provide it, or is otherwise causing a glitch whoich makes you circuit reset over and over.

a7

3 Likes

What is the source of the voltage?

It’s just one of these guys from Amazon, may that be my issue?!

5PCS 5V 1 Channel Relay Module... 5PCS 5V 1 Channel Relay Module Low Level Trigger, Modules - Amazon Canada

It’s a throttle signal, 0.45v is idle, and 5v would be wide open throttle! That’s why I have it set to trigger relay at 0.5v so after throttle is pressed I’m wanting the relay to turn on!

Disconnect the throttle signal and connect 5 V to A0, see what happens.

No schematic of the relay board; that's unfortunate. I don't see an optoisolator, just what's probably an NPN transistor to (hopefully) boost the current to the coil.

And you are powering the relay from the Arduino's 5V pin? You might want to measure the current draw. It's possible it's enough to put the Arduino's 5V regulator into thermal shutdown. Engage, heat up, shutdown, cool down, reboot, repeat.

1 Like

The relay stops cycling and stays off,? When the relay should be triggered by anything over 0.5v into the AO pin!

When I put a multimeter to ground and the D2 pin which should activate whenever AO pin receives over 0.5v, I get 0.8v when the relay is triggered, and then cycles to 0.0v and relay shits off, so it’s working but I have nothing connected to AO yet, maybe it need some sort of signal or else the arduino sends a signs anyways?!

Yes. An open analog input pin will be reading the wind and reporting random values all over the place.

Wire it to a potentiometer configured as a voltage divider (google it).

https://arduinoinfo.mywikis.net/wiki/Brick-Potentiometer-Joystick

Or just ground it so it reads zero, or put a value in that fakes a value that would otherwise be analog read.

Also - you should power your relay module from a better source of 5 volts than the pin on the Arduino. it may work, but generally that output should only be used for small currents so it's a good habit to get into, not using it.

a7

1 Like

Okay okay that’s making sense now, since when I ground AO it turns the relay on like it should when 0.5v-5v is connected should do, In my first prototype not on the breadboard I have a 12v-5v buck connector, would it be better to power the relay off that, it has extra out ports!

If you mean ? instead of !

Yes, the 5 volt output of your buck regulator is a perfect place to grab the 5 volts for the relay and other things that need power.

Obvsly, I hope, you should keep the total demand for current well below the rating on the regulator.

Buck and boost regulators can be presented with aspirational specifications, I would stay below 2/3 the promise made.

Testing regulators with expected load is recommended.

a7

1 Like

Thank you everyone for the reply’s! Project is working like it should! Cheers

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