Need help with setting up

Dear,

For a school project I am converting the analog signal from the HC-SR04 (Ultrasonic sensor) into a digital signal. This just doesn't work for me so I sought help.

The intention is that, for example, if the HC-SR04 indicates between 2 and 40 cm, there will be 5v on a pin. so that it can be used for further purposes.

See the Arduino Project and drawing below.


HC-SR04 Basic Demonstration
HC-SR04-Basic-Demo.ino
Demonstrates functions of HC-SR04 Ultrasonic Range Finder
Displays results on Serial Monitor

DroneBot Workshop 2017
http://dronebotworkshop.com

// This uses Serial Monitor to display Range Finder distance readings

// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13

#define trigPin 10
#define echoPin 13

float duration, distance;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

void loop() {

// Write a pulse to the HC-SR04 Trigger Pin

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the response from the HC-SR04 Echo Pin

duration = pulseIn(echoPin, HIGH);

// determine distance from duration
// Use 343 meters per second as speed of sound

distance = (duration / 2) * 0.0343;

// Send results to Serial Monitor

Serial.print("Distance = ");
if (distance >= 400 || distance <= 2) {
Serial.println("Out of range");

else {
serial.print(distance);
Serial.println(" cm");
delay(100);

delay(100);

Who can help me or at least give me tips

Thanks in advance,

Nick

There is no analogue signal from a SR04, unless you've hacked it.

Please remember to use code tags when posting code.

The pin should already be LOW at the start of the sequence, so no need to write it LOW.

Also, pulseIn does not return a float

1 Like

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

The code that you posted presumably prints the distance to the detected object. If so, then all you need to do is to test the value of the distance variable and if it is in the range that you are interested in then do whatever you want such as outputting HIGH on a pin

Is the distance being printed ?
Does it look reasonable ?
Do you know how to test whether the distance is within a range ?

Where are you stuck ?

1 Like

isn't pin 13 tied to the on board LED?
try using a different pin

1 Like

I changed it to 11.
But how can i get a 5v output if the ultrasonic sensor meassures between 2 cm and 40 cm?

What have you tried ?

2 Likes

the code first generates a short pulse to the sensor.

then pulseIn() listens for the echo of the ultrasonic pulse and generates a pulse on the arduino input pin

the time between the output and input pulse is proportional to the distance

1 Like

I Undestand how to sensor works.
But the problem now is that if the sensor measures a distance between 2 and 40 cmi want it to send a 5v signal to a pin on my arduino board.

The sensor can't do that.

I know thats why i want it to programm it

You'd have to lift off the microcontroller from the sensor and replace it with one of your own

I suspect not.

just set that pin for that condition

if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
     digitalWrite (thatPin, LOW);
}
else {
     serial.print(distance);
     Serial.println(" cm");
     digitalWrite (thatPin, HIGH);
}
1 Like

Im sorry but i dont understand it.

Which parts do you understand ?

distance is determined by generating an ultrasonic (sound) pulse and measuring the time it takes to reach an object and and be bounce back, the echo.

the following generates the pulse

pulseIn () waits for the ultrasonic pulse to return.

the following converts the time in msec to cm

there's nothing analog about this

1 Like

This Topic has been locked as it is the identical or too similar your other post.
Please use the original post and do not duplicate.