Ultrasonic sensor + Arduino programming

After purchasing this ultrasonic sensor (JSN-SR04T):

http://www.aliexpress.com/store/product/JSN-SR04T-integrated-ultrasonic-ranging-module-waterproof-type-ultrasonic-reversing-radar/221555_2041881738.html

I have got some problem understanding the specs and programming it. After some experimentation, and reading up Arduino’s version of the solution:

http://playground.arduino.cc/Main/UltrasonicSensor

The following is the outcome:

unsigned long echo = 0;
int ultraSoundSignal = 9; // Ultrasound signal pin
int ultraSoundSignalIN = 7; // Ultrasound signal pin

unsigned long ultrasoundValue = 0;

void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
pinMode(ultraSoundSignalIN,INPUT);

}

unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(15); // Wait for 15 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignalIN, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignalIN, HIGH); // Turn on pullup resistor
// please note that pulseIn has a 1sec timeout, which may
// not be desirable. Depending on your sensor specs, you
// can likely bound the time like this — marcmerlin
// echo = pulseIn(ultraSoundSignal, HIGH, 38000)
echo = pulseIn(ultraSoundSignalIN, HIGH); //Listen for echo
Serial.println(echo);
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;

}

void loop()
{
int x = 0;
x = ping();
//Serial.println(x);
delay(250); //delay 1/4 seconds.

}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: