In this tutorial I cover the following topics
- Introduction
- Microcontroller's pins
- Experiment 1
- Experiment 2
- Experiment 3
- Conclusions
- PWM -- Hardware switching
- Application 1: LED
- Application 2: servo
I explain different type of microcontroller's pins in Different voltage level coexistence part. Please refere to section Type of pins if this topic is new for you or you don't remember this material.
Turn on and turn off LED with low rate (1000ms for each state). Voltage changes from low level to high level and back.
Turned off (low voltage level, close to 0V):
Turned on (heigh volatge level, close to 5V):
High speed switching (1ms for each state).
Now voltage is very stable, around 2.2V:
This is in accordance with expectation as 1/(1+1) * 5V = 2.5V. Because as we have seen in Experiment 1 high voltage is not exactly 5V but 4.5V so 1/2*4.5V=2.25V.
Circuit with two "reference" LEDs: one (most left) always turend off, and second (most right) always on:
High speed switching but with different length of low and high state.
High 1ms, low 2ms. Expected voltage is 1/(1+2)*5V = 1.66V. Taking into account that volatge is rather 4.5V than 5V, expected result should be: 1/3 * 4/5V = 1.5V:
2/3 * 4.5V =3.0V:
1/10 *4.5V = 0.45V = 450mV:
The proportion between HIGH state and LOW state are important as this decide about (averaged) output voltage. All below timings have the same proportion:
1 2 3 4 5 6 7 8 9 10 11 |
HIGH = 1000 LOW = 1000 proportion = 1/1 HIGH = 100 LOW = 100 proportion = 1/1 HIGH = 1 LOW = 1 proportion = 1/1 |
but, as you have seen, output volatage was the most stable in the last case, where total time of one HIGH-LOW cycle was the shortest.
In consequence, you can say that both proportion and time window (total time of one cycle) is important.
Pulse Width Modulation
100% duty cycle (always on):
50% duty cycle (on half the time):
10% duty cycle
Unfortunately Tinkercad seems to have some problems in this simulation (LED state in case 2 and 3 is incorrect).
0% duty cycle (LED is off)
50% duty cycle (half LED brightness)
100% duty cycle (full LED brightness)
Similar approach as for LED can be applied to control servos.
0% duty cycle
50% duty cycle
100% duty cycle