How to output 1khz signal from PWM output on Arduino nano?

How to output 1khz signal from PWM output on Arduino nano?

See the Timer1 library.

#include <TimerOne.h>
//UNO only

void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);

Timer1.initialize(1000);  // Frequency, 1000us = 1khz
Timer1.pwm(9,512);       // 50% DC on pin 9

//Timer1.pwm(10,255);    // 25% DC on pin 10

// D.C. 
// 100 gives 10KHz
// You can use 2 to 1023 
// 0 & 1 gives a constant LOW 
// 1024 gives a constant HIGH
// 2 gives ~125ns HIGH pulses
// 1023 gives ~125ns low pulses
// 512 gives 50us
}

void loop()
{
}

Or just use tone();

see the reference section.....

Allan

Digital pin 5 and 6 of an Uno/Nano output ~980hz PWM by default.
Could be close enough.
Leo..

Can I modify the duty cycle of the PWM signal with a pot?

alex5678:
Can I modify the duty cycle of the PWM signal with a pot?

Yes.
Say it is -changing its duty cycle- its way of working.
Regards.

Sure.

analogWrite(pwmPin, analogRead(potPin) >> 2);

Leo..

Wawa:
Sure.

analogWrite(pwmPin, analogRead(potPin) >> 2);

Leo..

This command gives discrete values 0-1023?

Like 0,1,2,3...1023?

PWM takes 8-bit values (0-255).
The >>2 bitshift converts 10-bit (0-1023) to 8-bit (0-255).

See this page.
Leo..

What value should the POT have? 1k? 10k? 50k?...

Thank you...

Those will all work. 10k is common and a good compromise between current leakage (lower resistor value = larger current) and ADC input stability (lower resistor value = better stability).

I use this code in order to check the Variable Resistor, with the output connected to a LED, but it does not work well...Any help?

Thank you...

void setup()
{
    pinMode(11,OUTPUT);
    pinMode(14,INPUT); // 14/A0 
}
void loop()
{
    analogWrite(11, analogRead(14) >> 2);
}

Use https://www.arduino.cc/en/Tutorial/AnalogInOutSerial

Thank you I will try it...But can I achieve 0-1000 Hz PWM by tuning the Variable Resistor?

analogWrite(11, analogRead(14) >> 2);

Generates a constant PWM frequency of about 500Hz, with a varying duty cycle (not frequency) depending on pot position.
Use pin 5 or 6 for about 1kHz (post#3).

Google "Arduino PWM frequency".

If you want to vary the frequency, you could try the tone() function.
Leo..

vffgaston:
Yes.
Say it is -changing its duty cycle- its way of working.
Regards.

alex5678:
This command gives discrete values 0-1023?

Like 0,1,2,3...1023?

If your system is 10 bits to begin with, with min value of 0, and max value of (2^10)-1, then shifting right by 2 places is equivalent to dividing by 2^(2) = 4. So it is equivalent to dividing by 4...... although there will be a need for consideration of rounding....eg. rounding down. Eg 1023/4 ends up being 255 (maybe).

The first four values and the last four values of 0-1023 will be 0 and 255.
Leo..

Wawa:
analogWrite(11, analogRead(14) >> 2);

Generates a constant PWM frequency of about 500Hz, with a varying duty cycle (not frequency) depending on pot position.
Use pin 5 or 6 for about 1kHz (post#3).

Google "Arduino PWM frequency".

If you want to vary the frequency, you could try the tone() function.
Leo..

Sorry! I need constant frequency at 1kHz with variable duty cycle....I use pin 6 on Arduino nano...
As you wrote it outputs 980 Hz. To achieve accurate 1 kHz what should I do?

I am using this:

https://www.arduino.cc/en/Tutorial/AnalogInOutSerial

Thank you..

Wawa:
Sure.

analogWrite(pwmPin, analogRead(potPin) >> 2);

Leo..

so can you use this instead of the map() function to convert an analogRead() value to a uint8_t?:

uint8_t var = analogRead(analogPin) >> 2;

silly_cone:
so can you use this instead of the map() function to convert an analogRead() value to a uint8_t?:

2 divides by two, two times.
See the bitshift function.
Leo..