firmware_hardware
electrical_system
NTURT
I can't find appropriate meme for this lecture :(
PWM (Pulse-width modulation) is a technique used to replicate an analog signal using a digital signal. The principle behind PWM is simple: the microcontroller rapidly switches the voltage between on and off states. For instance, if the signal is on for half the time and off for the other half, it effectively emulates an analog signal with half the voltage of the digital signal.
This technique proves to be highly valuable in analog control applications, such as adjusting the brightness of an LED or regulating the power of a motor. By varying the duty cycle (the ratio of time the signal is on to the total time), PWM enables precise control of analog-like outputs using digital means. Its versatility and efficiency make it a fundamental tool in many electronic systems and applications.
As depicted in the graph above, a 5-volt signal with a 15% duty cycle can effectively emulate an analog signal of volts. The beauty of PWM lies in its ability to emulate analog signals of varying voltages by adjusting the duty cycle. In theory, a variable duty cycle can replicate any desired analog signal voltage, provided that the time precision is high enough.
To achieve the required time precision, PWM signals are often associated with timers, allowing for the generation of consistent and accurate pulse signals. By carefully controlling the duty cycle and time period, PWM enables precise analog-like control of various components and devices in electronic systems. Its versatility and reliability have made it a popular and indispensable technique in modern electronics.
To get started, launch STM32CubeIDE and create a new project. It's crucial to select a microcontroller with a PWM output capable timer. For this tutorial, we'll use the NUCLEO-H723ZG as an example.
Enable PWM generation at channel 2 of TIM3, the output pin is PC7, which is the D21 pin on the H723ZG, check the board user manual for clarefication.
Set the prescaler to 16-1 and counter period to 5000, prescaler determins how frequently the timer will receive a pulse, the default is the frequence of APB2. Prescaler value 16-1 means the signal frequency is devided by 16 times, higher frequency means higher precision. The counter peroid determins at what value will the timer counter reset, and pulse means the pin will output HIGH signal if the counter is above that value. Combine these two parameter we can output analog voltage with high perssion (given voltage is within the digital signal's boundary). For example, Counter Period=5000 and Pulse=1000 will output .
After the TIM3 has been initiallized, start the PWM generation with the function HAL_TIM_PWM_Start()
.
In the main loop of your program, you can implement a simple logic to increment the pulse by 30 with __HAL_TIM_SET_COMPARE();
during each cycle. This adjustment will result in a "breathing light" effect, where the LED's brightness gradually increases in a smooth manner.
The breathing light effect can be achieved using PWM. By continuously adjusting the duty cycle of the PWM signal with each cycle, the LED's brightness will appear to breathe, creating a visually appealing effect.
Video link
In conclusion, PWM (Pulse-width modulation) is a highly valuable and versatile technique used to output analog-like signals with a microcontroller. By rapidly switching the voltage between on and off states, PWM effectively emulates analog signals with varying voltages. This capability proves essential in controlling various components and devices, such as LEDs or motors, with precise and accurate control. PWM's ability to replicate analog signals using digital means makes it a fundamental tool in modern electronics, enabling a wide range of applications and enhancing the efficiency and performance of electronic systems.