PWM and Arduino

Before PWM and Arduino get to our point, that is, before reading our article on Pulse Width Modulation, I strongly recommend that you know what timer and fresco are, and I get started.. When electronic, signal processing or square wave are mentioned, PWM (Pulse Width Modulation) technique usually comes to mind first. The main purpose of this technique, which performs modulation process, is to control the power of the electricity given to the devices.

This technique, which stands for Pulse Width Modulation, is a technique that is mainly involved in electronic circuits such as signal processing or signal transmission, as well as in special application areas such as Arduino or electrical machines. In its simplest form, it can be defined as a signal modulation technique. In addition to the purpose of translating the signal information into suitable for transmission, it is also aimed at providing power control and supporting special circuits such as electric machines and solar cell chargers.

What is Duty Cycle?

This control is provided entirely by switching. The faster switching is performed, the greater the power of the signal transmitted by PWM. For example, if the PWM technique is needed in the signal sent to a lamp, maximum efficiency can be achieved when this technique is applied at a frequency of 120 Hz. pwm arduino There is a concept defined as "Duty Cycle", or mission cycle, and it is frequently encountered in PWM technique. The concept of a task cycle actually indicates the period of the process performed. If this cycle is at low levels, the transmitted power is low, while high power is transferred at high levels of the cycle.

PWM and Frequency control are actually like dimmable lamps that most of you see in your home. A kind of pocinciometer is used to adjust the intensity of the light, whereas it is applied as an on/off process. PWM technique can also be used in Arduino. With the PWM technique used within Arduino, analogue results can be obtained from digital results. In addition, square wave production, which is especially needed for control, is realized. pwm arduino Square wave, as it is known, provides "on" and "off" positions


Thus, when the square wave is sent, 5V is applied in the "on" position, while 0V is applied in the "off" position. This "ten" section is active in the width called "Pulse Width". In order to achieve the desired signal width, you need to apply modulation technique, which is the basic logic of PWM. We need to use the PWM technique to install a flashing LED circuit with Arduino or perform DC engine control with Arduino. This technique lies in the software embedded in Arduino. With the "analogWrite(…)" function, the amount of the task cycle is determined and square wave is obtained.

PWM Application Areas

  • Direct Torque Control (DTC):

    It is a PWM technique developed for use in AC motors. It is made of delta modulation technique. Thesignal sent to the engine is passed through a specific filter and the engine's torque and magnetic flux are kept under control.

  • Time Divider:

    They are especially used by microprocessorsin PWM outputs. They can also be used on circuits with variable task cycles.

  • Communication Systems:

    In telecommunication systems, the importance of signals is very great and the capabilities such as controlling these signals and complying with the modulation technique are also very important. Square wave and PWM are seen as the 2 most commonly used elements in the logistics communication circuits consisting of "0" and "1".

  • Power Transfer and Electrical Machines:

    Power control and adjusting the amount of power transferred are also important elements in the electrical and electronic world. This can happen thanks to PWM signal control in high frequency circuits.

  • Voltage Regulator Circuits:

    PWM technique can also be used in voltage regulator circuits. A number of electronic noises are formed as a result of the PWM technique, which can regulate the voltage with cutting and editing processes by switching to the appropriate task cycle. However, it is possible to overcome this situation with LC filter.

  • Sound Effects and Amplifiers:

    PWM technique can be used to achieve some sound effects together with oscillators. At the same time, PWM technique can be applied for control in amplifier circuits and amplifiers.

  • Other Electrical Fields:

    The designs of micro invertersused in solar energy systems and wind energy systems are carried out in accordance with the PWM technique. At the same time, the PWM signal is transmitted to FET type transistors. In addition, the PWM technique can be used for control purposes in robotic circuits and servo motor circuits.

MOSFET or IGBT can be used according to various circuit structures, while Variant can be used to adjust the input power. However, in some circuits, PWM control is used when these elements are not able to perform the desired function. Especially for engine speed control, PWM technique is frequently encountered.

What can be done with PWM?

  • You can turn down an LED and increase the beam.
  • You can provide an analog output; if digital outputs are filtered,
  • %0 voltage output between 100% and 0v – 5v)
  • You can generate sound signals
  • You can provide speed control for engines. You can generate modulated signal (such as Ifrared LED Controls)
void setup(){
     pinMode(13, OUTPUT);

void loop(){
     digitalWrite(13, HIGH);
     delayMicroseconds(100); Approximately 10% loop at 1KHz
     digitalWrite(13, LOW);
     delayMicroseconds(1000 - 100);


Principles of PWM Technique

The basis of the PWM technique lies in producing square waves. This square wave is usually as large as the average of the wave it references. In addition, the size of the wave in the systemis directly proportional to the size of the task cycle. There are multiple methods of obtaining the PWM wave. If we list them: ♦ Delta: Delta modulated PWM signal is obtained by integral receiver and limiter circuit structures. The integral result is "off" to the limits, and in other cases the square wave occurs because the "on" state occurs. ♦ Sigma – Delta: In this modulation, the output signal is removed from the referenced signal. Thus, the error signal is obtained. According to the result of the integration of the error signal, if it touches the limit, the state "off" is created, and if it is not, the "on" state is created and the square wave is obtained. ♦ Space – Vector: Square wave is obtained using algorithm for 3-phase AC applications.
pwm arduino
Sigma – Application of PWM Technique by Delta Method

Fast PWM and Usage;

PWM values faster than software-based PWM, i.e. analogwritecode, can be obtained using the ATmega chip's own PWM integrations

Records and Bits;

Be sure to refer to the ATmega datasheet page used in Arduino to make sure that the correct bit usage is between timers and between devices. There are 2 Fast Arduino PWM Modes available; In fast PWM we can work at a frequency of 62.5kHz and use factorical dimensions to slow down this frequency; 8, 64, 256 or 1024. This mode sounds complicated from the description, but it's just standard PWM , except that it's faster than normal PWM mode. Phase Verification PWMde will give us 31.25kHz, you can better understand it with the following factors to slow it down

timer can run between 0 and 255, or from 0 to a constant value.
in this code "x" "0", "1" or "2" timer is used for 0, 1 or 2.
timer setting
Option Prescale factor
TCCRxB = _BV(CS00);              1
TCCRxB = _BV(CS01);              8
TCCRxB = _BV(CS00) | _BV(CS01);  64
TCCRxB = _BV(CS02);              256
TCCRxB = _BV(CS00) | _BV(CS02);  1024

For Arduino Uno

Timer output Arduino output Chip pin Pin name
OC0A 6 12 PD6
OC0B 5 11 PD5
OC1A 9 15 PB1
OC1B 10 16 PB2
OC2A 11 17 PB3
OC2B 3 5 PD3

With the OCRxA register, you can set one of the pins for fast pwm.  

Timer0 PWM Output

Timer0 is the general use of arduino most sensors and libraries use this timer, but it is quite simple to change this timer, you can change the timer with a short CONTROL+F search in the library's .c file, but you can also change the timer0's PRESACE FACTOR reputation through "hardwarearduinocoresarduinowiring.c",but as a result, the militia, micros, delay commands may not work as you wish. We also cannot use a timer at the same time, so we must distribute the task to the timers according to the function, for example, the RadioHead library and the TextToSpeech library use the same timer, so you will encounter a compilation error, you just need to change the timer in the .c code of anyone to prevent this.  

Unmodified pwm pin frequencies


Uno, Nano, Mini

3, 5, 6, 9, 10, 11

490 Hz (pin 5 and 6: 980 Hz)


2 – 13, 44 – 46

490 Hz (pin 4 and 13: 980 Hz)

Leonardo, Micro, Yún

3, 5, 6, 9, 10, 11, 13

490 Hz (dirty 3 and 11: 980 Hz)

Uno WiFi Rev.2

3, 5, 6, 9, 10

976 Hz

MKR boards *

0 – 8, 10, A3 (18), A4 (19)

732 Hz

MKR1000 WiFi *

0 – 8, 10, 11, A3 (18), A4 (19)

732 Hz

Zero *

3 – 13, A0 (14), A1 (15)

732 Hz

Due **


1000 Hz


3, 5, 6, 9

pins 3 and 9: 490 Hz, pins 5 and 6: 980 Hz