Help getting these ultrasonic sensors to work?

Alright so I jumped into a project, I have the case made, parts put it, organized and soldered and am programming it however there is a hiccup. I need to run two ultrasonic sensors. It seems like it should be easy enough but they are not working for some reason.

I am using newping as my sensors have separate trig and echo pins. I used the original tutorial pins 12 and 12 on an arduino pro mini and it worked fine. For the project to make it more organized I used pins 6 - 9 for the ultrasonic sensors however now they read 0cm unless i tough a wire, then they read around 150cm.

So, what is going on? I figured they would work on any pins. 11 and 12 are digital pins, are they not? Do I need to move these over to pins 10 - 13?

We are not mind readers. We are not where you are, looking over your shoulder. In fact, we are having a VERY difficult time reading your code. Try posting it again. Use code tags (or highlight the code and click on the # icn above the editor).

lar3ry:
We are not mind readers. We are not where you are, looking over your shoulder. In fact, we are having a VERY difficult time reading your code. Try posting it again. Use code tags (or highlight the code and click on the # icn above the editor).

heres the code for the project but like i said the only problem i am having is that the ultrasonic sensor is not workin on pins 6-9 but did work on pins 11 and 12

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library
#include <NewPing.h>

#define LEFT_TRIGGER_PIN  6
#define LEFT_ECHO_PIN     7
#define RIGHT_TRIGGER_PIN  8
#define RIGHT_ECHO_PIN     9
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing leftsonar(LEFT_TRIGGER_PIN, LEFT_ECHO_PIN, MAX_DISTANCE);
NewPing rightsonar(RIGHT_TRIGGER_PIN, RIGHT_ECHO_PIN, MAX_DISTANCE);


#define I2C_ADDR    0x20  // Define I2C Address for controller
#define BACKLIGHT_PIN  7
#define En_pin  4
#define Rw_pin  5
#define Rs_pin  6
#define D4_pin  0
#define D5_pin  1
#define D6_pin  2
#define D7_pin  3

#define  LED_OFF  0
#define  LED_ON  1
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup() 
{
  Serial.begin(9600);
  lcd.begin (16,2);  // initialize the lcd
// Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
  lcd.setBacklight(LED_ON);
  lcd.clear();
}

void loop()  
{
  delay(500);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = leftsonar.ping(); // Send ping, get ping time in microseconds (uS).
  lcd.setCursor(0,0);
  Serial.print(uS / US_ROUNDTRIP_CM);
  lcd.print("Left = ");
  lcd.print("cm");
}

You seem to have forgotten to set the Trigger pins to OUTPUT, and the Echo pins to INPUT.

Aren't you glad yo posted your code? :)

lar3ry:
You seem to have forgotten to set the Trigger pins to OUTPUT, and the Echo pins to INPUT.

Aren’t you glad yo posted your code? :slight_smile:

The original demo code doesn’t do that though, but I will give it a try. I just feel that there is something odd since they display a distance value when I touch the wire.

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

I used a breadboard and my other pro mini with headers to try it out, same problem, reading nothing though if i touch the metal for the trigger pin it reads around 150cm, I'm going to try other pins and see if 11-13 works.

hmm now pins 2 and 3 are working i had a bad breadboarding wire, i guess ill try it on the other one again.

yeah not working, still reads 150 if i touch it, wtf.

I tried splitting up the wires and it worked on/off, so maybe the insulation is too thin and somehow allowing interference.

uhg i tried different wires, moved te trigger to pin 4 and echo to pin 2 to space them out and it's displaying stuff but its almost completely random, what the hell?

The others are acting the same too on a different arduino, I guess these ones are just too shitty to work properly at anything except for close range and even then they are doing a crap job, not changing the distance or displaying 0cm. So much for my ultrasonic measuring tape, wasted an enclosure $20.

Oh, I didn't notice you were using the library. It probably sets the pin modes for you.

Sounds like you might have a flaky ground or +5V on the one that gives random stuff. I have some super-cheap ones that I got from China for about $1.50 each, and they work just fine.

If you look at the datasheet, though, you'll find out that they will not measure down to 0, or over about 150 cm.

lar3ry: Oh, I didn't notice you were using the library. It probably sets the pin modes for you.

Sounds like you might have a flaky ground or +5V on the one that gives random stuff. I have some super-cheap ones that I got from China for about $1.50 each, and they work just fine.

If you look at the datasheet, though, you'll find out that they will not measure down to 0, or over about 150 cm.

Well yesterday these ones were measuring 300cm easily, I'm not sure what happened now, but I guess the project is dead until I figure it out, though I doubt that will happen. :/

The farther out you are trying to measure, the worse the consistency. If pointed at a nice, smooth surface, you'll get that, but for consistent results, I find it's less.

Anyway, hvae a look at this. It doesn't use the NewPing library, but just directly uses the module. Just duplicate the trigger and echo , for the other module.

const int trigPin = 2;
const int echoPin = 4;

void setup() {
  Serial.begin(19200);
  pinMode(trigPin, OUTPUT);    // Trigger pin to OUTPUT
  pinMode(echoPin, INPUT);     // Echo Pin to INPUT 
}

void loop() {
  unsigned long  duration;
  float inches, cm;
  // Send a ranging ping. Return the round trip time
  duration = ping();

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print(" in.,  ");
  Serial.print(cm);
  Serial.print(" cm.");
  Serial.println();
  delay(1000);
}

unsigned long ping() {
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  return pulseIn(echoPin, HIGH);
}

float microsecondsToInches(long microseconds) {
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return (float)(microseconds / 73.75 / 2);
}

float microsecondsToCentimeters(long microseconds) {
  return (float)(microseconds / 29.03 / 2);
}

lar3ry:
The farther out you are trying to measure, the worse the consistency. If pointed at a nice, smooth surface, you’ll get that, but for consistent results, I find it’s less.

Anyway, hvae a look at this. It doesn’t use the NewPing library, but just directly uses the module. Just duplicate the trigger and echo , for the other module.

const int trigPin = 2;

const int echoPin = 4;

void setup() {
  Serial.begin(19200);
  pinMode(trigPin, OUTPUT);    // Trigger pin to OUTPUT
  pinMode(echoPin, INPUT);     // Echo Pin to INPUT
}

void loop() {
  unsigned long  duration;
  float inches, cm;
  // Send a ranging ping. Return the round trip time
  duration = ping();

// convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

Serial.print(inches);
  Serial.print(" in.,  “);
  Serial.print(cm);
  Serial.print(” cm.");
  Serial.println();
  delay(1000);
}

unsigned long ping() {
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  return pulseIn(echoPin, HIGH);
}

float microsecondsToInches(long microseconds) {
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return (float)(microseconds / 73.75 / 2);
}

float microsecondsToCentimeters(long microseconds) {
  return (float)(microseconds / 29.03 / 2);
}

Alright I’ll look at that later, right now im tired of working on this thing.

Try taking out your modules from your project and testing each one in isolation. If they don't work that would mean that you have fried them or they are simply dead. Also check if you have connected the 5V and the GND pins properly i.e. you have not reversed them.

Which ultrasonic sensors are you using? try to Google the model number to find a datasheet which you can use as a reference while debugging things.

The NewPing library has an example code (Examples > NewPing > PewPing15Sensors) which can be used to interface 15 ultrasonic modules, try tweaking the example to interface two modules.

#define LEFT_TRIGGER_PIN  6
#define LEFT_ECHO_PIN     7
#define RIGHT_TRIGGER_PIN  8
#define RIGHT_ECHO_PIN     9
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing leftsonar(LEFT_TRIGGER_PIN, LEFT_ECHO_PIN, MAX_DISTANCE);
NewPing rightsonar(RIGHT_TRIGGER_PIN, RIGHT_ECHO_PIN, MAX_DISTANCE);


#define I2C_ADDR    0x20  // Define I2C Address for controller
#define BACKLIGHT_PIN  7
#define En_pin  4
#define Rw_pin  5
#define Rs_pin  6
#define D4_pin  0
#define D5_pin  1
#define D6_pin  2
#define D7_pin  3

#define  LED_OFF  0
#define  LED_ON  1
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

So, are pins 6 and 7 for the LCD or for the Ping sensors? Both is the wrong answer!

PaulS: ```

define LEFT_TRIGGER_PIN  6

define LEFT_ECHO_PIN     7

define RIGHT_TRIGGER_PIN  8

define RIGHT_ECHO_PIN     9

define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing leftsonar(LEFT_TRIGGER_PIN, LEFT_ECHO_PIN, MAX_DISTANCE); NewPing rightsonar(RIGHT_TRIGGER_PIN, RIGHT_ECHO_PIN, MAX_DISTANCE);

Nope, the LCD is using I2C on analog pins 4 and 5, thats just telling it the pin after the I2C adapter

define I2C_ADDR    0x20  // Define I2C Address for controller

define BACKLIGHT_PIN  7

define En_pin  4

define Rw_pin  5

define Rs_pin  6

define D4_pin  0

define D5_pin  1

define D6_pin  2

define D7_pin  3

define  LED_OFF  0

define  LED_ON  1

LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);




So, are pins 6 and 7 for the LCD or for the Ping sensors? Both is the wrong answer!

uhg my answer was put in the code.

No, my lcd is using I2C

Nope, the LCD is using I2C on analog pins 4 and 5, thats just telling it the pin after the I2C adapter

And I suppose when you turn on the backlight in setup(), you aren't messing with the left echo pin? Or is this just another case of "well, that code and wiring has been changed"?

I had huge problems with these sensors too, the issue turned out to be a combination of voltage dips and bad wiring. I would suggest running a decoupling capacitor across each sensor, feeding them from an external 5v power source and taking ten readings at a time and taking removing outliers.

Additionally you should stop moving (if it's a robot) for the scan