Pages: [1]   Go Down
Author Topic: Problem using 2 URM37 - Ultrasonic sensors  (Read 1125 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!
Not sure if this is my first post, so firstly thanks to all of you that make possible this amazing forum!

I´ve been trying to make an arduino project (Therething) based around 2 URM37 V3.2 Ultrasonic sensors. The hardware is very simple and basically it sends MIDI data when Distance is recieved from the two sensor, uses a 20x4 LCD screen, an encoder with push button and 2 led´s.

The sensors: http://milesburton.com/URM37_Ultrasonic_Distance_Measurement_Library

Schematic: http://www.buildbrighton.com/images/c/cc/Theremin-schematic.png

The project: http://www.buildbrighton.com/wiki/Therething

The library: https://github.com/milesburton/URM37

The Sketch: http://www.maledictis.com/multiman/Therething_Julian.ino

I have to thank one of the guys responsible of the project, he helped me a lot, but is momment for me to ask here as he no longer have the old hardware. When If  first saw the project (1,5 years ago) I downloaded the sketch and schematic, but I didnt started puting all together untill 3 months ago. Now the project has changed, and they are using HC-SR04 sensors, and a slightly different setup and code.

I decided to stick with the old design, as I bought already the URM37, not cheap at all, and they work better.
So... I tuned the sketch a bit and Ive got almost everything working, the sketch compiles in Arduino IDE1.x, but I just get response from one of the sensors. They both work by themselves, I can swap pins and they will react. The sensor that works send Midi out correctly.

I´ve tryed different things, firstly the old Sketch used to include "NewSoftSerial", I had to swap this to "SoftwareSerial" as its included in Arduino 1.x. I´ve noticed when downloading the URMSerial Library (URM37 sensor library) that some versions include a modified version of "SofwareSerial" and the old versions one of "NewSoftSerial"... so maybe my problem resides somewhere here... I also noticed there are different versions of this Library around, and not sure which one is the right one for me.

Hope somebody can help me out!
Thanks in advance.
Julian
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25713
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where's your test code that just gets and displays readings from your two sensors?
Does that work as you expect?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL thanks for the answer.

What you mean by the test code, the URM37 example files? all the examples I found are for 1 sensor. So apart from my Sketch I didnt try any other 2 sensor Sketch.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25713
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you've got close on a thousand lines of C predicated on two sensors working together, when you haven't proved that you have two working sensors?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well somehow Ive proved Ive got two working sensors, as I can swap the pins of each sensor and they respond accordingly, also using the example sketch from the URMSerial library (MeasurementBySoftwareSerial), I can see reading from the sensors, but just one at the time.

The Sketch uses the Serial port for MIDI output, and the Serial comunication with each sesnors trough Analog pins 0-1 for Sensor L, and  A2-A3 for Sensor R.

Sorry Im not an expert in code, so I gathered as much information as I could, test it, ask the creators of the projects... so finally decided to come here in case someone have faced this issue before.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25713
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <URMSerial.h>

const byte SENSOR_L_RX = 15;
const byte SENSOR_L_TX = 14;
const byte SENSOR_R_RX = 17;
const byte SENSOR_R_TX = 16;

URMSerial sensor_L;
URMSerial sensor_R;

void setup()
{
  sensor_L.begin(SENSOR_L_RX, SENSOR_L_TX, 9600);
  sensor_L.setTimeout(20);
  sensor_R.begin(SENSOR_R_RX, SENSOR_R_TX, 9600);
  sensor_R.setTimeout(20);

  Serial.begin(115200); // DEBUG PORT
}

void loop ()
{
  int left  = getMeasurement(sensor_L);
  int right = getMeasurement(sensor_R);
  //
  // an exercise for the reader.
  //
}
{
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so I changed those lines in the code, just the definition of the pins is different, but I still get no response from both of the sensors.

Also made a small Sketch to just read the values, but I just read from one. Might be an issue with the wrong SoftSerial library?

Thanks a lot!

Code:
#include <URMSerial.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

#define MENU_TIMEOUT 5000 // Milliseconds until the menu timesout

#ifdef IR_SENSOR
#define SENSOR_MIN 100
#define SENSOR_MAX 500 // Range of valid sensor readings
#else
#define SENSOR_MIN 10 // Lowest valid sensor reading
#define SENSOR_MAX 50 // Range of valid sensor readings
#endif

#define SENSOR_RANGE SENSOR_MAX

#define SENSOR_SCALE_FACTOR (127.0 / (float)SENSOR_RANGE)
#define SCALE_LENGTH 7

const byte SENSOR_L_RX = 15;
const byte SENSOR_L_TX = 14;
const byte SENSOR_R_RX = 17;
const byte SENSOR_R_TX = 16;

enum lcd_pins {
  LCD_RS = 11,
  LCD_RW = 10,
  LCD_ENABLE = 9,
  LCD_D4 = 8,
  LCD_D5 = 7,
  LCD_D6 = 6,
  LCD_D7 = 5
};

LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_ENABLE, LCD_D4,LCD_D5,LCD_D6,LCD_D7);

URMSerial sensor_L;
URMSerial sensor_R;

void setup()
{
  lcd.begin(20,4);
  sensor_L.begin(SENSOR_L_RX, SENSOR_L_TX, 9600);
  sensor_L.setTimeout(20);
  sensor_R.begin(SENSOR_R_RX, SENSOR_R_TX, 9600);
  sensor_R.setTimeout(20);

  Serial.begin(115200); // DEBUG PORT

}

void loop ()

{
  int left  = getMeasurement(sensor_L);
  int right = getMeasurement(sensor_R);
//  lcd.setCursor(0,1);
//  lcd.print(left);
//  delay(200);
//  lcd.clear();
//  lcd.setCursor(0,2);
//  lcd.print(right);
//  delay(200);
//  lcd.clear();

  //
  // an exercise for the reader.
  //
}
int getMeasurement(URMSerial s)
{
  int value = 0; // This value will be populated
  // Request a distance reading from the URM37
  switch(s.requestMeasurementOrTimeout(DISTANCE, value)) // Find out the type of request
  {
  case DISTANCE: // Double check the reading we recieve is of DISTANCE type
    value = max(0, min(value-SENSOR_MIN, SENSOR_MAX)); // use the 10-60cm range
    break;
  default:
    value = SENSOR_MAX;
    break;
  }
  return value;
 
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so Ive been playing a lot with the sketch, making a very simple one that just uses the sensors and serial comunication to see the measurement, it works like before, both sensors are propperly wired and they respond "individually" if I swap pins in the code or in the arduino board... but not together.

I´m downloading a clean installation of Arduino 1.04 and the latest URMSerial library to see if I messed things up with the libraries...

The more simple Sketch

Code:
// # Description:
// # The sketch for using the URM37 from DFRobot based on the Software Serial library
// # You could drive the URM37 with 2 digital pins on your Arduino board, and it works stable.

// # Connection:
// #       VCC (Arduino)   -> Pin 1 VCC (URM37 V3.2)
// #       GND (Arduino)   -> Pin 2 GND (URM37 V3.2)
// #       Pin 16 (Arduino) -> Pin 9 TXD (URM37 V3.2) Senor L
// #       Pin 17 (Arduino) -> Pin 8 RXD (URM37 V3.2) Senor L
// #       Pin 14 (Arduino) -> Pin 9 TXD (URM37 V3.2) Senor R
// #       Pin 15 (Arduino) -> Pin 8 RXD (URM37 V3.2) Senor R
// #

#include "URMSerial.h"
#include <SoftwareSerial.h>

// The measurement we're taking
#define DISTANCE 1

URMSerial urm_L;
URMSerial urm_R;

const byte SENSOR_L_RX = 17;
const byte SENSOR_L_TX = 16;
const byte SENSOR_R_RX = 15;
const byte SENSOR_R_TX = 14;

void setup() {

  Serial.begin(115200);                  // Debugg
  urm_L.begin(SENSOR_L_RX,SENSOR_L_TX,9600);                 // RX Pin, TX Pin, Baud Rate for Sensor L
  urm_L.setTimeout(20);
  urm_R.begin(SENSOR_R_RX,SENSOR_R_TX,9600);                 // RX Pin, TX Pin, Baud Rate for Sensor R
  urm_R.setTimeout(20);
  Serial.println("URM37 Julian Calvo Tests");   // Begining
 
}

void loop()
{
  Serial.print("Measurement: ");
  Serial.println(getMeasurement());  // Output measurement
  delay(100);
  Serial.print("Measurement2: ");
  Serial.println(getMeasurement2());  // Output measurement
  delay(100);
}

int getMeasurement()
{
  int value = 0; // This value will be populated
  // Request a distance reading from the URM37
  urm_L.requestMeasurementOrTimeout(DISTANCE, value); // Find out the type of request
  {
//  case DISTANCE: // Double check the reading we recieve is of DISTANCE type
  return value;
  }
}
int getMeasurement2()
{
  int value2 = 0; // This value will be populated
  // Request a distance reading from the URM37
  urm_R.requestMeasurementOrTimeout(DISTANCE, value2); // Find out the type of request
  {
//  case DISTANCE: // Double check the reading we recieve is of DISTANCE type
  return value2;
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys!

So Continuing with my mission in making this two sensors recieve values...

I made this simple Sketch for testing everything. Seems to be working for the guy who posted it online, with the only diference that in his example, "SofwareSerial" is not commented, instead just URMSerial.

Link to this post : http://arduino.cc/forum/index.php/topic,49284.0.html

This Simple Sketch switch an LED for each sensor On or OFF if the value recieved is under 25. As expected I just get readings from the second sensor.

Any help will be highly appreciated!!
Julian

Code:
#include "URMSerial.h"
#include "SoftwareSerial.h"

// The measurement we're taking
#define DISTANCE 1
#define TEMPERATURE 2
#define ERROR 3
#define NOTREADY 4

int ledPin = 18;
int ledPin2 = 19;
URMSerial urm;
URMSerial urm2;

void setup() {
Serial.begin(9600);
urm.begin(15,14,9600);
urm.setTimeout(20);
urm2.begin(17,16,9600);
urm2.setTimeout(20);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}

void loop()

{
int value;
urm.requestMeasurementOrTimeout(DISTANCE, value);
Serial.println(value);
if(value < 25)
{
digitalWrite(ledPin, HIGH);
}else
{
digitalWrite(ledPin, LOW);
}
delay(200);
int value2;
urm2.requestMeasurementOrTimeout(DISTANCE, value2);
Serial.println(value2);
if(value2 < 25)
{
digitalWrite(ledPin2, HIGH);
}else
{
digitalWrite(ledPin2, LOW);
}
delay(200);
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys, so I managed to solve my problems with a few changes...

Firstly, I decided to contact DFRobot Tech Support, as my issues were related to the URM37 sensors, and I have to say the help from DFRobot support was excellent. They tested my example and confirmed that Sofware Serial wasnt behaving as expected when using more than one virtual port, it seems like just the sencond initilized sensor in the setup will react...

They suggested me either try PWM mode, or directly using non emulated Serial comunication, using TX RX pins. So I got my Arduino MEGA 2560 and addapted the Hardware Serial example succesfully to see the two readings...

Now the whole Sketch is working correctly using TX1-RX1 & TX2-RX2 for the sensors, it still leaves two serial ports free in my case one will be for MIDI and the other still free for debugging.

Now Im back to print my PCB and make a nice case in my CNC!!
All the best guys!
Julian

In case somebody is interested here is the basic code:

Code:
// # Product name: URM V3.2 ultrasonic sensor TTL connection with Arduino

// # Description:
// # The sketch for using the URM37 from DFRobot to read values (0-300) from an ultrasound sensor (3m sensor)
// #   and writes the values to the serialport

// # Connection Sensor 1:
// #       Pin A0 (Arduino MEGA 54) -> Pin 1 VCC (URM V3.2)
// #       GND (Arduino MEGA)   -> Pin 2 GND (URM V3.2)
// #       Pin 18 (Arduino MEGA) -> Pin 9 TXD (URM V3.2)
// #       Pin 19 (Arduino MEGA) -> Pin 8 RXD (URM V3.2)
// # Connection Sensor 2:
// #       Pin A1 (Arduino MEGA 55) -> Pin 1 VCC (URM V3.2)
// #       GND (Arduino MEGA)   -> Pin 2 GND (URM V3.2)
// #       Pin 16 (Arduino MEGA) -> Pin 9 TXD (URM V3.2)
// #       Pin 17 (Arduino MEGA) -> Pin 8 RXD (URM V3.2)
// #

int URPower = 54; // Ultrasound sensor 1 power pin
int URPower2 = 55; // Ultrasound sensor 2 power pin

int val = 0;
int val2 = 0;
int USValue = 0;
int USValue2 = 0;
int timecount = 0; // Echo counter
boolean flag=true;
uint8_t DMcmd[4] = {
  0x22, 0x00, 0x00, 0x22}; //distance measure command

void setup() {
  Serial.begin(9600);                  // Sets the baud rate to 9600
  Serial1.begin(9600);                  // Sets the baud rate sensor 1 to 9600
  Serial2.begin(9600);                  // Sets the baud rate sensor 2 to 9600

  Serial.println("Init the sensor");
  pinMode(URPower, OUTPUT);
  pinMode(URPower2, OUTPUT);
  digitalWrite(URPower, HIGH); // Set to High
  digitalWrite(URPower2, HIGH); // Set to High
  delay(200); //Give sensor some time to start up --Added By crystal  from Singapo, Thanks Crystal.
}

void loop()
{
  flag = true;
  //Sending distance measure command :  0x22, 0x00, 0x00, 0x22 ;

  for(int i=0;i<4;i++)
  {
    Serial1.write(DMcmd[i]);
  }
  for(int i=0;i<4;i++)
  {
    Serial2.write(DMcmd[i]);
  }

  delay(40); //delay for 75 ms
  unsigned long timer = millis();
  while(millis() - timer < 30)
  {
    if(Serial1.available()>0)
    {
      int header=Serial1.read(); //0x22
      int highbyte=Serial1.read();
      int lowbyte=Serial1.read();
      int sum=Serial1.read();//sum

      if(header == 0x22){
        if(highbyte==255)
        {
          USValue=65525;  //if highbyte =255 , the reading is invalid.
        }
        else
        {
          USValue = highbyte*255+lowbyte;
        }

        Serial.print("Distance=");
        Serial.println(USValue);
        delay(50);

        flag=false;
      }
      else{
        while(Serial.available())  byte bufferClear = Serial.read();
        break;
      }
if(Serial2.available()>0)
    {
      int header=Serial2.read(); //0x22
      int highbyte=Serial2.read();
      int lowbyte=Serial2.read();
      int sum=Serial2.read();//sum

      if(header == 0x22){
        if(highbyte==255)
        {
          USValue2=65525;  //if highbyte =255 , the reading is invalid.
        }
        else
        {
          USValue2 = highbyte*255+lowbyte;
        }
       
        Serial.print("Distance 2=");
        Serial.println(USValue2);
        delay(50);

        flag=false;
      }
      else{
        while(Serial.available())  byte bufferClear = Serial.read();
        break;
      }
    }
  }
  delay(20);
  }
}
Logged

Pages: [1]   Go Up
Jump to: