A PWM (Pulse-Width Modulator) is a component used for controlling power to inertial electrical devices. It generates a periodic waveform with positive width which can be controlled and thus, the waveform’s average value modified.

The average value of voltage fed to the load is controlled by turning the switch between the supply and the load on and off at a fast pace. The longer the switch is on compared to the off, the higher the power supplied to the load will be.

The ConnectCore 6 has several PWM (Pulse Width Modulation) interfaces to manage devices such as servos and voltage regulators. In the ConnectCore 6 Hardware Reference Manual you can find information about the available PWM channels.

Digi adds to Android an API to manage these PWM channels. You can configure the PWM duty cycle, the frequency, and the polarity among other things. In the Digi APIx javadoc you can find a complete list of the available methods in this API.

Unless noted, all PWM API methods require the com.digi.android.permission.PWM permission.

If your application does not have the com.digi.android.permission.PWM permission it will not have access to any PWM service feature.

First of all, a new PWMManager object must be instantiated by passing the Android Application Context.

Instantiating the PWMManager
import android.app.Activity;
import android.os.Bundle;

import com.digi.android.pwm.PWMManager;

public class PWMSampleActivity extends Activity {

    PWMManager pwmManager;

    [...]

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Instantiate the PWM manager object.
        pwmManager= new PWMManager(this);

        [...]
    }

    [...]
}

Instantiate a PWM channel

The PWMManager allows you to create a PWM object representing a physical module’s PWM channel. If you don’t know how many channels are available in the device you can list them.

Method Description

createPWM(int)

Creates and returns a PWM object with the given interface number

listChannels()

Lists all available PWM channels in the device

The createPWM(int) method may fail if the provided channel is not available, throwing an PWMException.

Getting PWM channels
import com.digi.android.pwm.PWM;
import com.digi.android.pwm.PWMManager;

[...]

PWMManager pwmManager = ...;

// Create a PWM object for each available PWM channel.
int[] channels = pwmManager.listChannels();
PWM[] pwms = new PWM[channels.length];

for (int i = 0; i < channels.length; i++)
    pwms[i] = pwmManager.createPWM(channels[i]);

[...]

Manage PWM duty cycle

You can get and set a PWM duty cycle using the following methods:

Method Description

getDutyCycle()

Returns the duty cycle percentage of the PWM signal (0 to 100%)

setDutyCycle(double)

Changes the duty cycle percentage of the PWM signal

These methods may fail if there is any error while reading or configuring the PWM duty cycle, throwing a PWMException.

Getting and setting the PWM duty cycle
import com.digi.android.pwm.PWM;
import com.digi.android.pwm.PWMManager;

[...]

PWMManager pwmManager = ...;
PWM pwm = ...;

[...]

System.out.println("PWM channel: " + pwm.getChannelIndex());
System.out.println("Duty cycle: " + pwm.getDutyCycle());

pwm.setDutyCycle(42);
System.out.println("New duty cycle: " + pwm.getDutyCycle());

[...]

PWM example

Example: PWM

The PWM Sample Application demonstrates the usage of the PWM API. In this example you can list all the available PWM channels and set the duty cycle of that signal.

You can import the example using Digi’s Android Studio plugin. For more information, see Import a Digi sample application. To look at the application source code, go to the GitHub repository.