3 Replies Latest reply on Aug 26, 2014 6:41 PM by rwaldron

    Linux PWM Giving Unexpected Results


      Following up on the issues with galileo-io I was having I decided to try and create my own PWM function inside of Javascript. I followed information on Sergey's blog on using PWM inside Linux and came up with the following JS code:


      var fs = require('fs');


      function exportPWM(pin) {

        fs.writeFile('/sys/class/pwmchip0/export', pin);


      function enablePWM(pin, bool) {

        var bin;

        if (bool == true) {

          bin = 1;

        } else {

          bin = 0;


        fs.writeFile('/sys/class/pwm/pwmchip0/pwm' + pin + '/enable', bin);


      function setPWMperiod(pin, period){

        fs.writeFile('/sys/class/pwm/pwmchip0/pwm' + pin + '/period', period);


      function setPWMdutycycle(pin, duty_cycle) {

        fs.writeFile('/sys/class/pwm/pwmchip0/pwm' + pin + '/duty_cycle', duty_cycle);


      function PWM(pin, period, duty_cycle) {


        enablePWM(pin, true);

        setPWMperiod(pin, period);

        setPWMdutycycle(pin, duty_cycle);



      PWM(1, 1000000, 1000000);


      I make one function call which should just PWM pin 1 (pin 9 on the board) however the output is not consistent. The pin is connected to one leg of a common anode RGB LED, thus driving the PWM output at full duty on one pin should cause it to light up as a single color. The problem is that I can run the script over and over and get different results each time. For instance, running it seven times in a row yields the following LED behavior:

      ON, ON, OFF, quick blink, quick blink, quick blink, quick blink. I'm not quite sure what the issue is and I am having a hard time testing code due to the inconsistency.

        • 1. Re: Linux PWM Giving Unexpected Results

          I think the problem is your duty cycle and period are the same, so its trying to turn on and off at the same time, giving unpredictable results.


          Doing it from the shell prompt I did:

          cd /sys/class/pwm/pwmchip0

          echo -n "1" >export

          cd pwm1

          echo -n "1" >enable

          echo -n "1000000" >period

          echo -n "500000" >duty cyle


          This gave the expected results on my oscilloscope.

          • 2. Re: Linux PWM Giving Unexpected Results

            Some further information taken from the kernel.org PWM pages.


            When a PWM channel is exported a pwmX directory will be created in the

            pwmchipN directory it is associated with, where X is the number of the

            channel that was exported. The following properties will then be available:


            period - The total period of the PWM signal (read/write).

            Value is in nanoseconds and is the sum of the active and inactive

            time of the PWM.


            duty_cycle - The active time of the PWM signal (read/write).

            Value is in nanoseconds and must be less than the period.


            polarity - Changes the polarity of the PWM signal (read/write).

            Writes to this property only work if the PWM chip supports changing

            the polarity. The polarity can only be changed if the PWM is not

            enabled. Value is the string "normal" or "inversed".


            enable - Enable/disable the PWM signal (read/write).

            0 - disabled

            1 - enabled


            I hope this is helpful for you.

            • 3. Re: Linux PWM Giving Unexpected Results

              When you have issues with Galileo-IO, please file them on github: Issues · rwaldron/galileo-io · GitHub otherwise there is no guarantee that I will see them.