Arduino PWM Tutorial #1 – How To Change PWM Frequency

Introduction

When working with electronic devices, it is crucial to understand the concept of Pulse Width Modulation (PWM). PWM allows for precise control over various parameters of a signal, such as the pulse width and the mark to space ratio. However, it is important to note that the frequency of the signal is fixed. In this article, we will explore the significance of understanding PWM outputs and how they can be measured using an Arduino Nano board.

The Importance of Understanding PWM Outputs

Measuring PWM Outputs with an Arduino Nano

To investigate the standard frequency of PWM outputs, we will use an Arduino Nano board. It is essential to note that the Arduino Nano used in this experiment is a clone and not a genuine one. We will utilize the analog A0 pin, along with the ground and VCC pins, to connect a potentiometer module.

Connecting the Potentiometer

It is crucial to exercise caution when working with potentiometer modules, as their connections may differ from traditional potentiometers. In most potentiometer modules, the wiper is not aligned with the center pin, as one would expect. Instead, the positive terminal is positioned at the center. It is essential to double-check the connections to avoid any accidents or damage to the components. Once the correct connections are established, the potentiometer can be connected to the analog A0 pin, as well as the VCC and ground pins.

Configuring the Arduino Sketch

To measure the PWM outputs, we will use an Arduino sketch. Open a new sketch and navigate to the examples section. Choose the “Analog > Read Serial” example to load the code onto the Arduino Nano. Verify that the sketch is successfully uploaded by checking the TX and RX lights on the board.

Monitoring the PWM Outputs

To observe the PWM outputs, open the serial monitor in the Arduino IDE. You should now see a continuous stream of numbers scrolling down the monitor. As you adjust the potentiometer, these numbers will vary between 0 at one end and 1023 at the other.

The Importance of PWM Pins in Arduino

Arduino is a powerful platform for building and prototyping electronic projects. One of the key features of Arduino is its ability to use Pulse Width Modulation (PWM) pins. In this article, we will explore the significance of PWM pins and how they can be utilized effectively in an Arduino project.

Understanding PWM Pins

PWM pins on an Arduino board allow for precise control of the output voltage. Unlike traditional digital pins that are either high (5V) or low (0V), PWM pins can produce a variable voltage between 0V and 5V by rapidly switching the output signal on and off. This switching happens at a frequency that is too fast for the human eye to perceive, resulting in a perceived analog output voltage.

Identifying PWM Pins on Arduino Nano

Arduino Nano is a compact version of the Arduino board. However, identifying PWM pins on the Nano can be a bit tricky. Unlike the tilde (~) symbol on a standard Arduino board, the Nano uses a dot to indicate PWM pins. PWM pins on Arduino Nano include D3, D5, D6, D9, D10, and D11. Unfortunately, pin D13, which controls the built-in LED, is not a PWM pin. This means an external LED will need to be connected for PWM functionality.

Setting PWM Pins in Arduino Sketch

To utilize PWM pins in an Arduino project, we need to set a pin as an output and use the analogWrite() function to assign a value between 0 and 255 for varying the output voltage. The syntax for analogWrite() is as follows:

AnalogWrite(pin, value);

In the example below, pin 9 is chosen as the PWM pin, and a sensor value is used as the output value:

AnalogWrite(9, sensorValue / 4);

Since analogRead() functions return values ranging from 0 to 1023, to match the 0-255 range of analogWrite(), we need to divide the sensor value by 4 to scale it down appropriately.

The Importance of Setting Pin 9 as an Output for Analog

When working with analog functions on an Arduino board, it is important to properly set the pins as outputs in order to achieve the desired results. However, in some cases, forgetting to set a specific pin as an output does not seem to have a significant impact on the overall performance. This article explores the implications of not setting pin 9 as an output and how it can affect analog operations.

Varying the Brightness of an LED

One of the interesting observations found is that even without setting pin 9 as an output, it still manages to perform analog operations seamlessly. For instance, when an analog function is applied, such as controlling the brightness of an LED using a potentiometer, the LED’s intensity is easily varied. This suggests that Arduino automatically compensates for the missing output setting on pin 9.

Understanding PWM Frequency Differences

By exploring further into the documentation provided by Arduino, it becomes apparent that the default PWM frequency is stated as 490 hertz. However, upon a closer look, it is discovered that this frequency is not consistent across all digital PWM pins. To delve deeper into this matter, a visit to the Arduino playground is necessary.

On the playground’s page titled “Varying the PWM Frequency for Timer 0 or Timer 2,” there is a code snippet that allows for adjusting the PWM frequency on various digital pins. The author of this post, Mace Gr, has provided a comprehensive list of all the frequencies achievable by setting different divisor figures.

Different PWM Frequencies for Different Digital Pins

Upon examining the documentation further, it is revealed that the frequencies differ depending on the digital pins utilized. Pins 5 and 6 have one set of frequencies, pins 9 and 10 have another set, and so on. By referring to the information provided by Mace Gr, it becomes possible to customize the PWM frequency as desired for improved performance.

To summarize, while forgetting to set pin 9 as an output may not initially seem problematic, it is crucial to understand that this can have implications on the overall performance and compatibility with other digital pins. By carefully configuring the PWM frequency and understanding the differences between the various digital pins, it becomes possible to optimize the functionality of an Arduino board and achieve the desired analog operations efficiently.

The Mystery of Pin Frequencies: Unveiling the Truth

Diving into the intricate world of electronic circuits and components can be both fascinating and challenging. As electronics enthusiasts, we often find ourselves delving deep into the complexities of pins, timers, and frequencies. In this article, we will unravel the mystery behind pin frequencies and shed light on a common misconception.

A Closer Look at the Pins

Let’s start by examining the different pins and their corresponding timers. We have pin 9, which utilizes timer 1, while pins 5 and 6 operate with timer 0. On the other hand, pins 11 and 3 make use of timer 2. Understanding these timer allocations is crucial for our investigation.

An Additional LED on Pin D5

In an attempt to observe the behavior of the pins more closely, I have added a second LED to the breadboard, connected to digital pin D5. This pin happens to be a PWM (Pulse Width Modulation) pin. Now, my focus lies on pins D9 and D5. Implementing the necessary code, I have generated a second analog right line, applying the same pulse width data. The LEDs are now connected to pins 9 and 5.

The Frequency Analysis

Looking at the signals on a scope, I encountered a minor setback. The second LED’s frequency was not directly related to the first LED’s frequency, making it difficult to trigger off the observation. However, a careful examination of the two frequencies on different channels revealed a compelling pattern. Channel 1 exhibited a frequency of 490 hertz, while channel 2 displayed 976 hertz. These findings seem to correspond to the pin configuration, specifically pin 9 with a divisor of 64 (resulting in 488 hertz) and pin 5 with a divisor of 64 (equating to 976 hertz).

The Revelation: Correcting Misconceptions

As the article continued, a significant revelation appeared in the form of a comment by a user named Mecon83. They pointed out that my previous frequencies for pins 3, 9, 10, and 11 were slightly off. According to Mecon83, the correct formula to calculate the frequency is f = clock divided by 510 multiplied by n. Surprisingly, this formula yields a frequency of 490 hertz for pins 3, 9, 10, and 11, which contradicts my initial assumption of 488 hertz.

Thus, the mystery surrounding the pin frequencies dissipates as we accept the corrected formula provided by Mecon83. It is essential to rely on accurate formulas and diligent research to avoid misunderstandings in the world of electronics.

Choosing the Right Frequency: Exploring the Options

To create the desired frequency of 15 kilohertz, I began my search through various tables. Unfortunately, after thorough examination, I couldn’t find this specific frequency anywhere in the three tables. However, I did come across some close alternatives. Let’s dive into the details.

Options in the Top Table: Timing and Pins

In the top table, I noticed a couple of frequencies that piqued my interest. The first one was 8 kilohertz, and the second one was 62.5 kilohertz. Although these options were not exactly what I had in mind, they could potentially work as a compromise.

Exploring the Bottom Tables

Moving on to the bottom tables, I found two more intriguing frequencies – 31 kilohertz and 4 kilohertz. While the latter didn’t align with my requirements, the former seemed like a plausible option. With no better alternatives at hand, I decided to settle on the 31 kilohertz frequency.

Implementing the Changes

Now that I had settled on the frequency, it was time to put my plan into action. I focused on pin 9, which controlled the blue LED on d9. To change the frequency to 31 kilohertz, I added the line of code “TCCR1B = TCCR1B | ox01” in my setup function. With this implementation, the compiler seemed satisfied.

Varying Brightness and Assessing Results

An advantage of this setup was that I could still adjust the brightness of both LEDs using the potentiometer. Additionally, I modified the time base on the scope to accurately measure the frequency. The scope displayed a reading of 31.3, whereas it slightly overread at 31.37. Regardless, this minor discrepancy didn’t affect the functionality of my project.

Understanding PWM outputs is essential for anyone working with electronic devices. By measuring the PWM outputs using an Arduino Nano board, we can gain valuable insights into the frequency and variations of the signal. This knowledge enables us to make accurate adjustments and ensure optimal performance of our electronic projects.

PWM pins in Arduino enable precise control of output voltages, allowing for various applications such as controlling the brightness of LEDs, controlling the speed of motors, and generating analog signals for different purposes. Understanding the significance and proper usage of PWM pins is essential for anyone working with the Arduino platform.

Exploring the intricacies of electronic pins and their frequencies has enlightened us about the importance of precision and accuracy in understanding circuits. The journey to unveil the truth behind pin frequencies reminds us of the ever-evolving nature of knowledge and the value of communities that share their expertise. Armed with the correct formula, let us continue our exploration of electronics, always striving for accuracy and unlocking new possibilities.

I successfully achieved my goal of changing the PWM frequency to 31 kilohertz, even though it wasn’t the exact frequency I initially desired. Through exploration and compromise, I managed to find a suitable alternative that allowed me to proceed with my project. This experience further reinforces the importance of adaptability and resourcefulness in problem-solving endeavors.

Share.
Exit mobile version