Shop OBEX P1 Docs P2 Docs Learn Events
parallax laser range finder #28044 — Parallax Forums

parallax laser range finder #28044

martinpaul23martinpaul23 Posts: 7
edited 2013-12-05 08:44 in Accessories
hello,
am new to programming and am doing a project that am required to use a parallax laser range finder to detect an object and measure its distance. I want to use arduino to do this. am connecting the SIN pin of the sensor to analog pin 2 and SOUT to analog pin3. Then am using the code by Roy Eltham, the problem is there is no response from either the sensor, the arduino or the LCD display. where am I going wrong? could it be the connection with arduino or the sensor is not working?.please assist. Here is the code
/*
Simple LRF Demo
by Roy Eltham and Dino Segovis
*/


// include the library code:
#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


// include the SoftwareSerial library so we can use it to talk to the LRF
#include <SoftwareSerial.h>


#define rxPin 8 // connect to the LRF's SOUT pin
#define txPin 9 // connect to the LRF's SIN pin thru a 4.7k resister


// set up a new serial port
SoftwareSerial lrfSerial = SoftwareSerial(rxPin, txPin);


void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);


// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// set the data rate for the SoftwareSerial port
lrfSerial.begin(9600);


// send a U to the LRF so that it will autodetect the speed to communicate with us at (9600)
lrfSerial.print('U');
// the LRF will send back a : and a newline, we just read and ignore those here
lrfSerial.read();
lrfSerial.read();


//print splash screen
lcd.print("Laser");
lcd.setCursor(0, 1);
lcd.print(" Range");

delay(3000);
lcd.clear();
// send a E to the LRF to adjust camera to current lighting conditions
// lrfSerial.print('E');
// delay(1000);
//tell the user that the LRF is ready
lcd.print("LRF ready.");
delay(1500);
lcd.clear();


// Print a message to the LCD.
lcd.print("Distance in mm");
}


void loop()
{
// send an R to the LRF telling it to take a measurement
lrfSerial.print('R');


// read what the LRF sends back
char lrfData[32];
int offset = 0;
lrfData[0] = 0;
while(1)
{
lrfData[offset] = lrfSerial.read();
// if we get a : character that indicates the end of the read data (and that the LRF is ready for the next command)
if (lrfData[offset] == ':')
{
lrfData[offset] = 0; // null terminate the string of bytes and break out of the loop
break;
}
offset++;
}


// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);


// print out the data from the LRF (it sends back a string in the form "D = xxxx mm" where xxxx is the distance in milimeters
lcd.print(lrfData);


// delay a tiny bit before the next read
delay(100);


//integer to convert lrfData string to an integer value
int distance = atoi(&lrfData[4]);
//sound the alert if the distance is less than one meter
if (distance < 1000)
{
tone(5, 440, 200);
delay(200);
}
}

Comments

  • kfuremkfurem Posts: 19
    edited 2013-12-04 04:07
    First you can use the status light on the LRF to see if you are connecting the SIN pin--I'd start with that. I used to run the LRF on an Arduino Mega but used the UART port, no problems.
  • martinpaul23martinpaul23 Posts: 7
    edited 2013-12-04 08:21
    kfurem, i guess you mean the status led at the back of the sensor, well that one goes on immediately after connecting to arduino, but from there i get nothing else on the LCD. I've tried to see whether there is a problem with the code but there isn't. Is there anyone who have experienced the same? how did you go about it
  • martinpaul23martinpaul23 Posts: 7
    edited 2013-12-04 08:33
    please explain to me how you did it
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-04 12:00
    You mentioned connecting the LRF SIN and SOUT pins to analog inputs on the Arduino. But the code you show uses pins 8 and 9.
  • martinpaul23martinpaul23 Posts: 7
    edited 2013-12-04 21:01
    that was typing mistake, it is supposed to pin 2 and 3(analog)
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-05 08:19
    This still does not match the code. Are you using the same I/O pins as are declared in your code?
  • martinpaul23martinpaul23 Posts: 7
    edited 2013-12-05 08:24
    i have tried to use both, digital pins 8 and 9, on arduino and 2 and 3 on analog side. But none is working
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-05 08:26
    Can you describe where all 4 pins of the LRF are connected on your Arduino board?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-05 08:28
    Also this question really belongs in the sensors forum, not projects so I am going to move it there at this time.
  • martinpaul23martinpaul23 Posts: 7
    edited 2013-12-05 08:41
    SIN pin is connected to analog pin 3 through a 4.7k resistor, SOUT pin is connected to analog pin 2, vcc to 5v and GND to ground pin of the arduino. then am using the code below
    /*
    Simple LRF Demo
    by Roy Eltham and Dino Segovis
    */


    // include the library code:
    #include <LiquidCrystal.h>


    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


    // include the SoftwareSerial library so we can use it to talk to the LRF
    #include <SoftwareSerial.h>


    #define rxPin 2 // connect to the LRF's SOUT pin
    #define txPin 3 // connect to the LRF's SIN pin thru a 4.7k resister


    // set up a new serial port
    SoftwareSerial lrfSerial = SoftwareSerial(rxPin, txPin);


    void setup()
    {
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 4);


    // define pin modes for tx, rx, led pins:
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    // set the data rate for the SoftwareSerial port
    lrfSerial.begin(9600);


    // send a U to the LRF so that it will autodetect the speed to communicate with us at (9600)
    lrfSerial.print('U');
    // the LRF will send back a : and a newline, we just read and ignore those here
    lrfSerial.read();
    lrfSerial.read();


    //print splash screen
    lcd.print("Laser");
    lcd.setCursor(0, 1);
    lcd.print(" Range");
    lcd.setCursor(0, 2);
    lcd.print(" Finder");


    delay(3000);
    lcd.clear();
    // send a E to the LRF to adjust camera to current lighting conditions
    // lrfSerial.print('E');
    // delay(1000);
    //tell the user that the LRF is ready
    lcd.print("LRF ready.");
    delay(1500);
    lcd.clear();


    // Print a message to the LCD.
    lcd.print("Distance in mm");
    }


    void loop()
    {
    // send an R to the LRF telling it to take a measurement
    lrfSerial.print('R');


    // read what the LRF sends back
    char lrfData[32];
    int offset = 0;
    lrfData[0] = 0;
    while(1)
    {
    lrfData[offset] = lrfSerial.read();
    // if we get a : character that indicates the end of the read data (and that the LRF is ready for the next command)
    if (lrfData[offset] == ':')
    {
    lrfData[offset] = 0; // null terminate the string of bytes and break out of the loop
    break;
    }
    offset++;
    }


    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);


    // print out the data from the LRF (it sends back a string in the form "D = xxxx mm" where xxxx is the distance in milimeters
    lcd.print(lrfData);


    // delay a tiny bit before the next read
    delay(100);


    //integer to convert lrfData string to an integer value
    int distance = atoi(&lrfData[4]);
    //sound the alert if the distance is less than one meter
    if (distance < 1000)
    {
    tone(5, 440, 200);
    delay(200);
    }
    }
  • martinpaul23martinpaul23 Posts: 7
    edited 2013-12-05 08:44
    it is okey with me. thanks
Sign In or Register to comment.