My question is about applying rectangular signal to 10 ultrasound transducers (40k) with the delay precision of 4 to 5 microseconds. Is it possible to do so by Arduino UNO board ? And how can I write the code for it? (digital or analogue pins?)
In fact, my major is not electronic and it is the first time I'm trying to do these types of project.
Do you know how to program the Uno to send a square wave of any frequency to a pin ?
If not then you need to look at the examples in the IDE such as BlinkWithoutDelay. It is very far from the frequency you want but it is a start.
I am not on strong ground when it comes to how fast the Arduino runs and what it can do as my needs are modest in that respect and others here are better placed to provide such advice, which I am sure they will.
Thanks for your response, in the below there is a simple code that I just wrote to apply to the two pins 3 and 4, with 9 microseconds delay, is it wright?
But I believe applying more square wave to more pins would be more challenging with this type of coding!!!
You specify a precision (4-5 uS) but what range of delays do you want to apply?
Is that precision (1/5 cycle) high enough? A full cycle of 40 kHz takes only 25 microseconds.
Are the delays fixed per output or variable?
digitalWrite() takes too long for precision timing. You will need to use direct port manipulation.
But I believe applying more square wave to more pins would be more challenging with this type of coding!!!
Yes it would. An array of pin numbers and a for loop to access them would be more effective but I see that you used different delay values for different pins. Will the delays, however you do them, have a fixed pattern or will it vary ?
Consider 5 digital pins (UNO board), each pin send a pulse wave to one transducer,
The first one with zero delay (no phase), just a pulse wave with 25 uS width.
The second transducer, with the same width but 2 uS delays compared to the first one
.
.
.
And finally the last pin sends pulse with 5 uS delays compared to the first one.
(All the waves are sent continuously to the pins but each pin has a small delay).
You're pushing the limits of what's possible with the Arduino. The micros() function is not capable of timing delay shorter than about 4µs because of the overhead of calling the function.
The processor (in most Arduino variants) has a 16MHz clock. That means it executes 16 million instructions per second or one instruction per 0.0625µs. In 2µs, you can only execute 32 instructions.
This means that calling another function is going to be difficult - the overhead of pushing values on the stack and returning from the function will eat up most of those 32 instructions. You need to map out exactly what you want to do, work out how many instructions that is and then add NOP (no operation) instructions to pad the time out. Have a look at the code behind the delayMicroseconds() function. For very short delays, it actually uses NOP. The comments in that code show you how the time calculation is done.
Synchronising multiple PWM timers is possible and should certainly fit into 32 instructions.
If I increased the delays to more than 4 microseconds, then what would be the code for applying signals to different digital pins?
If I just know the type of coding (template), then I can change it for any delays.