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.
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
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.
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.
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
// # 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;
}
}
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.
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:
// # 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);
}
}