-
• #2
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.
-
• #3
Thank you very much for these explanations.
I am trying to make it chirp then! -
• #4
It works. I found out how a "pulldown resistor" would solve the noise problem, and a with a bit of testing I was able to generate the 40khz pwm and record it in a 4096 bytes waveform at 20khz.
Here is a photo of the nokia LCD on which I visualize the result.
The Chirp duration was 100ms, during which 4 spikes were recorded.
I will try to record the signal with my raspberrypi, maybe it can handle a higher frequency.Now one of the issue I have left is the 3.3V. Is there a way - of course with an external source of power - to have the 40khz PWM at twice or thrice the voltage?
With some googling I found circuits such as:
MAX756 or LT1073The MAX757 spec specifies an "Oper. Freq." of 500kHz. Does this mean that If I provide it with a 40khz PWMed 3.3V from the espruino it will deliver a 40khz square signal at 5.0V ?
1 Attachment
-
• #5
That's great!
Personally, to get a 5v signal for the output I'd just use a transistor: Rather than trying to produce a 5v signal, it's easier to use the transistor to 'pull' a signal down to 0v, and just to connect the other side of the speaker to 5v (or an even higher voltage).
There will be a bunch of stuff on it online - it looks like there's a good tutorial (for Arduino) here: http://www.instructables.com/id/Use-Arduino-with-TIP120-transistor-to-control-moto/
You can also use the ULN2003 and solder it on the board (it's just a chip with several different channels and the resistors built-in), or even the L293D which allows you to pull both high and low.
I have bought a couple "C7210" ultrasound emitter / receiver.
The emitter needs a 40khz pulse, and I would like to analyze the echoed signal on the receiver.
I tried to proceed like this:
C9 => Emitter
C5 => Receiver
( the second pin of emitter and receiver is to GND ).
Also I am not sure if the 3.3V of the esperuino are enough to make the ultrasound emitter.
So far it looks like I've recorded only parasites. Can the espruino generate and record a 40khz signal? Am I missing something?