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.

490 questions
3
votes
3 answers

Arduino Code for Pulse Skip Modulation (PSM)

I have recently bought this light dimmer, and only when I received it I noticed it works with PSM (Pulse Skip Modulation) and not PWD (Pulse Width Modulation). Does anyone know how to write Arduino code and do the wiring for this dimmer? I can't…
Guy Sopher
  • 133
  • 1
  • 4
3
votes
3 answers

Arduino Nano PWM frequency

I have been using an Arduino Nano as a PWM controller for a heating element. I am currently using D11 (PB3). I could change, although all pins with PWM are in use, and would require a re-write and re-wire. This produces a signal at ~490Hz, but I…
Milliways
  • 1,645
  • 2
  • 18
  • 29
3
votes
1 answer

How can I maintain PWM output while still performing other tasks?

I want to generate a PWM signal that I can turn on and off with a button press without using interrupts. Is there a way to keep the PWM output going while running other processes?
user20985
3
votes
3 answers

Is it possible to control the speed of DC motors with Arduino's analogWrite() using analog pins?

I'm trying to control the speed of two DC motors that are connected to a dual H bridge. I always used digital PWM pins with analogWrite() to change the speed, but since I'm making a project that will be using most of the digital pins, I wonder if I…
Alvaro Gaita
2
votes
3 answers

can i give a vcc to 14v?

What is maximum voltage PWM pins can give? can i give a vcc to 14v?
2
votes
0 answers

Control output of XL6009 Boost converter with Arduino

I wonder if someone could help me with figuring out how to control XL6009 Boost converter via Arduino. I have this popular board, and it has potentiometer to adjust the output voltage. However, I want to adjust output voltage based on sensor reading…
Twoface
  • 21
  • 1
2
votes
2 answers

How close to 5 kHz can I set the PWM frequency on an UNO pin 11?

I'm rebuilding a laser CNC machine and using LaserWeb4 as the control interface and GRBL 1.1 as the low-level interface to the stepper motors, limit switches and the laser. I have all the motion sorted out and am working on the laser…
Transistor
  • 629
  • 5
  • 14
2
votes
2 answers

16 bit PWM on a mega

I have two Arduinos, a nano and a mega. I want to output 16 bit PWM, but can seem to only get it working on the nano. I'm adapting the code from this website:…
user1637451
  • 41
  • 1
  • 5
2
votes
3 answers

Arduino - Getting error - 'TCCR2' was not declared in this scope

This code below: void setup() { #if defined(__AVR_ATmega168__) // 62.5KHz PWM for the ATMega168 -> only on Arduino pins 9 and 10 // set prescaler to 1 // (sbi means "set bit register", cbi means "clear bit register") cbi(TCCR1B,…
john smith
  • 33
  • 1
  • 5
2
votes
3 answers

Arduino Nano PWM output acting strange after running for a day or so

I have an Arduino Nano, and I have some issues with it. Basically it needs to control the brightness of a lamp via PWM (plus some circuitry of course). It's hooked up via serial to a little x86 embedded board which runs Linux, and that board is…
notadam
  • 121
  • 5
2
votes
3 answers

Get 25 kHz PWM on a 16 MHz Arduino

I am trying to output a 25 kHz software PWM on at least four pins simultaneously with variable duty cycles on each pin individualy. I've got all the actual PWM outputs via internal timers 2x8 1x16 with variable duty cycles and am looking for a way…
2
votes
0 answers

Implement a discrete controller /transfer function with arduino uno

i want to implement a discrete transfer function at z domain with arduino UNO.I know that i must do the inverse z transform and find the difference equation.Afterwards i must use PWM because arduino UNO has not DAC. Is there anyone who knows about…
1
vote
1 answer

Arduino on/off PWM

When I turn on DC motor with a button I get a a fixed PWM value that goes into a loop till button state changes. What should I change that I could change X value with potentiometer in a loop? int X = A0; int SW = 2; int Xval=5; int SWval; int…
Jimmy
  • 13
  • 2
1
vote
2 answers

Arduino Mega PWM - 100ms Period - SSR and Heater

I'm not very advanced in Arduino "core" functions, so I need your help. I'm using a Solid State Relay in order to control a heating element, with a PID Library (PID_v1.h) to obtain the OUTPUT of the PWM. The code itself is not a problem, so that…
Everton JS
  • 11
  • 2
1
vote
0 answers

How to get PWM frequency to ~14KHz?

I found this question: Set PWM frequency to 25 kHz It is useful, however, I want to use it to generate ~14KHz? int d = 0; // pwm value void setup() { // Sets Timer1 to Phase Correct // F_CLOCK / ( Prescaler * ORCR1A * 2 ) = Freq…
laoadam
  • 45
  • 5
1
2 3