Pulse width modulation

Pulse width modulation (analog value output)

The eight lower digital outputs (power outputs) can be pulse width modulated. Pulse width modulation allows to control the speed of motors or the brightness of lamps.

While pulse width modulation is active, the output voltage is not continuous. The output voltage is turned off and on automatically by the interface. There are two parameters. The frequency and the duty cycle.

Pulsbreitenmodulation

Pulsbreitenmodulation

The frequency (f) specifies how fast the output is turned on and off. The frequency shall be set so high that no flickering on lights or jerking on motors can be observed.

The duty cycle specifies how long the output is turned on in one cycle(f). A duty cycle of 80% means that the output is turned on for 80% oft the time and turned off for the rest of the time.

By default the pulse width modulation is turned off. Service USB plus and Service USB classic are supporting two different modes of pulse width modulation. They are used depending on the purpose of the operation.

 

Pulse width modulation different for each output.

It is possible to have a different duty cycle for each output. This is mainly used, if several lamps with different brightness or several motors with different speed are operated.
request = 0
wIndex = 9
wValue = frequency

While turning on pulse width modulation, one frequency is defined for all outputs. The frequency is not changed while in operation. The brightness and speed is regulated with duty cycle. Because of technical reasons, the amount of different duty cycles depends on the frequency.

 

frequency different duty cycles possible wValue
500 Hz 4 3
250 Hz 8 7
125 Hz 16 15
62,5 Hz 32 31
31,5 Hz 64 63
16,5 Hz 128 127
puls width off (normal state) 0 0

Values for wValue for setting the frequency

For most applications a frequency of 125Hz is a good choice. The frequency is high enough to minimise flickering and jerking and 16 different possible duty cycles should be sufficient.

send SERVICE vendor request 0 wValue 15 wIndex 9 device id 0

After turning on the pulse width modulation, each output can be set, using the set SERVICE output value instruction. This instruction accepts values between 0 and 65535. In contrast to normal state, the upper Byte corresponds to the number of the output between 0 and 7. The lower byte corresponds to the duty cycle between 0 and the maximum possible range.

For turning on the output 7 to 3/16 duty cycle (approx. 20%), the upper byte must be set to 7 and the lower byte to 3 ( hexadecimal notation : 0703).

Hexadezimal 0703 is 1795 in decimal notation. The instruction to set the output 7 to 3/16 duty cycle is set SERVICE output value 1795

In programming environments which did not accept numbers in hexadecimal notation, you can use the following formula:
decimal value = (output number * 256) + duty cycle

After turning on the pulse width modulation, the behavior of all instructions for setting the digital outputs is affected. Each bit does not correspond any longer to a specific output line. Because all instructions for setting the outputs are internally interrelated. The bitwise instructions for setting outputs, like set SERVICE output value bit or set SERVICE output value by mask do not work any longer as expected.

All outputs can be regulated individually and every time you set the output, the output number and the duty cycle must be set at the same time.

This method has two disadvantages:
The output values can only be set by the set SERVICE output value instruction.
The output values can not be turned off at the same time

There is a second operation mode for pulse width modulation to neglect the above disadvantages.

 

Pulse width modulation common for each output

To allow all instructions for setting the digital outputs to work like in normal state so that each bit corresponds to one output line, the pulse width is defined just once for all outputs. This mode is mainly used for motors that are running too fast. In this mode a 9V motor can be used on the 12V outputs or the robotics motion can be slowed down.

request = 0
wIndex = 10
wValue = duty cycle

While turning on pulse width modulation, one duty cycle is defined for all outputs. The frequency is fixed at 125Hz and can not be changed. The duty cycle is set in the range between 0 and 15. A 0 turns the output off and a 15 turns the output on with 15/16 duty cycle = 93%.

send SERVICE vendor request 0 wValue 8 wIndex 10 device id 0

Hint: A test program for all different kinds of pulse width modulation is in the folder „XCode"