6 Replies Latest reply on Mar 7, 2015 8:16 AM by Frederic Philips

    MRAA PWM not working

    pruesch

      Hi,

       

      I'm running the following code on the Edison:

      #include <unistd.h>
      
      #include "mraa.h"
      float value = 1;
      int
      main ()
      {
          mraa_init();
      //! [Interesting]
          mraa_pwm_context pwm;
          pwm = mraa_pwm_init(9);
          if (pwm == NULL) {
              return 1;
          }
          mraa_pwm_period_us(pwm, 100);
          mraa_pwm_enable(pwm, 1);
      
          while (1) {
              value = value + 0.01f;
              if (value >= 1.0f) {
                  value = 0.0f;
              }
              mraa_pwm_write(pwm, value);
              usleep(5000);
      
              float output = mraa_pwm_read(pwm);
              printf("Current PWM: %f\n", output);
          }
      //! [Interesting]
          return 0;
      }
      

       

      this is basicly the PWM example.

       

      But unfortunately, I can't get an useful output on the PIN9 of the Arduino Base Board.

        • 1. Re: MRAA PWM not working
          CMata_Intel

          Hi pruesch

           

          Are you having an error message or the issue is that you are not obtaining results in the pin 9?

           

          Regards;

          CMata

          • 2. Re: MRAA PWM not working
            CMata_Intel

            Hi pruesch

             

            Were you able to successfully work with PWM and MRAA?

             

            CMata

            • 3. Re: MRAA PWM not working
              llatta

              I can confirm the observation from pruesch that PWM write is not working. For me (using Arduino expansion board pin 3, in standard config as PWM0), I would always get a very minimal duty cycle, less than 1 usec, no matter what value gets written.

               

              I think there is a bug in mraa_pwm_write, looking at its source it's trying to read back the period from the device. I'm suspecting that is failing, as mraa_pwm_read also always gives me 0.

              I was able to work around it by using mra_pwm_pulsewidth_us insead of mraa_pwm_write, which seems to work:

               

              int periodUSec = 200;
              mraa_pwm_period_us(pwm, periodUSec);
              mraa_pwm_enable(pwm, 1);
              float value = 0.0f;
              while (1) {
                  value = value + 0.01f;
                  //mraa_pwm_write(pwm, value);
                  mraa_pwm_pulsewidth_us(pwm, (int)(value * periodUSec));
                  usleep(50000);
                  if (value >= 1.0f) {
                      value = 0.0f;
                  }
              }

               

              BTW, I'm using MRAA 0.5.4.46, recently updated via opkg.

              • 4. Re: MRAA PWM not working
                KurtE

                I am running into this as well.  It does not fail all of the time, but some of the time.  I have an app that is the start of my Console for the Hexapod, that I use PWM to pin 3 to control the back light to the Adafruit TFT display.  Some of the time the fading works correctly, and other times it is always dark.

                 

                Just had a dark session, that I ran with a journalctl -f running in a different window.  Where I am seeing several:

                Dec 28 18:01:24 Edison libmraa[365]: pwm: Pin already exported, continuing

                Dec 28 18:01:33 Edison libmraa[365]: pwm: Error in reading period

                Dec 28 18:01:38 Edison libmraa[365]: pwm: Error in reading period

                Dec 28 18:01:43 Edison libmraa[365]: pwm: Error in reading period

                Dec 28 18:01:43 Edison libmraa[365]: pwm: Error in reading period

                Dec 28 18:02:09 Edison libmraa[365]: pwm: Error in reading period

                • 5. Re: MRAA PWM not working
                  KurtE

                  Note: I am trying out a patch on my machine, where I updated the mraa_pwm_write to use a cached version of the period instead of trying to read it each time you call it.  The cached value is update every time you either read or write the period through the API.  So far my Auto dimming of the Adafruit 2.8" TFT display is working with it.

                   

                  I generated a pull request of these changes, so the main MRAA people can take a look and decide to include it or not. 

                   

                  Kurt

                  • 6. Re: MRAA PWM not working
                    Frederic Philips

                    Hey Guys,
                    Have any of you got the PWM working? I am at a dead end trying to generate a pwm on Pin 3(pwm0). No matter what period I use I get a constant frequency of 196.8 Hz.
                    I have posted this problem in this thread:

                    Problems with Intel Edison(SPI, PWM and Timer)

                    Thanks in advance