Home > News & Updates > Electronics News Updates > On Arduino due PWM frequency

On Arduino due PWM frequency

Summary of On Arduino due PWM frequency


Arduino Due, based on the ATSAM3x8E Cortex-M3 running at 84 MHz, can produce much higher PWM frequencies than AVR Arduinos. Bit-banged pin toggling is slow (≈200 kHz with digitalWrite; ≈16.8 MHz with direct port writes). Removing loop overhead on Due can reduce frequency significantly. For highest, use hardware PWM controllers. Arduino IDE uses Atmel CMSIS libraries; system configuration and clock setup are in the IDE hardware system files.

Parts used in theArduino Due PWM Frequency:

  • Arduino Due board (ATSAM3x8E 32-bit ARM Cortex-M3)
  • Arduino IDE (example version 1.5.7)
  • Example code using digitalWrite
  • Example code using direct port manipulation
  • CMSIS libraries and system files in Arduino hardware distribution

On Arduino Due PWM Frequency

September 21, 2014, 9:45 pm

I just got myself a couple of Arduino Due boards. While they were released almost two years ago, I have not really got a chance to look at these until quite recently. Arduino Due is based on Atmel’s ATSAM3x8E 32-bit ARM Cortext-M3 processor. The processor core runs at 84 MHz, which is significantly faster than its 8-bit AVR counterpart ATmega328p which runs at 16 MHz. For an ATmega328p, the highest achievable PWM frequency is 8Mhz (square wave), so we should be able to generate much higher frequency signals on an Arduino Due. But how high can we go? Let’s find out.

If you use the bit-banging method (e.g. pin toggling), the highest achievable output frequency is actually quite abysmal. For example, the following code on Due generates a 200.7 kHz square wave on pin 8 (compiled with Arduino 1.5.7):

The Arduino code base is not very efficient and this code is actually not much faster than the same code on an ATmega328p (126.2 kHz for comparison, compiled with Arduino 1.0.5). By the way, the reason a while loop is used inside the loop() function is that the loop function has some extra instructions for checking the serial port. So if we did not use the while loop, extra instructions would be executed after outputting a LOW, causing the duty cycle to change and lowering the overall waveform frequency. This overhead is not very noticeable on ATmega328p (116.9 kHz without the while loop versus 126.2 kHz with the loop). But on Arduino Due, this difference is much more drastic. If the while loop is removed, the output frequency will drop to 145.4 kHz which is a 30% degradation from 200.7 kHz!

On Arduino due PWM frequency

Of course, we can improve the code efficiency above quite a bit by using direct port manipulation. If we replace the digitalWrite with the following code, we will get an output square wave on pin 8 of roughly 16.8 Mhz. This is a huge improvement over the meager 200.7 kHz with digitalWrite.

We can do even better if we use the dedicated hardware (e.g. via PWM controller) to generate our waveform rather than big-banging with software. But before doing that, let’s take a look at how the the 84 Mhz clock frequency is obtained on the Due.

Under the hood, Arduino IDE uses Atmel’s CMSIS compliant libraries. They source code and binaries are located under /arduino-1.5.7/hardware/arduino/sam/system/ with the Arduino IDE distribution.

In /arduino-1.5.7/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/source/system_sam3xa.c you will find the following definitions:

 

For more detail: On Arduino due PWM frequency

Quick Solutions to Questions related toArduino Due PWM Frequency:

  • What processor does the Arduino Due use?
    The article states the Arduino Due uses Atmel's ATSAM3x8E 32-bit ARM Cortex-M3 processor running at 84 MHz.
  • How fast can bit-banged digitalWrite toggle a pin on Due?
    The article reports about 200.7 kHz using digitalWrite on pin 8 with the given example.
  • How much faster is direct port manipulation compared to digitalWrite?
    Direct port manipulation produced roughly 16.8 MHz on pin 8, a huge improvement over ≈200.7 kHz with digitalWrite.
  • Does removing the while loop affect frequency on the Due?
    Yes; removing the while loop reduced the output frequency from 200.7 kHz to 145.4 kHz in the example, a 30% drop.
  • Is the Arduino IDE code efficient for high-frequency toggling?
    No; the article notes the Arduino code base is not very efficient and limits toggling speed compared to direct port access.
  • What is recommended for generating the highest PWM frequencies?
    Using the dedicated hardware PWM controller rather than software bit-banging is recommended for best results.
  • Where does Arduino configure the Due system clock?
    The article indicates clock setup and system definitions are in the Arduino IDE hardware/system CMSIS files, for example under arduino-1.5.7/hardware/arduino/sam/system/.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top