1

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)
while(pi.read(27)==1):
   for i in range(0,9):
      pi.write(2,data_GPIO2[i])
      pi.write(3,data_GPIO3[i])
      pi.write(6,data_GPIO6[i])
  • I can think of no way of using pigpio to achieve this task. Also pigpio uses a client/server architecture for Python and your GPIO writes will take of the order of a millisecond each. Perhaps try C or bare metal. – joan Mar 5 '18 at 8:33
  • To clarify pigpio would work at microsecond resolution, it will not work at sub-microsecond resolution. – joan Mar 5 '18 at 10:46
  • @joan, Can give me a library or another method to can write GPIO under 1 microsecond using C or bare metal that you mentioned. Before that, This program has been written on Arduino mega2560 (using Timer 16Mhz) and it run well. – Gab Mar 6 '18 at 4:37
  • The Arduino is bare metal, there is no operating system doing background tasks which take time. I doubt you will be able to do this under Linux without external hardware support. Why not just use an Arduino? – joan Mar 6 '18 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.