Introduction: Water Heater Controller / Scheduler
This instructable will show you how to build a simple water heater controller using a Raspberry Pi. This is intended to be used with a knob style thermostat control found on most electric and gas tank water heaters and does not require modifying your existing thermostat control or water heater in any way.
Disclaimer: This system is not designed to be robust in any way. While it is useful for controlling the thermostat settings on a schedule one should consider using only when it can be supervised.
Step 1: Materials Required
- Microsoft Visual Studio 2015 Community Edition
- RaspberrySharp.IO solution file
- Mono (on Raspbarrian) We will get to that later
- Win32 Disk Imager (optional for writing OS images to SD cards)
- WinSCP (for copying files to the Pi)
- Putty (for remote console access - optional if you have the Pi connected to a monitor)
- Raspberry Pi Model B
4GB SD Card for Raspberry Pi Pre-installed with Raspbian Wheezy (The easy route if you do not want to download and write your own OS images to the SD card)
- Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi
- Half-size Breadboard
- Breadboard Jumper Wires
- 22AWG Solid Core wire (you can also use this for jumper wires but I recommend getting both)
- Dual H-Bridge Motor Driver for DC or Steppers - 600mA - L293D
- DC 12V 0.07A 3.5RPM High Torque Gear Box Electric Motor 37mm
- 1 X 37mm DC Geared Motor Mounting Bracket Holder + 6mm Hex Coupling
- 12V 1A Power Supply
- If you plan on using wifi you will want an adapter for your Pi
- To protect the Pi you may also want to consider a case enclosure for Raspberry Pi Model A or B. I used the one linked below but Adafruit offers a variety of cases
The only parts I reused (did not purchase specifically for this project) is the plastic component for connecting the motor shaft to the water heater thermostat control, and the base that the motor is mounted on. This should not be a roadblock though, as I am sure there are other materials that will work just as well. I will provide some alternatives in the "Preparing the Motor" step.
Additionally not all water heater thermostat controls are the same, and not all water heaters are easily accessible or near a power source. So be sure to examine your water heater controls and accessibility before starting this project.
I will say now that the soldering required is minimal. You just need it to connect the wires on your motor.
- Soldering Iron
- Soldering Iron Stand
- Wire Cutter / Stripper
I will mention that you may be able to get away without soldering as the wires easily wrap around the leads on the motor. I recommend soldering them just to ensure good conductivity.
Step 2: Preparing Raspberry Pi
If you are new to Raspberry Pi you can go to https://www.raspberrypi.org/help/quick-start-guide...
If you ordered the pre-installed SD card you are pretty much set. Just insert the SD card plug in a keyboard, an HDMI monitor/TV and power up the Pi. For the sake of brevity I will assume you already have the Pi powered up with an internet connection.
If you have not already enabled SSH you will want to do that.
If SSH is enabled you can use Putty to connect to your Pi over your network. This way you do not need to keep a keyboard and monitor hooked up to your Pi.
Before disconnecting the keyboard and monitor you can obtain your Pi's IP address by typing:
Using the IP address of your Pi you can connect remotely via SSH using Putty:
- Double click Putty.exe and enter the IP address of your Pi, then press Open.
- Login using your credentials (default username is pi)
This project is done in C# so you will want to install Mono. Be patient it may take a bit. Type the following:
sudo apt-get install mono-complete
The Pi is now ready to run the controller software that we will write using Microsoft Visual Studio on a Windows machine.
Beyond this project if you want to learn more about writing C# apps on the Pi (and Linux in general) check out:
Step 3: Preparing the Motor
Using the AWG22 wire:
- Cut 2 wires about 4" in length
- Using the cable stripper remove the plastic insulator exposing about 1/8" to 1/4" of the copper
- Connect these to the leads on motor by wrapping the exposed copper wire through and around the hole
Now heat up the soldering iron and apply a small amount of solder on the wire and leads. Don't worry it does not need to be perfect.
If you have no experience soldering this is a good start since it is not really risky or difficult.
If you refine this project or have another project that requires more soldering I suggest taking a look at:
Mounting the Motor to a Base
Time to get creative...
One part that I reused was a stainless steel base from a paper towel holder as a base for my motor. If you ordered the Motor Mounting Bracket Holder then all you need is something heavy that you can screw the mounting bracket to.
Of course, this will depend on how much space you have where your water heater is located. I also used a small stool that I sat my base on.
One alternative is to simply use a wood block of some sort, like a 1 foot section of 4x4. That should be heavy enough and easy to screw the mounting bracket to. While I did not test this myself, I am confident it will work. The base is just something for the motor to sit on that will provide stability.
For other ideas on creating a base, stand or both I suggest browsing through other instructables. PVC looks like a versatile material that is easy to work with:
Connecting the Motor Shaft to the Water Heater Control
I also used a spare plastic part from a toy to connect my motor shaft to the water heater thermostat control. Your water heater control may be different so you will want to examine your control first before going this route.
One alternative is to use the top of a prescription medicine bottle. Really any plastic bottle top with large enough radius and sufficiently strong should work. The idea is to cut a small hole that will fit tightly over the motor shaft. Then connect it to the water heater control using two 3/4" wood screws. NOTE: The screws should barely be longer than the bottle top. You do not want to screw too far into your thermostat control.
- Mark the center with a marker
- Start cutting a small hole and check to see how close it fits
Do not cut the hole too big. It should be just large enough where you can force it onto the motor shaft.
Once you have a good fit remove it. It will be easier to screw to the water heater control.
Using a screwdriver (power tools not necessary or recommended):
- Begin screwing into the bottle top on each side on a flat surface.
- Place the bottle top over you water heater control and firmly continue screwing into it.
- Force your motor shaft back into the top.
Step 4: Wiring Up the Pi
Using your jumper wires, follow the layout above to connect your motor, power supply, L293D chip to the Pi. This is from an Adafruit tutorial, the only modifications is that we are using a 12V 1A power supply rather than a battery pack.
If you have never wired up a breadboard before, do not fear. The wire colors in the case do not matter. Just emulate the diagram above by using your jumper wires. Your L293D chip will pop right into place. If you are still uncomfortable I suggest watching this video - it will make you feel better:
For the power supply I simply cut the connector off the end to expose the wires. I then stripped the wires and plugged them directly into the breadboard. If you do not want to cut your power supply you can get an adapter at Adafruit:
Step 5: Prepare Visual Studio: Raspberry Sharp IO Solution
If you have not installed Microsoft Visual Studio 2015 Community Edition on a Windows machine you will want to do that now. It is free although you will have to create a Microsoft account to continue using after 30 days of installing.
Once you have Visual Studio 2015 installed you will want to extract and open the Raspberry Sharp IO solution file.
- Right click on raspberry-sharp-io-master.zip (download link in Step 1) and choose Extract all.
- In Visual Studio go to File -> Open Project/Solution
- Browse to where you extracted the raspberry-sharp-io-master folder and open the RaspberrySharp.IO.sln file.
Step 6: Prepare Visual Studio: Scheduler Project
Download and extract the Scheduler.zip file (Attached Below). It contains the Scheduler program Visual Studio project. For organization purposes I suggest extracting the Scheduler into the raspberry-sharp-io-master folder that you previously extracted.
In Visual Studio add the Scheduler.csproj to the current solution:
- In the Solution Explorer right click on Solution Items and select Add -> Existing Project. Browse inside the Scheduler folder that you extracted and open the Scheduler.csproj file.
This should open the project and the Program.cs file.
Step 7: Editing and Building the Scheduler Program
Before building the Scheduler you may want to change some things.
You can change the path to the schedule file by editing this line at the top of Program.cs
const string SCHEDULE_FILE_PATH = @"/home/pi/scheduler/schedule.txt";
To change the log paths alter these lines in Logger.cs
const string SCHEDULE_LOG_PATH = @"/home/pi/scheduler/logs/pi_schedule.log";<br>const string ERROR_LOG_PATH = @"/home/pi/scheduler/logs/pi_error.log";
Once you have made your changes, or if you leave the default, be sure to create the directories and files using the path and name you defined in the code.
To create a directory when you are logged into the Pi type:
To create a new blank file type:
Do the same for the log files.
In Visual Studio to build the project go to Build -> Build Scheduler
This will build the Scheduler.exe file and place all the dependent files in the bin\Debug directory of your project. Your project will be under the raspberry-sharp-io-master folder.
For instance the bin\Debug directory of the Scheduler project on my development machine it is:
Step 8: Running the Scheduler on the Pi
Using WinSCP you can easily copy your build project files to the Pi. Browse to your bin\Debug folder on the Windows machine and then to your folder on the Pi (/home/pi/scheduler).
Copy all of the files from the bin\Debug folder on the Windows machine to your directory on the Pi.
Now create your schedule in the schedule.txt file. Be sure to enter your schedule in the format of ScheduleTime, ScheduleCommand with each time being on a new line:
6:39 PM, HEATUP 6:40 PM, HEATDOWN
You can add as many scheduled events as you want. You might want to start by testing.
Use Putty to connect to your Pi for remote console access so you can run the scheduler.
The scheduler has a few arguments you can run besides just the schedule.
To test running the motor forward:
sudo mono Scheduler.exe Forward
To test running the motor reverse:
sudo mono Scheduler.exe Reverse
To test reading the schedule file:
mono Scheduler.exe Schedule
When you are ready to run the scheduler run it without any arguments and append an & so it will run in the background:
sudo mono Scheduler.exe &
Once the scheduler is running you can logout of the Pi. As long as the Scheduler program is never killed (which should only be because of power failure or a reboot) it will run indefinitely.
If you modify the schedule.txt file you will need to kill the Scheduler program start it again so it will re-read the schedule.txt file. Unfortunately since I did not include a script to restart it you will have to kill it manually.
Step 9: Troubleshooting
If you receive an error building your project and it is related to missing Raspberry.IO.GeneralPurpose reference you may need to add it to your project:
- Right click on the Scheduler project and select Add -> Reference
- In the Reference Manager click to toggle on Raspberry.IO.GeneralPurpose and click OK
If the Scheduler throws an exception when the timer fires or when trying to test the motor using the supported arguments, it may be because you are not running with necessary privileges. Be sure to run it as sudo.
These type problems can be difficult, however, many times it is something simple.
- Check to ensure power is supplied for the motor (this is the 12V 1A power supply)
- If the power supply wire is stranded (not solid) it can be a pain, twist the wire so to get it as straight as possible
- Ensure all your jumper wires are connected fully (reseat them to ensure)
- Check your wiring, review the diagram and ensure all the pins are connected correctly.
- Unplug the motor from the breadboard and wire it directly to your power supply to ensure they both work (if so then you know it is definitely something wrong in your circuit) Be sure to match the hot and neutral wires.
Step 10: Wrap Up
The Pi is extremely overpowered for this project, however, that power provides flexibility to add onto it. For instance it is not difficult to write a web app on the Pi to control the water heater using a web browser - on a phone as well.
I plan to add those sort of capability to this project as well as experimenting with servos. I believe the best and safest implementation would be to use a servo with analog output to allow detection of the beginning and end of the thermostat. This would prevent the possibility of burning out the motor and allow it to be calibrated so that the program could keep track of the temperature. That is outside of my ability for now but I will update this instructable with any additions/changes.
We have a be nice policy.
Please be positive and constructive.