Multiple XL-Maxsonar reading


I'm attempting to set up a code to read multiple XL-MaxSonar-EZ/AE (1300) ultrasonic sensors, but I'm having a bit of trouble. I've set them up as a daisy chain, but when I look at the data it shows that the first sensor reads normally, but the second sensor in the chain always shows a much higher value.

They are both facing the same obstacle and should be outputting similar values. I have tried switching the sensors and the readings remain the same.

The issue is either in how I am implementing the code, or with how I have wired it (I have been messing around with the input to pin one as it is the only one I'm unsure of).

Any advice or suggestions would be welcomed.

Thank you!

update: Both sensors are now reading reasonable values, but occasionally the first one will spike (it will read 371 and then drop down to the expected value). I'm not entirely sure what is causing it or how to fix it, but I'll let you know when I do. Unless someone else believes they know already and would like to contribute. All ideas are welcomed. :)

Thank you!

The issue is either in how I am implementing the code, or with how I have wired it

Probably at least one of those things, but you didn't feel it necessary to share either.

Yeah, yeah. You could just ask nicely you know^. No need to be snarky to a newbie.

Here's a link to the data sheet. The wiring scheme is on page 6 and the description of the pin layout that it refers to is on page 2.

I'm not much of a coder, but I was attempting to modify a code I found for a single sensor.

const int anPin = 0;
const int anPin2 = 2;
const int Pulse = 7;
long anVoltA, anVoltB;
int sum=0;
int avgrange = 0;

void setup(){
 pinMode(Pulse, OUTPUT);


void loop(){
    digitalWrite(Pulse, HIGH);
    digitalWrite(Pulse, LOW);
    anVoltA = analogRead(anPin)/2;
    anVoltB = analogRead(anPin2)/2;

I'm not familiar with the device and can't easily read PDFs on my phone, but there seems to be no allowance for the speed of sound; you appear to trigger, then read back straight away.

The pulse that triggers the device is held long enough for it to get a reading, according to the data sheet. Although that's not to say timing isn't an issue.

I've managed to eliminate the data spikes (somehow) and now I need to try to synchronize all the aspects of the circuit. That is to say, I don't want to be reading in data at a rate faster than the clock cycle for the entire circuit. It needs time to update before getting new information, but I'm not sure how long that clock cycle is. (Where's an oscilloscope when you need one?)

I'll keep posting updates in my progress, but it the meantime any advice/suggestions will be greatly appreciated :)

The trigger pulse may indeed be long enough, but even if the ping goes out on the leading edge, it won't have travelled very far in thirty microseconds.

"175mS after power-up, the XL-MaxSonar® is ready to begin ranging. If Pin-4 is left open or held high (20uS or greater), the sensor will take a range reading. The XL-MaxSonar® checks the Pin-4 at the end of every cycle. Range data can be acquired once every 99mS. Each 99mS period starts by Pin-4 being high or open, after which the XL-MaxSonar® calibrates and calculates for 20.5mS, and after which, twenty 42KHz waves are sent."

I'm pretty sure that the pulse only triggers data acquisition. The waves are sent out after calculating the range, which at that point pin 4 is held low for at least 99mS.

Here are a couple links that can help you: analogRead? It looks as though you have a mixture of analog and PW modes in your code. If you are trying to use your sensor in PW mode, then you need to use the PW pins on the Arduino, not the analog pins.

Also note the mode filter code example in the Arduino Playground link. It works much better than averaging.