# Generate multiple waves with specific number steps in a cycle wave

I want to generate a square wave has frequency 40khz on multiple GPIO pins of Raspberry Pi3 with time in order to start to generate wave (phase delay) is different. I have used a function of PIGPIO library to do this but it only supplies the resolution of phase delay is 1 microseconds. While that, I want the smaller resolution about 0.5microseconds. So I used another way is that consists of the following steps:

• step 1: f=40khz =>> cycle=25us. I divided cycle into 10 steps according to 10 bits(0 or 1) =>> each step will take time is 2.5us. The data of 10 steps for each GPIO pin are calculated from a software. Besides that, a number of bit0 and bit1 in 10bits can different so I can create different phase delay for each pin. - step
• step 2: define GPIO4 pin as the OUTPUT pin then generate 40khz wave on this pin using hardware_clock on GPIO4 pin
• step 3: define GPIO27 pin as the INPUT pin
• step 4: connect GPIO4 to GPIO27
• step 5: wait pin GPIO27 go low (to sync all signal with 40khz) then write 10 bits for each GPIO pin that I want to generate.

But results is that it seems was not delay between every time write a bit for GPIO pin so it toggles quite fast and lead to create frequency is not accuracy. This is my code used. Please show me a way to can do this algorithm.

``````import pigpio
pi = pigpio.pi()
GPIO=[2,3,4,6]
data_GPIO2=[0,0,0,0,0,1,1,1,1,1]
data_GPIO3=[0,0,1,1,1,1,1,0,0,0]
data_GPIO6=[0,0,0,1,1,1,1,1,0,0]
for i in range(0,4):
pi.set_mode(GPIO[i], pigpio.OUTPUT)
pi.hardware_clock(4,40000)
pi.set_mode(27, pigpio.INPUT)
pi.set_pull_up_down(27, pigpio.PUD_DOWN)