Automatic Water Barrel Filler
by IJWEbbers in Circuits > Microcontrollers
9367 Views, 90 Favorites, 0 Comments
Automatic Water Barrel Filler
A relative asked me if I could create something that automates refilling a water barrel used for watering a small garden greenhouse. They didn't want to plug in a pump every few hours only to have turn it off again ten minutes later when it has filled up again. So I've scavenged some stuff that I had lying around to create a project and decided to make a instructable so that other people might get some inspiration from it.
Since I didn't want to mess with the cable connected to the pump, I decided to modify an old power strip in a way so that it could be controlled by a microcontroller. this might prove useful in other projects as well.
I also did not want the pump to switch on/off every second because of the water level jumping above and below a certain threshold. To fix this I used 2 float switches, one which is the maximum water level and one forthe minimum water level.
Supplies
Electronics:
- 1x ATmega328P Xplained Mini with cable for programming
- 2x polypropylene float switch
- 1x 5v relay module 1-channel (i used a KY-019)
- 3x male to female jumpers
Bigger bits:
- 1x old outlet power strip
- 1x water barrel
- 1x submersible pump with waterhose
- 1x powerbank to power the ATmega328P
- 1x plastic container to put the electronics in and keep them dry
- 2x small triangle bracket or similar to mount the float switches on
Tools:
- stuff to keep everything together with (duct tape, hotglue, screws)
- tools to remove bits of hard plastic (stanley knife, chisel, drill or something similar)
- screwdrivers
Software (download):
- AtmelStudio 7.0(https://www.microchip.com/mplab/avr-support/atmel-studio-7)
Make Some Room Inside the Power Strip
WARNING: MAKE SURE THE POWER STRIP IS NOT PLUGGED IN WHILE WORKING ON IT!!
(The power strip might look different on the inside for you, but generally the same method should work.)
Since we want the relay to be inside the power strip, some room needs to be created on the inside. So open it up and cut away some the metal and plastic pieces at the end of the strip (or somewhere else, as long as the metal is still connected to the wires) so that the relay module fits inside. You should also pick a place where you want the jumpers to come out from and make a small hole for them on a side of the casing (I did this at the far end).
Connect the Relay
Now that there is room to fit the relay into, it is time to connect it up.
First off you need some more wire, so pull the power cable through a bit further and remove the outer casing of the cable that you pulled in. You need enough wiring so that you can reach the relay and go back to the original connection point.
after that, cut the "Line" wire (brown in the EU, black or red in the US) in a way so that it can be connected up to the relay. Then connect one end to the "COM" of the relay module and the other end to the "NO" of the relay module.
Now close the power strip back up and it is ready to be controlled by a microcontroller.
Install the Float Switches
To know when the power strip needs to be turned off or on, we need some float switches installed inside the water barrel.
Connect the float switches to the triangle brackets, and connect them to the inside of the barrel with screws or other means at the desired switching levels. also make a small hole in the barrel for the wires of the float switches to go through.
Make sure to close up the holes with some hot glue to make it watertight again.
Connect It All Up
We installed all the components, so lets connect them all up according to the image.
I connected a plastic container to the side of the barrel to put all the electronics in, so that it won't just be hanging around the side.
Creating the Program
I mainly learned to code in C, so this project is written in C.
If you have not yet downloaded and installed the needed software, download and install it now.
- Open AtmelStudio.
- Click on "File" -> "New" -> "Project".
- Click on "GCC C Executable Project". Give your project a name and location to store. Click "Ok".
- Search for the ATmega328P. Click "ATmega328P" -> "Ok".
- Click in the Solution Explorer on "main.c" to open the main program.
Adding the Code
Delete the code already present in main.c
Copy and paste the following code in main.c
#define F_CPU 16000000 #include <avr/io.h> uint8_t top_switch_state; uint8_t bottom_switch_state; void floatSwitchInit(void) { /* top sensor */ DDRD &= ~(1<<0); //set pinD0 as input PORTD |= (1<<0); //use pull-up resistor on pinD0 /* bottom sensor */ DDRD &= ~(1<<1); //set pinD1 as input PORTD |= (1<<1); //use pull-up resistor on pinD1 } void relayInit(void) { DDRB |= (1<<1); //set pinB1 as output PORTB &= ~(1<<1); //set portB1 low } int main(void) { //Initialize the components floatSwitchInit(); relayInit(); while (1) { //if water is above top level, turn off pump top_switch_state = PIND & (1<<0); if (top_switch_state == 0) { PORTB &= ~(1<<PORTB1); } //if water is below bottom level, turn on pump bottom_switch_state = PIND & (1<<1); if (bottom_switch_state == (1<<1)) { PORTB |= (1<<PORTB1); } } }
Run and Test the Code
Connect the microcontroller to the computer and follow these steps to select the right tool and program the microcontroller.
- Click on the hammer tool.
- Select the "mEDBG*ATML" debugger/programmer.
- Select interface "debugWIRE".
- Click "start without debugging".
This will build the program and write it to the microcontroller.
Now you can check if everything works by plugging in your power strip, plugging something in the power strip (like a desk light) and move the float switches up and down. The thing that is plugged inside the power strip should be on when:
- both switches are down.
- the bottom switch is up and it was already turned on.
it should be off when:
- both switches are up.
- the bottom switch is up and it was already turned off.
Now for Real
The barrel fills up until the top float switch is pushed up and waits until the bottom float switch goes down for the plugged in pump to turn on again.