Ultrasonic Distance Sensor , need help

Hi All.

I am new to Arduino and this forum. I have built a car parking sensor using an HC SR04 distance sensor, i would like to switch the power OFF to the Distance sensor when the car is parked to save battery life as the Red LED in the sketch stays ON.

I have uploaded the sketch i am using, can anybody help me to modify it to facilitate the RED LED switching OFF.

Regards

Ray

Code (1).txt (1.44 KB)

How is it powered ? A parking sensor is only needed when the car is powered so can't you just power the circuit from a connection that is only live when the ignition is turned on ?

Hi ukhelibob.

The sensor is fixed to the rear wall of the garage, sensor is to prevent my wife from going through the wall.

Regards

Ray

OK. But the question remains. How is it powered ?

Hi ukhelibob.

I have uploaded the sketch from an Arduino Uno R3 on to an ATTINY 85, and using a 240 vac to 9 vdc Transformer.

Regards

Ray

Let's stick with the Uno for now. At least then the pin numbers in your program make sense. Which of the two LEDs is the red one and how are they wired ? It would be better to give them more descriptive names to differentiate between them.

Presumably
digitalWrite(led2, LOW);turns the red LED on to indicate that the driver should stop. How long do you want it to remain on after the stop condition is met and do you really care if it stays on as the system is mains powered ? Turning the LED off will not, of course, stop the mains transformer from consuming power.

I must say that as you are working in centimeters you are not giving much warning before lighting the red LED. Once you get the simple version working how about flashing the red LED with increasing frequency as the stop point is approached ?

Hi ukhelibob

Green LED on Pin 10, Red LED on Pin 11. I did not realise that the Transformer would still use power if the Sensor package was asleep, I would like inches, but as yet I have not got to the stage of compitance to do that, so I guess I will leave the system on the mains, as the power consumption is possibly minimal, I therefore just need to know how to change from centimetres to inches.

Regards

Ray

manofgresley:
I would like inches, but as yet I have not got to the stage of compitance to do that, so I guess I will leave the system on the mains, as the power consumption is possibly minimal, I therefore just need to know how to change from centimetres to inches.

Wow, in addition to grammar and punctuation I guess they've just completely stopped teaching math too.

There are 2.54 centimeters to an inch. So if you divide a measurement in centimeters by 2.54 you get inches. There's no coding trick, it's just simple math.

For reference, I will post the OP's code in its entirety, here:

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 Red POS to Arduino pin 11
 Green POS to Arduino pin 10
 560 ohm resistor to both LED NEG and GRD power rail
 More info at: http://goo.gl/kJ8Gl
 Original code improvements to the Ping sketch sourced from Trollmaker.com
 Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
 */

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

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

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  digitalWrite(led2,LOW);
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

(Note to OP: when sketches are this short, it makes things much easier if you post them this way, between code tags.)

Anyway, I've refactored your code, and put in a conversion to inches.
Still, I can't do everything (nor should I), and the code still needs your attention: see my comments.
Also, you might want to test out the measurements against a tape measure: I'm not 100% sure of the accuracy.

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 Red POS to Arduino pin 11
 Green POS to Arduino pin 10
 560 ohm resistor to both LED NEG and GRD power rail
 More info at: http://goo.gl/kJ8Gl
 Original code improvements to the Ping sketch sourced from Trollmaker.com
 Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
 */

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

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

void loop() {
  int duration, distance;

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

  // get duration in microseconds
  duration = pulseIn(echoPin, HIGH, 11544); // max dist = 6 ft 6 in; see comments below
  
  // The speed of sound is about 343 meters per second.
  // That's about 1125 feet per second, or 13500 inches per second.
  // Therefore, it takes 74 microseconds for sound to travel 1 inch,
  // but that's just for a one-way trip!
  // In our case, the sound is doing a round trip, so it
  // takes double the time: each inch of one-way distance
  // translates to 148 microseconds of round trip time.
  
  distance = duration / 148; // distance in inches
  // distance = (duration/2) / 29.1; // old code (for centimeters)

  if (distance == 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance / 12);
    Serial.print(" ft ");
    Serial.print(distance % 12);
    Serial.println(" in");
  }
  
  if (distance < 4) {  // Only four inches?! Are you sure??!!
    // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
    digitalWrite(led2,LOW);
  }
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  // what to do with LEDs when distance == 0 (i.e. invalid reading) ??

  delay(250); // because I figure 500 milliseconds is too long
}

I did not realise that the Transformer would still use power if the Sensor package was asleep,

The power supply would be connected to the mains whether or not any current was being taken from its output and thus would still use power. As to the sensor going to sleep, what you had said was that you wanted the read LED to turn off but not how soon after it had been turned on. If you want the whole system to turn off after a period then you have the interesting problem to solve as to what is going to turn it on again.

Hi all.

Many thanks for all your help in getting me to understand Arduino programming.

Just a quick word for Delta G, I am 66 years old and a retired "MECHANICAL ENGINEER" with a Honours Degree in said subject, a 6 years Mechanical Engineering Apprenticeship with Rolls - Royce Ltd; at the Derby, UK, works, I do therefore know how to convert Metric to English, I have been doing so since 1968, when RR transferred all their Engineering Drawings from Imperial to Metric.

True, my grammer is not so good, and I do not yet know how to write it in Arduino language or whatever it's called, that's why i asked the question, please be a little humble to use beginners who come from another discipline, no offence taken, no offence intended.

Regards

Ray

"Scientists study what is known , Engineers create what is not known "

Well if you are a Mechanical Engineer, I would expect you to not have to ask how to convert centimeters to inches. I have no way to know how old you are, and most kids these days just say "Math is stupid, I'll never use it, not how do I figure this out."

manofgresley:
I therefore just need to know how to change from centimetres to inches.

This statement would imply that you don't know how wouldn't it. How else should I read that?

Hi Delta-g

I am afraid you still don't get it, as I have previously stated, I have known how to convert Imperial to Metric and visa versa for nearly 50 years, its writing it out in Arduino sketch I had not considered doing as I know exactly what distance 20 cm's is, it was you who decide it best to use inches, as it was iyo better, so I went along with you (being a complete novice at Arduino) I only need to know how to write it into a sketch.

Regards

Ray

manofgresley:
it was you who decide it best to use inches

What the hell are you talking about. I never suggested using one unit over another? I don't care if you do it in Egyptian cubits or light-years. You said and I quote:

manofgresley:
I therefore just need to know how to change from centimetres to inches.

and I told you that was done by a simple division.

I know exactly what distance 20 cm's is, it was you who decide it best to use inches, as it was iyo better

I don't think so.
In post #6 you said

I would like inches,

which is the first mention of inches that I can find in this thread.

manofgresley:
Hi Delta-g

I am afraid you still don't get it, as I have previously stated, I have known how to convert Imperial to Metric and visa versa for nearly 50 years, its writing it out in Arduino sketch I had not considered doing as I know exactly what distance 20 cm's is, it was you who decide it best to use inches, as it was iyo better, so I went along with you (being a complete novice at Arduino) I only need to know how to write it into a sketch.

Regards

Ray

In other words, you wanted to know how to get the Arduino to do arithmetic.

Here is a demo for you to try out:

// arithmetic demo for Arduino
// by odometer - 8 Nov 2015

void setup() {

  Serial.begin(9600);

  int a = 11;
  int b = 4;
  int c, d;
  
  // addition
  c = a + b;
  Serial.print(a);
  Serial.print(" plus ");
  Serial.print(b);
  Serial.print(" is ");
  Serial.println(c);

  // subtraction
  c = a - b;
  Serial.print(a);
  Serial.print(" minus ");
  Serial.print(b);
  Serial.print(" is ");
  Serial.println(c);

  // multiplication
  c = a * b;
  Serial.print(a);
  Serial.print(" times ");
  Serial.print(b);
  Serial.print(" is ");
  Serial.println(c);

  // division (integers)
  c = a / b;  // quotient
  d = a % b;  // remainder
  Serial.print(a);
  Serial.print(" divided by ");
  Serial.print(b);
  Serial.print(" is ");
  Serial.print(c);
  Serial.print(" with a remainder of ");
  Serial.println(d);

  // division (floats)
  float x = a;  // conversion to float
  float y = b;
  float z = x / y;
  Serial.print(x);
  Serial.print(" divided by ");
  Serial.print(y);
  Serial.print(" is ");
  Serial.println(z);
}

void loop() {
}

Hi heli bob and odometer

I did say I would prefer inches, but I am more than competent to use Metric, what I meant in my statement was not to how convert one to the other but to alter the sketch, I did not realise how simple it was.

I was initially reacting to you saying i was leaving it late using cm's for the led to start flashing I wrongly assumed you were talking about using Imperial, but I still do not understand what is wrong with using centimetres, or is it a software issue either way i would use Egyptian if it was better.
Thanks to both heli bob and odometer for altering my sketch anyway, I will try to use it next week, cannot do any earlier as I am away from home at the moment.

Regards Ray

His comment wasn't about the units. His comment was about the number of centimeters. 20cm isn't very far. If the wife is that close to the wall when the light comes on, what are the chances she'll look up from her phone and see it in time?

If this were for my wife I would use one of those big spinning reflector lights like they used to put on police cars and I would have it start alerting her at about 2 meters with a loud siren. Otherwise she'll not only crash the wall but she would tear up my fancy new warning system in the process.

I still do not understand what is wrong with using centimetres,

Nothing is wrong with using centimetres nor did I suggest using inches What I was alluding to was that turning on the warning LED at 4 centimeters was not giving much warning to the driver. Nor, incidentally, would 1.57 inches.

I like the idea of a flash rate increasing as the car gets closer while reversing, as Bob suggested, but is a LED going to be visible enough?

Delta_G's spinning reflector and loud siren might possibly be overkill :D, but a loud beeper might not be a bad idea, one similar to those used as reverse-warnings on trucks, but with a 'beep-rate' that increases as distance decreases.