Mobile Development 6 min read

Using PWM Interface on Android Things: A Step‑by‑Step Guide with Raspberry Pi

This article explains the principles of Pulse Width Modulation (PWM), outlines the steps to open, configure, enable, and close a PWM connection on Android Things, and demonstrates a complete Raspberry Pi hardware example with Java code to control a servo motor.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Using PWM Interface on Android Things: A Step‑by‑Step Guide with Raspberry Pi

1. Introduction to PWM

Pulse Width Modulation (PWM) is a common method for outputting proportional control signals to external devices via a digital output pin. Servo motors use the pulse width of a PWM signal to determine rotation angle, while LCD displays adjust brightness based on the average PWM value.

PWM is a digital square‑wave signal defined by frequency and duty‑cycle. Frequency (Hz) indicates how many pulses repeat per second, the period is the inverse of frequency, and duty‑cycle (percentage) describes the proportion of the period that the signal is high.

For example, a 50 % duty‑cycle PWM signal is active for half of each cycle.

2. Usage Steps

2.1 Open Connection

Create a PeripheralManagerService instance and call openPwm() with the desired PWM name to obtain a Pwm object.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        mPwm = mPeripheralManager.openPwm(PWM_NAME);
    } catch (IOException e) {
        Log.w(TAG, "Unable to access PWM", e);
    }
}

2.2 Configure Signal

Before starting the signal, set the period with setPwmFrequencyHz() and the duty‑cycle with setPwmDutyCycle() .

2.3 Enable Signal

Start the PWM output by calling setEnabled(true) . To stop it, call setEnabled(false) .

public void initializePwm(Pwm pwm) throws IOException {
    pwm.setPwmFrequencyHz(120);
    pwm.setPwmDutyCycle(25);
    pwm.setEnabled(true);
}

2.4 Close Connection

When communication is finished, release resources with close() .

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPwm != null) {
        try {
            mPwm.close();
            mPwm = null;
        } catch (IOException e) {
            Log.w(TAG, "Unable to close PWM", e);
        }
    }
}

3. Demo Case

The following example controls a servo motor (referred to as a "camel" in the original text) by adjusting the PWM duty‑cycle, demonstrating how different duty‑cycles change the rotation angle.

3.1 Hardware Preparation

Raspberry Pi 3 development board

Servo motor

3.2 Circuit Diagram

(Circuit image omitted for brevity)

3.3 Code Implementation

File: pwmDemo/app/src/main/java/com/chengxiang/pwmdemo/MainActivity.java

public class MainActivity extends AppCompatActivity {
    // PWM output name
    private static final String PWM_NAME = "PWM0";
    private Pwm mPwm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PeripheralManagerService service = new PeripheralManagerService();
        try {
            // Open PWM and set duty‑cycle to control rotation angle
            mPwm = service.openPwm(PWM_NAME);
            mPwm.setPwmFrequencyHz(50);
            mPwm.setPwmDutyCycle(4);
            mPwm.setEnabled(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPwm != null) {
            try {
                // Close connection
                mPwm.close();
                mPwm = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.4 Result

By changing the parameter of setPwmDutyCycle() to 4 (Figure 1) and 8 (Figure 2), the servo motor rotates to different angles, as shown in the accompanying images.

(Result images omitted for brevity)

JavaIoTRaspberry PiAndroid ThingsPWMservo control
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.