Implementing a XL-MaxSonar sensor to Atmega 1280.

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

I voted for Seoul. Hope that helped.

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

What do these magic numbers mean?

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?

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?

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).