• Detecting mains voltage with an arduino safely

    Drew Pilcher09/04/2022 at 08:00 0 comments

    Right now I'm working on building an "automatic transfer switch". This device will switch the load from my solar system to mains if the inverter shuts down.


    I'll document that in another post, but one problem I had to solve to make it work was to detect the line voltage with an arduino.

    At first it sounds pretty simple right? Reduce the voltage with a resistor divider, then rectify it with a diode and a cap.

    But the issue with this is I have 2 inputs, and for electrical safety I want to switch the hot AND neutral. That means I need some kind of isolation if I want my arduino to be able to monitor both.


    Here's the solution I came up with using parts I had lying around:

    It's an opto-coupler utilizing a phototransistor and a neon lamp instead of a LED. Although the lamp is fairly dim, it produces a good response from the photo-transistor. I used this arrangement with a 680k resistor and a 5V power supply:

    I don't know the photo-transistor part number because it was just something from my junk box, but the lamps were still in the bag; the part number for those is GT-NE6S1325T


    I'm not going to lie, I forgot neon lamps need current limiting resistors (150k for this one), and the lamp exploded instantly the first time I tried it. Luckily, I did have the foresight to put on safety glasses before flicking the switch on a circuit connected to mains. :)


    With the circuit tested, I assembled it like this:


    Then I added the following layers;

    • Heatshrink on the individual wires, especially the mains ones (not pictured)
    • Clear heatshrink over the whole assembly (not pictured)
    • A layer of aluminum foil over that. Both to block ambient light, and reflect light internally:
    • Then a layer of heatshrink with internal adhesive (to add some rigidity):


    I tested the module again after assembly and it works perfectly! I might need some filtering of the output signal if it has a 60Hz flicker, but my multimeter reads it just fine.

  • Proof of Concept

    Drew Pilcher08/17/2022 at 08:32 0 comments

    I started with the following components (prices include shipping):

    2x L02M100N-1 monocrystalline solar panels from ECO-WORTHY. $90 each. Mine have slightly different specs due to being purchased a year apart.

    1x PG-12V55-FR Powersonic 12V 55Ah Sealed Lead Acid Battery. $120 Old but unused from ebay.

    1x Tracer1206AN Epever MPPT Solar Charge Controller. $49 from ebay.

    1x PS-1200JCR Giandel 1200W Sine Wave Inverter. $200 from amazon.

    Basic math:
    The chest freezer draws an average of 40W continuously (tested with a kill-a-watt).

    Dividing by the 90% max efficiency of the inverter gives 45W

    For approx 16 hours a day the system will run on the lead acid batteries. Estimating the efficiency of the batteries is difficult. I believe it varies a lot depending on the range of the battery charge you're using. Lets assume 80%.

    (16h * 45W / 0.8) + (8h * 45W) =  1260Wh per day required from the solar panels

    Assuming and average battery voltage of 13V. 16h * 45W / .80 / 13V = 69 Ah per day from the batteries.

    Each solar panel produces 100W nominally for approximately 4 hours per day (depends on geographic location and climate). That's 400Wh per panel per day.

    The charge controller claims "up to 98% efficiency" which is probably not realistic. Lets call it 90%

    Each panel therefore produces 360Wh per day. So I would need 4 panels.

    Getting started:

    My proof of concept was to simply hook everything up and see if it works. Meaning it can charge the battery and power the freezer at least briefly.

    I decided to located the electronics in the basement instead of near the panels to make the system easier to tinker with.

    I used an extension cord to carry the DC from the solar panels inside. Including an extremely jankey connection under the panels which I need to replace asap.

    Conclusions after testing:

    • The system is able to power the fridge for several hours (I didn't time it exactly). Battery capacity needs to be increased, as expected.
    • The battery charges fully over the course of a day. Although the voltage
      cutoffs don't allow the full nameplate capacity of the battery to be used.
    • I probably need a different charge controller, as my current one can only charge at 10A. That's only ~130W.
    • The 1200W inverter is important. Even though the freezer only draws 40W. I tried running it on a 400W non-sine wave inverter, and it tripped instantly every time the freezer tried to start.
    • I believe I witnessed exactly why they say not to restart a compressor cooler within 3 minutes of powering it off (Apparently it can cause the compressor to try to spin in reverse). Well, when you do that it doesn't make any sound, but the freezer starts drawing 500W! instead of the usual 40. I will need some kind of circuit to prevent this from happening if the inverter trips, or a switchover to mains occurs.
    • I've been manually swapping the freezer between solar and grid power, but it's quite annoying because of the 3 minute wait. I should build the switch over circuit soon.