- Hands-On Robotics Programming with C++
- Dinesh Tavasalkar
- 360字
- 2021-06-24 15:30:33
How PWM works
In the previous Blink.cpp program, we applied a digital signal from the RPi to the LED. Digital signals either have a HIGH state or a LOW state. In, HIGH, state the Raspberry Pi pins produces a voltage of 3.3V and in a LOW state, the pins produce a voltage of 0V. Consequently, at 3.3V, the LED is on with full brightness and at 0V, the LED is turned off:

To reduce the brightness of the LED, we need to reduce the voltage. To reduce the voltage, we use PWM. In PWM, a single wave with one full repetition is called a cycle and the time taken for a cycle to complete itself is called a period. In the following diagram, the red lines represent one complete cycle. The time taken to complete that cycle is called a period:

The time duration for which a signal remains HIGH is called a duty cycle, as shown in the following diagram:

The duty cycle is represented in percentage format, and the formula for calculating the duty cycle is as follows:
Duty cycle = (time duration for HIGH signal / total time) X 100
In the preceding diagram, the signal remains HIGH for 7 milliseconds, and the total time period for a single cycle is 10 milliseconds:

Duty cycle = 70% or 0.7
Consequently, the duty cycle is 0.7 or 70%. Next, to find the new voltage value, we need to multiply the duty cycle with the maximum voltage value, which is 3.3V:
Vout = Duty cycle X Vmax
Vout = 0.7 X 3.3
Vout = 2.31V
At a duty cycle of 70%, the voltage that's provided to the LED will be 2.31V and the brightness of the LED will reduce slightly.
Now, if we reduce the duty cycle to 40%, then the voltage that's provided to the LED will be 1.32V, as shown in the following diagram:

Now that we have understand how PWM is used to reduce the voltage at RPi data pins, let's take a look at the softPWM library, using which data pins can be used as PWM pins.
- Modular Programming with Python
- GeoServer Cookbook
- 深入實(shí)踐Spring Boot
- PHP+MySQL網(wǎng)站開發(fā)技術(shù)項(xiàng)目式教程(第2版)
- Mastering Predictive Analytics with Python
- Java 9模塊化開發(fā):核心原則與實(shí)踐
- Learning Concurrent Programming in Scala
- Mastering Linux Security and Hardening
- HoloLens與混合現(xiàn)實(shí)開發(fā)
- 精通MySQL 8(視頻教學(xué)版)
- R Data Science Essentials
- 物聯(lián)網(wǎng)系統(tǒng)架構(gòu)設(shè)計(jì)與邊緣計(jì)算(原書第2版)
- Python預(yù)測分析實(shí)戰(zhàn)
- 一步一步學(xué)Spring Boot:微服務(wù)項(xiàng)目實(shí)戰(zhàn)(第2版)
- Mastering VMware vSphere Storage