Ultrasonic Range Sensor MB7360

Hi:
I am working on this Ultrasonic Range Sensor by Max Electronics, MB7360:

Unlike the HC-SR04, it does not have separate pins for trigger and echo but a single pin does the job of both. The obstacle distance can either be calculate from voltage(using pin 3) or pwm(using pin 2). I am trying to use pwm. The arduino code is as follows:

void setup () {
Serial.begin(9600);
pinMode(pwPin1, INPUT);
}

void read_sensor(){
sensor1 = pulseIn(pwPin1, HIGH);
cm = sensor1/10 // converts the range to cm
inches = cm/2.54 // converts the range to inches
}
Is there an equivalent of the pulseIn function for the imp? Or do I explicitly have to send a high-low pulse at the pin? What I have done for the imp so far is as follows: It gives me a distance of 42mm but I don’t know if makes sense:

// Ultrasonic Range Sensor HRXL-MaxSonar(MB7360)
class HRXL {
// consts
static TO = 100; // timeout in ms

// pins
_trig   = null;
//_echo   = null;

// aliased methods
_tw     = null;
_tr     = null;
_hu     = null;
_hm     = null;

// vars
_ts     = null; // pulse start time
_te     = null; // pulse end time

constructor(trig) {
    _trig = trig;
    

    _hu   = hardware.micros.bindenv(hardware);
    _hm   = hardware.millis.bindenv(hardware);
    _tw   = _trig.write.bindenv(_trig);
    _tr   = _trig.read.bindenv(_trig);
}

function read_mm() {
    local st = _hm(); // start time for timeout
    // Quickly pulse the trig pin
    _tw(0); _tw(1); _tw(0);

    // Wait for the pulse to go high, start timing
    while (_tr() == 0 && (_hm() - st) < TO);
    _ts = _hu();

    // Wait for pulse to go low, stop timing
    while (_tr() == 1 && (_hm() - st) < TO);
    _te = _hu();

    //if ((_hm() - st) >= TO) return -1;
    return (_te - _ts);
    
    
}

}

//local r = range.read_cm();

// Ex Usage
trig <- hardware.pin8;

trig.configure(DIGITAL_IN);

range <- HRXL(trig);
server.log(range.read_mm()+" mm");

Thanks,
Apoorva.

There is no equivalent of pulseIn.

I would try wiring the trigger signal into the sense signal using a diode.

@hugo, just curious to know whether you could use “pin.configure(inputMode, onPinStateChangeCallback)” method in this case and use a global variable which measures time of pulse inside callback function, as this is called every time it detects an input state change, or is this method not very reliable / accurate.

Given that it’s 1us per millimeter, a callback adds too much latency and hence wouldn’t give good results.

However, the MB7360 sensor here has a serial output. To use it, you just need an inverter (the 7380 has inverted output - ie correct TTL levels, but the 7360 is pseudo RS232 which means it’s the wrong way up). The serial output gives 1mm resolution and deals with everything for you - there’s really zero reason to be using the pulse width method which is particularly ugly.

To use this, you just need a 7404 (eg 74LVC04) to invert the signal before feeding it into an imp UART RX pin. eg: http://assets.nexperia.com/documents/data-sheet/74LVC1G04.pdf

Both can run at 3.3v (though you get longer range with 5v supply). If you’re running the sensor at 5v then the inverter should be powered from 3.3v, and be 5v tolerant, as the 74LVC series is.

@hugo I tried calculating the distance using voltage instead of pwm. Worked out well.