Power supply controller programming

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.

Hope someone can help. Thanks

I have the hardware setup

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

void loop(){
  // read the value on analog input
  value = analogRead(analogInput);

  if (value >= 1023) {
  else if (value <= 0) {

  // Relay, on when vin >11.5v & <=14.5v, off when vin <11.5v & >14.5v
  if ((value <= 437) && (value > 538)) {
  digitalWrite(Relaypin, Low); 
  else if ((value > 427) && (value <= 538)) {
  digitalWrite(Relaypin, High); 
  // print result over the serial port
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2));
  Serial.println(" volt");

  // sleep...

(Minor note on forum stuff: When posting code, hit the "#"-button and place your cut-n-paste code between the code-brackets)

"Wonder if it will work" .... well, why do you not try it?

If you have a problem telling us what it DID do, and what you EXPECTED it to do, then the answer will be more helpfull :)

BTW, for values which never ever change (until you recompile a new version) add the keyword const like

const int Relaypin = 4;

this will help you find programming errors (if you in a lrge code forget it was non-changing) and may consume fewer bytes of the precious RAM store.

if ((value <= 437) && (value > 538)) {

I don’t think that, in my universe at least, there are too many values for value that are less than or equal 437 that are also greater than 538.


Pauls, Don't get what you trying to say.

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?

Don’t get what you trying to say.

You are trying to turn the pin on if the value is less than 437 AND is greater than 538.

Pick a value. Say 250. That’s less than or equal 437, right? It is not greater than 538, though, so the pin will not be turned on.

What values will cause the pin to turn on? None, because there are no values that are less than 438 AND greater than 538.

Now, if you meant less than 438 OR greater than 538, you need to change && (and) to || (or).

If you meant something else, describe when you want the pin turned on, in English, with your hands in your pockets (not waving around).

Thanks for the clarification regarding my mistake. As you see i am not familar with programming logic..