I am having problems with wrong readings from EZ1 and my Arduino. I am using the PWM mode and I have checked all tutorials/samples I found. So my questions would be:
Is there a "tested-by-someone-from-LMR" code sample for Arduino and EZ1 in PWM mode? (just to triple check a working code sample)
How long should I allow for it to warm up? (I saw everything from none to 350 ms (startup time) + 100 ms (2 20 Hz cicles))
How often can I take a read (pulseIn)? I have seen everything from 10ms to 50ms (20 Hz) to 300ms to allow for the echo to return!
Can I just do one pulseIn after the previous one (and some delay)? I saw several people "resetting" the EZ1 by sending a LOW-HIGH-LOW signal before each reading
I get sometimes several readings with a zero (0) value. This should not happen (?) because for very close objects (0-15 cm) I should get a "15" as per specifications (after conversion to cm). How should I interpret a "0"? an error to be ignored? object really close? object really far? I can get 7-8 "0" readings in a row...
Which is actually the minimum range of the LV EZ1? (I saw 15 cm... but sometimes I get a 12 cm rading... this might be because of the decimal rounding...)
This is for a continuously-moving obstacle-avoidance robot. So if you consider the worst case of all my research it became impossible:
I need about 7 readings to have a good array for a median filter
I need them 3 times (left, front, right)
I need to allow for 300ms between each reading
This would result on a 6.3 seconds time before I can make a decision!!! :-P
I presume you have carefully read the datasheet already? It answers many of your questions. I also found this tutorial after some simple googling.
It says readings can be taken every 50mS (20 Hz rate).
The datasheet also mentions ‘free running’ range operation for continous range output. If you leave the RX pin unconnected or held HIGH, the output will be continous. So you should be able to just leave the sonar in continous mode and just take readings when you want them via an analogRead command. You might need to use some averaging to get smooth values.
The warmup time of 250mS is only for startup. So just wait at least that long after power-on before trying to get range info. Take note what the datasheet says about calibration during startup. If objects were within 14 inches during startup, it may have affected your range data for objects at that distance.
Which output mode are you using? The analog voltage range? There’s a scaling factor based on your supply voltage (Vcc/512). With a 5V supply, your range measurements should be +/- about 0.5V.
Since objects 0-6 inches away will be reported as 6 inches away, you can place the sonar 6 inches back (or as far as you can) on your robot. This way most of the 0-6 inch range is over the body of your robot.
On my MiniEric robot I added a EZ1 sensor to find out when to stop in front of the candle so the arm is positioned in a good position. I use this code to read the sensor in PWM mode:
int Read_EZ1_Sensor() { int cm=0; unsigned long value = 0; value = pulseIn(EZ1Pin, HIGH); value=value/58.87; cm=int(value); return cm; }
I read the sensor every 49ms per datasheet instructions and the minimum range is 15cm (6 inch) while the max range is 254 inch (6.45 meters) with 1 inch resolution. The sensor has to be mouted high enough to not catch echoes from the carpet or other low objects on the floor while it calibrates itself (needs a clear path of minimum 14 inches). You do not need to take several measurements and average, the sensor is factory calibrated to 1 percent acuracy and in use is better than 2 percent, according to the datasheet. I always got good readings with this sensor, but it bothered me that I can’t read it faster than 49ms (I get the same result until 49ms is passed). I used a Ping sensor for fast measurements at short distances (for a balancing robot).
Yes I saw that tutorial… but it is using the analog mode (not the PWM) and it is actually “calibrating” the sensor on every iteration?
Are you suggesting me to use the analog mode instead (for the “free running” range operation)?
Also… what about the zero readings? how should I treat them? I thought they should not exist at all
Tonight I will try everything again.
PS: I thought on buying an IR sensor instead… but I would like to make this one work! It worked good enough for my first robot, but now that I want a robot that does not stop, I am having more problems…
I’ve been using two EZ4’s that are “daisy chained” together (ie free running but they alternate). I’ve had no problems using the PWM readings in this manner. I’m not counting every millisecond though and don’t mind waiting for the next reading.
Sorry for the confusion and for having this newbie problem
Still not 100% sure but I think that the 0 readings might come because of a bad connection of the cable to the EZ1 sensor… I will re-check and test again
BTW: For the rest of my questions… thanks for the answers and comments!
We are sorry to hear you have been having problems with your MB1010. We also realize it has been some time since this thread has been active, but we want to make sure your receive resolution for any sensor issues.
Following you can find code example for using our LV-MaxSonar-EZ series sensor with an Arduino using the pulse-width output.
/ Test code for the Arduino Uno Written by Tom Bonar for testing Sensors being used for this code are the MB10X0 from MaxBotix All PW inputs are coded in this for simplicity. Remove the comments to use the additional sensor inputs / const int pwPin1 = 3; long pulse1, sensor1;
//This section of code is if you want to print the range readings to your computer too remove this from the code put /* before the code section and */ after the code void printall(){ Serial.print(“S1”); Serial.print(" “); Serial.print(sensor1); Serial.println(” "); }
void loop () { read_sensor(); printall(); delay(50); // This delay time changes by 50 for every sensor in the chain. For 5 sensors this will be 250 }