PDA

View Full Version : Using SR04, SRF05, SRF06, DYP-ME007 ultrasonic sensors on Teensy 3



Ectar
02-14-2013, 03:03 AM
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?

Ectar
02-15-2013, 03:03 AM
Well, Looks like no one helping me out, so I will help myself :)

Here is a piece of code which does the job:



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);
}

PaulStoffregen
02-15-2013, 08:55 PM
Does the original Ping library work? All it does is use pulseIn, very similar to this.

Ectar
02-19-2013, 03:40 AM
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.

Cstrom
03-19-2013, 01:50 AM
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


#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


#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:


#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();
}

PaulStoffregen
03-19-2013, 02:31 AM
Where's the best place to buy one of these sensors? I'll get one and test this, when it arrives.

Ectar
03-19-2013, 02:39 AM
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


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/ultrasonic-wave-detector-ranging-module-distance-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.

Cstrom
03-19-2013, 02:52 AM
@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?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

Ectar
03-19-2013, 04:27 AM
@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... ??

Cstrom
03-19-2013, 11:27 PM
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 :p

PaulStoffregen
03-19-2013, 11:45 PM
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.

linuxgeek
05-28-2013, 04:08 AM
@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



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

Ectar
05-28-2013, 04:45 AM
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