 # I need an spwm code for arduino nano

Please house I need an spwm code for pin 6 of arduino nano to generate variable frequency of 210 to 390hz based on a reference voltage change. That is the arduino should be able to adjust the pwm frequency at pin 6 from a minimum of 210hz to a maximum of 390hz based on a feedback voltage at the reference pin.
Please I will appreciate if I can have a clue to write the code.
Thank u

There a million possible different frequencies between 210hz and 390hz. How fine a grain do you need?
Do you currently have code to measure the feedback voltage? Should be at least able to use the sample code that comes with the IDE.
Paul

Thank you Paul for the prompt reply.
I need it to adjust the frequency based on increase in output voltage and decrease in output voltage on the reference.
The frequencies to adjust should be 210, 240, 270, 300, 330, 360, and 390hz.
I will be using resistor network feed to analogy pin A0 as the feedback to adjust the frequency based on rise and fall of output voltage

Please can you help me... I'm pretty new to arduino coding

Ok, we all were at one time. What have you tried and what does it do?
Paul

How do you want to set the duty cycle(on/off percent) of the pwm at your frequency?

The duty cycle should be at least 90%

Actually I don't know how to calculate spwm frequency but I can measure any voltage on arduino between 5-320vdc and write an if code

Does this do what you want?

``````// pin 6 of arduino nano to generate variable frequency
// of 210 to 390hz based on a reference voltage change.

void setup() {}

void loop()
{
tone(6, map(analogInput(A0), 0 , 1023, 210, 390));
}``````

I want the different frequencies to be varied from 210, 240, 270, 300, 330, 360 and 390hz

If spwm means "sinusoidal pwm" there is a good reference here
https://github.com/Irev-Dev/Arduino-Atmel-sPWM

I want the different frequencies to be varied from 210, 240, 270, 300, 330, 360 and 390

I want the different frequencies to be varied from 210, 240, 270, 300, 330, 360 and 390

You have written that several times, but have never stated what you think "spwm" means to you.
Paul

Since those are all multiples of 30, divide the limits by 30 and then multiply by 30:

``````void loop()
{
// From 210 (7 * 30) to 390 (13 * 30)
tone(6, map(analogInput(A0), 0 , 1023, 7, 13) * 30);
}
``````

Thank you Johnwasser how can I write the code so that the arduino will adjust the frequency based on the following voltage range from the feedback: @210hz(260-290v), 240hz(230-260v), 270hz(200-230v), 300hz(179-200v) 330hz(140-170v), 360hz(110-140v), 390hz(80-110v)

I want to generate a pure sine wave form to oscillate a device at a particular voltage and vary the frequency

Do you know how to generate a sinewave output at a fixed frequency?

Yes I do.... But this is entirely different

I don't understand that.

Can you post code which generates a sine wave at any one of your desired frequencies.

i am anxious to see how you avoid switching glitches when you change from one frequency to another.
Paul