Go Down

Topic: Using hc-sr04 sensors with less pins (Read 50 times) previous topic - next topic

Antoniobeta

Hi guys,
I´m trying to build a robot with UNO, but I need more pins. I´m using the hc-sr04 sensors.
The sensors have four pins:

  • The VCC pin
  • The GND
  • The ECHO pin
  • The TRIG pin


I thought that if I connected the Trig pin to the same pin, and firts I turned it HIGH and I read the signal of the ECHO pin of sensor one, and later I turned it LOW, and HIGH again and read the signal of ECHO pin of sensor two I could save one pin but when I did that last night the sensors gave me some errors reading distances (for example sometimes i says that the distance was 0 when there was nothing in front of the sensor)

What could be hapening? Is correct to do what I have done?

Cactusface

HI Antoniobeta,
                        I don't really see what your trying to do!!  Connect the Trig pin to the same pin? What same pin, you don't make it clear.

Here's the code I use with all 4 pin, U/S modules, does the job for me:

Where's your code, what pins are you using.... More info please.

Regards

Mel.

Code: [Select]
long scanner(long cm)
{
const int pingPin=7, EchoPin=8; // Set these to whay your using...
long duration;

// The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse before to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(EchoPin, INPUT); 

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);
digitalWrite(pingPin, LOW);
duration = pulseIn(EchoPin, HIGH);
delay(100);

// convert the time into a distance
// inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return (cm);
}
//------------------------------------------------------------------
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

Zapro


Antoniobeta

Thank you zapro!! That´s what I was looking for.

Cactusface I was meaning to connect the two trig pins to the same pin  of the arduino and read a sensor at a time.

Go Up