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
} 
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);
  • Brilliant! Problem solved and i took your advice to just use a trigpin instead of trig and echo. Thanks! – 40089972 Apr 12 '16 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.