Phone Controlled Coop Door and Electric Fence Charger Using ESP8266 and Blynk

Circuits for a homemade electric fence charger and a smartphone remote controlled coop door.
By zimirken · Oct 26, 2018 ·
  1. zimirken
    Disclaimer: This is not an introductory electronics guide. I'm going to be laying out explanations of all the circuits (dumb nerd stuff). This is fairly technically advanced, and if you don't already have a good bit of experience with electronics and arduino, this article might be hard to read. In particular the electric fence charger involves high currents, power electronics, and high voltages. A homemade electric fence charger may put you at liability if it malfunctions, but I am not a lawyer. :idunno Don't come crying to me if you burn your house down or zap yourself. The way the fence circuit is designed, it's really hard to get any sort of constant arc, but any machine can be a smoke machine if you use it wrong enough so...


    I'm a factory engineer, so I deal with process automation every day. When I got chickens, I knew I was going to be automating everything at some point. This has been made much easier with the use of esp8266 and blynk. So far I have remote door control, and remote control homemade electric fence charger.

    I've put a lot of work into these finished* designs, wasted several pcb boards, and burned out several components on the way. I have both a 3d printer and a small CNC for making PCB boards, but you don't really need either of those to make these projects. All of the parts were bought on aliexpress or ebay. Most of these parts I buy in quantity (10x-100x) because it's far more economical. (You can buy 1 part for $4 shipped or 10 parts for $10 shipped, sometimes even 100 parts for $20.)

    These projects run off of 12 volts, so they can be powered by battery + solar. However, they were not optimized for minimum energy draw. The door circuit uses 78mA idle, and I didn't measure the fence charger as it has a pulse current higher than my meter. I currently use a small 5Ah lead acid battery on a 1.5 amp smart charger, and the charger keeps up just fine. The electric fence charger requires a battery as a buffer as it can draw pulses of >15 amps. If you wanted to minimize energy draw, the first thing is to change out the linear regulators for switching regulators.

    battery and charger.jpg

    Door Controller

    The one that is going to be most useful for most people is the door control, so I'll cover that first. The door control uses an 8rpm gear motor from ebay, and two reed switch sensors for open and closed detection. My current programming really isn't automatic, but everything is set up to be able to be automated. Also, with blynk, you can have an always up to date time, and there are libraries to get sunrise and sunset times for any day and location, which I'm currently working on programming.

    door board board.PNG door board schematic.PNG door winch rope.jpg installed door board and winch.jpg door switch.jpg door board top.jpg door board bottom.jpg

    door board schematic sectioned.PNG
    A: The LM1117 is a linear voltage regulator with a heat sink. You'll need to replace this if you want to reduce power usage. C2 NEEDS to be at least 1000uF because the esp8266 pulls huge pulse currents when transmitting on wifi.

    B: The is the esp8266 and all the required pull resistors. CH_PD and RST need to be pulled high to keep the chip on. GPIO 0 and 2 need to be free floating (10k internal pullup) or slightly pulled high during startup. If they are dragged low by anything the esp starts in program mode. This means you can't connect these two io pins directly to things like transistors or sensors. Since they are connected to mosfets, they will float up and turn the mosfets on. GPIO1 and GPIO3 are able to be whatever during boot, so they are connected to the door reed switches. Door 1 and Door 2 are screw terminals to connect to reed switches that sense the open and shut states of the door. They are pulled high by R4 and R5.

    C: Ideally you would use 12v relays so you aren't powering them them through the linear regulator, but that's what I had on hand. Q1 and Q2 are IRL540N mosfets that can be turned on by 3.3 volts. D1 and D2 protect the mosfets from inductive spikes from the relay coils.

    D: There are two screw terminals that connect to the winch motor. C3 is a big ceramic capacitor in parallel with the motor, because the inductive spikes of the motor were resetting the esp without it.

    I 3D printed the parts for the winch, but there's nothing wrong with just bolting the motor to some wood and whatnot. I also cnc milled my pcb, but that's not really necessary either.

    installed door board and winch.jpg

    door switch.jpg
    The door position sensors are just cheap reed switch sensor things for security systems. There's a magnet glued to the door.


    The program is currently very simple. It just connects to wifi and allows blynk to manually control the relay pins. It also sends the door status to blynk only when there is a pin change to reduce bandwidth. Blynk doesn't like when you send updates a hundred times a second. Eventually I plan on adding auto open and close using time data sent from blynk and a sunrise and sunset library.

    Electric Fence Charger

    I have a three wire electric fence that you can see in the picture below. At first I bought the cheapest charger and it performed as expected. The only chickens that ever got zapped were the ones that used the fence as a pacifier. Even then, they weren't impressed, and neither was I. Also the slightest piece of grass made the fence completely harmless. I thought about buying a better charger, but have you seen the prices?

    outside view of coop with electric fence in background.jpg
    fence esp board schematic.PNG fence esp board board.PNG charger esp board top.jpg charger esp board bottom.jpg fence power board schematic.PNG fence power board board.PNG charger power board bottom.jpg charger power board top.jpg

    This electric fence charger uses an automotive ignition coil. This means that big long fences will add enough capacitance and draw down the output voltage. However, a small fence like mine lets the coil get big long arcs and very high voltages which cut right through the chickens feathers to let them know who's boss. The ignition coil and the way the circuit works means that the energy output is limited to less than half a joule. In any case, my chickens now have a very healthy respect for the fence. Also, thanks to blynk, I can turn the fence on and off from anywhere with my phone. So can my wife, which is good because she is almost as scared of the fence as the chickens are.

    The fence charger uses an ignition coil as a transformer, and operates it just like it would be used in a car. First, the mosfet (igbt in my case) turns on and allows current to flow through the coil. This charges up the magnetic field of the ignition coil. As the magnetic field builds in the coil, the current goes up to some 3-4 amps or so. The magnetic field in the coil stores around 0.1 joules of energy. When the pulse from the esp ends after 5 milliseconds or so, the current flow to the ignition coil gets cut off, and it gets real mad. It produces huge voltages on the primary and secondary as the magnetic field collapses. The voltage on the primary is limited by a neon bulb to a hundred or two volts so it doesn't damage things. However the secondary produces voltages in excess of 20 thousand volts. This voltage will be lower when it's connected to the fence due to the capacitance of the fence and the parasitic resistance from grass and stuff.


    The fence charger is divided into two boards with a long servo cable connecting them. This is because the ignition coil and associated power electronics generate a lot of electrical noise. This noise really screws with the esp. (one even fried from just the noise) I have the esp board about 2 feet away from the power board and coil, and it doesn't interfere any more.
    fence power board schematic sectioned.PNG

    A: I fried a few mosfets when the ignition coil noise locked up the esp and the ignition coil got stuck on. So I added this part. The capacitor in series with the esp signal limits the amount of time the esp can turn on the coil. After a hundred or so milliseconds, the capacitor C5 charges up, which blocks the current going into Q2, which turns the coil back off. R2 allows the capacitor to discharge in between pulses, while bieng too high value to allow the esp to still turn on the coil through it. D2 discharges the capacitor faster when the esp pin goes low.

    B: This is just a standard level shifting non inverting mosfet driver circuit. Just 2 3904 transistors and 1k resistors. When Q2 is fed current from the esp, it pulls the base of Q3 low, which turns it off, which allows the collector of Q3 to be pulled high through R3. This circuit is also designed to be able to turn off a mosfet very quickly, as the turn off speed affects the strength of the inductive kickback pulse.

    C: This is where the magic happens. Two screw terminals to wire the coil into, in parallel with 4 fat ceramic capacitors. I just used what I had laying around, no specific value other than >300v and big. I used an IGBT for power switching because it has a really high voltage rating of 600 volts. You will need a high voltage rating of at least 600 volts. You can replace it with a mosfet or maybe a power transistor if you really want to. Thanks to the drive circuit in B section you don't need a logic level mosfet. I originally used a mosfet but the ones I had kept burning out due to not high enough voltage rating so I switched to that IGBT. Put a heat sink on it just in case. There's no flyback diode on the ignition coil because the whole purpose is for it to ring up to several hundred volts. That's what creates the >20KV on the secondary. Oh, as far as the ignition coil wiring goes, positive to positive, negative to negative, fence grounding rods to the coil negative terminal, high voltage terminal to the fence.

    D: This is a snubber using a neon bulb. The neon bulb conducts around 90 volts and keeps the inductive kickback voltage on the coils primary side from getting too high. This bulb does flash every pulse on my charger. If it isn't the coil probably isn't ringing as stong as it should be.

    Oh, I tested lots of other resistance values for all the resistors on this board and nothing worked except these. Change them at your own peril.

    fence esp board schematic sectioned.PNG

    This is the separate esp board for the fence charger. This is what controls the coil and what allows you to turn the fence on and off with your phone.

    A: The esp itself of course. Lost of pins on it have to be tied high using R1, R3, R2, and R5. R4 is a 470 ohm resistor that goes back to the power board to turn on and off the coil.

    B: Just like the door circuit, just an LM1117 3.3v linear regulator on a big heat sink. You need the 1000uF capacitor to supply the esp with surge current during wifi transmissions.


    This program is a little more complicated than the door program. It is designed to send a very short pulse to the power board to turn it on for a few milliseconds. There is programming to be able to enable and disable the fence with blynk. There is also programming to be able to change the coil charge time and time between pulses using blink. However, I found good default numbers and I haven't seen need to try to change them. The default of 5 milliseconds charge time is plenty enough to fully charge the coil without wasting electricity and overheating the coil or mosfet.

    The coil charge time uses a straight delay function, while the pulse rate uses a non blocking delay so the esp can do things during. The fence defaults to on in case of a power outage. Also, there is programming to turn on the built in led light when it gets a wifi connection for troubleshooting.


    Blynk is the app that ties this all together. The esps communicate with the blynk server and the blynk server talks to the app on your phone. This makes everything super easy as you don't have to do any messing about with router settings or port forwarding or having your own server or anything like that. The arduino program side of it is also super simple.

    Blynk App.png

    This is the running display of my current program. I can turn the electric fence on and off. I have two indicators for door open and door closed. I also have buttons for door up and down. A tutorial for the app side of blynk is kindof outside the scope of this article, but you can look it up. Suffice to say, the blynk app and the esp have virtual pins they can both read and write. This is how I set up communications between them. The blynk app can also directly control output pins, which is how I have the motor controls set up.

    down button.png up button.png fence enable.png door down indicator.png door up indicator.png edit screen.png

    Here are screenshots of all the buttons I have set up currently. You can see how V0 and V1 correlate to the programming of the door board sensors.

    That's pretty much it. I've had this all in place for about a week now and it's been working flawlessly. The only thing is that I've had to route the esp wifi through my coop router, and that connects to my home wifi. But that's because my coop is covered in metal siding and the wifi router is in just the right spot to get signal out to my home network. Otherwise the esp8266 has pretty impressive wifi range. Let me know if anything isn't clear in my explanations on the circuits.

    Oh, right, a bill of materials:

    Blynk Tutorial:

    ESP8266 arduino IDE tutorial:

    Share This Article

Recent User Reviews

  1. Anonymous
    "Excellent info!"
    5/5, 5 out of 5, reviewed Jan 3, 2019
    Very informative read! Good pics & diagrams too.
  2. schmidty13
    "Excellent walk thru!"
    5/5, 5 out of 5, reviewed Oct 26, 2018
    There is a ton of info here. I don't understand much of the technical aspects...yet. I am working on that and am saving this to return to when I do.


To make a comment simply sign up and become a member!

BackYard Chickens is proudly sponsored by: