Software > Software

Problems with LV-MaxSonar-EZ1 code

(1/2) > >>

Brandon121233:
Hey everyone
I'm having some difficulties with making the LV-MaxSonar-EZ1 ultrasonic rangefinder, work with my arduino board. I'm trying to use the analog output from it to measure the distance in inches. The manual says that
--- Quote ---Outputs analog voltage with a scaling factor of
(Vcc/512) per inch. A supply of 5V yields ~9.8mV/in.
and 3.3V yields ~6.4mV/in. The output is buffered
and corresponds to the most recent range data.
--- End quote ---
This is the code I have so far:


--- Code: ---int ultraAn = 0;    // select the input pin for the ultrasonic sensor
int val = 0;       // variable to store the value coming from the sensor
int dist = 0;      // variable to store the converted distance in inches

void setup()
{
  Serial.begin(9600);        // setup the serial port to send the values back to the computer
}

void loop() {
  val = analogRead(ultraAn);    // read the value from the sensor
  Serial.println(val);           // print the value to the serial port   
  dist = (val/9.77);          // converts the analog value to distance in inches
  Serial.println(dist);         // prints the distance in inches   
  delay (2000);                // delays 2 secconds in between readings
}

--- End code ---

and it send me data through the serial link like this:

--- Quote ---103

10

33

3

102

10

1001

102

997

102

998

102

999

102

1001

102

--- End quote ---

but those values were definitely no accurate, so any help would be great. Thanks

Brandon121233:
sorry i looked into it for a couple of hours and I think I answered my own question, I needed to convert the analogRead into the actual voltage value instead of the 0-1024 value it was, now its working pretty well. Now that thats working, does anyone know like a link that would point me in the right direction for determining the distance with the TX pin which the data sheet says:
--- Quote ---TX Delivers asynchronous serial with an RS232 format,
except voltages are 0-Vcc. The output is an ASCII
capital R, followed by three ASCII character digits
representing the range in inches up to a maximum of
255, followed by a carriage return (ASCII 13). The
baud rate is 9600, 8 bits, no parity, with one stop bit.
Although the voltage of 0-Vcc is outside the RS232
standard, most RS232 devices have sufficient margin
to read 0-Vcc serial data. If standard voltage level
RS232 is desired, invert, and connect an RS232
converter such as a MAX232.
--- End quote ---

can anyone help me with the pseudo code of how to retrieve the ACSII characters from the arduino?

dunk:
so i have never used the arduino but micro controllers tend to have a UART (or sometimes USART) port.
this is just like a RS232 port (which is what the serial port on PCs and older laptops is) except RS232 ports run at higher voltage so they can be used over longer distances. to convert from the logic level on the UART to RS232 voltages you would use a chip such as the MAX232 level shifter.

so, search for pages describing the arduinos UART. if you are actually looking for circuit diagrams, try using the keyword "MAX232".

hope this gives you a starting point.

dunk.

Hal9000:
Ding dong?

http://www.arduino.cc/en/Tutorial/HomePage

Brandon121233:
Yeah thanks
I was aware of the tutorials, specifically the sonar one, but that has code for the sensor that outputs ping time delays and the microcontroller counts the delay and everything, I am not aware of any tutorial on reading serial ASCII commands sent from an ultrasonic sensor. I know how to go about it basically so its not a big deal, I just didn't want to have to spend a couple of hours trying to figure it out, if someone had already done that. Thanks for your help so far-and I might have some more questions later on about this.

Navigation

[0] Message Index

[#] Next page

Go to full version