$f(t)$ → **pulse** wave
|||
|-----|-----|
| $y_{max}$ | $0
---
## Duty Cycle | **calculations**
we can expand,
$$\bar{y} = \frac{1}{T}\int^T_0f(t)\ dt$$
---
## Duty Cycle | **calculations**
$$
\bar{y} = \frac{1}{T}\int^T_0f(t)\ dt
$$
$$
= \frac{1}{T} \left(\int_0^{DT} y_\text{max}\ dt + \int_{DT}^T y_\text{min}\ dt\right)
$$
---
## Duty Cycle | **calculations**
$$
\bar{y} = \frac{1}{T}\int^T_0f(t)\ dt
$$
$$
= \frac{1}{T} \left(\int_0^{DT} y_\text{max}\ dt + \int_{DT}^T y_\text{min}\ dt\right)
$$
$$
\bar{y} = \frac{1}{T} \left(D \cdot T \cdot y_\text{max} + T\left(1 - D\right) y_\text{min}\right)
$$
---
## Duty Cycle | **calculations**

$$\bar{y} = D\cdot y_\text{max} + \left(1 - D\right) y_\text{min}$$
---
## Duty Cycle | **calculations**

$$\bar{y} = D\cdot y_\text{max} + \left(1 - D\right) y_\text{min}$$
can now compute → how long signal at ($y_{max}$, $y_{min}$)
---
### PWM Period
determines → number of times signal **repeats per second**
depends on **application**
---
### PWM Sampling Theorem
---
### PWM Sampling Theorem
> number of pulses in the waveform is equal to the number of Nyquist samples
---
### PWM Sampling Theorem
> number of pulses in the waveform is equal to the number of Nyquist samples
Nyquist rate → **twice** the highest frequency
---
### PWM Sampling Theorem
> number of pulses in the waveform is equal to the number of Nyquist samples
Nyquist rate → **twice** the highest frequency
directly related to → [Nyquist-Shannon Sampling Theorem](https://autonomy-course.github.io/textbook/autonomy-textbook.html#adc-sampling-rate)
---
### PWM example | servo motor
---
### PWM example | servo motor control
servos/RC servos → small, cheap servomotors/actuators

---
### PWM example | servo motor control
servos/RC servos → small, cheap servomotors/actuators

used for radio control and small-scale robotics
---
### PWM example | servo motor control
- controlled by → PWM signal
---
### PWM example | servo motor control
- controlled by → PWM signal
- width/duty-cycle of pulse → position of motor
---
### PWM example | servo motor control

---
### PWM Generation in Microcontrollers
- **built-in PWM components**
- timer ICs
---
### PWM Generation in Microcontrollers | _e.g.,_ **Arduino**
generating PWM → **simple lines of code**!
```
analogWrite(pin, value)
```
Note:
- that **not all pins** of an Arduino can generate a PWM signal. In the case of Arduino Uno, there are only 6 I/O pins (3,5,6,9,10,11) that support PWM generation and they are marked with a tilde (~) in front of their pin number on the board.
---
### PWM Generation in Microcontrollers | _e.g.,_ **Arduino**

---
### PWM Generation in Microcontrollers | _e.g.,_ **Arduino**
various duty cycle examples
```
analogWrite(PWM_PIN, 64); // 25% Duty Cycle or 25% of max speed
analogWrite(PWM_PIN, 127); // 50% Duty Cycle or 50% of max speed
analogWrite(PWM_PIN, 191); // 75% Duty Cycle or 75% of max speed
analogWrite(PWM_PIN, 255); // 100% Duty Cycle or full speed
```
---
### PWM Generation in Microcontrollers | _e.g.,_ Raspberry Pi
---
### PWM Generation in Microcontrollers | _e.g.,_ Raspberry Pi
multiple **GPIO pins** → for generating PWM signals

---
### PWM Generation in Microcontrollers | _e.g.,_ Raspberry Pi
consider this [code](https://circuitdigest.com/microcontroller-projects/raspberry-pi-pwm-tutorial) → controlling LED brightness
```python[0|1|4-5|6|9-11|12-14]
import RPi.GPIO as IO #calling header file which helps us use GPIO’s of PI
import time #calling time to provide delays in program
IO.setwarnings(False) #do not show any warnings
IO.setmode (IO.BCM) #we are programming the GPIO by BCM pin numbers. (PIN35 as ‘GPIO19’)
IO.setup(19,IO.OUT) # initialize GPIO19 as an output.
p = IO.PWM(19,100) #GPIO19 as PWM output, with 100Hz frequency
p.start(0) #generate PWM signal with 0% duty cycle
while 1: #execute loop forever
for x in range (50): #execute loop for 50 times, x being incremented from 0 to 49.
p.ChangeDutyCycle(x) #change duty cycle for varying the brightness of LED.
time.sleep(0.1) #sleep for 100m second
for x in range (50): #execute loop for 50 times, x being incremented from 0 to 49.
p.ChangeDutyCycle(50-x) #change duty cycle for changing the brightness of LED.
time.sleep(0.1) #sleep for 100m second
```