Introduction: Ultrasonic Sensor to Measure Water Height

How to easily build an sensor to measure water height.

Step 1: Step 1: Materials and Tools

Materials

- a Particle Photon

- a breadboard

- breadboard wires

- a micro-USB cable

- a red LED

- a green LED

- a yellow LED

- three 220 Ohm resistors

- a ultrasonic sensor (HC-SR04)

- Tupperware box

Tools

- Soldering iron

- Hot glue gun

- Wood glue

- Duct Tape

Step 2: Step 2: Soldering

Solder two breadboard wires to each led. This makes sure there is enough room to put the LEDS wherever you like.

Step 3: Step 3: Circuitry

- Connect the pin labeled “UCC”at the ultrasonic senor to pin 3V3 on the Particle Photon

- Connect the pin labeled “trigger” at the ultrasonic senor to pin D4 on the Particle Photon

- Connect the pin labeled “echo” at the ultrasonic senor to pin D5 on the Particle Photon

- Connect the pin labeled “GND” at the ultrasonic senor to pin GND on the Particle Photon

- Connect the green LED to pin to D0 with an 220 Ohm resistor in between

- Connect the yellow LED to pin to D1 with an 220 Ohm resistor in between

- Connect the red LED to pin to D2 with an 220 Ohm resistor in between

Step 4: Step 4: Code

Go to https://build.particle.io Before you put in the code you should add the library “HC_SR04 and

select it. You cannot just type “Include but have to realy include it in the created app from the library.


Use this code:

// This #include statement was automatically added by the Particle IDE.

#include

int cm = 0;

int trigPin = D4;

int echoPin = D5;

int Red = D2;

int Yellow = D1;

int Green = D0;

HC_SR04 rangeFinder = HC_SR04(trigPin, echoPin);

void setup()

{

pinMode(Red, OUTPUT);

pinMode(Yellow, OUTPUT);

pinMode(Green, OUTPUT);

}

void loop()

{

cm = rangeFinder.getDistanceCM();

if (cm<20 && cm>0){

digitalWrite(Red, HIGH);

digitalWrite(Yellow, LOW);

digitalWrite(Green, LOW);

Particle.publish( "distance", "HIGH water " + (String) cm);

}

else if (cm>40){

digitalWrite(Red, LOW);

digitalWrite(Yellow, LOW);

digitalWrite(Green, HIGH);

Particle.publish( "distance", "LOW water " + (String) cm);

}

else if (cm==-1){

digitalWrite(Red, HIGH);

digitalWrite(Yellow, HIGH);

digitalWrite(Green, HIGH);

Particle.publish("distance", "ERROR " + (String) cm);

}

else {

digitalWrite(Red, LOW);

digitalWrite(Yellow, HIGH);

digitalWrite(Green, LOW);

Particle.publish("distance", "MEDIUM water " + (String) cm);

}

delay(3000);