1

I'm using the red Ultrasonic Range Sensor with

Sig -> Digital Pin 4

VCC -> 5V

GND -> GND

The code uploads fine and i can hear the sensor clicking away, but for some reason it won't print the distance into the Serial Monitor.

//Settings
int echoPin= 4;
int trigPin= 4;
unsigned long pulsetime = 0;
unsigned distance = 0;
unsigned OldDistance = 0;
void setup (){
 pinMode (echoPin, INPUT);
 pinMode (trigPin, OUTPUT);
 Serial.begin(9600); 
}
void loop(){
 //Work out the distance
 digitalWrite(trigPin, LOW);
 delayMicroseconds(100);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(100);
 digitalWrite(trigPin, LOW);
 pulsetime = pulseIn(echoPin, HIGH);
 distance = pulsetime / 58;
 delay(10);
 //Send value if it's changed
 if (OldDistance != distance) {
 Serial.print(distance); 
 OldDistance = distance;
 }
 delay(50); // Wait .1 Seconds
} 
asked Apr 12, 2016 at 22:14
3
  • What kind of sensor? Commented Apr 12, 2016 at 22:26
  • Is it this one? rhydolabz.com/wiki/?p=895 Commented Apr 12, 2016 at 22:36
  • Yup, that was the one. Commented Apr 12, 2016 at 23:31

2 Answers 2

1

You have to manually switch from OUTPUT (for sending the trigger pulse) and INPUT (for reading the distance) mode on the shared IO pin.

//Work out the distance
pinMode(trigPin, OUTPUT); // << ADD THIS
digitalWrite(trigPin, LOW);
delayMicroseconds(100);
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT); // << ADD THIS
pulsetime = pulseIn(echoPin, HIGH);
distance = pulsetime / 58;
delay(10);

To save some confusion you might like to change your code to just use sigPin instead of a pair of pins that are both the same pin. That way you would avoid confusing bits like:

pinMode (echoPin, INPUT);
pinMode (trigPin, OUTPUT);

Which of course is the equivalent to:

pinMode (4, INPUT);
pinMode (4, OUTPUT);
answered Apr 12, 2016 at 22:40
1
  • Brilliant! Problem solved and i took your advice to just use a trigpin instead of trig and echo. Thanks! Commented Apr 12, 2016 at 23:30
0

Two suggestions:

In setup() after you open the serial port, execute something like Serial.println("Sensor Test Program");, and in loop, print the distance value with something like Serial.print("dist: "); Serial.println(distance);

That will tell you two things:

  1. whether your port is open and working at all; and
  2. whether control reached your output statement (in loop) and that it tried to print something.

In this case, it seems unlikely that control didn't reach Serial.print(distance); but more information, at least in the early stages, helps when things don't work as you'd expected them to.

You should have seen some output from your output statement, even if it was wrong. No output suggests:

  • control didn't get there (perhaps due to a program crash, especially if this is a fragment of a large program);
  • the terminal program and the Arduino's baud rates don't match;
  • the terminal program was opened to the wrong port;
  • the terminal program wasn't opened at all;
  • the calculation (or operation of the device, perhaps in the manner @majenko suggested) failed such that the distance calculation always yielded zero, and the 'if' statement was therefore never satisfied (my guess), i.e., back to my first point, control didn't get there.
answered Apr 12, 2016 at 23:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.