Hi,
I am a newbie, I have the hardware setup but not too sure about the coding at all.
I have found code on voltage readout and relay control separately but not sure how to combine them to make my circuit work.
I have the voltage comparator at analog-in pin 1 and relay controller hardware at digital out pin4. Need to turn on the relay to turn on the power supply(12v) when the analog voltage read between below 14.50v and turn off at 14.50v.
Need to turn on the relay to turn on the power supply(12v) when the analog voltage read between below 14.50v and turn off at 14.50v.
If you connect a 14.5 volt source to the Arduino, smoke will be released.
When you get your replacement Arduino, connect the 14.5 volt source to a voltage divider. Google that term, so you know what a voltage divider is, how to choose the resistors, and how to connect it and the 14.5 V source to the Arduino.
You use analogRead to read the supply voltage, as a value between 0 and 1023, where 1023 corresponds to the reference voltage (usually 5.0 volts).
You will need to know where on that 0 to 1023 scale your scaled down (thanks to the voltage divider) 14.5 volt value falls.
Whenever you get a value lower than that, turn on the relay’s pin (using digitalWrite()). When you get a value higher than that, turn the relay’s pin off.
Keep in mind that there is some jitter in the Arduino’s ADC, so a constant input voltage will result in an analogRead output that varies by 1 or 2. Set up some tolerance, so the relay is not being turned on and off too often.
Also, the relay may not be directly controllable by the Arduino, if it’s voltage and current levels don’t fit in the Arduino’s range. Having said nothing about the relay, I don’t know if yours does or does not fit.
Thanks for the reminder regarding the more than 5v connection to the arduino. I already figured the hardware part of it, the voltage divider and relay control stuff. The intention is to turn on relay when analogRead is above 11.5v and less than 14.5v but off when voltage is 11.5v and more than 14.5v. Need the startup relay setting to be off.
Just not sure how to code it, wonder if the following will work…
// variables for input pin and control Relay
int analogInput = 1;
int Relaypin = 4;
int prev = LOW;
int refresh = 1000;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // !! resistance of R1 !!
float R2 = 22160.0; // !! resistance of R2 !!
// variable to store the value
int value = 0;
void setup(){
// declaration of pin modes
pinMode(analogInput, INPUT);
pinMode(Relaypin, OUTPUT);
// begin sending over serial port
Serial.begin(9600);
}
void loop(){
// read the value on analog input
value = analogRead(analogInput);
//Serial.print("value=");
//Serial.println(value);
if (value >= 1023) {
Serial.println("MAX!!");
delay(refresh);
return;
}
else if (value <= 0) {
Serial.println("MIN!!");
delay(refresh);
return;
}
// Relay, on when vin >11.5v & <=14.5v, off when vin <11.5v & >14.5v
if ((value <= 437) && (value > 538)) {
digitalWrite(Relaypin, Low);
delay(1500);
return;
}
else if ((value > 427) && (value <= 538)) {
digitalWrite(Relaypin, High);
return;
}
// print result over the serial port
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
//Serial.print("vout=");
//Serial.println(vout);
Serial.print(vin);
Serial.println(" volt");
// sleep...
delay(refresh);
}
No, i am just modifying a code i found. I hope it would do what i want but i am really not sure. Was hoping if someone who with more experience can give some help look if the code would work as per what i wanted it to do and if there is a simpler or better way to do it.
Regarding why i have not try it. I am still building my LM317 based variable power supply before I can test the hardware.
And I just added the bracket for the code instead of whatever this was trying to say cos i cannot figure out what is the deal with hit the #-button:(Minor note on forum stuff: When posting code, hit the "#"-button and place your cut-n-paste code between the code-brackets) Is this a mac vs pc issue?