Anotherpower.com Forum

Renewable Energy Questions/Discussion => Solar (heating or electric) => Topic started by: DJ on December 18, 2016, 01:57:34 am

Title: Solar to electric Water heating.
Post by: DJ on December 18, 2016, 01:57:34 am

I would like to make up a controller / converter with an Arduino to take the (excess) output from my solar panels and use it on an old resistance  water heater.

There is -some- detail I have seen of this on another forum although the poster is an expert on such things and just posts some code and expects others to work out the components and circuit involved.  I'm way too much of a beginner for that But if given the circuit, parts and code I could probably nut it out.
I understand the power can be fed direct to a normal 240V element as we use here buy way of PWM to get the panel voltage and the element resistance matched.

I have spent hours today trying to find something like this but apart from what looks like some grossly overpriced commercial components that look very simple, I can't find anything like I want. Plenty of circuits to control a water pump on an an absorption heater but nothing to convert Low voltage Panel output to drive a standard heating element.  And please dear god, no need to tell me that this is inefficient or whatever else, I have read it 50 times already.  It IS efficient when I have power I have no other use for and an old electric HWS sitting round rusting.

I plan to use this initially anyway as a pre heater for the regular HWS.  The solar may only get the temp of this heater to 40-50 o and that feeds into the regular tank making the power I have to pay for from the grid to get it to full temp, a lot less than it would be otherwise and I am still guaranteed a full hot shower. I was also thinking that there is no reason this could not also power an electric stove element. Just needs to be sufficient power as a resistance element is a resistance element no matter how you apply it.  Blow some air across said element and you have a space heater.

If anyone knows of or has info or links to a suitable circuit with all the info that a beginner can build, I would really appreciate the heads up. Right now I don't even seem to be able to come up with the right name of what I am searching for.
Title: Re: Solar to electric Water heating.
Post by: frackers on December 18, 2016, 04:28:01 am
The first thing to think about is the voltage you have available from your panels and what the heater requires. Here is an example:

If the heater element in the tank is 220v and you have 8 panels of 250 watts each available, then assuming they are 60 cell panels with a maximum output at about 28 volts then just wire them in series and get a relay that can switch 220v DC (MUST be DC rated) and energise it  via the existing thermostat in the heating element. So that is 2kw at about 220volts and all its cost is the panels and a relay.

This extra relay is required because when contacts are broken on a DC circuit an arc starts up. On AC, the arc dies out due to the voltage changing all the time - on DC it doesn't "quench" and you end up with heating in places you really don't want. The thermostat contacts will be rated for the full power of the element but will be AC rated but they will be fine for the small current that a relay draws.

When the tank is up to temperature, be aware that the open circuit voltage from the panels will be up to 8 x 40 (i.e. 320) volts - another reason for having a suitably rated relay. This voltage will pull down to 220 or below when the contacts close on the tank cooling.

With a smaller solar array  upconverting the voltage a bit (but not too much otherwise you'll 'use up" the current output capacity of the panels and end up with a lower voltage) to balance the load of the heater to the panels output.

With a larger array then its making sure  you don't burn out the heater.
Title: Re: Solar to electric Water heating.
Post by: DJ on December 18, 2016, 08:15:56 am

I was thinking of something more like a KW and maybe 150V or less driving the heater.
I am under the highly likely incorrect impression that by using an arduino somehow to create a PWM output, the 240 element can be made to use all the current available at whatever amps it is at regardless of the voltage.

My understanding was that if you have say 1 KW @ 150V from the panels and feed it into the 240 element,  The element would only supply 250 w or whatever heating power. If you modulate or tweak the output from the panel, you could get the full 1Kw even at a much lower voltage.
The commercial unit I have seen on the net looks very small and basic and I thought this was how it was done as was the other thread I saw that only had the arduino code and was missing the rest of the info in the puzzle.

As far as the arcing of the relay/ thermostat contacts, you are saying to set up so the  thermostat switches the relay which is connected to the element?
I have bought some SSR 40A relays recently which are DC to AC. Would they be OK or is something else required? Any Difference with AC and DC being they are solid state and I imagine nothing to arc?
Title: Re: Solar to electric Water heating.
Post by: Pete on December 18, 2016, 02:54:13 pm
Hi DJ, just wondering what sort of solar system you have. Is you system a stand alone battery setup or only a grid connect?
If you have a stand alone system there may be a simple way to do what you want.
You could use your regulator to switch the heater on. It would depend on what sort of regulator you have.
When the regulator switches to float you could use that signal going to the LED that indicates that to drive a relay that switches the panels to the water heater.
You could use your arduino for that, just program it to read the input voltage from the LED, and switch a relay on to power the water heater. Of course you would leave the thermostat in the heater so that you don't boil the tank.
There are low voltage elements available, I have seen 12 volt ones, not sure if there are other voltages too.
Otherwise you could use a cheap square wave inverter to drive the element. As it is a purely resistive load it won't mind a square wave driving it.
Hopefully others will come up with more ideas.
Pete
Title: Re: Solar to electric Water heating.
Post by: 144VDC on January 14, 2017, 01:13:35 pm
Hi DJ,
I have a stand alone (off grid) solar system and use my excess power to heat my domestic water. I have a 7000 watt array and most days 1/4 to 1/2 is excess. I use a pwm shunt regulator, that I built, to control my charge voltage. It shunts the excess power to a 120 gal. hot water tank with three 240v 3500w (10500w) elements. This system keeps us in hot water all the time.
I have a 144v battery bank, so my charge voltage is 174v and float voltage is 160v. Heater elements are just big resistors; the more voltage the more current and therefore the more wattage up to the point they burn out. My point is, any heater can be used at any voltage up to a maximum where the watt density is too high and it burns out. My elements are 4655watts @160v and my pwm duty cycle drops it down from there to whatever is needed to maintain the battery voltage.
Title: Re: Solar to electric Water heating.
Post by: DJ on January 14, 2017, 08:46:48 pm

I figured a work around which seems to be doing the job well.

I couldn't work out anything with the arduino so went for a cheap, bulky and effective solution instead.

To upsccale the voltage I have been using a UPS coupled to a pair of batteries. The UPS I already had and is 1000W rated. As the element in the HWS is 3600W, I purchased a PWM controller from fleabay for about $11 I think it was.  This works great as I can vary the load through the PWM so the 3600W element does not overload the 1000W UPS.

The setup is basicaly an off grid system which I am just playing and learning with. I have a Cheap but really effective PWM solar controller which allows me to set battery voltage and load on/ load off voltages as well.  I have this driving a 25A SSR which switches the AC PWM in and out according to battery charge.  When the batterys hit their pre designated float level, the controller turns everything on and the water starts heating. When it drops, the heater switches out.

ATM I am using the setup to drive a small fridge and an Urn for hot water for my  8 or so daily coffee's. It's working brilliantly  for that.  I have instant hot water all day long and the urn having it's own thermostat stops the water boiling away.
The Ac PWM is rated to 4 KW so with a suitable size inverter or UPS, could do a water heater with no problem.

My Learning solar system ( which it is doing well) is only 2 panels for a bit over 500W rating but with this setup I can drive loads well over it's rating.
I'm just using some car batteries atm which I know are not right but I have the controller set so I don't take them under 12V and really only use the flat charge capacity rather than drain them to any appreciable degree.

I am looking to get a lot more used panels and just grid back feeding from there although I think I'll expand my play system to about 1 KW. I'm enjoying playing and experimenting with it and there isn't much of that in a regular home/grid feed system.
I also want to set up at least one of my diesel engines with an induction motor so I can use that for backfeeding and If I can ever find a proper stand alone generator head at a reasonable price I'll do that too for the once every 5 years blackouts we have!  :0)
Title: Re: Solar to electric Water heating.
Post by: eraser3000 on January 15, 2017, 07:38:02 am
DJ,
I have had a go with this kinda thing for awhile now and have a few questions.

Have you tried actually varying the load with the PWM controller, while load was connected to the UPS?

I have a hard to believing that the UPS is handling the load being pulsed on and off without out tripping out.

If you have, what is the PWM base frequency and which SSR are you using?

Thanks in advance.

Eraser3000
Title: Re: Solar to electric Water heating.
Post by: DJ on January 15, 2017, 11:30:42 am
DJ,
I have had a go with this kinda thing for awhile now and have a few questions.

Have you tried actually varying the load with the PWM controller, while load was connected to the UPS?

Yes, many many times.  I can up the output when it's sunny and the other day I had it running at just 125 watts on the cloudy day which didn't boil the kettle all day to my surprise.  I match the PWM output to the power the panels are producing and go out and adjust it many times a day. I try to set it so as to maintain a constant value on the batteries but sometimes I turn it up so the batteries drain to their very low level drain before the controller kicks out and recharges the battery's so the process cycles. I have put in an amp meter and can gauge by the load gauge on the UPS how much I am putting out.  The other day I had it running at .5A and I have had it up to 5A which is the UPS limit.

Quote
I have a hard to believing that the UPS is handling the load being pulsed on and off without out tripping out.


Mate, you believe what you want. I have been doubted loads of times on things I have done over the years usually by people that think unless something is expensive, complicated and with a shipload of failure points built in, it can't work.  get that every single day on what I have on my YT channel even though 50 other people have built my designs and posted them working and more still have told me about what they have built and done with them.
 
I'm not trying to sell you anything or claim I'm smarter than anyone else, just telling people of whom I know there are some out there like me that don't have the knowledge to build one from an arduino, the cheap and low tech way I found of making it work using off the shelf bits cobbled together.

Quote
If you have, what is the PWM base frequency and which SSR are you using?

No idea on the frequency. Didn't even give it any thought. I bought the thing as a prebuilt unit I just had to wire up off ebay.
Initially I wanted it to control the blowers on my oil burners but I didn't like the way it made them buzz and I wasn't sure if some of them were universal or Capacitor start and didn't want to fry the windings.

What I needed for the heater was a way firstly to ramp up the 24V DC to 240V Ac. Been doing that for years with inverters for my work on location and had some UPS units I was given when the batterys were stuffed which were better because they were pure sine wave where all my cheapie inverters were square.

The next thing was how do I limit that current?  Water heater is 3-6Kw here.  Didn't matter if I didn't give it full tilt, just have to give it what I can which will be the inverter rating and not overloading it.  Logical way, chop the power. What do I have or can get to do that?  Motor speed controller.
Now all I need to do is switch the thing off when the sun goes down or a cloud comes over so I don't drain the batteries and the UPS shuts off which won't restart automatically.

The cheap arse PWM solar controller has that function built in like many of them do so all I need to do is get a relay that has a Low DC side and an AC high side. Initially bought some of the little blue mechanical relays but then saw the SSR's so bought them which arrived first.
Cobbled it all together and I have now made a board up with it all on so it doesn't look a complete dogs breakfast and that was it. Put in a breaker for the panel feed and another one on the battery charge and that was it. UPS feeds direct through some joined car battery cable.  As it's going to pull around 100A at full load, it's unfused and I couldn't find a breaker nor want to muck around wiring up multiple on a bus bar.

 The SSR is one of those white square ones on fleabay. I think I have a 25A unit connected but it could be a 10. I bought a few different capacity's of the same output and wired it up and put it in a junction box so I didn't accidentally fry myself. I have seen it mentioned they should be heat sunk but this one never gets much more than being detectably warm. Maybe because it's being so under driven even if it is a 10A unit. 
Title: Re: Solar to electric Water heating.
Post by: lighthunter on January 17, 2017, 12:18:49 am
Hi DJ are you using a pwm similar to this? I think it switches at a terribly annoying 2khz when connected to a motor winding.  ;D.

I tried using a triac based control which varies the voltage of the "switch on" point in the AC cycle.
It worked ok and the 24v pj inverter had no issue with it but my grid tie hated it. Therefore i discontinued using it.

Now what you are doing appears to be a little different. Rather than firing on every half -cycle (at varying time point); you are likely skipping whole cycles and conducting complete cycles alternately. I'm guessing only so anyone who understands pwm driven ssr's in a sine wave application, please confirm or enlighten us.

Neat idea, I might have to try it. Something tells me my grid tie wont like it either. They wrote some very clever analyzing software in it. Almost maddening.
Title: Re: Solar to electric Water heating.
Post by: eraser3000 on January 17, 2017, 04:49:03 pm
lighthunter, What size load are you trying to run? I have found that the load needs to be 1/5 of the inverter's capacity or less. So if you have a 1000 watt inverter, try 200watts per SSR and only fire SSR 2 if SSR 1 is fully on, and SSR 3 if SSR 2 is fully on. Once your GTI is at load it can handle more, so you would then only be bashing it with 200watts at a time.

DJ you mentioned you are using one of the "white square ones on fleabay". Those are usually only good for the half cycle firing at max. Usually your PWM controller output goes high then the SSR sees the signal and there is a delay after the delay SSR waits for zero cross point on sine wave and SSR goes fully closed. If the PWM controller then goes low SSR has another delay and then waits for zero cross point to "open up again".  Usually that means out of your 120 half sine points per second and the added delays in there (10-30 ms) you are only getting about 40 points you will realistically close or open on. With Random SSRs you get alot more control and little delay but they can cause some noise issues.
Title: Re: Solar to electric Water heating.
Post by: lighthunter on January 17, 2017, 08:06:53 pm
lighthunter, What size load are you trying to run?

It wws either 3000 or 2000w element, probably the 3. I didnt use ssr though.
I dont own one as of yet. I was using the triac fire angle delay circuit which produces nasty switching harmonics every half cycle. Interesting ideas you have. I will need to play with it again
and see if i can get it to behave better. I do have my house loads sequenced as you suggest but was trying to adjust triac dimmer at a reasonable point and cycle it on and off with plc. It was the on off moments the gti didnt like. You got me thinkin now.

Title: Re: Solar to electric Water heating.
Post by: DJ on January 21, 2017, 04:16:02 am

DJ you mentioned you are using one of the "white square ones on fleabay".

This is what I am using. I did not look up the technical details.  Don't know, don't care, not interested.   ;D
It's working just fine and that's what I WAS interested in.
I think the one I may be using is only 10A if that makes any difference. I bought 4 of them in different sizes and I think the one I put in the box was a 10 as thats our full 240V std socket amperage anyway and far more than the UPS will hold.

As I said, just got the off the shelf bits and pieces and cobbled them together and they did exactly what I wanted. I'm afraid I'm pretty electronically ignorant. I want to learn about it but it seems so overwhelming. While I can buy off the shelf boards and controllers and get them to do what I want, I'll remain happy.
That said, I just opened another 2 Arduino Nano's I ordered.
Wondering If I'll ever use the things now.  :0)

(http://i.ebayimg.com/images/g/~PkAAOSw-0xYa2AC/s-l1600.jpg)
Title: Re: Solar to electric Water heating.
Post by: DJ on February 03, 2017, 06:31:48 am

I had a score on fleabay on the weekend and got myself a good brand 2000W 24V pure sine wave inverter with remote control Sell said it was never used and I totally believe it. Price paid, Just under $125. Price it is still being sold in the store for, $1525.00  Bargain.

I hooked it into my test system to replace the UPS I have been using.  With the doubt cast here I was a Little apprehensive about putting it on the system with the PWM but I did it anyway and works without any problem what so ever.
I can boil the kettle at 1800W ( I purposefully allow some margin) and the thing is quite happy as long is the sun is shining to stop the batteries dropping too fast and the low voltage protection cutting in.

Clearly its not a fluke and the setup is creditable. All I did was unplug the UPS, Plug in the inverter, fire it up and it worked exactly the same.

For anyone wanting to drive a resistive load from panels over their rating and with no electronics fabrication knowledge, this is an easy and low cost way to do it.
Title: Re: Solar to electric Water heating.
Post by: eraser3000 on February 07, 2017, 07:18:18 pm
That is great DJ, I'm glad it worked for you. The reason I asked is just that others including me have had issues with several  brands of inverters, that just aren't happy with massive loads being turned on and off rapidly. 
Which inverter model did you just pick up that is working really well?
Again, glad its working in your application, that is the overall goal with all this technology, meeting our needs.
Cheers
Title: Re: Solar to electric Water heating.
Post by: DJ on February 08, 2017, 04:53:07 am

This is the one I got.
https://www.jaycar.com.au/2000-watt-24vdc-to-230vac-pure-sine-wave-inverter/p/MI5712

Been running it about a week now and it's working perfect. No issues with the PWM at all. The remote for the inverter has a display in Volts, watts and amps so I can align that with what the solar controller tells me the panels are producing and go a bit under so I can pump the max power into the Kettle. I got a little multi display meter off ebay so I'll wire that up into a box so U can use it to see the loads things are drawing or producing for a bit more info.

Maybe I should buy some more of the PWM controllers as they work and others don't seem to?
I'm thinking that it might be the pwm's that have caused problems others have seen rather than the inverter? I have a couple more inverters up the back, Might drag them out and see how they do. If they are OK, would suggest it is more the compatibility of the PWM than the inverters driving them.
Title: Re: Solar to electric Water heating.
Post by: eidolon on February 09, 2017, 04:37:06 pm
You ought to give those NANO a try.  I used to build stuff and now it is just easier to do it with a NANO.  Seen some youtube videos where they use an inverter to power a heating element.  They don't talk about the control much.  So are these PWM controllers the ones for 24V DC motors?  I would think that frequency is too high for consistent triggering at 50Hz.
Title: Re: Solar to electric Water heating.
Post by: eidolon on February 10, 2017, 08:37:41 am
I thought about this last night.  There are more than a dozen ways you could do this ending in about the same result.  An inverter is the practical way to do this since you would need a considerable array voltage to do this directly considering the loads you have.  The nano should be given a try to automate this.  The programming would be rather simple and I could supply tested code to copy and paste. The code could be this simple.

Read battery voltage

If (Battery > 28.00) SSR = 1

If (Battery < 26.80) SSR = 0

digital Write (SSR)

Serial print data to adjust calibration

Delay 5 seconds

So, every 5 seconds it is decided to either turn on or off the SSR.  Actual voltage transitions could be determined later.  Not an authority on battery chemistry, but you need to get up around 14V to get a full charge and long battery life.  I don't know if your controller is three stage and it may not matter.  An actively used battery seldom gets out of bulk charge mode like a car.

You have the nano. SSR can be wired directly to it.  A voltage divider with a pot for calibration is needed to monitor battery.   Easiest power source is a phone wall wart, just make sure it is actually 5V as some are not regulated.  The inverter will always be on when this is used so plug into that.

NANO - Like saying you have a car, it has four wheels.  China will put any label they think will sell.  It may not have a bootloader or USB or even be UNO based.  Find a usb cable and plug it into your computer.  Universally they all seem to be loaded with BLINK program.  If it flashes you are good to go.  Don't solder in the supplied connectors.  They will be the bain of your existence.  Just take resistor leads, fold them over, crimp and solder.

Download  Arduino software then try downloading the CH340 driver.  There are instructions on the net.  I have no memory of how I did it.  Find BLINK program and edit it to make delays 4 times longer.  This will prove you can communicate with the NANO.  In TOOLS select board you are using first. 
Title: Re: Solar to electric Water heating.
Post by: eraser3000 on February 10, 2017, 09:51:22 am
Actually in reality it gets a little more complicated.  Really you want to hold the voltage at float. so if that number is 27.9 then you want something like a PID adjust PWM to maintain 27.9.
So you would want to evaluate a minimum of 2-4x your adjust rate.
I don't use Arduino's much, but this page has some general PID information and implementation on an Arduino of some kind.
https://create.arduino.cc/projecthub/mjrobot/line-follower-robot-pid-control-android-setup-e5113a

void PID()
{
  P = error;
  I = I + error;
  D = error-previousError;
  PIDvalue = (Kp*P) + (Ki*I) + (Kd*D);
  previousError = error;
}

Using something like below can get you into some problems.
The 5 second delay helps.

If (Battery > 28.00) SSR = 1

If (Battery < 26.80) SSR = 0

What you would probably use is analogWrite()

https://www.arduino.cc/en/Reference/AnalogWrite

And use the PID output value as your write value.


Cheers
Title: Re: Solar to electric Water heating.
Post by: eraser3000 on February 10, 2017, 10:07:35 am
That being said, a simply on at X and off at X will work just fine on smaller loads.
Title: Re: Solar to electric Water heating.
Post by: eidolon on February 10, 2017, 03:01:38 pm
"What you would probably use is analogWrite()"

Not at all. You would be mixing two frequencies. I used to make light shows this way. It isn't too bad if you PWM at 20 Hz. but that gets less than 3 bits of resolution averaged. It just might drive an inverter nuts.  The battery acts like a buffer.  Just trying to do something a beginner would understand. Like to keep it under 5 components or people get intimidated. I'd do PWM right off the DC buss of the inverter or modulate the boost. I bought a stack of 2,000W inverters and half of them I just ripped out the damaged H bridge section. Most entertainment electronics today work fine off HV DC.  I PWM my water heaters directly off the PV array.
Title: Re: Solar to electric Water heating.
Post by: DJ on February 11, 2017, 06:49:53 pm
I PWM my water heaters directly off the PV array.

Could you explain how you do that?

I saw a program for an arduino to do that but unfortunately the Wizzard that put it up expect people to be able to figure out the circuit and components for themselves which I am not anywhere near up to doing. I have a bunch of arduinos I bought but a little disillusioned.  I can't find the sort of circuits and programs I want.  I know I need to spend more time learning them which I intend to do but right now trying to pack up house and find new digs takes priority. I do have time to cobble the bits and pieces together as I have but to concentrate on learning something new is going to take a better and more relaxed state of Mind.  :0)

I will try and get the circuit below working.  At the present time I have a token battery system and would like to kick the power into the UPS at night or when the batterys get low instead of having to plug the ups in and out all the time.
This simple circuit looks like it would do the trick nicely.

I take it I would reverse the Commands?  IF > 28   SSR =0
                                                               <26    SSR=1

If you had the program and Pin out for the board I'd be very much interested to see it.
Title: Re: Solar to electric Water heating.
Post by: eidolon on February 11, 2017, 07:23:24 pm
You are in the land of 240V. In the US they have small office type water heaters that provide water for just a sink.  These use 120V heater elements and 2,000W elements are available and cheap. Your heating elements will require a much higher array voltage than is common.   In doing PWM, a capacitor stores energy and releases at a later time. Say an array can supply 5A and the PWM duty cycle is 50%.  When the FET turns on there is 5A from the array and an extra 5A from the capacitor.  Just connecting a fixed resistor to the array would greatly drag down the voltage.  By pulsing it it can be supplied with full voltage and current.  Controlling the duty cycle keeps the panel at the optimum voltage.

How much voltage can your PV array supply?  With 240V heaters, dropping the supply voltage to half that (120V) reduced the power to 1/4. dropping it to 60V would be 1/16.  That makes standard heaters pretty useless.

Using an inverter like you do solves some of that issue. It takes a battery to stabilize it.  As said before, batteries long term will not like it if they can not be charged to well over 14V periodically.  If the charge controller can supply enough current, you can keep it at close to 29V just like a bulk charge in a car. Not ideal, but you can get 5 years out of it. The program I suggested waits till voltage is high enough and then looks every 5 seconds to see if it has dropped too much, a slow PWM.

I'm traveling right now but in a few days I will write the program and list it here.  Then we can work on building it.
Title: Re: Solar to electric Water heating.
Post by: DJ on February 12, 2017, 05:00:38 pm

Excellent, Thank you!
Title: Re: Solar to electric Water heating.
Post by: eidolon on February 14, 2017, 02:35:41 pm
Here is the working program.  Load it into a sketch and rename it before saving.  If it does not compile too much or too little was copied.  The  program checks the battery every 5.5 seconds. Shorting pin 12 to ground speeds up the read to calibrate voltage divider every .5 seconds.  Try to use a resistor to the the battery 47K or higher to prevent possible possible to the micro from over voltage. Use a 2K to 5K pot to adjust voltage. This is a convenient site to calculate voltage dividers.

http://www.raltron.com/cust/tools/voltage_divider.asp

  For  Vin 28V input, R1 = 47k add 2.5K for a 5K pot, so 49.5K for the calculator value.  V out is 3.3V, chosen because it is also available on board for testing.  Using the calculator R2 is 4.71K.  Subtracting 2.5K for the pot gives 2.2K for the lower resistor.
V out is 3.3V, chosen because it is also available on board for testing.  Using the calculator R2 is 4.71K.  Subtracting 2.5K for the pot gives 2.2K for the lower resistor.  Just measure a known voltage and adjust the pot till the screen in tools measures that voltage.

The programming is very basic, but using this as a boiler plate other functions can be added.  Clicking on TOOLS, select the correct board.  Selecting serial monitor will allow you to see the data coming out of the board.



// SIMP_DIVERT_24 for Heater
// relay out PIN #10
// This is a simple SSR driver that turns on at
// at a fixed voltage and off at a lower
// Samples four times and adds those values together.
// This number is in tens of mv.
// Data is sent out serially to allow calibration
// every half second due to delay.
// Typical operating voltage gives an A/D count of about 750
// Divider values to create 3.3V
// Normal delay is 5 seconds. grounding pin 12 makes that faster.
// LED lights once per loop and when relay on.
// created 02/14/17

int battery   = 0;      // calculated  voltage in mv
int SSR       = 0;      // relay output


void setup() 
{
 Serial.begin(9600);    // setup serial port speed for data in TOOLS
 pinMode(13, OUTPUT);   // sets the digital LED pin 13 as output, onboard LED
 pinMode(10, OUTPUT);   // SSR relay out
 pinMode(12, INPUT);    // ground for 1 second loop
 digitalWrite(12,1);    // enable internal pullup (early version compatable)
}

void loop() 
{                                   // this is the start of the program
 
// turn LED pin 13 ON for once per loop blink
digitalWrite(13,1);                 // blink while reading data

// A resistive voltage divider produces about 3V3 from battery
// READ ANALOG VALUE FOUR TIMES AND ADD TOGETHER
// A/D values go from 0 to 1023

battery = analogRead(2);            // get first sample
delay(20);                          // delay for 20ms
battery = battery + analogRead(2);  // get second sample
delay(20);                          // delay for 20ms
battery = battery + analogRead(2);  // get third sample
delay(20);                          // delay for 20ms
battery = battery + analogRead(2);  // get fourth sample

// turn LED pin 13 OFF 
digitalWrite(13,0);                 // turn off blink after 60ms
 
// determine battery state
if (battery > 2900)                 // is voltage high enough?
  {
    SSR = 1;                        // turn relay ON
    digitalWrite(13,1);             // turn LED ON
  }
 
if (battery < 2800)                 // has it dropped too low?
  {
    SSR = 0;                        // turn relay OFF
    digitalWrite(13,0);             // turn LED OFF
  }
 
// output state to pin 10 
digitalWrite(10,SSR); 

//print out battery data in TOOLS
Serial.print((float)battery / 100);    // battery voltage 2 decimal places           
Serial.print("V battery   ");               
Serial.print(SSR);                     // relay state           
Serial.println(" relay  ");            // go to next line

delay (500);                           //  delay for printing every half second

// ROUTINE FOR DETERMINING LOOP TIME
// look at pin 12, is is shorted to gnd?   
if (digitalRead(12) == 1) delay (5000); // if not delay another 5 seconds
 
 }                                      // end of program