3 Replies Latest reply on Jul 25, 2014 4:02 PM by Intel_Alvarado

    40 kHz PWM output


      I want to generate a 40 kHz PWM output on the intel Galileo board, without using the Arduino platform. The PWM signal has a constant period, but different duty cycle for each period.

      First I generated a software Linux timer, and toggled a High and Low signal to produce the PWM signal. 

      However, this doesn't work for 40 kHz as the gpio pins have throughputs of 230 Hz max.

      Now I'm trying to access the CCY8C95 PWM chip directly. I can get it to output a 40 kHz square wave by selecting the appropriate clock and periods, but that doesn't change the duty cycle for each period.

      I thought of detecting an interrupt whenever the PWM changes state, and then changing the duty cycle,

      but it seems like the cypress chip can only generate an interrupt when the slowest clock is used (367 Hz).

      Any ideas on how to change the duty cycle for for each period at a frequency of 40 kHz?

        • 1. Re: 40 kHz PWM output

          Hi mpan,

          The first step you have to do is export the PWM on a port.

          The command to do so is

          echo -n "3" > /sys/class/pwm/pwmchip0/export

          If you have already exported it, it should display a message similar to this

          -sh: echo: write error: Device or resource busy

          Next you need to enable a PWM on a port write “1” to a corresponding enable file using this command:

          echo -n "1" >  /sys/class/pwm/pwmchip0/pwm3/enable

          To set the PWM period in nanoseconds use the following command: 

          echo 25000 >/sys/class/pwm/pwmchip0/pwm3/period

          And to set the PWM duty cycle enter the following command

          echo 12500 > /sys/class/pwm/pwmchip0/pwm3/duty_cycle

          Now, if you want to modify the duty cycle on each period you can write a Python script. In that script you can modify the value of a given duty cycle for a variable one. The command to do so is

          os.system (command)


          To do this you have to import the library OS. This link shows this in more detail https://docs.python.org/3/library/os.html#os.system.


          • 2. Re: 40 kHz PWM output


            So it seems that even if I use your method, there's a limitation on the output of the pwm.

            If I set

            echo 70000 > /sys/class/pwm/pwmchip0/pwm3/period


            I get an output of about 13kHz. But if I try to increase the frequency, and say:


            echo 60000 > /sys/class/pwm/pwmchip0/pwm3/period


            the signal goes to 0.


            So it doesn't seem like I can get to 40 kHz using this.

            • 3. Re: 40 kHz PWM output

              Hi mpan

              We have double checked the output of the PWM signal with an oscilloscope and the fastest speed we could get using this method was about 14kHz. We know you can get faster speeds with the IDE. We will try the Arduino source code in order to know how to get higher frequencies in the Linux side. As soon as we test it we will let you know our results.