Thanks Shaun. I found a very nice PWM library (shown in my part2) which sets up all the registers and has a 'safe' mode to protect millis() and delay() by not changing anything related to Timer0
@craiglarson67937 жыл бұрын
Great stuff. You highlight one of the many abstractions that Arduino programming language/IDE manages for users in the background. I'm guessing that it may be easier to go straight to C programming for the PWM and Timer peripherals where the full power of all the options are available. But then it wouldn't be an Arduino topic. Thanks for your videos and clear instructions!
@cowsongs2 жыл бұрын
THANK YOU. This is huge for driving any non-trivial load off an Arduino pin. For an LED, it's fine. But if you want to drive some other entire circuit module, maybe some transistor circuit, etc, and you need to approximate smooth DC, a PWM at high frequency can very efficiently and simply be filtered to reasonably smooth DC with a small cap and resistor, much more easily (and with quicker response) than low freq 490 Hz PWM.
@JulianIlett11 жыл бұрын
Yes, delay() and millis() are affected by changing the prescaler for timer0. If you want to avoid that, you should change timer1 and/or timer2 instead. Not sure what side effects would result from doing that though - anyone know?
@webslinger20115 жыл бұрын
Was fumbling around for 3 days looking for this. This will help on my arduino audio school project. Thanks!!!
@T1iCanon9 жыл бұрын
Thank you Julian , I fixed that ugly humming noise of a dc motor by changing the frequeny , now it is even more controlable and really quiet.
@mjosbesh7 жыл бұрын
What frequency did you use? I'm using 976 HZ (pins 5&6) currently and the dc motor sound is annoying at low PWM.
@chbonnici6 жыл бұрын
I tried this and works perfectly as you said. Very helpful. A thousand thanks
@ECOENERGI16 жыл бұрын
Hi Julian, I used this info on one of my projects to eliminate audible noise that I was getting at the lower 976hz. Thanks for sharing, regards Roger
@elninorosario8 жыл бұрын
just a suggestion! .. well u might think that a pencil is harmless when u use it to point adruino pins. But unfortunately graphite(pencil lead) is a conductor of electricity.. so be careful👍
@icarossavvides26414 жыл бұрын
I think you'll find that the discrepancy between the expected and measured nominal 31Khz frequency at the end of the video is down to being on too slow a 'scope range, if you set to 1us/div you'll likely be closer to target.
@alf30712 жыл бұрын
i don't get why it is so hard to change the frequency, I still don't get how it works and have to copy pre made code
@CrazyHHO196 жыл бұрын
hello i was wondering if you would be able to help me.i have puls generator.how can i program it to run 10% duty cycle at 0.64 v input and gradually increacing voltage to 3.86v with duty cycle 90%.its for my project where voltage coming from throttle position sensor potenciometer control my pulse/duty cycle generator which then directly control fuel injector.any advice most appreciated cheers joe
@lalortech52866 жыл бұрын
Hello, does anyone knows where I can download a copy of the header.h file, as Julians website has been down now for some time? I need a copy to be able to compile and upload to Arduino Nano.
@ArcanePath3608 жыл бұрын
setting pins as output is usually a good idea if you've had another sketch previously on it, setting it as an input. This is how pins are destroyed. It's not necessary just good practice.
@seancarney16476 жыл бұрын
Does the nano have the capability to read a frequency and control a device based on that frequency as it changes?
@TYGAMatt4 жыл бұрын
This is many moons old, but would like to ask how you got the PWM library to install. I've had no luck! Tried installing from manage libraries but it doesn't show up and tried the include library/zip file from the sketch. Just says the PWM.h file doesn't exist. Most frustrating.
@TYGAMatt4 жыл бұрын
Ignore my message. I did that thing that you should never do, and that's actually take notice of what the error says. All sorted and will compile now. Ooops
@kbssaprodussoes8 жыл бұрын
void setup() { TCCR2B = (TCCR2B & 0xF8) | value; } For the lazy, just change "value" for 1 to 7 according to this: 31372.5 Hz -> 1 3921.57 Hz -> 2 980.392 Hz -> 3 490.196 Hz -> 4 (default) 245.098 Hz -> 5 122.549 Hz -> 6 30.6373 Hz -> 7
@Felipe-dg6kp7 жыл бұрын
hello. Thank you i dont speak enghlish i have a question if i want set frecuency to 62.5 khz ¿ i can with arduino nano but not in the timer 0 (for the millis and delay)?
@aitorsierra11 жыл бұрын
changing timer prescaler affects to some functions such as delay() or servo library ?., Thanks
@superrodder200210 жыл бұрын
if you used a different frequency crystal to control the clock speed of the chip, wouldn't that change the frequency for the pwm output?
@mustafaglnr87806 жыл бұрын
with the last included codes, will produce 50kHz frequency from arduıno as a PWM pulse.?
@h3ctor19915 жыл бұрын
Thank you so much Julian, it's a excellent tutorial. Regards from Colombia!
@TheGiselaSchumacher9 жыл бұрын
I'm designing a voltage controlled phase controller for AC lamp dimming that I intend to drive via an Arduino Uno. I'm attaining an analog voltage by passing the PWM outputs through a simple R/C network to generate a constant voltage proportional to the duty cycle of the PWM signal. To get a voltage output that changes fast but still has no noticeable ripple, I need a high frequency PWM signal. I've had this prescaler change work on both timers 0 and 1 of my Arduino Uno, giving me adequately high frequencies for my project. The only problem is, serial communications rely on timer 0, and the Arduino needs to be controlled via serial. This being said, most people would think that using timer 2 would be the obvious answer. So did I. However, when I set the prescaler of timer 2 to 1, PWM on pins 3 and 11 stopped working all together. an analog value from 0 to 254 yielded a logic 0, and 255 yielded a logic 1. Can anyone tell me why changing the prescaler on timer 2 of the Arduino Uno doesn't work? Am I doing something wrong, or is it just not possible to do? Thank you for any help!
@TheGiselaSchumacher9 жыл бұрын
Nevermind the issue stated in the last comment. I made a silly mistake. Well, in essence, so did the sources for how to change PWM speeds. They all say to use a code like this:TCCRnB = TCCRnB & 0b11111000 | mode;with mode being the prescaler setting. That works great for timers 0 and 1, because their mode is 3, or 011 in binary. since we're increasing the speed, setting one of those two LSBs to 0 will change their state as seen by a truth:011 & 010 = 010011 & 001 = 001011 & 000 = 000 (noting that this shuts off the PWM all together)However, timer 3 has an initial prescaler of 4, or 100 in binary, so a speed increase will yield these truths:100 & 011 = 000100 & 010 = 000100 & 001 = 000100 & 000 = 000As you can see here, using the commonly given code for prescaler alteration, an attempt to increase the speed will simply disable the PWM for 3 and 11 all together, hence my problem. A simple solution is to change the line of code to this:TCCR2B = TCCR2B + (4 - mode);with mode still being the same prescaler values.Setting mode to 1 will subtract three from the original value of TCCR2B without affecting anything beyond the prescaler values, so, knowing that 100 in binary is 4 in decimal, we can calculate the new prescaler:TCCR2B + (4 - mode)4 + (4 - 1) = 4 + -3 = 4 - 3 = 1I have tested this new code, and it does in fact work for the Arduino Uno PWM pins 3 and 11. The code can also be altered for TCCR0B and TCCR1B as such:TCCRnB = TCCRnB + (3 - 1);NOTE: once the timer speed has been altered using this code, altering it again with the same code will yield errors. This could be remedied with a code like this:int oldPrescaler = x; //set x to 3 for TCCR0B/TCCR0B, and 4 for TCCR2B void setPWMSpeed(newPrescaler){ TCCRnB = TCCRnB + (oldPrescaler - newPrescaler); oldPrescaler = newPrescaler;}This code will automatically compensate for a previously set prescaler if you should ever wish to change it again during a program.Noting how this hasn't been covered anywhere on the internet that I can find, I hope this helps anyone else stuck on the same issue!
@TheGiselaSchumacher9 жыл бұрын
I apologize if the code is confusing in line with the text, I forgot that KZbin does that in comments. If you need any piece of the code in proper format, I can email you a text file of it. Happy coding!
@atleastsomethinghapp9 жыл бұрын
Thank you for this very clear and useful lesson!
@wiraramadhan22276 жыл бұрын
how can you change the arduino prescaller to 8 of a timer ? hmmm
@boubalex58468 жыл бұрын
Can I take from arduino, PWM with duty cycle 20% and frequency 10Khz or 20 khz???
@ztitan6910 жыл бұрын
How can you change the exact frequency to 10khz?
@JulianIlett10 жыл бұрын
I did that in the follow up video
@SuperLefty200010 жыл бұрын
Julian Ilett Hey Julian, my friend created a non-overlapping pwm signal by using PIC18 without any extra hardware. Have you tried it with PIC? I mean how is it non possible with an Arduino?
@NaeemAwan368 жыл бұрын
will u give me the link of follow up video for 10 khz cuz i didnt find it in ur channel i wana generate 20 khz pwm
@chbonnici6 жыл бұрын
enjoy kzbin.info/www/bejne/b3u7eHybgph1ias
@chbonnici6 жыл бұрын
when I load ver5 pwm iI could not get it to work keep getting message "Error compiling for board Arduino /Genuino Uno"
@sunilshailon66567 жыл бұрын
can pwm frequency be changed in an arduino uno similarly??
@keltoumaiaiouss38696 жыл бұрын
How can I set the output to 20 kHz in arduino mega please I need your help
@sirius8ly8 жыл бұрын
Just a quick question. Is the analogWrite duty cycle pre-established. I am going for a 50 percent duty and I am generating the 31khz from D9 like you showed in your video. Thank you.
@backcountry528 жыл бұрын
analogWrite(pin,duty) is the syntax for the function. Duty is a scaled input from 0-255. 0 commands a 0% duty cycle, 255 commands 100%. For 50 percent enter 127.
@sirius8ly8 жыл бұрын
+backcountry52 Thanks for the info. I really appreciate it. Cheers!
@bobc515110 жыл бұрын
if you need two output frequencies can you program the second to have a 90 deg phase shift?
@JulianIlett10 жыл бұрын
Yes, I think so. I'm going to produce a video on that quite soon. It's for a charge pump circuit where I want two PWM signals 180° out of phase, but 90° would be just as easy to implement.
@stevec50007 жыл бұрын
How can I set the output to 130 Hz? I tried running the sketch in Tutorial #2 but it won't compile, it's worthless!
@Inertia8884 жыл бұрын
I see that your question is from a few years ago, and I am honestly just beginning to understand how to manipulate these frequencies. But I wonder if changing the clock speed would allow us to have more control over a wider range of frequencies? Hopefully I don't sound too illiterate to you. Mostly I hope you have since solved this issue but as a beginner, trying to think this through seems to be helping me to grasp the concepts at a deeper level. Maybe I'm even close. Would love to hear what your solution was to this problem. Cheers!
@IngenieurEdis8 жыл бұрын
Hello. Is it possible to switch multiple frequencies with switches ? For exampe i want to drive a DC Motor with 500HZ, 1000Hz and 2000HZ , but i dont wont to change the code every time, but to change it with a switch mechanical. Is there a possibility to program a code witch recognizes witch switch i turned on, so the PWM works with the frequency. Sorry for my bad english.
@EngineeringNibbles8 жыл бұрын
You simply change the pwm % pwm frequency has no impact on the speed of your motors
@paulmeynell88665 жыл бұрын
Excellent as usual
@mihajlopetkovic20038 жыл бұрын
When you change frequency and after that upload different sketch, does it set PWM frequency to default?
@thepvporg7 жыл бұрын
I think its changed to that speed when the sketch runs, thats how I understand the underclocking process.
@usmanyaqoob67654 жыл бұрын
Hello can i reduce frequency to 10hz
@josemalibran41746 жыл бұрын
Hey man I have a question, is it possible to work PWM at low frequencies? on the order of 4 to 10 Hz? Greetings man
@Fleurlean49 жыл бұрын
Very helpful man you are, thank you.
@troydry14028 жыл бұрын
I'm trying to run an rgb led off pins 3 5 and 6 but thinks pins 5 and 6 are the same why is this happening
@JulianIlett8 жыл бұрын
+Troy Dry pins 5 and 6 use the same internal timer
@engrmhhassan76998 жыл бұрын
how to change frequency upto 50hz?
@tejeshwvardhan11658 жыл бұрын
can all 6 pwm run on same frequency of about 10khz?
@Inertia8884 жыл бұрын
I have a related question that I am currently in the process of attempting to understand. I would like to know if each PWM channel, can output a different frequency simultaneously. For example, have Pin5 send a lower frequency to one motor, Pin9 send a somewhat faster frequency to a 2nd motor, and also have Pin11 send a higher frequency to a third motor?
@ctbully7 жыл бұрын
A link to the tables would be nice :-)
@MrBobWareham10 жыл бұрын
Padding resistors should always be used at each end of a pot to stop short outs say 1k0 or lower but love your Arduino Videos Bob
@Dahaksha8 жыл бұрын
@3.05 you say that the pin D13 is missing, it's actually located on the down-left side of the arduino, just look for it. Great video nonetheless.
@Inertia8884 жыл бұрын
I think he meant that he was wishing that D13 was a PWM channel, since it already has an onboard LED. Would be handy for experimenting.
@mendebil6 жыл бұрын
This is awesome. thanks for sharing
@baadikarim4 жыл бұрын
Plz code 3-phase
@alingeorge81607 жыл бұрын
Sir very informative...
@sirius8ly8 жыл бұрын
Thanks Jullian.
@mhalw1211 жыл бұрын
hi Julian, i have been watching your video but i got questions. it is really possible to change the frequency of pwm up to 500kHz or it is limited up to 64kH on pin 5 and 6? thank you.
@JulianIlett11 жыл бұрын
I think you can go to quite high frequencies, but you will lose resolution. So at 500kHz you may only get 3 or 4 different pulse width values.