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!
// 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);
}
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.
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!
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.
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?!
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.
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!