I put instructions for calibrating your water sensor in the sketch before I decided to make an Instructable.

<p>//-----------Christmas Tree Water Level Sensor Sloppy Code and Tutorial---------//<br>/* Brief Foreword: I originally copied the code for the serial output of the sensor from an elegoo example sketch, and added the if/else stack to trigger the LEDS. After a couple of requests to post the code, I went through and cleaned up/commented throughout the sketch. tl;dr Half of this sketch is originally from Elegoo's example program. </p><p> This code/program works with any arduino or clone you have. I used an arduinoNano for mine, but you can use an Uno or Mega if thats what you have. Just make sure you switch the 'LED const int' values to the appropriate pins you're using. </p><p> I slapped this device together very quickly, and spent very little time testing the water level values. I cannot guarentee that your depth sensor will yeild the same values at the same depths as mine. To address this, I wrote a sloppy callibration instructable below. </p><p> --------**********CALLIBRATION/SET UP INSTRUCTIONS********---------------------------- [1]- WITHOUT EDITING any of this sketch, upload sketch to your arduino and open serial monitor. [2]- Insert depth sensor into water to a depth that you would consider the minimum amount needed to trigger the blue LED (full indicator). Write down this value and note it as 'full'. [3]- Repeat step 2 to determine the value you will assign to the 'low' value. Anything above this value but below the 'full' value will trigger the yellow LED. Any value below this value will trigger the red LED. [4]- Plug the values determined from steps 2 and 3 into the const int's 'full' and 'low' below. [5]- Upload sketch again with updated values, and you're done! */</p><p>const int full = 200; const int low = 50;</p><p>int depthSensor = 0; //set depthsensor L pin to Analog 0. You can set to any analog pin you want. int lastValue = 0; char printBuffer[128];</p><p>// The following is setting the LED pins to the corresponding digital pins. You can set these to any digital pin you want. const int blueLED= 2; const int yellowLED= 3; const int redLED= 4;</p><p>//-------After setting your water level and pin assignments above, ideally you should't have to change any of the code below. void setup() { //Begin communication with serial port. This is used to set test your depth sensor and determine which values you want to assign to your full/low/empty values. Serial.begin(9600);</p><p> //Set LED pins to OUTPUT pinMode(blueLED, OUTPUT); pinMode(yellowLED, OUTPUT); pinMode(redLED, OUTPUT); }</p><p>void loop() { // get sensor value: int value = analogRead(depthSensor); </p><p> //If the change from last value to current value is greater than 10, display the current value. if(((lastValue>=value) && ((lastValue - value) > 10)) || ((lastValue 10))) { //Displays depth sensor value to serial port. sprintf(printBuffer,"ADC%d level is %d

",depthSensor, value); Serial.print(printBuffer); Serial.println(); Serial.println(value); Serial.println(); lastValue = value; // sets last value to current value for next loop. } if(value>=200) { digitalWrite(blueLED, HIGH); digitalWrite(yellowLED, LOW); digitalWrite(redLED, LOW); } else if((value <200)&& (value>=50)) { digitalWrite(blueLED, LOW); digitalWrite(yellowLED, HIGH); digitalWrite(redLED, LOW); } else { digitalWrite(blueLED, LOW); digitalWrite(yellowLED, LOW); digitalWrite(redLED, HIGH); } delay(250); }