Poll
Question: City?
Seoul - 1 (100%)
Seoul - 0 (0%)
Total Voters: 1

Pages: [1]   Go Down
Author Topic: Implementing a XL-MaxSonar sensor to Atmega 1280.  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
West Lafayette, IN, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
PhD student, Purdue Univ.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, all

I am currently trying to get data of range finder sensor (XL-MaxSonar) using ATmega 1280 board with the following code. The problem is that serial monitor keeps showing wrong outputs which does not respond actual distance measurement at all. I already tried to play around with pin number which ends up with fail.
Could anyone give me a advice?

Thank you very much in advance.



int configPin = 39;//Set the sonar Calibration Pin

void setup() {                    //begin of program
  Serial.begin(9600);             //serial baud rate 9600
  pinMode(configPin,OUTPUT);       //make Calibration pin output
}

void loop(){                     //looping of program
  digitalWrite(configPin,HIGH);    //raise the reset pin high
  delay(120);                      //start of calibration ring
  float sensorvalue = analogRead(0); //get analog sensor value from pin 0
  float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
  Serial.print("Sensed a-d value:"); //print a-d text
  Serial.println(sensorvalue);       //print a-d value
  Serial.print("Inch value=");      //print inch text
  Serial.println(inchvalue);        //print inch value
  delay(1000);                      //optional delay 1 second
  digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
  delay(1000);                      //delay 1 second
}


* IMG_0149.JPG (2849.68 KB, 3264x2448 - viewed 38 times.)
Logged

Sunghun Jung

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I voted for Seoul. Hope that helped.

Code:
  float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
What do these magic numbers mean?

Quote
The problem is that serial monitor keeps showing wrong outputs which does not respond actual distance measurement at all.
You are printing the sensor output. Does that vary with distance?
Logged

West Lafayette, IN, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
PhD student, Purdue Univ.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, the following code,

float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches

was included to convert measurement data to usable data which is in inch unit. Also, yes, there is sensor output which does not vary.
Do you have any idea?
Logged

Sunghun Jung

Australia
Offline Offline
Sr. Member
****
Karma: 13
Posts: 446
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have used some of the XL and WR series  but after playing with the analogue output decided to use the serial output for readings.
You could temporarily use this output to check that the unit is working.

I can't tell which pin on the XL you are using as the "config pin" (i.e. which pin is connected to pin 39 of the Arduino) but it is not necessary to use this each time you want a reading.
the unit takes 175 ms to fire up yet you only have a delay of 120 ms. I would suggest not toggling this pin but if you must, then do it once in the Setup() of the sketch - not once for every loop.
The calibration only needs to happen once and that is automatically when it starts up. Make sure there is nothing close to the sensor when it starts up as this can upset the calibration.

Keep things simple to start with, e.g.

void loop()
{
  int val = analogRead(0);    // read the input pin
  Serial.println(val);             // debug value
}

This is all you need to get basic readings out of the unit. Pin 4 of the XL is internally pulled high and will constantly give readings on pin 3.
Once you get this, then apply conversion calculations.

According to the datasheet the analogue output of the XL has a scaling factor of (Vcc/1024) per cm. Therfore, seeing the ADC of the arduino gives a max reading of 1024 for VCC (assuming you are running the XL and Arduino at the same voltage) one unit of ADC reading equals 1 cm of distance. The calculation for inches should be (analogue reading / 2.54) = inches distance reported by the XL.
(It may be worthwhile connecting Aref to Vcc).

« Last Edit: January 30, 2012, 12:41:02 am by lemming » Logged

Pages: [1]   Go Up
Jump to: