Go Down

Topic: Ultrasonic sensor activates relay code £££ (Read 1 time) previous topic - next topic

kristian30041995

Mar 26, 2017, 09:14 pm Last Edit: Mar 27, 2017, 09:23 am by kristian30041995
Hi all!

I am currently doing a project to automate the filling of a tank. I am using an ultrasonic sensor to monitor the level. I will want the system to control an alarm and a solenoid valve, so for this I will use two relays.

I have attached what I think is a suitible circuit diagram, I just need help with the code.

So the system will have 1 input
-Ultrasonic range sensor HC-SR04

And 2 outputs
-1 relay to open the solenoid valve to fill the tank
-1 relay to turn on the flashing beacon if the coolant level gets too low or too high


I want the system to:

1. Activate the solenoid valve relay to start filling the tank when the HC-SR04 sensor reads a range of 7cm and not stop filling untill it reads a range of 3cm. (tank is large and shallow with calm surface)

2. Activate the flashing beacon relay if the reading from the HC-SR04 sensor reads greater than 9cm or less than 1cm.


I have set this circuit up and put some example code on the arduino. This example code uses the ultrasonic sensor to measure the distance and I can view this distance in serial monitor.

// defines pins numbers
const int trigPin = 12;
const int echoPin = 13;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, H IGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}


However I really need help from a coding genius to to add the relay functionality!

Is this especially difficult?!

How much would code for this cost????

PaulS

Quote
Is this especially difficult?!
No. You use digitalWrite() to turn the pin that the relay is connected to on or off.

Depending on how the relays are connected, HIGH might be on or it might be off. You have to test.

An if statement (if(distance >= minDist && distance <=maxDist)) can be used to see if a value is in a range.

The body of the if statement is where you'd diddle with the appropriate relay(s).
The art of getting good answers lies in asking good questions.

cedarlakeinstruments

How much it would cost is probably determined in part by how well the code is written. Simply translating what you've said into code is trivial. So let's say $50.

Taking a more nuanced approach, I can say that liquid sloshes around, so the programmer should consider some sort of filtering on the returned values. The amount and type of filtering will depend on the liquid's characteristics: honey will slosh less than gasoline, for example. Since you said the tank is large and the surface isn't usually moving, then any liquid motion will be directly as a result of pumping in fluid. Probably need some testing to determine how the liquid's motion affects the ultrasonic sensor's response.

Since the mounting of the sensor will affect distance to surface, there may be a zero-calibration routine needed, or at least a fixed offset from zero that you can adjust and rebuild the software.

Do you need to deal with a bad sensor? What happens if you start pumping liquid but the distance doesn't change? How long does the pump stay on before deciding that something's wrong? Does this same software have to work for different sized tanks?

This is a perfect example of a "sketch" that can cost $35 or $3,500 depending on how complex the REAL behavior has to be.
Electronics and firmware/software design and assistance. No project too small

6v6gt

You might also want to consider other methods of measuring the liquid level which may be potentially more reliable. Here is just an example using a pressure sensor: http://www.ti.com/lit/an/snaa127/snaa127.pdf

If the liquid is flammable, an ultrasonic sensor with a piezoelectric element may be unsuitable.

Go Up