Arduino (Nano): Switch between Timer and PWM (analogWrite)

Amrit Pal Singh
2 min readMar 12, 2021

Background

There is a fixed relation between the PWM outputs and the timers available in Arduino. Arduino Nano has 3 Timers and 6 PWM output pins. The relation between timers and PWM outputs is:
Pins 5 and 6: controlled by timer0
Pins 9 and 10: controlled by timer1
Pins 11 and 3: controlled by timer2

So, if you are using a timer (for some other purpose like generating timer interrupts) corresponding pins cannot generate PWM output. These outputs can not perform analogWrite. The output can be either LOW or HIGH.

Switching between Timer and PWM functionality

In some use-cases you might want to switch between Timer and PWM functionality. Here is the way to switch between a timer and PWM.

I was in a situation where pins 9 and 10 were used as PWM outputs. In the same use-case a timer was also required to tick every 4 seconds. So for this interrupt timer1 was required which is the only 16 bit timer available in Arduino Nano. This was the conflicts and due to board design the outputs can not be changed.

In the above code 2 functions are implemented to enable and disable the timer. These functions enable_timer and disable_timer can be used to switch between timer operation and PWM output on Pin 9 and 10. Don’t forget to call disable_timer before analongWrite on these pins.

--

--

Amrit Pal Singh

Cloud Software Engineer | Product Development | I write about Tech and Travel | Profile https://bit.ly/3dNxaiK | Golang Web Dev Course - https://bit.ly/go-gin