ArduinoGeneral

URM07 getting 0cm with Adafruit feather M0 wifi

userHead simone_andreini 2020-01-13 19:37:38 6841 Views19 Replies
Hi everyone, I'm having truble with setting up the example code for multiple sensors and also for implementing a small modification of it.
I don't know why but it constantly reads 0cm even with a dedicated Serial1 port on the adafruit.

i'm using visual studio code with platformIO installed.
Code: Select all

#define NUMBER_OF_SENSORS 3

class UltrasonicSensor{

    private:
        #define MAX_DISTANCE 750
        unsigned char i=0,j=0;
        unsigned char Rx_DATA[8]; //6 for the message, 1 for the start of the comunication and 1 for the end. tot=8
        unsigned int smallest_distance = MAX_DISTANCE; //750mm is the maximum readable    


        /**
         * Distance Measurement Package
         * it's a group of 12 bites
         * FORMAT: {0x55,0xAA,0x(Sensor_number)(Sensor_number),0x00,0x02,0x(Sensor_number)(Sensor_number+1)}
         * CRC: the last field is the 1 byte checksum
         * */
        unsigned char CMD[NUMBER_OF_SENSORS][6]={
            {0x55,0xAA,0x11,0x00,0x02,0x12}, //first sensor
            {0x55,0xAA,0x22,0x00,0x02,0x23}, //second one
            {0x55,0xAA,0x33,0x00,0x02,0x34}, //...
            //{0x55,0xAA,0x44,0x00,0x02,0x45}, //forth one if needed
        }; 

        /**
         * Function that uses the UART protocol to read the sensors distance
         * */
        void readAllSensors() {
            for(j=0;j<NUMBER_OF_SENSORS;j++){
                    for(i=0;i<6;i++){
                        //Serial.print("writing instruction:");
                        //Serial.println(CMD[j][i]);
                        Serial1.write(CMD[j][i]);
                    }
                delay(150); //wait for the data to be returned
                readDataIn();
            }
                
        }

        /**
         * Function that performs the reading from the serial1 port with uart protocol.
         * it uses the global variables given by the manifacturer for this class
         * */
        void readDataIn(){
            unsigned int distance=0;
            //Read Returned Value
            //Serial.println("DEBUG pre while");
            
            while(Serial1.available()){            
                //Serial.println("while DEBUG"); 
                Rx_DATA[i++]= Serial1.read();
                //Serial.print("recieved data: ");
                //Serial.println(Rx_DATA[i]);
                //Read distance value if not overwritten will give back the previous value
                distance=( (Rx_DATA[5]<<8) | Rx_DATA[6] ); 
                //Serial.print("DEBUG: i value while reading = ");
                //Serial.println(i);
            }

            if(isCheckSumOk()){
                printDistance(distance);
                analyzeDistance(distance);
                
            }
        }

        /**
         * Funciton that performs a 1 byte checksum on the reading
         * MODE: the 6th data is the checksum value. so is the one that will be compared to the one obtained
         * NOTE: the Rx_DATA contains also the 0 and 7 value in memory, so they won't be needed to be analyzed
         *          so the 6th value, but we need to discard the 0th one.
         * */
        boolean isCheckSumOk(){
            //this variable
            uint8_t check = 0;
            for (int k = 0; k < 6; k++)
            {
                //Serial.print("Debug checksum: Rx= ");
                //Serial.println(Rx_DATA[k]);
                check = check + Rx_DATA[k];
            }
            if (check == Rx_DATA[6])
            {
                return true;
            }else{
                return false;
            }           
        }

        /**
         * Function that checks if the measured distance is smaller than the smallest one currently measured
         * */
        void analyzeDistance(unsigned int readed){
            if (readed < smallest_distance)
            {
                smallest_distance = readed;
            }
        }

        /**
         * function that prints out the distance measured from the module
         * */
        void printDistance(int distance){
            Serial.print("URM07-UART module["); //print distance
            Serial.print(j);
            Serial.print("]get_Dis= ");
            Serial.print(distance);
            Serial.println(" cm");
        }
    
    public:
        void setup() {
            Serial1.begin(19200); //Serial1: Ultrasonice Sensors Serial Communication Port, baudrate: 19200
            //Serial.begin(19200); //Serial: USB Serial Monitor
            Serial.println(Serial1.available());
        }

        /**
         * Function that returns the smallest distance measured from all the sensors
         * */
        int getSmallestDistance(){
            //the smallest distance has to be reset to the max measureable in order to get the updated minimum
            smallest_distance = MAX_DISTANCE;
            readAllSensors();
            return smallest_distance;
        }
        

};
Code: Select all
#include <Arduino.h>
#include "UltrasonicSensor.h"

UltrasonicSensor sensor;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  delay(2000);
  sensor.setup();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  sensor.getSmallestDistance();
}



I've also tried to use an arduino UNO and from the LEDs on the board it seems that it never transmit the data but only recieves it, but from debugging it is the opposite.



someone had the same problem and know hot to use those sensors? because it's kinda wierd that even the example found here https://wiki.dfrobot.com/URM07-UART_Ult ... 3#target_4 does not work at all.
2020-02-14 23:33:21 hi i've manage to make the sensor work most of the times. in some situation, with unkown causes, they measure 65535cm. how can i fix that problem? userHeadPic simone_andreini
2020-02-14 23:33:21 hi i've manage to make the sensor work most of the times. in some situation, with unkown causes, they measure 65535cm. how can i fix that problem? userHeadPic simone_andreini
2020-01-15 19:41:42 ok i think that is something.

Could you also provide a code segment that can be used to program those sensors?
userHeadPic simone_andreini
2020-01-15 19:41:42 ok i think that is something.

Could you also provide a code segment that can be used to program those sensors?
userHeadPic simone_andreini
2020-01-15 05:59:21
simone_andreini wrote: I've also tried to set the addresses with this library https://github.com/DFRobot/DFUART
but still the sensors replies once every 5/10 loops which is too slow for my application.
also that library code is very slow and 3/4 loops provides a wrong result. So i would like to use the wiki one otherwise those sensors won't be very userfull.

Still i've checked that the checksum (fixed from the original post with a k=0) is getting worg data from the sensors
userHeadPic simone_andreini
2020-01-15 05:59:21
simone_andreini wrote: I've also tried to set the addresses with this library https://github.com/DFRobot/DFUART
but still the sensors replies once every 5/10 loops which is too slow for my application.
also that library code is very slow and 3/4 loops provides a wrong result. So i would like to use the wiki one otherwise those sensors won't be very userfull.

Still i've checked that the checksum (fixed from the original post with a k=0) is getting worg data from the sensors
userHeadPic simone_andreini
2020-01-15 05:40:12
simone_andreini wrote: I've also tried to set the addresses with this library https://github.com/DFRobot/DFUART
but still the sensors replies once every 5/10 loops which is too slow for my application.
also that library code is very slow and 3/4 loops provides a wrong result. So i would like to use the wiki one otherwise those sensors won't be very userfull.
userHeadPic simone_andreini
2020-01-15 05:40:12
simone_andreini wrote: I've also tried to set the addresses with this library https://github.com/DFRobot/DFUART
but still the sensors replies once every 5/10 loops which is too slow for my application.
also that library code is very slow and 3/4 loops provides a wrong result. So i would like to use the wiki one otherwise those sensors won't be very userfull.
userHeadPic simone_andreini
2020-01-15 05:15:37 Hi, thanks for the advice
nana.wang wrote:Hi thank you for your support and contacting.
Please check hardware connection as wiki shows. For the mainboard maybe has special power supply problem, please check it first.
If it is not hardware connection problem, please run the sample code then figure whether is the code problem.
the connection is correct. as shown in the wiky i've connected 3v to vcc, GND to GND of the feather M0, the sensor TX to the RX of the M0 and the RX of the sensor to the TX of the board.

The sample code does not work unfortunatly. it seems like the sensor never writes back to the board.

I've also tried to set the addresses with this library https://github.com/DFRobot/DFUART
but still the sensors replies once every 5/10 loops which is too slow for my application.
nana.wang wrote: Here are some notes:
1. With a single URM07 module, a generic device address 0xAB can be used instead if the device address is unknown.
2. Before the power on the URM07 module is not started, set the TX port to high and lower the RX port, and maintaining more than 1s can enable the module parameters to revert to factory settings.
3. After the factory reset, the module can not start normally to enter a normal communication state, the on-board LED light flash with 10Hz. The module starts normally when powered back on.
4. After the module is started, the module on-board LED only lights up the indication during the process of receiving the data and processing, and the LED goes out when in the standby state.
  • 1. i've tried that right now but i do not get any response from it
Can you indicate me any guide to proper program those sensors with their address (instead of using that library that i've found), and then i can test the wiki program. because right now it does not work at all.
userHeadPic simone_andreini
2020-01-15 05:15:37 Hi, thanks for the advice
nana.wang wrote:Hi thank you for your support and contacting.
Please check hardware connection as wiki shows. For the mainboard maybe has special power supply problem, please check it first.
If it is not hardware connection problem, please run the sample code then figure whether is the code problem.
the connection is correct. as shown in the wiky i've connected 3v to vcc, GND to GND of the feather M0, the sensor TX to the RX of the M0 and the RX of the sensor to the TX of the board.

The sample code does not work unfortunatly. it seems like the sensor never writes back to the board.

I've also tried to set the addresses with this library https://github.com/DFRobot/DFUART
but still the sensors replies once every 5/10 loops which is too slow for my application.
nana.wang wrote: Here are some notes:
1. With a single URM07 module, a generic device address 0xAB can be used instead if the device address is unknown.
2. Before the power on the URM07 module is not started, set the TX port to high and lower the RX port, and maintaining more than 1s can enable the module parameters to revert to factory settings.
3. After the factory reset, the module can not start normally to enter a normal communication state, the on-board LED light flash with 10Hz. The module starts normally when powered back on.
4. After the module is started, the module on-board LED only lights up the indication during the process of receiving the data and processing, and the LED goes out when in the standby state.
  • 1. i've tried that right now but i do not get any response from it
Can you indicate me any guide to proper program those sensors with their address (instead of using that library that i've found), and then i can test the wiki program. because right now it does not work at all.
userHeadPic simone_andreini
2020-01-13 19:37:38 Hi everyone, I'm having truble with setting up the example code for multiple sensors and also for implementing a small modification of it.
I don't know why but it constantly reads 0cm even with a dedicated Serial1 port on the adafruit.

i'm using visual studio code with platformIO installed.
Code: Select all

#define NUMBER_OF_SENSORS 3

class UltrasonicSensor{

    private:
        #define MAX_DISTANCE 750
        unsigned char i=0,j=0;
        unsigned char Rx_DATA[8]; //6 for the message, 1 for the start of the comunication and 1 for the end. tot=8
        unsigned int smallest_distance = MAX_DISTANCE; //750mm is the maximum readable    


        /**
         * Distance Measurement Package
         * it's a group of 12 bites
         * FORMAT: {0x55,0xAA,0x(Sensor_number)(Sensor_number),0x00,0x02,0x(Sensor_number)(Sensor_number+1)}
         * CRC: the last field is the 1 byte checksum
         * */
        unsigned char CMD[NUMBER_OF_SENSORS][6]={
            {0x55,0xAA,0x11,0x00,0x02,0x12}, //first sensor
            {0x55,0xAA,0x22,0x00,0x02,0x23}, //second one
            {0x55,0xAA,0x33,0x00,0x02,0x34}, //...
            //{0x55,0xAA,0x44,0x00,0x02,0x45}, //forth one if needed
        }; 

        /**
         * Function that uses the UART protocol to read the sensors distance
         * */
        void readAllSensors() {
            for(j=0;j<NUMBER_OF_SENSORS;j++){
                    for(i=0;i<6;i++){
                        //Serial.print("writing instruction:");
                        //Serial.println(CMD[j][i]);
                        Serial1.write(CMD[j][i]);
                    }
                delay(150); //wait for the data to be returned
                readDataIn();
            }
                
        }

        /**
         * Function that performs the reading from the serial1 port with uart protocol.
         * it uses the global variables given by the manifacturer for this class
         * */
        void readDataIn(){
            unsigned int distance=0;
            //Read Returned Value
            //Serial.println("DEBUG pre while");
            
            while(Serial1.available()){            
                //Serial.println("while DEBUG"); 
                Rx_DATA[i++]= Serial1.read();
                //Serial.print("recieved data: ");
                //Serial.println(Rx_DATA[i]);
                //Read distance value if not overwritten will give back the previous value
                distance=( (Rx_DATA[5]<<8) | Rx_DATA[6] ); 
                //Serial.print("DEBUG: i value while reading = ");
                //Serial.println(i);
            }

            if(isCheckSumOk()){
                printDistance(distance);
                analyzeDistance(distance);
                
            }
        }

        /**
         * Funciton that performs a 1 byte checksum on the reading
         * MODE: the 6th data is the checksum value. so is the one that will be compared to the one obtained
         * NOTE: the Rx_DATA contains also the 0 and 7 value in memory, so they won't be needed to be analyzed
         *          so the 6th value, but we need to discard the 0th one.
         * */
        boolean isCheckSumOk(){
            //this variable
            uint8_t check = 0;
            for (int k = 0; k < 6; k++)
            {
                //Serial.print("Debug checksum: Rx= ");
                //Serial.println(Rx_DATA[k]);
                check = check + Rx_DATA[k];
            }
            if (check == Rx_DATA[6])
            {
                return true;
            }else{
                return false;
            }           
        }

        /**
         * Function that checks if the measured distance is smaller than the smallest one currently measured
         * */
        void analyzeDistance(unsigned int readed){
            if (readed < smallest_distance)
            {
                smallest_distance = readed;
            }
        }

        /**
         * function that prints out the distance measured from the module
         * */
        void printDistance(int distance){
            Serial.print("URM07-UART module["); //print distance
            Serial.print(j);
            Serial.print("]get_Dis= ");
            Serial.print(distance);
            Serial.println(" cm");
        }
    
    public:
        void setup() {
            Serial1.begin(19200); //Serial1: Ultrasonice Sensors Serial Communication Port, baudrate: 19200
            //Serial.begin(19200); //Serial: USB Serial Monitor
            Serial.println(Serial1.available());
        }

        /**
         * Function that returns the smallest distance measured from all the sensors
         * */
        int getSmallestDistance(){
            //the smallest distance has to be reset to the max measureable in order to get the updated minimum
            smallest_distance = MAX_DISTANCE;
            readAllSensors();
            return smallest_distance;
        }
        

};
Code: Select all
#include <Arduino.h>
#include "UltrasonicSensor.h"

UltrasonicSensor sensor;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  delay(2000);
  sensor.setup();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  sensor.getSmallestDistance();
}



I've also tried to use an arduino UNO and from the LEDs on the board it seems that it never transmit the data but only recieves it, but from debugging it is the opposite.



someone had the same problem and know hot to use those sensors? because it's kinda wierd that even the example found here https://wiki.dfrobot.com/URM07-UART_Ult ... 3#target_4 does not work at all.
userHeadPic simone_andreini