The third blog installment for this challenge presented the theory for pulse width modulation. The post talked about the concept of duty cycle and provided static waveform drawings to reinforce the concept. This post further enforces the PWM theory by providing a bench demonstration of the operation using some basic Arduino code and an oscilloscope.
The L298N driver library for the Arduino supports a method of setting the speed of a motor.
The speed range is from 0 to 255.
// Change speed
motor.setSpeed(45);
Three different speed values were tested 45, 127 & 254. An oscilloscope sample was taken at each value.
The oscilloscope was attached to the Arduino output that produces the PWM signal to the L298N module.
As the value in the code changed so did the width of the pulse change as seen on the oscilloscope. As the value in the code changed so did the duty cycle of the waveform change. As the value in the code changed so does the motor speed change.
{gallery}Oscilloscope PWM Outputs |
---|
setSpeed(45): 17% duty cycle |
setSpeed(127): 49% duty cycle |
setSpeed(254): 99% duty cycle |
The value of 45 in the code was the first entry that resulted in any movement for the motor. The value of 127 is half of the maximum value of 255. The value of 254 is the maximum that could be set and the oscilloscope still displays the waveform. This testing was done with a 12VDC motor. The code value to establish the required train speed will need to be determined on the track.
Well, that completes the bench work. I'm reasonably confident I have everything that I need to now start testing with a locomotive.