Start with PWM
Let’s lay down some theory before we start the testing. A cycle is an interval of time during which a complete excursion of a value sequence is completed. In the drawing, the analog sine wave starts at 0V, rises to +5V, then falls to -5V and returns to zero. A cycle can also exist if a single polarity power supply is used.
In the waveform column A, row 1, referred to as A1, the value of the digital signal in a time interval starts at 0V, rises to 5V for a period, then returns to 0V to complete the cycle. This complete excursion is one cycle of the digital signal. In waveform A2, in the same time period, two excursions of the digital signal have occurred. This would be two cycles. Similarly, waveform A3 shows four cycles of the digital signal occurring.
The time it takes for the digital signal value to complete one cycle is called the period of the digital signal. The number of cycles completed in one second is called frequency. Until 1965, the frequency was indicated in cycles per second.
Today, the more common technical term for the unit of measurement for frequency is Hertz, with the symbol Hz. You may have encountered Hertz in describing mains electrical power, or more likely the speed of a computer’s CPU (when measured in GHz).
The term ‘duty cycle’ refers to a comparison of the time interval at which the digital signal is high (5V) versus low (0V). If the digital pulse is high for 50 percent of the time interval, as in waveform B1, the digital signal has a duty cycle of 50 percent.
In waveform B2, the digital signal is high 25 percent of the time: a 25 percent duty cycle. Waveform B3 establishes a digital signal that is high for three-quarters of the time interval, so has a duty cycle of 75 percent.
By varying the duty cycle of the digital signal, the pulse width changes. Changing the width of the pulse is referred to as a Pulse Width Modulated digital signal.
Now let's take the PWM signal and apply it to an analog device like a DC motor. In waveform C1 the duty cycle of the PWM signal is 50%. The average voltage (indicated in red) would be half of the supply. Note in the drawing the voltage is +12V since it is the supply on the locomotive side of the L298N module. Waveform C2 is a 25% duty cycle so the average voltage would be lower still. In Waveform C3 the duty cycle is 75% therefore the average voltage supply to the motor would be higher.
Pulse width modulation is a technique that allows us to adjust the average value of the voltage that’s going to the electronic device by turning on and off the power at a fast rate.
Finish with L298N
The L298N is a motor driver module that provides both speed and direction control of a DC motor. A motor driver module takes the low voltage input from a controller like Arduino and produces a voltage sufficient to drive the motor. The module can also control the direction of DC motors connected to the driver. The direction of DC motors is controlled by the logic signal sent to the motor driver module from the Arduino. This is all taken care of in the L298N driver library for the Arduino.
The L298N module contains an H-Bridge circuit. The integrated circuit contains four switching elements that can be transistors or MOSFETS.
The logic signal applied to the circuit from the Arduino determines what switching elements are open and closed. By activating two particular switches at the same time we can change the direction of the current flow, thus changing the rotation direction of the motor.
The voltage supplied to the H-Bridge is the train voltage PWM signal. Recall the average of a PWM signal varies with the width of the pulse. If we combine the PWM and the H-Bridge, we can have speed control (PWM) and direction control (H-Bridge) over the DC motor.