PWM frequencies on pin 9,10,11



  • Hi All,

    I use pro-mini's in many different projects, and of course for mysensor nodes and repeaters and so on. All of them are the 16 MHz 5 VDC versions with a boot loader that makes them think they're Arduino Uno's..😜
    I use them as a standard, which enables me to pick any pro-mini lying around in my workspace and use it to test a new revision in my sensors...

    Now for the question: for a specific project I want to change the frequencies of the PWM pins 9, 10 and 11. As a default they seem to be 490.20 Hz and I need some kHz. What happens if I want to reuse these pro-mini's with altered PWM frequencies for a my-sensor node.
    Are the PWM pins set by the mysensors (library) code automatically?

    Thanks for your answers,

    Boozz


  • Mod

    By default, pins 9, 10 and 11 are used by the nrf radio but they can be remapped using softspi. See https://www.mysensors.org/build/ethernet_gateway for instructions.

    As long as the pins are not used by the radio, MySensors shouldn't affect the pins. Changing the pwm speed can affect the timers though, which can have an impact on the MySensors code. I am not sure which timers are used by MySensors, but timer1 is used by almost anything so changing that will probably have side-effects.

    See https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM and http://playground.arduino.cc/Code/PwmFrequency for details on different PWM settings.


  • Hardware Contributor

    and you will need to be careful in sketch because if you need to change your pwm freq, you will certainly need to change prescaler settings for interrupt as @mfalkvidd rightly told you. And that would need more care if you use softspi for radio...because of timings etc...but that's doable 🙂
    imho I would prefer use hardware spi for radio (more reliable I think) and use other pins for pwm.

    I have never done this in arduino yet (multiple software pwm) but I did it on microchip mcu few years ago. I was able to pwm almost all pins on these 8bit mcu. For this, like explained in links above, I was using output compare registers and I was updating the needed pins in interrupthandler. You can use the howto explained of course, but if you use an interrupthandler you can play with others pins when interrupt triggers.
    So it was sort of "blink without delay" using output compare mode timers and few optimizations..like this you can pwm lot of pins 😉

    Note : I was not using things like pinwrite ...but was directly writing in the corresponding 8bit PORT pin, faster than the function which takes cycles and sometimes you want something precise...


  • Hero Member

    I was having some probs with the led dimmer sketch the other night beacuse i wanted to use PIN6 rather than PIN3.
    My LED strip would flicker....had me puzzled for a bit until i remembered the mysensors v2 beta ( im using quite an old cut of it ) uses pin 6 for the RX/TX/ERR leds...
    So i just reassiged them in my sketch...and problem solved!
    #define MY_DEFAULT_TX_LED_PIN A2
    #define MY_DEFAULT_RX_LED_PIN A3
    #define MY_DEFAULT_ERR_LED_PIN A4

    This may not be relevant to your posts... but i thought it was a PWM frequency issue initially!



  • Thanks for the answers.

    I'm not planning using soft-spi, as it would affect all of the hardware I've made so far (approx 30-40 PCB's for adaption of pro-mini and nrf's as well as some standard temperature and analog in connection).

    I guess at the moment it's the best for me to label the pro-mini's involved or add some extra code in the setup() part of the arduino code. I used this site for the examples how to set the pwm frequencies for the different pins.

    BR,

    Boozz



  • You my friend are a genius, i was having flickering on my dimmer and couldn't get to the bottom of it. I was using pin 6 for controlling my LED strip.

    @gregl said:

    I was having some probs with the led dimmer sketch the other night beacuse i wanted to use PIN6 rather than PIN3.
    My LED strip would flicker....had me puzzled for a bit until i remembered the mysensors v2 beta ( im using quite an old cut of it ) uses pin 6 for the RX/TX/ERR leds...
    So i just reassiged them in my sketch...and problem solved!
    #define MY_DEFAULT_TX_LED_PIN A2
    #define MY_DEFAULT_RX_LED_PIN A3
    #define MY_DEFAULT_ERR_LED_PIN A4

    This may not be relevant to your posts... but i thought it was a PWM frequency issue initially!



  • I've found that adding this to setup() can cure flickering:

      //Increase Timer frequency to prevent flicker in Pins 5,6 PWM
      TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00); 
    

 

309
Online

7.9k
Users

8.8k
Topics

93.8k
Posts