Table of contents for Water Filtration
- Water Filtration Design Phase 1
- Water Filtration Design/Construction Phase 2
- Water Filtration Design/Construction Phase 3
- Water Filtration Construction Phase 4
- Water Filtration Construction Phase 5
- Water Filtration Construction Phase 6
- Water Filtration Construction Phase 7
- Water Filtration Construction Phase 8
- Water Filtration Construction Phase 9
- Water Filtration Construction Phase 10
- Water Filtration Construction Phase 11
- Water Filtration Construction Phase 12
Last week, we left you with an idea on how to make a float switch. We are still working on that portion, not finished or satisfied yet, and we’ll finish that up at a later date. This week, we are going to start programming the Arduino so that it will control the solenoid sprinkler valve. The float switches from last week will eventually be what triggers the program to close the valve and thus shut off the pump feeding the RO system.
The software we are using to program the Arduino is from the Arduino website. On their site, they have the Arduino programming language, reference, and the programming environment needed to tell this micro controller what to do.You can download everything you need to work on the Arduino at their site. Full instructions are given there to download, install, and setup your Arduino. You can find a list of distributors here that serve most of the world. You can expect to pay around $30 US for an Arduino Duemilanove, which is what we are using. Any of the Arduino or Arduino compatible clones that use the Atmega168 chip will work for this project.
We are also using an Arduino shield board form the RepRap foundation store which are no longer available. That being said, we highly recommend you find some type of shield board that provides screw terminals for wiring connections to the Arduino. It makes things much nicer and much cleaner.
Let’s go through each element in turn. We are going to start by putting together the Arduino circuit.
Circuit layout
Materials:
- Breadboard/prototyping board
- Jumper Wires
- Arduino
- Two LEDs
- Two 180 Ohm resistors
- Two 10k Ohm resistors
- Two switches (pushbutton or toggle doesn’t really matter)
- USB A to B cable for the Arduino
Having never put together a circuit like this before, or any circuit for that matter, we ran into problems with the switches. First, there was the whole debouncing issue. It turns out, switches don’t produce clean signals when you flip them from one state to the next. There is a surprising amount of jitter in the signal as the contacts bounce off each other and then finally settle to the new state. You take care of this in code by allowing a settle time before you report the switch as having changed states. We implemented this based on the Debounce code sample provided by the Arduino documentation here.
After that was taken care of, we ran into an interesting problem where the switch wasn’t acting coherently. Sometimes it would switch states nicely, other times it wouldn’t switch at all or would switch several seconds after the switch was thrown. This left us puzzled for some time until we remembered having read somewhere about pull-down resistors being used on switches and stuff. Turns out that a pin on a micro controller will want to stay in one state or another unless it has a resister causing it to pull to ground. We added a 10k ohm resister from the switch pins to ground. This eliminated all problems and the circuit and program worked flawlessly thereafter.
Okay, we have a rough diagram of the circuit for the Arduino, switches, and LEDs for this test circuit below. This will make up a portion of the final circuit but will obviously have some additions/changes as we finalize the design. For this test circuit we will be powering the Arduino from the USB port. In the final circuit, however, we will be running the Arduino from a 12V line that we regulate ourselves.
The V+ shown in the diagram is one of the 3.3 or 5V sources on the Arduino itself. We’ve used both and had no problems with either. PIN_D2, PIN_D3, PIN_D9, and PIN_D10 refer to the digital pins 2, 3, 9 and 10 on the Arduino.

Here is the diagram of the circuit.
Above is an image of what the setup should look like.
Here is another image of the breadboard setup, this is a close up so you can see how the resistors are put in.
Adruino code
Once you have the above circuit breadboarded, you are going to need to program your Arduino to do something interesting. Below is the code I developed after seeing the tutorial code from the Arduino website on debouncing switches. Nothing fancy here. It uses four pins and no interrupts. If you need different pin assignments, simply change them at the top of the file under the Pin Assignments comment. We didn’t hardcode anything.
//Pin Assignments.
int pinClean = 10;
int pinDirty = 9;
int pinPower = 2;
int pinValve = 3;//Floatswitch States.
int cleanState;
int dirtyState;//Debounce stuff.
int cleanPrevious = LOW;
int cleanReading;
long cleanTime = 0;
long cleanDebounce = 100;
int dirtyPrevious = LOW;
int dirtyReading;
long dirtyTime = 0;
long dirtyDebounce = 100;void setup()
{
pinMode(pinPower, OUTPUT);
pinMode(pinValve, OUTPUT);
pinMode(pinDirty, INPUT);
pinMode(pinClean, INPUT);
dirtyState = digitalRead(pinDirty);
cleanState = digitalRead(pinClean);
digitalWrite(pinPower, HIGH);
}void loop()
{if ((dirtyState==HIGH) && (cleanState==LOW))
{
digitalWrite(pinValve, HIGH);
}
else
{
digitalWrite(pinValve, LOW);
}//Read and debounce the switchs.
cleanReading = digitalRead(pinClean);
if (cleanReading != cleanPrevious && ((millis() – cleanTime) > cleanDebounce))
{
if (cleanState == HIGH) {cleanState = LOW;}
else {cleanState = HIGH;}
cleanTime = millis();
}
cleanPrevious = cleanReading;dirtyReading = digitalRead(pinDirty);
if (dirtyReading != dirtyPrevious && ((millis() – dirtyTime) > dirtyDebounce))
{
if (dirtyState == HIGH) {dirtyState = LOW;}
else {dirtyState = HIGH;}
dirtyTime = millis();
}
dirtyPrevious = dirtyReading;}
Copy and paste that into a new Arduino document and load it into your waiting Arduino.
A bit of explanation is in order for the first if-statement in the loop() function. We test that dirtyState is HIGH and cleanState is LOW because of the planned configuration of our float switches. We want to have a switch on in the dirty water tank at all times until it becomes too empty–floatswitch near the bottom. We want the clean tank to stay low until it gets full–float switch at the top. If you end up with a different configuration in your tanks or different switches–ours are normally off–then you will need to make adjustments to the code in this if-statement to deal with that.
We’ve run the code in this test configuration for two full days testing it periodically by hand operating the switches with no faults or problems. The code seems to work well but we are going to put overflow points in our clean water storage barrel that go to a drain just to be on the safe side.
Testing
This code should light the LED attached to PIN 2 as soon as it comes on. This is your power indicator. PIN 3 should only light under one switch combination: PIN 9 off and PIN 10 on. This corresponds to your clean tank having room in it–off–and your dirty tank having water in it–on. Any combination of switching should result in the appropriate response outlined here. The only time we have made this circuit and code fail is if we toggle the switches too fast. This causes the debounce routine to loose steps and get confused ending up with a possibly reversed state. Since this isn’t likely to happen in the working environment we aren’t too worried about it.
If anyone has some good ideas for test procedures, let us know and we’ll implement them and let everybody else know about it.
Next week
We’re winding down with this project–we know, we keep saying that but things just keep being built and no end in sight! We’ve got the float switches to finish and install and the rest of the real circuit to finish and mount. Not a whole lot–we’ve got most of it done right now.
If anyone has a better, cleaner, clearer way to do what we are doing here in the Arduino code, then drop us a line. Neither of us are pros at this and we appreciate any help we get. Thanks for stopping by!
Be safe and sustainable!
P&S
No related posts.







