Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: Using SR04, SRF05, SRF06, DYP-ME007 ultrasonic sensors on Teensy 3

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    12

    Question about using SR04, SRF05, SRF06, DYP-ME007 ultrasonic sensors on Teensy 3

    Trying to use new ping library with SR04, SRF05, SRF06, DYP-ME007 - http://code.google.com/p/arduino-new-ping/
    But obviously getting bunch of errors:

    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp: In static member function 'static void NewPing::timer_us(unsigned int, void (*)())':
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:151:2: error: 'OCR2A' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:152:2: error: 'TIMSK2' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:152:16: error: 'OCIE2A' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp: In static member function 'static void NewPing::timer_ms(long unsigned int, void (*)())':
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:167:2: error: 'OCR2A' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:168:2: error: 'TIMSK2' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:168:16: error: 'OCIE2A' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp: In static member function 'static void NewPing::timer_stop()':
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:177:2: error: 'TIMSK2' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:177:17: error: 'OCIE2A' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp: In static member function 'static void NewPing::timer_setup()':
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:195:2: error: 'ASSR' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:195:15: error: 'AS2' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:196:2: error: 'TCCR2A' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:196:15: error: 'WGM21' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:197:2: error: 'TCCR2B' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:197:15: error: 'CS22' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:198:2: error: 'TCNT2' was not declared in this scope
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp: At global scope:
    D:\stm32\arduino-1.0.3\libraries\NewPing\NewPing.cpp:214:4: error: expected constructor, destructor, or type conversion before '(' token

    Because this library using Atmega specific stuff.

    What can I do to make this work?
    Last edited by Ectar; 02-14-2013 at 04:35 PM.

  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Well, Looks like no one helping me out, so I will help myself

    Here is a piece of code which does the job:


    Code:
    int dypOutputPin = 2; // TRIG
    int dypInputPin = 3;  // ECHO
    long distance;
    long cm;
    
    void setup(){
      pinMode(dypOutputPin, OUTPUT);
      pinMode(dypInputPin,INPUT);
    }
    
    void loop()
    {
      // The DYP-ME007 pings on the low-high flank...
      digitalWrite(dypOutputPin, LOW);
      delayMicroseconds(2);
      digitalWrite(dypOutputPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(dypOutputPin, LOW);   
      // the distance is proportional to the time interval
      // between HIGH and LOW
      distance = pulseIn(dypInputPin, HIGH);
      cm= distance/58;                        
      delay(20);
      Serial.println(cm);
    }

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,171
    Does the original Ping library work? All it does is use pulseIn, very similar to this.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    It wont work work, Ping uses one wire to operate, Sensors I mentioned are using 2 wires, one for Trigger another for Echo (Response).
    But on other hands we can extend Ping library to support separate signal pins.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by Ectar View Post
    It wont work work, Ping uses one wire to operate, Sensors I mentioned are using 2 wires, one for Trigger another for Echo (Response).
    But on other hands we can extend Ping library to support separate signal pins.
    Ectar, thank you so much for posting your solution. I just got a teensy 3.0 (thanks for the great toy, Paul!) which is my first Arduino module so I am brand new to this. I bought a HC-SR04 and was struggling a bit to get it working. Your code worked, first try.

    Converting Ping to use two wires, I think would look something like this. Sorry, I don't know CPP and as I said, this is my first experience with anything like this. All credit on this goes to the Ping original author, Caleb Zulawski, and Ectar. I only mean this to be an example, with no warranty, etc.

    Ping2.h
    Code:
    #ifndef Ping2_h
    #define Ping2_h
    
    #if defined(ARDUINO) && ARDUINO >= 100
    #include "Arduino.h"
    #else
    #include "WProgram.h"
    #endif
    
    class Ping2
    {
      public:
        Ping2(int triggerPin, int echoPin);
        Ping2(int triggerPin, int echoPin, double in, double cm);
        void fire();
        int microseconds();
        double inches();
        double centimeters();
      private:
        int _pinTrig;
        int _pinEcho;
        double _in;
        double _cm;
        long _duration;
    };
    
    #endif
    Ping2.cpp
    Code:
    #include "Ping2.h"
    
    Ping2::Ping2(int triggerPin, int echoPin)
    {
      pinMode(triggerPin, OUTPUT);
      pinMode(echoPin, INPUT);
      _pinTrig = triggerPin;
      _pinEcho = echoPin;
      _in = 0;
      _cm = 0;
      _duration = -1;
    }
    
    Ping2::Ping2(int triggerPin, int echoPin, double in, double cm)
    {
      pinMode(triggerPin, OUTPUT);
      pinMode(echoPin, INPUT);
      _pinTrig = triggerPin;
      _pinEcho = echoPin;
      _in = in;
      _cm = cm;
      _duration = -1;
    }
    
    void Ping2::fire()
    {
      pinMode(_pinTrig, OUTPUT);
      digitalWrite(_pinTrig, LOW);
      delayMicroseconds(2);
      digitalWrite(_pinTrig, HIGH);
      delayMicroseconds(5);
      digitalWrite(_pinTrig, LOW);
      pinMode(_pinEcho, INPUT);
      _duration = pulseIn(_pinEcho, HIGH);
      
    }
    
    int Ping2::microseconds()
    {
      return _duration;
    }
    
    double Ping2::inches()
    {
      if(_duration != -1){
        return _duration / (74+_in) / 2;
      }else{
        return -1;
      }
    }
    
    double Ping2::centimeters()
    {
      if(_duration != -1){
        return _duration / (29+_cm) / 2;
      }else{
        return -1;
      }
    }
    Example:
    Code:
    #include <Ping2.h>
    
    Ping2 ping = Ping2(2,3);  // Ping with trigger on pin 2, echo on pin 3
    
    void setup() {
      Serial.begin(115200);
    }
    
    void loop(){
      ping.fire();
      Serial.print("Microseconds: ");
      Serial.print(ping.microseconds());
      Serial.print(" | Inches ");
      Serial.print(ping.inches());
      Serial.print(" | Centimeters: ");
      Serial.print(ping.centimeters());
      Serial.println();
    }
    Last edited by Cstrom; 03-19-2013 at 01:53 AM. Reason: Formatting

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,171
    Where's the best place to buy one of these sensors? I'll get one and test this, when it arrives.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Quote Originally Posted by Cstrom View Post
    Ectar, thank you so much for posting your solution. I just got a teensy 3.0 (thanks for the great toy, Paul!) which is my first Arduino module so I am brand new to this. I bought a HC-SR04 and was struggling a bit to get it working. Your code worked, first try.
    You are welcome, code looks good, I actually created small class as well, which pretty similar did have time to share, but this pretty good ! Except I ditched inches, don't need those

    Quote Originally Posted by PaulStoffregen View Post
    Where's the best place to buy one of these sensors? I'll get one and test this, when it arrives.
    Anywhere, ebay, robotshop, and so on ! I got my sensor (DYP-ME007) here - http://www.goodluckbuy.com/ultrasoni...ce-sensor.html

    @Cstrom, btw, are you powering your sensor from +3V ? Because I have one HC-SR04 as well (bought for super cheap off ebay), and It doesn't work with Teensy3 when powered from 3V, but works OK with my Arduino Leonardo powered from 5V ! On other hand DYP-ME007 works well in both cases.
    Last edited by Ectar; 03-19-2013 at 02:43 AM.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by Ectar View Post
    @Cstrom, btw, are you powering your sensor from +3V ? Because I have one HC-SR04 as well (bought for super cheap off ebay), and It doesn't work with Teensy3 when powered from 3V, but works OK with my Arduino Leonardo powered from 5V ! On other hand DYP-ME007 works well in both cases.
    @Ectar, same. I had to power mine @ 5v.

    @Paul, I got my unit off ebay from this seller for $2 USD (I'm not affiliated in any way). http://www.ebay.com/itm/270810920267...84.m1497.l2649

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Quote Originally Posted by Cstrom View Post
    @Ectar, same. I had to power mine @ 5v.
    Are you using some sort of logic level convertor 5V>3.3V or it just works? I was not sure if it is OK to attach device powered 5V to Teensy powered 3.3V, logic level are different... ??

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by Ectar View Post
    Are you using some sort of logic level convertor 5V>3.3V or it just works? I was not sure if it is OK to attach device powered 5V to Teensy powered 3.3V, logic level are different... ??
    No logic level converter. I don't know enough for it to even have crossed my mind

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,171
    I recently tested this on the Parallax Ping. I posted an oscilloscope screenshot and info on this page:

    http://www.pjrc.com/teensy/td_libs_Ping.html

    I ordered some cheap 4-pin modules from an Ebay vendor, shipping from China. When they arrive, I'll test them and of course share the results.

  12. #12
    Senior Member
    Join Date
    Jan 2013
    Location
    San Francisco Bay Area
    Posts
    641
    @Paul
    Did you ever try this module?

    I'm using the code above in Ping2. I just tried this and it generally works, but sometimes I get strange readings. Possibly when not getting a reflection.
    It also tends to slow the output when it happens, which makes me think that it reaches the timeout & detecting the next trigger.
    My first time using this type of sensor, so not sure if I'm thinking about it right.

    I'll probably see if I can get the NewPing library to work next.
    http://forum.arduino.cc/index.php?topic=106043.0

    Code:
    Microseconds: 3771 | Inches 25.48 | Centimeters: 65.02
    Microseconds: 3771 | Inches 25.48 | Centimeters: 65.02
    Microseconds: 3746 | Inches 25.31 | Centimeters: 64.59
    Microseconds: 187359 | Inches 1265.94 | Centimeters: 3230.33
    Microseconds: 187359 | Inches 1265.94 | Centimeters: 3230.33
    Microseconds: 187138 | Inches 1264.45 | Centimeters: 3226.52

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    When there is no reflection it should timeout and return 0.... at least in my version of library it works like that, but I haven't tries Ping2
    Last edited by Ectar; 05-28-2013 at 04:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •