1 Reply Latest reply on Mar 20, 2015 9:20 AM by Intel_Peter

    PWM from linux GPIO

    =Jack=

      Hi all,

      I try to trigger the PWM at pin D9 on Galileo Gen2 board with 2.5Hz, 50% duty cycle from linux GPIO control, but not very well.

      Currently, I can only do this by follow code:

       

      #!/bin/bash

      echo -n "23" > /sys/class/gpio/unexport

      echo -n "23" > /sys/class/gpio/export

      echo -n "out" > /sys/class/gpio/gpio23/direction

      echo -n "0" > /sys/class/gpio/gpio23/value

       

      while true; do

        echo -n "1" > /sys/class/gpio/gpio23/value

        usleep 200000

        echo -n "0" > /sys/class/gpio/gpio23/value

        usleep 200000

      done

       

      but I don't think this is a good idea to use a while loop and usleep to do that.

      I read some other information about pwm in:

      EmutexLabs

      Intel Galileo - Programming GPIO From Linux - Malinov Family Web Presence

       

      and write another code for test PWM :

      (only for test, but still wonder how to get 2.5Hz if this code can work successfully, because the max value of period is only 41666666ns)

       

      #!/bin/bash

      echo -n "4" > /sys/class/gpio/unexport

      echo -n "22" > /sys/class/gpio/unexport

      echo -n "23" > /sys/class/gpio/unexport

      echo -n "70" > /sys/class/gpio/unexport

      echo -n "7" > /sys/class/pwm/pwmchip0/unexport

       

      echo -n "4" > /sys/class/gpio/export

      echo -n "22" > /sys/class/gpio/export

      echo -n "23" > /sys/class/gpio/export

      echo -n "70" > /sys/class/gpio/export

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

       

      echo -n "out" > /sys/class/gpio/gpio4/direction

      echo -n "1" > /sys/class/gpio/gpio4/value

       

      echo -n "out" > /sys/class/gpio/gpio22/direction

      echo -n "1" > /sys/class/gpio/gpio22/value

       

      echo -n "out" > /sys/class/gpio/gpio23/direction

      echo -n "0" > /sys/class/gpio/gpio23/value

       

      echo -n "0" > /sys/class/gpio/gpio70/value

       

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

      echo -n "41666666" > /sys/class/pwm/pwmchip0/device/pwm_period

      echo -n "20833333" > /sys/class/pwm/pwmchip0/pwm7/duty_cycle

       

      no error, but it looks like nothing happening. (or maybe it is to fast that I cannot see the change from LED.)

       

      Any advice to achieve 2.5Hz, 50% duty cycle on pin D9?

       

      Thank you

        • 1. Re: PWM from linux GPIO
          Intel_Peter

          Hello =Jack=,

           

          If you're trying to do a PWM signal of 2.5Hz and 50% duty cycle, I believe it is a better solution to use a normal GPIO, and simply toggling from on and off, because 2.5Hz is a very slow frequency and a regular GPIO should not have any trouble doing it. Either way you choose, why don't you try using MRAA? mraa: Main Page

           

          Peter.