Questions tagged [pwm]

PWM is a form of signal modulation that allows digital output to approximate an analog signal. Use this for questions about using the PWM pins (marked with ~).

A Pulse Width Modulation (PWM) signal is a series of digital pulses. Each pulse makes the signal go high (on) for a short period, and it then drops back to low (off) for a short period before the next pulse. The result can be referred to as a square or rectangular wave, because of its appearance if it is graphed as voltage over time.

There are two important attributes which describe a PWM signal. Firstly, the frequency determines how often the pulses happen. Frequency is expressed in Hertz (Hz), which is the number of pulses per second. PWM signals generated by the Arduino usually have a frequency of 490 Hz (or 980 Hz in some cases).

The second attribute of a PWM signal is the duty cycle. For human-readable purposes, it is often expressed as a percentage. This determines how long a pulse lasts compared to the space between pulses. For example, a pulse could last 5 milliseconds, and be followed by a 15 millisecond gap before the next pulse. This is a 25% duty cycle, because the pulse is high for a quarter of the total time. A 10ms pulse followed by a 10ms gap would be a 50% duty cycle, and so on.

PWM is very useful for controlling the speed of a DC motor. Raising or lowing the duty cycle will cause the motor to speed up or slow down. A conventional analog approach to motor control would raise or lower the voltage instead, which can result in poorer performance.

Similarly, a PWM signal can effectively control the brightness of an LED. Raising and lowering the duty cycle will increase and decrease the apparent brightness of the light.

The most common way to produce a PWM signal on the Arduino is using the analogWrite() function. The frequency is fixed, but it allows specification of the duty cycle in the range 0 to 255 (where 255 represents 100%).

The tone() function also allows a PWM output to be generated. It has a fixed 50% duty cycle, but allows specification of the frequency in Hz. It is primarily intended for generating simple sounds on a speaker, but could be used for other purposes as well.

497 questions
27
votes
1 answer

What is the frequency of PWM output on Arduino

What frequency do Arduinos use for normal PWM when you use analogWrite()? And is it different for different Arduino model? I'm interested specifically in the Mega 2560, but also want to know if it's consistent between models. I've seen a passing…
Duncan C
  • 5,752
  • 3
  • 19
  • 30
22
votes
2 answers

Why do some pins have a different PWM frequency?

According to the Arduino reference for analogWrite(), the PWM frequency on most pins is ~490 Hz. However, it's ~980 Hz for pins 5 and 6 on the Uno, and for pins 3 and 11 on the Leonardo. Why are these different? Is it a deliberate design feature, or…
Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
15
votes
2 answers

Set PWM frequency to 25 kHz

I currently can set four PWM pins to around 31 kHz with the following code: void setup() { TCCR1B = TCCR1B & B11111000 | B00000001; // Set PWM frequency for D9 & D10: pinMode(pwmPin9, OUTPUT); // Sets the pin as output pinMode(pwmPin10,…
user16307
  • 237
  • 2
  • 4
  • 15
13
votes
6 answers

How to output a true analog voltage at output pin

Regarding my program, it is a program that does some calculations and then outputs a voltage based on the result using analogWrite function. However my problem is that I had done my programming based on a misconception that analogWrite function via…
bytk
  • 133
  • 1
  • 1
  • 9
11
votes
2 answers

Increase PWM bit resolution

I would like to increase the PWM bit resolution of the Arduino Uno. At this moment it is 8-bit which I consider too low. Is this possible without losing the ability of interrupts and delays? Koen EDIT This setup delivers a 16-bit resultion void…
KoenR
  • 177
  • 1
  • 3
  • 11
10
votes
1 answer

Controlling water temperature accurately

I have following setup: Arduino Duemilanove Small water heater connected with optical relay (PWM is ok) Waterproof DS18B20 temperature sensor Unknown amount of water (but temperature sensor and heater are always underwater). How can I accurately…
Olli
  • 265
  • 2
  • 10
9
votes
2 answers

Does millis() conflict with the PWM pins associated with timer 0?

I've read that the millis() function uses the same timer as a couple of PWM pins. If you're using those PWM pins, will millis() still return the correct value?
Aurast
  • 295
  • 2
  • 7
9
votes
1 answer

How can the Arduino Uno support up to 12 servos if it only has 6 digital PWM pins?

According to this: The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not…
user1265
  • 93
  • 1
  • 3
8
votes
2 answers

How precise is the timing of pulseIn()?

I've been using the pulseIn() function for processing PWM-based binary data encoding. It works well for distinguishing pulses which are significantly different lengths, e.g. 500us vs. 1500us. That makes it more than sufficient for handling typical…
Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
7
votes
6 answers

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

I want to generate a 15 kHz pulse with an Arduino using Timer1, but the problem is that if we want a 15000 Hz clock we need to initialize the timer with 1/15000 seconds or 66.66 microseconds, but we can only pass integers without any decimal…
astrick
  • 193
  • 1
  • 9
7
votes
9 answers

How to eliminate noise from PWM controlled 12V fan at low speed

I am trying to build a simple fan controller (single fan). So far I have successfully created a circuit which powers the fan with an external 12v DC adapter and added a transistor (NPN) to switch it on and off. The transistor is connected to a PWM…
Fieg
  • 175
  • 1
  • 1
  • 8
7
votes
3 answers

Write PWM with only 8 bit? (Timer2)

I have a problem with the Servo library. I need to read a PPM signal and create a PWM signal of each channel. The problem is that PPM has a higher resolution than PWM so I want to use the 16 bit Timer1 for reading PPM. However the Servo library…
betion
  • 115
  • 2
  • 6
7
votes
4 answers

Adjust time calculation after Timer0 frequency change

I have an Arduino Nano with an 328P and need all 6 PWM pins. Thus, I had to adjust the prescaler and WGM Mode of Timer0. It is now in phase correct PWM mode with a prescaler of 1. TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00); TCCR0B =…
Splitframe
  • 173
  • 5
6
votes
1 answer

PWM signaling with Arduinos: What is the reason for the ground connection?

This may sound like a very dumb question and I apologise in advance. I've gone through reading about PWM and working on some example sketches to control PC fan speeds using PWM and Arduino. In all documents I read, it is mentioned that "PWM is a…
Phil
  • 435
  • 2
  • 4
  • 12
6
votes
1 answer

Difference between PWM and regular output port for servos?

I have been controlling my servomotors for a while now only using pin 0. After some research, it has became apparent to me that the ports with ~ are the PWM pins. I thought that all pins sent out PWM signals, therefore the motor was able to move…
Fraïssé
  • 905
  • 5
  • 13
  • 16
1
2 3
33 34