Arduino Tutorial 8: Understanding Pulse Width Modulation (PWM) and the Arduino Analog Write Command
HTML-код
- Опубликовано: 30 июл 2024
- You guys can help me out over at Patreon, and that will keep this high quality content coming:
/ paulmcwhorter
In this video we dive into how the Arduino Analog Write command really works. We connect the arduino to an oscilloscope and take a look at what the waveforms really are that are being generated.
You can get the following Elegoo kit, to follow these lessons and play along at home
amzn.to/3c9SJrO
You can get more details at our WEB site HERE:
toptechboy.com/arduino-tutori...
[Disclosure of Material Connection: I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com. This means if you visit the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.]
#Arduino - Наука
You: Trying to set a half brightness to the LED.
LED: -_-
Aight, that's the best joke I've heard all day
Just change int to float and change the number to 127.5
Hahahaha. Nerdy. ❤️
😂😂😂😂😂
you won the internet for a day
The moment I knew Paul was my favorite teacher was when he felt the need to re-square his boards because it bothered him - then explain it bothered him. His organization makes me so happy.
I’ve been binging all of these over my 2020 Xmas break. Thanks, Paul, for putting these learnings together! You’re an EXCELLENT teacher.
Having things square (in alignment) is an OCD trait of mine. I also obsess over things like keeping my cash all facing the same way front side up and stacked in denominational order. A picture on the wall makes me crazy if it is off level by 2 degrees. Grid-lined paper is greater than sliced bread to me.
@@arubaguy2733 "keeping my cash all facing the same way front side up and stacked in denominational order." YES. bonus points if you flatten bent corners on bills
the moment i knew was 2 seconds into the first video
the sheer attention to detail sets him apart
cheers!
Some of the best camera work I've seen on the internet RUclips lessons.
agreed, this is honestly amazing
Correct
Definitely. Great performance for continuous recording without weird cut off blips
I have worked all my life in the electrical/electronic field And I Wished I had a Tutor like you . I'm now in my seventies and still learning Thanks Paul You are the best .
Your explanations are great: I'm a total novice and yet I understood perfectly everything you talked about.
Plus, I love the fact you're teaching things that allow us to understand what's going on, instead of just telling (for example) that PWM works the way it does "because it does".
I'm really glad I've found your videos!
Awesome, thank you!
lesson 8 and i've learned a lot about arduino and how to get a "caffeine overdose" from those iced coffee
HAHAH, thanks Paul McWhorter
"A real way of teaching engineering"
Well explained sir
I can see my dream come true only because of your fantastic tutorials. I want to run a CNC machine using Arduino. I will be greatful to you for rest of my life.
Hows the CNC machine going?
What’s a CNC machine?
@@danieldickinson9167 CNC stands for Computer Numerical Control and it is used to move the several axis of a tool by computer control. Industry uses CNC for welding metals, drilling, cutting and more on many materials. There are CNC drills, milling machines, lathes, routers, plasma cutters, water jet cutters, welders, EDM machines, benders, grinders, and probably many other machines that are CNC. People are now building their own 3D printers which are a type of CNC machine.
It was interesting that when you dropped the pwm signal to 60 with the capacitor in the circuit the led actually cut out as the forward voltage was too low. This of course doesn’t happen without it as it’s being pulsed with 5v for a short time. So an led can run dimmer with pwm than it can with analog input. That’s really cool!
Really you are a fantastic teacher, it is a good way to explain PWM. now I understand PWM very well! Thanks.
Absolutely agree. Can't imagine it being demonstrated any other way, now that I have seen this lesson. Way to go, Paul!
Just wanted to thank you for this series. I've been "shotgunning" circuits and code by just copying and making up my own stuff for too long. You really have made understanding what is going on a lot easier!! Having a good teacher that is interested and motivated in the subject makes all the difference in my opinion. Thanks again and let's keep rolling!!
I didn't really understand Pulse Width Modulation before but I do now. Watching you build the circuit, then actually seeing a pictorial representation of what's happening as you changed the values in the program really made it clear. A lot clearer than reading an explanation in a book.
The amount of preparation and thought that goes in making these videos is heartwarming. You're a fantastic teacher Sir
Dear Paul!
Thank you for this in depth series of the Arduino. This is very promising and I'll can't wait to see all of it.
Thank you again sir!
I like that you go through the basics and code before jumping right into fancy sensors and motors like other lessons. You give us a good background and concepts so we create a renown things 👍
I am a senior Electrical Engineering major at a good school and I didn't understand PWM at all when it was taught to me. Now it makes complete sense. Thank you so much Paul!
PWM is something like turning a light switch on and off fast or slow causing it to be brighter or dimmer. Enjoying your classes. Thanks
my notes for this lesson
analogWrite("pinNumber","value between 0-255"); the value 0-255 corresponds to 0-5 volts
this command uses - pulse width modulation: this means that the Volts are only either 0 or 5 but pulse on and off at a speed that is dependent on what value you are using.
how to view it: imagine a person is running at a speed of 5mph, for 0.1 seconds before they stop for 0.1 seconds. from your perspective it would appear that the person is running at a speed of 2.5mph even though they are actually only ever running at a speed of either 5mph or 0mph
brilliant
@@MrAshwindersingh well i dont get it. ..
Is it because the image persists in the eye for 0.1 s approximately. So even when it stops our eye continues seeing it for 0.2 secs and that is why it appears that he is slower and running at 2.5 mph?
8 lessons in 3 days, and still going strong! I love it, Paul
8 in 2 days mine
that's a lot of coffee!
Hearing you say “hook a brother up” just cracks me up. Seriously though - these tutorials are GREAT! You are an amazing teacher. So clear and concise. Thank you so much.
These kind of lessons are empowering in the sense that I am learning about what's actually going on rather just putting words into code. It is not boring at all. Understanding the fundamentals is allowing me to think further into the codes and asking myself "what is really going on when I put this code in and is it what I want the Arduino to do?" The more of these kinds of lessons, the more I feel I am gaining in fluid knowledge that I can apply rather than just regurgitating information.
I'm a 50 year old technician that went to tech school before PC's were popular. Maybe it's the fact that I'm mature now and have a greater attention span but I think you are just a kind soul and it translates to a good teaching style. You are definitely taking the apprehension I had in learning Arduino and the associated programming to a manageable level. I've thoroughly enjoyed your lessons and look forward to learning more.
Sir there could not be an easier video explaining these concepts Bravo and a salute to your efforts 👍
I have been wanting to understand PWM for so long and have been looking all over the internet for this. This is by far the best video that explains PWM to me with this oscilloscope. Thanks, Paul for this.
Hey Paul, like always, enjoying and actually learning from these videos, instead of other places. Thanks a lot once again. Keep it up.
I loved seeing the voltage on the oscilloscope, it really brings home, in a very visual way, how PWM works. I would love to have seen the Fade sketch on the oscilloscope to see what that would look like. Thanks for another great vid
as an apprentice electrician who understands the electrial side more than anything else, i think you did an amazing job at explaining PVM. im loving the classes man i wish my electrian classes were this fun! keep it up man!
I really love these videos, I’m a freshman doing mechanical engineering! I love these so much thank you
Well designed course for PWM. Thank you, Paul.
I would like to summarize the concept of PWM: a programmer sets the time for a circuit to be on and sets the time for a circuit to be off, in an on-and-off cycle. The result is the final average output voltage is set to a fixed value.
To easily undestant the effect of PWM, I do not think the best example is LED brightness. I believe the best example is use PWM to set the speed of a computer fan. Students will be able to visually see and understand PWM can set or change how much wind the fan can blow to them.
Thank you, Paul!
agreed, the eye doesn't respond linearly to brightness
It's actually amazing what you can do with pwm. We use it quite a bit with flight controllers to control our brushless motors for our drones. These motors are supplied constants battery voltage up to about 25v but the pwm occurs through a data line which is just 0-5v that turns the motors off or on by applying the battery voltage or removing It. It handles not only setting the rpms of the motors but ramping them up slowly or quickly and even active braking of the motor rpms by momentarily reversing the motor. Not to mention the radio control signal is based on pwm as well. I was one told to think of pwm as cruising at speed on a bicycle. You can stop pedaling and coast. If you want to keep that speed you don't have to pedal constantly but rather push the pedal full force a quarter turn every now and then to keep the speed constant. I probably didn't explain that very well but it made sense to me at the time.
I’m just starting arduino I didn’t think this lesson was important but wow
Paul, you do an excellent job of describing things. It is very easy to follow. You are a great teacher. Please continue to produce videos!
Thank you! This series is excellent. This is probably the best, most understandable explanation of PWM that I've yet seen.
"Does this make sense?"
Absolutely! Bringing the capacitor into it was brilliant. Gives me a better understanding of those, too!
I'm really just starting out learning Arduino coding and electronics, and this series so far is the best for learning that I've tried so far!
I actually regret not seeing this sooner
who is here in this quarantine
me!!!
i am doing this is surprising how many items we use every day could be working with there's
Week 10 (excluding school holidays) of lockdown in Scotland here :-)
me i loved this class
Me!
The oscilloscope was a perfect tool to show the point. I was familiar with the concepts before watching the video, but seeing the square wave, and then the wavy analog voltage still felt enlightening. Nice job.
You are a great teacher, Paul. I have tried to learn these things several times but never went beyond the first lessons. Now I am hooked to your course. The ice coffee may help, but I think it is rather your teaching excellence. Thank you.
Paul I work in heavy equipment design and particularly in the hydraulic systems which power the mechanisms. Sometimes the valves we use are PWM type valves. Now with this understanding I can see better how the valves are working. In those systems they have PWM drivers which apparently produce enough amperage to actuate the valve coils. And I believe the signal we send the drivers is just a lower amperage signal. Does that sound correct?
The arduino simulates the analog voltatage by switching from plus 5v to 0 back and forth in a frequency to get the voltage
unless you use a capacitor right? I'm just asking because im not sure.
@@vitustillebeck4965 yaahh....
I genuinely feel like you are one of the best teachers I have come across and I dont just mean with arduino I've watched a few learning videos on the old RUclips and you are by far the most clear and explanatory and you make the lessons engaging I wouldn't say you ramble at all you just want to be clear and precise. Plus you know you've found a good teacher when you're disappointed that no homework was set haha.
I might have learned this few decades ago and have forgotten, but the way that you have explained PWM is embedded such that it will never be forgotten. Thank you.
Excellent!
Hi Paul. loving the videos and looking forward to each one. only thing I noticed is your scope was set to 0.5mS per division and the period was 4.2 divisions. that to me is just a fraction over 2mSec not as you said 4mS. just me being eagle eyed. but loving it. when you getting onto inputting via switches or sensors?. keep up the excellent videos and not tried the ice coffee yet...
I could have misread it, thanks for the observation.
Not drinking Ice Coffee could get you banned from the channel, though:)
@@paulmcwhorter Now Paul, you know it's really tea in that cup. HaHa. Fantastic tutorial series so far! Learning so much. Thanks
@@paulmcwhorter actually you read the period from the screen and the msec/division scaling correctly, you just made a mistake when you used the calculator. You might want to put an overlay of the video at that point with the correct period of 2.1 msec rather than 4.2 msec.
Thank you for the Arduino Tutorials sir!!
I know that capacitor opposes sudden change in voltage. ic = C dV/dt. So, when the pin output goes from LOW to HIGH, capacitor should take large impulse current as dV/dt goes to +inf. Similarly when the pin output goes from HIGH to LOW capacitor should supply large impulse current back to Arduino pin.
Is it safe in the long run? Are digital/analog pins capable of taking current from external circuits??
The things you are saying are correct . . . I was just trying to show a quick and dirty demo of how capacitors can turn AC into DC. If you were really going to do this, you should do some circuit design.
It is safe, the Arduino pin when off is effectively floating and does not "sink" current from the cap, and the charge will sink quickly through the LED. Every digital pin is capable of handling 5V. What do you think it is doing for the digitalRead?
You sir have a gift for teaching. I have bought your kit and have been enjoying every lesson. Who ever said you can't teach and old dog new tricks has not met you. Thank you very much!!!
No doubt this is one of the best lessons explaining PWM!
I fundamentally disagree with this video. It is well understood that good Arduino videos should be accompanied by hot coffee with cream and lots of sugar. 😊
BANNED!
😂
@@paulmcwhorter lol. Merry Christmas.
😂
I learned PWM in physics but never really understood it as clearly as I did today thank you, this is another great tutorial in this series!
Brilliant explanations throughout your lessons Paul. Well paced and the content just enough in each session. Thank you
Excellent explanation and demonstration Paul. I’ve certainly heard of PWM before but now have a clear understanding of exactly what it means.
Can’t get enough of your videos it feels like I’m back in school in the best way possible!
Been watching these from Tutorial #1, currently learning the Arduino in university as we move forward with various microcontrollers, and these videos are putting me steps ahead of the lecture from class as you explain the material in a very detailed way. Appreciate it, I'm going to watch all of them.
Thank you so much for breaking out the oscilloscope. I never even knew I wanted to know this level of detail. Outstanding series so far and looking forward to the rest
Hi Paul. Yes understood PWM. Your teaching is great! I've seen so many poor teaching videos and read some poor books on the subject, that I'd given up. Your tutorials have me looking forward to what you're going to teach us next! As a 73 year old ex I. T. trainer that's praise indeed
The way you explain it, it is easy to understand. Love learning about this stuff.
I loved this lesson! Especially the fact that you used the oscillator. Made it 100 times easier to understand!!
Paul, what a good man you are. I'm a mechanical engineer student, and getting into electronics has been a long time wish. Thanks for putting out quality content for free.
I have been watching each of your tutorial, and following with practicals on my breadboard : I am finally getting somewhere with Arduino! Excellent teaching method. I can see the value of setting the variables and having no numbers in the loop.Great demonstration of PWM and use of that capacitor .
I had a basic bbn understanding of PWM, but when you hooked the cap in at the end, a HUGE lightbulb went off in my head. Thank you, thank you, thank you !!!
Yes, I now understand pulse width modulation, PWM. Great visual.
I like that you keep things neat and organized on the video, it makes it easier to understand.
Well done Paul, thoroughly understood. Wish you were my professor back in the days. Thank you! Great job!
I like the fact that you look serious but funny at the same time.
And of course, the wonderful way of explaining stuffs.
A big thumbs up!
I'm in the field of communication engineering but even then this video helped me have a different outlook on PWM.Great work on the video and your explanation is simplistic, intuitive and to the point. Thanks for the insightful video
I have to reach out and say thank you, and I'm in awe of your lessons here. Just bought my kit, and can't wait to start playing with it. I'm a teacher myself and do short video lessons and am looking for great applications to show my students of mathematics and science. You break things down really well!
Yes, I understand. As I commented earlier, I am using your course as a refresher. I learned all this almost 50 years ago.
This is great. I'm having so much fun watching these videos. I've watched them all at least three times.
I enjoyed the class, great demo of the O-scope. Made PWM very clear and showed exactly what was going on. Thanks
I am an automotive instructor, and PWM is our bread and butter nowadays. I always have difficulty teaching this. You have given me some great ideas to give to my students. I am so glad I decided to look for a channel like this. You are awesome!
Great to hear!
This is a great explanation concerning the way LEDs creat light, (as a diode) and the way the voltage "strikes" (for lack of a better term) to produce light! This is most excellent, you explain things like this, along with the visual aids, wonderfully!
Thanks Paul
I'm finally making the leap with a good understanding of electronics to software development. That was a great video of PWM. You have a very impressive course so far. great job!! 10/10
Great lesson. I finally understand pulse with modulation. Showing the O scope and what you call rambling (which helped me associate previous knowledge with this lessons knowledge) was perfect. Keep the rambling!!! And thank you!!!
I don't have someone to teach these Arduino lessons to me, and I really want to learn how to use them. Now, thanks to Paul, I can learn all of the basics and the difficult parts of programming with ease. Thankyou so much for these lessons!
Welcome Abord!
Great job explaining PWM, very easy to understand especially with the info from the oscilloscope. Thanks for the great videos, looking forward to going through the rest of them.
You made it very clear. I don't notice rambling because I play the videos at twice normal speed since I'm able to follow. Everything seemed relevant to my thinking. The series is very helpful as a refresher so far, thanks!
Paul you are a fantastic Guru. I'm a Mechanical Engineer and used to hate these topics because I never understood. Your videos are so smooth, I'm falling in love with the Electronics. If I had discovered you or Guru like you 20 years back, my life would have been different. May be i would have landed in NASA, ISRO... Thanks fall all your support. Let peace prevail.
Best explanation about pulse width modulation that I could find over internet. Thanks for teaching me
Thank you Paul! Really enjoying this series so far... Like others have commented, you do a great job of explaining what goes on 'underneath the hood' and it is easy to understand. The visual of the oscilloscope also helped me understand PWM. I'm looking forward to binge watching the rest of your tutorials! :D
Glad you enjoy it!
You are not rambling at all sir!! Everything is sooo clear that even my gf, who has no background whatsoever, is understanding better than I could have ever explained to her. I wish I had a mentor like this back when I studied electronics engineering. I'm watching your lessons just for the sake of it and loving it. Keep it up, I'll keep sharing...
Hi Paul, you made it very clear to my understanding PWM. Thank you! 🙏
Ramble On Paul, I've never learnt so much in such a short time, Loving the videos Mate!!!
I completely understand what you're saying, I'm a mechanic and have worked with electronics for 20 years or more. Really appreciate you going to the trouble of teaching, cheers.
I understood that Pulse Width Modulation works for an given Voltage in a given period of 4 milliseconds it stimulates b/w 5 volts and 0 volts and averages out the signal to get the desired o/p and also that capacitors will help us to smooth out the signals in every interval of time when voltage is being applied.
Thanks Paul, I understand it clearly you truly are a great teacher and also tech savvy
I'm glad I found your channel keep the work up!! ❤
Thanks a lot M. McWorther, your explanations over all that you teach are cristal clear, even for a french Canadian guy. Have a good day.
Paul, you explained PWM very clearly. Just makes me want to know more and keep following your lessons
Top notch teaching. I am working my way through all of the examples and homework.
I wish my high school electronics teacher (long time ago) had half your ability.
Thank you Paul, I've really learnt of you and your tutorial videos are the best academic videos I've ever watched. You describe everything in detail and I really enjoy watching your videos. I appreciate your time, patience, and effort. Thank you Thank you Thank you
I really appreciate these tutorials. Very in depth it's impossible to get lost
Just bought my kit yesterday, today is here, programmed some codes , even fading and Im gonna go through all the playlist I hope, just commenting to say thank you for such clear explanation
Your explanation at the end really gave me the full picture. Keep up the great work!
Glad to hear it!
To practice my analogWrite's I designed a breadboard with 4 LEDs and 4 resistors, I grounded them to the rail on the breadboard then provided individual power to the AWM enabled pins. Then I wrote a program where they would light up in sequence getting progressively brighter as they went. I assigned variables to my brightness, the speed, and the pins themsleves, and an off voltage of 0 for the pins that weren't to be illuminated at the specific sequence. It turned out perfectly. I showed my kids how adjusting the variables could turn the speed up and down. They are very curious about this new kit that dad is always messing with ha ha. Maybe one day they will watch your videos as well. Thank you for being such a great teacher!
Hey Paul, I'm loving these tutorials. I had 0 knowledge of electronics going into this but you explain things in a way that anyone regardless of prior knowledge can understand. What I particularly like about these tutorials is that I already understand programming so I can focus solely on the electronics portion. It also allows me to get creative with my solutions for the assignments you give at the end of the videos too, there are a lot of Eureka! moments that remind me of when I first learned to program. I'm really looking forward to working through your Introduction to Robots series once I complete this one. You rock!
This explaination was a charm! Understood the concept perfectly :)
Paul, you are one of the best teachers I've ever heard. Your knowledge of electronics is simply amazing. I used to watch my dad work with electronics and have always been intrigued. Your videos are very interesting (I didn't think you rambled on), and the way you slowly and simply explain things is exactly how I learn. Thank you for sharing your wealth of knowledge. Yes...I do understand PWM now. Oh...and yes, I need things lined up as well. :-)
Very cool!
Great video again Paul! Thanks! I actually learned a lot about pwm working with model trains and digital command control. PWM is used by a decoder to control the motor and make the locomotives go from 0 to full speed. It was great to see the graphic representation on the oscilloscope, so thank you for showing us that. I love these tutorials!
Very cool!
That was a pretty good explanation of PWM. I come from a 30+ year building automation background and really appreciate your style of teaching. MANY years ago, we had to write our code in BASIC, but I had no experience with C++ style programming until 8 lessons ago. Really looking forward to carrying on with these lessons and sharing the experiences with my 10 year old granddaughter who is really taking an interest too.
You keep talking about "Getting Under The Hood". Really under the hood has had me wondering for years. Hope to learn about the firmware that makes these controllers recognize commands and decides what to do with them... right down to the 1's and 0's.
Thanks for sharing your wealth of knowledge with us Paul!
Another great video. The demo with addition of capacitor was very clear. Must say Paul, I thought I sort of understood PWM before I watched the video. Now I KNOW I do! Thanks a lot.
You are, by far, the best professor I saw talking about the matter!
...and I am an elec. engineer.
Thank you.
In lieu of black coffee I watched this while drinking beer, and I followed just fine. Yes, you were very clear.
Thanks Paul you are always so clear in what you say. I now fully understand PWM. You are such a good teacher!
Wow such a good lesson on PWM, Just treated myself to a scope too so being able to watch it and understand what I'm watching is awesome. Thank you.
Great to hear!
Yes, very informative! And your use of capacitors makes sense too, the cap charges and discharges with each period, effectively normalizing the voltage seen by the led
You are a great teacher Paul. I watched your series on the Raspberrry PI, and have hooked my Arduino to it. Just waiting post surgery for eyes to settle to do these sessions.