Dimmable LED
- this is the fifth project in my arduino tutorial.
- for this project we will control how bright or dim an LED is by using a PWM signal.
Introduction
- so far we have only dealt with basic digital signals, that is, the signal can only ever be either HIGH or LOW (1 or 0)
- to control how birght or dim an LED is, we will require an analog source, or something similar to analog.
- in our case, we are not going to use analog signals, but something called a PWM signal.
- Pulse Width Modulation(PWM) is a way to mimic analog signals but using purely digital sources.
- a PWM is created by outputting a square ware but repeatedly switching the signals from on (HIGH) to off (LOW).
- the signal is turned on for a short duration, then turned off for a short duration, then turned on for a short duration and so forth.
- the ratio of how long the signal is left HIGH vs how long it is left LOW is called Duty Cycle
- duty cycle is a very important concept when it comes to PWM signals.
- for more information on PWM and Duty Cycle, please see wikipedia here.
- a PWM is created by outputting a square ware but repeatedly switching the signals from on (HIGH) to off (LOW).
Materials
- materials needed for this projects are:
- Arduino
- BreadBoard
- wires
- (one) LED
- (one) 220 ohm resistor
Circuit
the circuit for this project is fairly simple. here is the circuit diagram
Here is the circuit i built for this project
Things to note:
- we are using pin 6 for our PWM signal.
- only certain pins on the arduino are set up to output PWM signals.you can check the table here to see a list of avaliable pins on each arduino board.
- you can also look at the pin numbers on the arduino. pins with PWM capability are ususally marked with a ~
Code
here is a screnshot of the code for this project. as usual, the complete code can be found here.
things to note:
- interestingly,to output a PWM you do not need to use pinMode to set the pin as an output.
- analogWrite()
- this writes a PWM signal to the given pin
- duty cycle is controlled by giving a number between 0 (0% duty cycle) and 255 (100% duty cycle).
- please see here For more on analogWrite()
Results, Conclusions,and Final Remarks
- if all went well, you should have an LED which slowly gets brighter then turns off before slowly getting brighter again.
- PWM signals can also be used to control the speed of DC motors.
More to do
to continue on with this project, here are some more things to do:
- try using diffrent values for duty cycle and see the results.
- try changing the duty cycle and the delay times to get a smoother brightening of the LED
- hint: it may help to add more calls to analogWrite() and delay()
- try making it so that instead of switching off after 100% duty cycle, the LED slowly dims back to being off.
Thank you for checking out my arduino tutorial. if you have any question, suggestions, or comments, please leave a comment below. And check back again for the next part of this tutorial.
No comments:
Post a Comment