# 16bit 7kHz PWM-output

Hi,

I have to put my output to a 16Bit PWM signal of a Frequency of 7 kHz.
I am totally new with ths issue.

I am using an arduino nano.

I found two solutions for each problem:

The 16 Bit PWM-Signal:

The 7.8kHz output Signal:
https://arduino-info.wikispaces.com/Arduino-PWM-Frequency

Is it possible to set both settings?

Thank you!!

To get 16-bit PWM you obviously need a 16-bit timer. While you can coax two different duty cycles out of a single timer, you can only have a single frequency. So, you would need two 16-bit timers or use a software generated PWM. Check this link for ideas on SW PWM.

A PWM signal is characterized by its Frequency, ON-period, and OFF-period; what has it to do with 16-bit?

I suspect @DKWatson and @GolamMostafa have lost sight of the goal. But, just to make certain we are all on the same page, is this what you want...

anneme:
I have to put my output to a 16Bit PWM signal of a Frequency of 7 kHz.

The arduino nano is having a 8Bit PWM output regularly.
And I want to have an 16 Bit output.

I found out that it could easier to have a 4kHz 16 Bit PWM output on Pin D9 and D10.

But Coding Badly you are totall right!

anneme:
The arduino nano is having a 8Bit PWM output regularly.

Having said the above, have you wanted to mean that the 'duty cycle (ON-period)' (of the PWM Signals genertaed by analogWrite(DPin, arg2); function) can be be varied by arg2 whose value is limited to 8-bit (0x00 - 0xFF)? If so, then the meaning of 16-bit PWM Signal is now clear to me. In Technical Literature, we must try our utmost to make one-to-one correspondence in the context of a term and its unambiguous meaning.

“16bit PWM” usually means 216 different possible settings; each period of the output waveform is divided into 65536 parts, and you get to set how many are off and how many are on using a 16bit number that tells the timer when to switch…

To get 16bit resolution at 7kHz actual period would required a clock of 65536*7000 = 458MHz+.
An Arduino can’t do it. Actually, I don’t know of anything that can do it.
At 16MHz, an Arduino could do about 11bits of precision for PWM at 7kHz, max.
(16MHz/7000 = 2285.7; slightly more than 2048 = 211)

westfw gets this one.

On a 16 MHz Arduino UNO you can get 16-bit PWM (65536 steps) at 244 Hz or 11-bit PWM (2048 steps) at 7812 Hz or 2286 steps at 6999.12 Hz but you can't get 16-bit at 7 KHz.

johnwasser:
On a 16 MHz Arduino UNO you can get 16-bit PWM (65536 steps) at 244 Hz or 11-bit PWM (2048 steps) at 7812 Hz or 2286 steps at 6999.12 Hz but you can’t get 16-bit at 7 KHz.

westfw:
“16bit PWM” usually means 216 different possible settings; each period of the output waveform is divided into 65536 parts, and you get to set how many are off and how many are on using a 16bit number that tells the timer when to switch…

To get 16bit resolution at 7kHz actual period would required a clock of 65536*7000 = 458MHz+.
An Arduino can’t do it. Actually, I don’t know of anything that can do it.
At 16MHz, an Arduino could do about 11bits of precision for PWM at 7kHz, max.
(16MHz/7000 = 2285.7; slightly more than 2048 = 211)

Thank you very much for your answers.
They are realy helpful.

I also have to connect an LCD-Display to my arduino.

So what do you think is the max. bits and frequency I can use, that the display is still able to work?

Sorry I am totally new with this issues.

Let's start from the other end...what is that PWM output going to be driving and what are its actual requirements? A datasheet for whatever it is would be good.

That will tell you if any Arduino is going to be suitable or if you need something more powerful. Arduinos are good but they can't do everything.

Steve

I found out that an 11 Bit would be perfekt and a frequency of 3kHz.

This is the display I am using: https://de.rs-online.com/web/p/lcd-displays-monochrom/5326824/

For measuring I am also using two analog inputs of the nano.

At 3kHz you can get more than 12 bits. 16MHz/3kHz = 5333, log2 (5333) = 12.38 bits equivalent

MarkT:
At 3kHz you can get more than 12 bits. 16MHz/3kHz = 5333, log2 (5333) = 12.38 bits equivalent

That means that the ON-period of the 3 kHz PWM signal can be varied by an incremental value of about 0.08 us ((1/3000)/212). Am I correct in my calculation?

Hi,
What do you need the PWM output for?
What is the device that needs the high frequency PWM?

Thanks.. Tom.. anneme:
I found out that an 11 Bit would be perfekt and a frequency of 3kHz.

You do NOT get a free choice of the number of bits and the frequency. If you use 11-bit PWM on a 16 MHz Arduino you get 7812 Hz (or 1/8th, 1/64th, 1/256th, or 1/1024th that if you use the clock prescale). Perhaps you are using an 8 MHz (3.3V) Arduino Nano? Then you get half that: 3906 Hz. You DON’T get 3 kHz.
Is 3 kHz the ONLY frequency that will work? Is it the minimum? Is it the maximum? Why is the answer no longer 16-bit and 7 kHz?
If you want to use one of the available combinations of bit width and frequency:

``````/*
PWM16Begin(): Set up Timer1 for PWM.
PWM16EnableA(): Start the PWM output on Pin 9
PWM16EnableB(): Start the PWM output on Pin 10
PWM16A(unsigned int value): Set the PWM value for Pin 9.
PWM16B(unsigned int value): Set the PWM value for Pin 10.
*/

// Set 'TOP' for PWM resolution.  Assumes 16 MHz clock.
// const unsigned int TOP = 0xFFFF; // 16-bit resolution.   244 Hz PWM
// const unsigned int TOP = 0x7FFF; // 15-bit resolution.   488 Hz PWM
// const unsigned int TOP = 0x3FFF; // 14-bit resolution.   976 Hz PWM
// const unsigned int TOP = 0x1FFF; // 13-bit resolution.  1953 Hz PWM
// const unsigned int TOP = 0x0FFF; // 12-bit resolution.  3906 Hz PWM
const unsigned int TOP = 0x07FF; // 11-bit resolution.  7812 Hz PWM
// const unsigned int TOP = 0x03FF; // 10-bit resolution. 15624 Hz PWM

void PWM16Begin()
{
// Stop Timer/Counter1
TCCR1A = 0;  // Timer/Counter1 Control Register A
TCCR1B = 0;  // Timer/Counter1 Control Register B
TIMSK1 = 0;   // Timer/Counter1 Interrupt Mask Register
TIFR1 = 0;   // Timer/Counter1 Interrupt Flag Register
ICR1 = TOP;
OCR1A = 0;  // Default to 0% PWM
OCR1B = 0;  // Default to 0% PWM

// Set clock prescale to 1 for maximum PWM frequency
TCCR1B |= (1 << CS10);

// Set to Timer/Counter1 to Waveform Generation Mode 14: Fast PWM with TOP set by ICR1
TCCR1A |= (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << WGM12) ;
}

void PWM16EnableA()
{
// Enable Fast PWM on Pin 9: Set OC1A at BOTTOM and clear OC1A on OCR1A compare
TCCR1A |= (1 << COM1A1);
pinMode(9, OUTPUT);
}

void PWM16EnableB()
{
// Enable Fast PWM on Pin 10: Set OC1B at BOTTOM and clear OC1B on OCR1B compare
TCCR1A |= (1 << COM1B1);
pinMode(10, OUTPUT);
}

inline void PWM16A(unsigned int PWMValue)
{
OCR1A = constrain(PWMValue, 0, TOP);
}

inline void PWM16B(unsigned int PWMValue)
{
OCR1B = constrain(PWMValue, 0, TOP);
}

void setup()
{
Serial.begin(9600);
PWM16Begin();

// On the Arduino UNO T1A is Pin 9 and T1B is Pin 10

//  PWM16A(0);  // Set initial PWM value for Pin 9
//  PWM16EnableA();  // Turn PWM on for Pin 9

PWM16B(0);  // Set initial PWM value for Pin 10
PWM16EnableB();  // Turn PWM on for Pin 10
}

void loop()
{
int analogValue = analogRead(A0);
unsigned int PWMValue = map(analogValue, 0, 1023, 0, TOP);
PWM16B(PWMValue);  // Update the PWM at the end of the current cycle
}
``````

I am converting the PWM Signal to a DC-voltage.

The more bits I have, the better the quantization will be and I am using the high frequency the get a better result of the sinal after it became low pass filtered.

anneme:
The more bits I have, the better the quantization will be and I am using the high frequency the get a better result of the sinal after it became low pass filtered.

So you have a classic trade-off. Higher frequency gets you smaller filter components but higher quantization error. Lower frequency gets you better quantization but larger filter components.
Have you thought of using an active filter? That might allow lower frequency and better quantization without going over your size limits.

johnwasser:
So you have a classic trade-off. Higher frequency gets you smaller filter components but higher quantization error. Lower frequency gets you better quantization but larger filter components.
Have you thought of using an active filter? That might allow lower frequency and better quantization without going over your size limits.

John,
Have you any link to read a bit more on this concern?; is it just the matter on the time constant?
Thanks.
Regards.
Vicente