Arduino PWM Tutorial #1 – How To Change PWM Frequency

You can vary the pulse width, the mark to space ratio, but the frequency is fixed. So the first thing i wanted to do is look at the pwm outputs and see what the standard frequency is so heres a little arduino nano its a clone, not a genuine one and onto analog a0, which is this pin here and ground and vcc ive attached. A little potentiometer module now youve got to watch these potentiometer modules because, as you can see, um the uh wiper is not on the center pin. Positive is on the center pin, and i didnt realize that initially and i plugged it in uh as you would, a normal pot and smoke started coming out, but anyway, once i worked out how to do it. Um you just uh lets get rid of the magnifying glass you just connect the potentiometer up to analog a0 and vcc and ground. Now ive opened a new arduino sketch, but im going to go to examples and theres one called analog, read serial so im going to close the original sketch and im just going to work from analog, read serial. So in fact, im going to load this into the arduino now lets uh make sure that goes in. There are the tx and rx lights and thats gone in okay. So now what should happen is if i put up the serial, monitor im, getting a stream of numbers coming out there and if i turn the potentiometer, those numbers vary between 0 at one end and 1.

024123 at the other. So its reading the potentiometer value and converting it to a digital number, so the next thing to do is to send the analog value from the potentiometer out to one of the pwm pins. Now only certain of the digital pins are pwm and on the nano, its a bit difficult to see on a normal arduino youve got a little tilde on the nano theres just a dot, so d3 is pwm. D5, d6, d9, d10 and d11 are all pwm pins. Um d13 isnt, which is a shame because theres already an led on d13, which is the uh l, led that one there um, but we cant pwm that so im gon na have to put an external led on. So i think what ill do for the moment is ill put it on, i dont know, say d9, so ive gone to the arduino website and the reference section and im looking at analog right and analog write is the way you set a pin to pwm and The syntax is in the example below we need to set a pin as an output in the setup function, and we need to do an analog write of whatever pin. We choose to a value of our choice now because pwm is runs from naught to 255 and the analog read runs from naught 102 3. We have to divide the value by 4., so ive inserted into my sketch the analog write line, which is here analog, write to pin 9, which is one of the pwm pins sensor, value which ive got from doing an analog read which is up here sensor value Is analog, read, a0 and dividing it by 4 to convert the range not to 1023 to naught 255.

? Now, interestingly, i forgot to put up in the setup setting pin 9 as an output, but it doesnt seem to matter so when you do an analog right. It must do that for you, because if we look down here on the board my led is on and if i turn the potentiometer, i can vary the brightness of the led. So now ive put my oscilloscope across the led and you can see on the screen that weve got um anyway that works. Now. The frequency on channel one is 490 hertz and if you look at the documentation on arduino youll see that they do. Uh quote the pwm frequency as 490 hertz, but ive been digging a bit further into this and its not the same on all the digital pwm pins. If you go to playground.arduino.cc forward slash code forward, slash pwm frequency youve got a piece of code here where you can actually alter the um frequency of the pwm outputs. Now it makes reference to a post this one arduino dot, cc, yap2, uh so, and so so so ive gone to that and its called varying the pwm frequency for timer, 0 or timer 2. and theres a chap here, whats his name mace gr, who has put Down all the frequencies that you can set up on the various digital pins by putting in different divisor figures and the frequencies are different for pins five and six pins, nine and ten pins.

Eleven and three. So at the moment, im looking at pin 9 here and that appears to use timer 1, but pins 5 and 6 use timer 0 and pins 11 and 3 use timer 2. So ive put a second led onto the breadboard on digital pin d5, which is also a pwm pin so im now looking at d9 and d5 and in the code ive put a second analog right line, putting the same uh pulse width data, but ive got it Now, on pins, nine and five, and if i look at the scope um, i cant trigger off the second one because its not a direct multiple of the first one. But if you look at the two frequencies on channel, one ive got 490 hertz and on channel 2, ive got 976 hertz. Now that sort of ties up with this pin 9 with a divisor of 64, 488 hertz well ive got 490 and then up here pin 5 with a divisor of 64. 976 hertz. Well, ive got 976 hertz, so that makes sense. Now, if you scroll down a bit youve got this chat. Mecon 83, saying mace gr im afraid that the frequencies in your post for pins 3, 9 10 11 are a little off. The correct formula is f, equals clock, divided by 510 n, and then he says that gives frequency of 490 hertz so thats where the mystery is. It is actually 490 hertz for pins 3, 9, 10 and 11, not 488 hertz.

Now i wanted a frequency of about 15 kilohertz and unfortunately that doesnt appear anywhere in any of these three tables. There is um in the top table, so thats timer, zero, uh, pins, five and six about eight kilohertz or 62.5 kilohertz and in the bottom, two tables theres, a 31 kilohertz or four kilohertz. So im gon na have to compromise and go for 31 kilohertz. So on my pin, nine, which is my blue, led thats on d9 im going to try and change the frequency to 31 kilohertz, so theres pin 9, so its timer 1 and im just going to copy that line. Tccr 1b equals tccr1b and that binary value or setting and setting will be for kilohertz ox01. The top one in that table so ive pasted that line of code tccr 1b equals tccr1b. The binary number in the ox01 into my setup function and the compiler seemed happy with that, and ive still got the ability to vary the brightness of both leds on the potentiometer and on the scope we now have on channel one ive changed the time base a Bit there 31.3 its reporting and whats. It say on here: 31 250.. Okay, my scopes slightly overreading im getting 31.37, but anyway its 31 kilohertz and ive still got 976 hertz on my channel 2, which was the green led, which was on digital pin d5. So thats it ive changed the frequency of the pwm on one of the timers to 31 kilohertz.

Now you have to be aware that this can interrupt um on timer 1. Certain functions. If you change time of 0, it affects millies and the delay function. Timer.

Share.
Exit mobile version