You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • I'm afraid at the moment, Espruino can't handle much over 20kHz - and even 20kHz doesn't leave much time for the interpreter to do anything else.

    Also, your example might not do what you expect... You're outputting a 40kHz square wave (which Espruino can do just fine - in fact it can go way beyond that). You're then changing the duty cycle of that square wave at 4kHz with wout, which is producing a 15 Hz sine wave (because wout's buffer has a single sine wave in 256 samples, and 4000/256 = 15.625).

    For the input, what you're doing looks good for 40kHz (although Espruino may struggle to read the input that quickly).

    But: If you want to reliably see a 40kHz sound wave, you'll have to sample at at least 80kHz (because a sine wave goes both up and down in 1/40000th of a second) - and that'll almost certainly be too fast for Espruino.

    All I could suggest you do is to output a small 40kHz 'chirp' using the normal PWM:

    analogWrite(C9,0.5,{ freq:40000});
    setTimeout(function() { digitalRead(C9); }, 10); // stop roughly 10ms later
    

    (I don't know if this'll power your ultrasonic transmitter - you'll have to check the datasheet)

    And then you could set up an analog circuit with a diode, capacitor and resistor on the ultrasonic receiver that would convert the high frequency AC echo into DC, which you could read at a lower sample rate.

About

Avatar for Gordon @Gordon started