Raspberry Pi LESSON 9: Using GPIO Pins for PWM to Simulate Analog Output (Pulse Width Modulation)

preview_player
Показать описание
Guys the pinout I show in this video has an error on Tx/Rx. You can download your own correct Pinout from my WEB site HERE:
Announcing the Most Awesome Raspberry Pi Lessons of All Times! This time we RUMBLE!

In this class series, we will be using the most excellent Sunfounder Ultimate Raspberry Pi kit, available here: (Affiliate Link)

In this lesson I will show how to simulate analog output voltages by using Pulse Width Modulation, or PWM. We show how brightness of the LED can be controlled through proper use of PWM signals to the GPIO pins.

If you guys are interested in the oscilloscope I am using, you can pick one up here (affiliate link):

You guys get your hardware ordered so you can follow along at home!

You will also need a Raspberry Pi. I suggest the Raspberry Pi 4. If you do not already have one, this is the most suitable gear I could find:

The Raspberry Pi's are sort of pricy right now, so you can look on ebay or elsewhere to see if there are any deals. You will need a SD card. If you do not already have one, this is a good one:

I like using a wireless keyboard and mouse to have fewer wires. You can certainly use your USB keyboard and mouse, but if you want a nice wireless one, this one works on the pi. We demonstrate this by using a button switch to control a LED.

You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

#GPIO
#PWM
#RASPBERRYPI
Рекомендации по теме
Комментарии
Автор

The way you explained the PWM concept is probably the BEST. Nobody else explained it that way.

Bob-tujq
Автор

The old days 60 hertz was great information. 😎 Thank you Paul.

qzorn
Автор

I AM LEGEND! It is great to have near-analog output from the Pi. Lookout nuclear reactors, we McWhorter aficionados are coming for you! Thanks for more of your most excellent lessons Paul.

cbrombaugh
Автор

thanks to Jesus Christ for allowing this learning platform through paul.
man your videos had helped me out of, a lot of holes

timothydemyers
Автор

Love watching this series
Even tho I'm not doing it
One day I shall learn from this and will be glad to achive such
Anyway. I followed all you arduino and have learnt so much
And will try this next step of tuts. As such good details as always
You do an amazing tutorials

spudnickuk
Автор

Hey Paul, great lesson! The oscilloscope was a great visual aid to actually see what the pin was outputting. I was thinking about getting one but I’m saving my coin for a 3d printer. Can’t wait for those lessons to start.

warrenscorner
Автор

I really like these leasons. One thing I would love knowing by the end of this is how to send data over network. I want to monitor my vacation homes basement via the raspberry pi and it would be really handy to see the numbers at home.

Another this I think you should mension is how to do VNC. Sometimes ssh isnt the best option. I am currently using VNC and it is great.

whoknows
Автор

Another fun project. as I was listening to your video here. I created a simple program that blinks with a longer delay between each blink. I as well, added PWM to count from 0 to 50 and back down again in a loop. fun and games. :)

OZtwo
Автор

Let's build a Function Generator.OK, OK it would take a bit more but I can dream can't I. I always love the PWM lessons. Thanks, Paul.

opalprestonshirley
Автор

Thank you so much Paul Happy New YEAR! my best wished to you and your family!

minhtruong
Автор

I am legend. I did include 0% and 100% in the code. I don't have an oscilloscope, but I checked it with a multimeter and the voltages were as expected. I'm not set up to make a video, but I can add the code as a comment if you wish.

WilliamBurlingame
Автор

This is a very nice tutorial about the difference in Frequency and Duty Cycle so thank you.

If the LED is a Motor, it is hard to start or stop a motor at 100Hz and 50% duty cycle, is there a way to ramp up the duty cycle from 0 to 100? I have been struggling to find how to do it.

Thank you again.

doccinator
Автор

Great stuff, always useful and very well explained! as usual thank you for your time and effort in sharing your knowledge! looking forward to more!

jalopyjones
Автор

Good lecture also you showed how to use Oscilloscope in the circuit. Also I have ordered the Oscilloscope.

anwar
Автор

Hi Paul, thanks for the lesson. The LED in my circuit does not dim when I reduce duty cycle, and it does not blink when I reduce frequency. There is absolutely no change from the state of 'ON', or 'OFF' based on the gpio output code. I don't have an oscilloscope so can't test what's going on. Any suggestions on what may be happening here, other than getting an oscilloscope :) ?. The code and circuit are the same as your video, triple checked. Thanks again!

PrabudhGoel-xi
Автор

In addition to this lesson: If you are using the "gpiozero" package, you have to write it a bit different:

from gpiozero import PWMLED
# imports the package needed to use the PWM functions

from time import sleep

# create an LED object of your choice
# the pins are called with their BCM names
# the frequency sets the LED "on" and "off" in 1sec. -> freq=100 -> 100Hz -> 100 times per Sec.

pwmLED = PWMLED(pin=26, frequency=100)

# to adjust the brightness you have to use "*.value = "float number between 0.0 and 1.0"
# "*.value" adjusts the Duty Cycle when used:

pwmLED.value = 0.5

# this line sets the Brightness of the LED to 50%

sleep(2)

# LED is "ON" for 2 sec.

dahakar
Автор

Thank you for the video,
I am testing this on my side and I notice something unexpected, when I set the frequency to 100hz I only get 98.Hz, however this difference increase with higher frequencies like 1KHz I get only 860Hz, do you know what is the reason for this ?

lalmiahmed
Автор

sir what is the difference between pwm.start and pwm.ChangeDutyCycle? Both are used to change the duty cycle right?

md.monirmostafiz
Автор

How we can control the current in multimeter means increasing the current in multimeter through PWM

shwetamali
Автор

Your camera was perceiving the blink at 100Hz for a little while. @29:35. The frame rate and frequency were in sync or something.

RyBred