Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 10794

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 ~).

3 votes

STM32 VGA Output - don't understand why lines are jagged

I found a similar issue when I developed code to display on a VGA monitor. The reason for the slight jitter is that when an interrupt fires, it can't stop in the middle of an instruction. So there ar …
Nick Gammon's user avatar
  • 38.7k
2 votes
Accepted

Arduino PWM 1kHz and 10-bit resolution

frequency desired const long timer1_OCR1A_Setting = F_CPU / 1000; const byte outputPin = 10; // Timer 1 "B" output: OC1B void setup() { pinMode (outputPin, OUTPUT); // set up Timer 1 // Fast PWM … top at OCR1A TCCR1A = bit (WGM10) | bit (WGM11) | bit (COM1B1); // fast PWM, clear OC1B on compare TCCR1B = bit (WGM12) | bit (WGM13) | bit (CS10); // fast PWM, no prescaler OCR1A = timer1_OCR1A_Setting …
Nick Gammon's user avatar
  • 38.7k
2 votes

How to make precise square wave?

Timer 1 on the Atmega328P (such as is found on the Uno) to get you into that ball park: const unsigned long countTo = 33658; // F_CPU / 8 / 29.71 / 2 void setup () { pinMode (10, OUTPUT); // PWM … , top at OCR1A TCCR1A = bit (WGM10) | bit (WGM11); // PWM TCCR1B = bit (WGM13) | bit (CS11); // PWM, prescaler of 8 OCR1A = countTo - 1; // zero relative OCR1B = (countTo / …
Nick Gammon's user avatar
  • 38.7k
1 vote
Accepted

Sine Wave Inverter

two PWM signals together into a AC signal. … Then there was a transformer and RC filter to smooth out and amplify the PWM. Sorry I can't be more help, but it was a while ago, and I didn't make all of it. …
Nick Gammon's user avatar
  • 38.7k
1 vote

Frequency generator

The other answers about the way you can configure the timers are very good, but Edgar Bonet's comment goes to the heart of the matter. Do you need accuracy? Your example of 98.23Hz suggests that you …
Nick Gammon's user avatar
  • 38.7k
2 votes

Write PWM with only 8 bit? (Timer2)

kHz: const byte LED = 3; // Timer 2 "B" output: OC2B const long frequency = 50000L; // Hz void setup() { pinMode (LED, OUTPUT); TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1); // fast PWM … , clear OC2B on compare TCCR2B = bit (WGM22) | bit (CS21); // fast PWM, prescaler of 8 OCR2A = ((F_CPU / 8) / frequency) - 1; // zero relative OCR2B = ((OCR2A + 1) / 2) - 1; …
Nick Gammon's user avatar
  • 38.7k
2 votes
Accepted

What should happen when sending analogWrite signal to a non-PWM pin?

Based on my understanding though, sending analog signals to a non-PWM pin would just check for a threshold and send either LOW or HIGH, as opposed to being able to send variable voltages to a PWM pin. … What is the point of PWM pins if its also possible to send variable high/low signals to non-PWM pins? Well, the PWM pins output high/low signals asynchronously. …
Nick Gammon's user avatar
  • 38.7k
1 vote

What prescaler to use for finer control?

The lower prescalers will give you finer control. The bigger prescalers necessarily have coarser control. From my web page about timers is this table of prescalers: n -- Prescale 1 -- -- Prescal …
Nick Gammon's user avatar
  • 38.7k
5 votes
Accepted

How to make sure Arduino's PWM pulses are phase shifted?

TCCR2A = bit (WGM20) | bit (COM2B1) | bit (COM2A1) | bit (COM2A0); TCCR2B = bit (CS21); // phase correct PWM, prescaler of 8 OCR2A = dutyCycle; // duty cycle out of 255 … OCR2B = 255 - dutyCycle; // duty cycle out of 255 } // end of setup void loop() { } How it works The code uses Timer 2 (an 8-bit timer) to count up to 255 in phase-correct PWM mode. …
Nick Gammon's user avatar
  • 38.7k
2 votes

Generating a PWM frequency greater than 125 kHz using Arduino Uno

Using this slightly modified version of your code: // A sketch that creates an 8 MHz, 50% duty cycle PWM and a 250 kHz, // 6-bit resolution PWM with varying duty cycle (changes every 5 µs // or about …
Nick Gammon's user avatar
  • 38.7k
1 vote

PWM and Analog Signal Quality

Something like this should work: With this code on a Uno: void setup () { } void loop () { analogWrite (3, analogRead (A0) / 4); } For slightly more details see my post about doing that.
Nick Gammon's user avatar
  • 38.7k
1 vote

Timer CTC Set and Clear modes -- How to un-Clear and un-Set?

The comment by the OP under the question is: I meant how do I set the OC1A pin when i'm using the clear-on-match CTC mode (and vice versa) I know toggle changes it every match, but I don't understand …
Nick Gammon's user avatar
  • 38.7k
3 votes

Why does an RGB LED between VCC and PWM work?

Do the PWM pins accept current whilst low? The pins sink current when low. And yes, that is commonly done. The pins are not "do nothing" at LOW, and "put 5V there" at HIGH. …
Nick Gammon's user avatar
  • 38.7k
1 vote
Accepted

How can I set 4 Arduino Leonardo PWM-ports between 200Hz and 1kHz?

PWM, fast PWM or something else) and whether each output should be the same or different frequencies. According to the Leonardo product page: PWM: 3, 5, 6, 9, 10, 11, and 13. … Provide 8-bit PWM output with the analogWrite() function. So even if you lose two to Timer 0, you should have 5 outputs left (it looks like the "B" side of Timer 1 and Timer 4 share the same pin). …
Nick Gammon's user avatar
  • 38.7k
4 votes

How to run multiple functions one after another?

Why my loops are very complicated? OK, here is your loop: uint8_t fade_seq; // <--- initially zero void loop() { loop1: fade_seq++; while (fade_seq==1) { fade_red(1); } Serial.println(fade_ …
Nick Gammon's user avatar
  • 38.7k

15 30 50 per page