Hello! The US 100 Ultrasonic will be used to detect the presence of an object in just a range of 3 inches. It should be displaying a 1 if an object is detected and 0 if otherwise. I modified the codes from someone but unfortunately, it doesn’t work the way it was supposed to be and I’m having a hard time trying to find out what went wrong. I hope someone can help me. Thank you. Below would be the codes and the printscreen of the output.
/*-----( Import needed libraries )-----*/
#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define TRIGGER_PIN 11
#define ECHO_PIN 10
#define MAX_DISTANCE 14 // Maximum distance we want to ping for (in centimeters).
//Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.println("UltraSonic Distance Measurement");
Serial.println("YourDuino.com terry@yourduino.com");
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
DistanceIn = sonar.ping_in();
Serial.print("Ping: ");
Serial.print(DistanceIn); // Convert ping time to distance and print result
// (0 = outside set distance range, no ping echo)
Serial.print(" in ");
delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
DistanceCm = sonar.ping_cm();
Serial.print("Ping: ");
Serial.print(DistanceCm);
Serial.println(" cm");
if ( (DistanceCm <= 7) && (DistanceCm != 0) )
{
Serial.println("1");
}
else if ( (DistanceCm > 7) && (DistanceCm !=0) )
{
Serial.println("0");
}
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
// None
//*********( THE END )***********
Note the min range of 2 cm. So 3" (7.62 cm) should work but it’s close to the min range.
@OP;
So try a longer distance but also open up the max distance allowed, from 14 to 250 cm to allow for some measurement. Change one of the calls to return a time measurement to get the RAW data.
sonar.ping();
I’d have to look at the library and make sure it’s code is compatible w/the waveforms in the above link. Is the trigger pulse correct ? Is the code measuring the PW of the echo, and not a delay (trigger to echo). Is the conversion factor correct ?
Note the min range of 2 cm. So 3" (7.62 cm) should work but it’s close to the min range.
@OP;
So try a longer distance but also open up the max distance allowed, from 14 to 250 cm to allow for some measurement. Change one of the calls to return a time measurement to get the RAW data.
sonar.ping();
I’d have to look at the library and make sure it’s code is compatible w/the waveforms in the above link. Is the trigger pulse correct ? Is the code measuring the PW of the echo, and not a delay (trigger to echo). Is the conversion factor correct ?
What power supply are you using for the device ?
I already changed the distance but it still doesn’t work. I used a different code I found from the internet and it worked so the problem is probably in the codes. Unfortunately, the codes that worked, gives a different output to what I prefer and I’m having a hard time modifying it. Here’s the codes:
// Demo sketch
// "is sketch will output distance info via the UART port
// port assignment
// change as may be necessary
const int trigger=6;
const int echo=7;
float distance;
void setup(){
Serial.begin(9600);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
}
void loop(){
// Trigger US-100 to start measurement
// Set up trigger
digitalWrite(trigger,LOW);
delayMicroseconds(5);
// Start Measurement
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
// Acquire and convert to mtrs
distance=pulseIn(echo,HIGH);
distance=distance*0.0001657;
// send result to UART
Serial.println(distance);
delay(50);
}
Your next step to debug this is connect a two channel O’scope to the Trigger and output pin os the US device. Then check that the timeing is correct for input trigger and you do see an output pulse. Once you verify the hardware is working then you can start debugging the code.
encrypted05:
I used a different code I found from the internet and it worked so the problem is probably in the codes. Unfortunately, the codes that worked, gives a different output to what I prefer and I’m having a hard time modifying it.
By 'different output' you mean m vs inches and cm ? That's easy to fix. Change the multiplier in this line.
```
distance=distance*0.0001657;
```
To output cm, it should be 100x larger. To output inches it should be 2.54x smaller than that (cm) number.
Also what Arduino are you using ?
The library you tried doesn’t use the pulseIn() function. Instead it attempts to setup a timer to do the pulse measurement. The timer names change w/the varying MCUs used. The author appears to have tried to code the library for the 32U4 and 328.