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
}
-
What kind of sensor?Majenko– Majenko2016年04月12日 22:26:04 +00:00Commented Apr 12, 2016 at 22:26
-
Is it this one? rhydolabz.com/wiki/?p=895Majenko– Majenko2016年04月12日 22:36:43 +00:00Commented Apr 12, 2016 at 22:36
-
Yup, that was the one.40089972– 400899722016年04月12日 23:31:16 +00:00Commented Apr 12, 2016 at 23:31
2 Answers 2
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– 400899722016年04月12日 23:30:39 +00:00Commented Apr 12, 2016 at 23:30
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:
- whether your port is open and working at all; and
- 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.