hc-sr04

I have to do a project where with an ultrasonic sensor HC-SR04 detects a person and activates relays (connected to lights) with some delays decided by me. The sensor's range of action is editable by a potentiometer. I have no idea which way to start could you give me a hand?

Get the HCSR04 working on its own. Google "arduino hcsr04". There should be something to get you started within the 1.2 million hits.

Learn to read the potentiometer with analofRead().

i didn't find nothing that correspond to my project

It is an unreal expectation that you will find code that exactly matches what you want to do. You can find information on how to connect the HCSR04 and code to get ranges from it. I linked to a tutorial on how to read a potentiometer. Once you can read the rangefinder and pot separately read up on the if statement and digitalWrite() in the language reference. Start with the basics and build a part at a time. When you understand each part then you can more easily add them together.

Kolonna:
i didn't find nothing that correspond to my project

Did you ask the people that did the same class last year?

It's not a project I'm doing in school because I'm on stage now

Time to learn to think for yourself then.

Have you done anything in the meantime?
Got the sensor to detect a distance, for starters?
Got to read the pot?
Got to switch on/off your relay? (tip: use Blink for that; pro tip: use Blink Without Delay).

Hi :slight_smile: I'm a student who is completing an HND in systems engineering, my graded unit project inlvolves and arduino, ultrasonic sensor, relay switch, buzzer and a pump. When the ultrasonic see's my given high and low parameters is switches the relay which activates both my pump and buzzer for the duration of filling the tank.

I have the programme working fine :):slight_smile: :slight_smile: however i'm experiencing rapid switching when the sensor reaches around my high and low level. Through some research I have now added hysteresis to help issues, however, i can't seem to get it to work properly.

I have posted the current adapted programme below, can anyone help? Please.....

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int relayPin = 13;

// defines variables
long duration;
int distance;
int lastDistance;
int hysteresis = 2;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(1000);

// Sets the trigPin on HIGH state for 30 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
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;

lastDistance = distance;
if ((lastDistance + hysteresis) <= 12 && (lastDistance - hysteresis) >= 4){
digitalWrite(buzzer, HIGH);
digitalWrite(relayPin, HIGH);
}

else{
digitalWrite(buzzer, LOW);
digitalWrite(relayPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

For your program: do fix your indentation (ctrl-T in the IDE) and post with code tags.

For the hysteresis: the tank fill level IS your hysteresis.

if (level < MINIMUM) pumpOn();
if (level > MAXIMUM) pumpOff();

No risk of fast on/off switching this way - assuming a healthy difference between the two levels. To be really sure you may consider adding a delay between sensing a low level and switching on the pump, so it won't switch on due to a single stray reading.

Is my browser messing threads or was this a thread hi-jack?

I think I may have hi-jacked the thread.....:open_mouth: I'm unsure who to begin a new topic....

How to begin a new topic.

How to make a link?

How to make a link?

Fixed