how to turn a relay off and on when voltage = x help ?

Hi,

Im a beginner on the arduino and programming !
I have been having fun learning the basics and now would like to attempt
to make a battery swapper !

I would like to start by swapping two batterries ( make the primary the charging and make the charging
the primary .

But im starting slowly , firstly Im learning how to monitor a 12v battery via voltge divider ,
and read the value and send it to serial monitor .

So i am experimenting ....

I have put a circuit together and monitor the voltage of a 12volt battery .
for testing purposes i added a trim pot to manipulate the voltage of the 12v battery myself.

I set it to switch relay off if sensorValue is below or equal to 400 .

this works , but i have a problem !
when the sensorValue reaches 400(or batterylevel) it fluctuates as it will be under load .
so sensorValue will be at 400 399 401 for a while and the relay connects and disconnects !

how do i turn it off once and only allow the relay to turn back on once i tell it to ?

Iv gone into dumb mode thinking about it ,

I hope iv explained my problem , if not let me know .

any help with direction is appreciated .

I know i can do this by time and not by sensing voltage , i would like to see if its possible
by reading the value of the voltage and switching relays on or off when voltages get to
a set level .

Hais.

// These constants won't change.  They're used to give names
// to the pins used:
const int pot = A0;  // Analog input pin that the potentiometer is attached to
const int redled = 9; // Digital output pin that the LED is attached to
const int yellowled = 2; // LED yellow on digital pin 2
long previousmillis = 0;
int redstate = LOW;
int yellowstate = LOW;
int sensorValue = 0;        // value read from the pot
int outputValue = 0;  
int val = 0;
int volts =0;
int relay = 11;
int relaystate = LOW;

long interval = 300;


// value output to the PWM (analog out)

void setup() {
  pinMode (redled, OUTPUT);
  pinMode (relay, OUTPUT);
  pinMode (yellowled, OUTPUT);
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(pot);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  unsigned long currentmillis = millis();
  

    if (currentmillis - previousmillis > interval) {
      previousmillis = currentmillis;
      
      if (redstate == LOW) 
      redstate = HIGH;
     
      else 
      redstate = LOW;
      
      digitalWrite (redled , redstate);
    
    }
    
    if ( sensorValue >= 400) // if volatge is over x volatge 
    {
      digitalWrite (relay, HIGH);
      digitalWrite (yellowled, HIGH);
    }
     else {
    digitalWrite (relay , LOW);
    digitalWrite (yellowled, LOW);
   
     }
     
 
  // volts = (sensorValue / (sensorValue/11);
  Serial.print ("Volts To be termined \t  ");
  Serial.print("SensorValue  ");
  Serial.print (sensorValue, DEC);
  Serial.print("\t outputvalue ");
  Serial.println (outputValue, DEC);
  
 delay (300);
 
}

Moderator: Edited to put code in box. AWOL

I know i can do this by time and not by sensing voltage , i would like to see if its possible
by reading the value of the voltage and switching relays on or off when voltages get to
a set level .

Of course it is possible. While you are reposting code, though, consider that you might want for the voltage to get to/under a threshold, and stay there for a period of time, before switching. In other words, switching as soon as the threshold value is reached may not be the best option. Low voltage for a period of seconds would be a reason to switch, not just low voltage. The reason for this is that the Arduino will see a low voltage if something that the battery is powering turns on, but the voltage may immediately rise again, so swapping batteries immediately would be unnecessary.

      if (redstate == LOW)
      redstate = HIGH;
     
      else
      redstate = LOW;

Could be written as:

      redstate = !redstate;
  outputValue = map(sensorValue, 0, 1023, 0, 255);

It would be faster to compute the output value like so:

  outputValue = sensorValue / 4;

Thanks for the reply ,

your advice on the programming shortcuts are great thanks .

so you see my dilemma with this project .....

Yes If we set a level , and battery reaches to below this level for a certain amount of time ,
then make the switch , may work ..

but yes battery voltage will rise once load has been disconnect by relay when the switch is made .

So how would i keep this relay off , and only turn on when second battery makes the switch .
second battery will have same problem as the first .

right now im just testing it with a small incandescant 12v lamp which the ardunio switches off when
sensorValue is reached , but relay turns off and on when SensorValue is reached .

Hais.

So how would i keep this relay off , and only turn on when second battery makes the switch .
second battery will have same problem as the first .

The Arduino is obviously going to have to monitor both batteries.

If both are high, no switching is required.

If one is low, and the other is high, consider switching to the high battery. Switch only if the high battery is not the current battery.

If both are low, switch to the one with the highest voltage.

By reading only once a minute, you'll avoid the constant toggling back and forth between active batteries.

You can add a tolerance, too. If the difference between the readings is less than the tolerance, do nothing. That is, if one battery reads 399, and the other reads 401, and the tolerance is 10, the difference (2) is less than the tolerance, so do nothing. If one reads 388, and the other reads 399, the difference (11) is greater than the tolerance, so switch, even though both are low.

um , I better give you more info on my project ambition in the end , as right now im testing the
basics with just one battery and a lamp load , lamp is just my example of the load .

I have built a bedini SSG charger ! , its basically an efficient charger .
the bedini is able to run off one battery and charge a bank of batteries on the charge side .
ie
1 run battery and say 3 batteries being charged !

lets just stick with
1 battery running the bedini and 1 battery on the charge side .

Primary run battery will have 12.8v(for example ) and battery on charge may be depleted to say 11.8v

Just as a beginning , I want to just accomplish swapping these two batterys ,
when the Primary batterys is depleted to a certain voltage , make the swap.

Hais.

I was thinking of something like
initial set swop value to 0:

int Swop=0;

And then

if ( sensorValue >= 400) // if volatge is over x volatge
{
Swop=1;
}

if ( Swop==1) // if volatge once has been over/under the thresh

{
make the swop
}

The swop value will remain 1, until you reset it to 0 (i think XD)

The swop value will remain 1, until you reset it to 0

It will. The battery swap will occur as soon as the voltage drops to 400/1023 * 5.0 (1.95 volts), even if that drop occurs only for a few microseconds due to load. If that's OK, go for it.

wow , ill give it a go and report back my findings ,

Thanks .

I was going to give up on it for a while , thinking i delved to deep to early , and i should
Practise more programming as im really a beginner , and then maybe return to it .

Sometimes i feel its maybe im getting older and my brain is going into dumb mode .
So i return to things that are easy to do and get them to work to stimulate my brain .
Helps me to start thinking . LOL

I was thinking of doing blinking without delay example (using millis) to swop batterys at predermined times .
and then once I figure out how to do it via reading the voltages to do it this way .

But thanks guys , keep me informed if you have anymore ideas .

Hais.

Hi paul ,

your suggestion

The swop value will remain 1, until you reset it to 0

The above works :slight_smile:

Because I only have one relay to play with at the moment ,
I coded my sketch to turn relay on and run a load (small 12v globe )
and when the voltage went below the sensor value

relay =1

if (relay ==1 ) {
digitalWrite (relaypin, HIGH);

I was also able to send the running time to serial port , this was intersting to
be able to see how long battery was able to maintain voltage with this load .
gives you an idea of the battery condition .

Thanks
Im going to try to find a relayboard (or shield ) .
Anyone know of where i should look in australia ?
Most places seem to be out of stock of relay shield .

I might have to make one up or buy a relayboard .

Hais.