Example Code for Raspberry Pi-PWM Control

Last revision 2026/01/06

Control a servo via the expansion board's PWM port.

Hardware Preparation

  • 1x DFR0604 IO Expansion HAT for Raspberry Pi, Purchase Link
  • 1x Servo
  • 1x Raspberry Pi

Software Preparation

  1. To use the analog and PWM ports in the board, download the driver library first. Press Ctrl+Alt+T to open the terminal, input the following command and hit Enter.
    git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Expansion_Board.git  
    
  2. Download the library and unzip the file. Input the following command into the terminal:
    tar -xvzf DFRobot_RaspberryPi_Expansion_Board  
    
  3. Enter the directory of the uncompressed files:
    cd DFRobot_RaspberryPi_Expansion_Board/raspberry/  
    
  4. Use nano editor to create and edit the code file.

Wiring Diagram

  • Plug the IO expansion board into Raspberry Pi.
  • Connect a servo to PWM P0.

Steps

  1. Create a file via the command of nano:
    sudo nano servoTest.py 
    
  2. Add the Sample code content.
  3. Press Ctrl+X, and then press Y to save and exit nano editor.
  4. Input command sudo raspi-config to open the IIC interface.
  5. Input command python servoTest.py to run file servoTest.py.

Sample Code

servoTest.py

# -*- coding:utf-8 -*-
# servoTest.py

import time

from DFRobot_RaspberryPi_Expansion_Board import DFRobot_Expansion_Board_IIC as Board
from DFRobot_RaspberryPi_Expansion_Board import DFRobot_Expansion_Board_Servo as Servo

board = Board(1, 0x10)  # Select i2c bus 1, set address to 0x10
servo = Servo(board)

if __name__ == "__main__":
  # Board begin and check the board's status
  while board.begin() != board.STA_OK:
    print("Error")
    time.sleep(1)
  print("Board begin success.")

  servo.begin()         # servo control begin

  while True:
    print("part of servos move to 0°")
    servo.move(0, 0) # PWM0
    '''
    servo.move(1, 0) # PWM1
    servo.move(2, 0) # PWM2
    servo.move(3, 0) # PWM3
    '''
    
    print("part of servos move to 180°")
    servo.move(0, 0) # PWM0
    '''
    servo.move(1, 0) # PWM1
    servo.move(2, 0) # PWM2
    servo.move(3, 0) # PWM3
    '''

    print("All servos move between 0°~180°")
    for i in range(0, 181):
      servo.move(board.ALL, i)
      time.sleep(0.008)
    time.sleep(0.002)
    for i in range(0, 181):
      i = 181 - i
      servo.move(board.ALL, i)
      time.sleep(0.008)
    time.sleep(0.002)        

In the function servo.move() of the example, the first parameter is the PWM pin of the expansion board, the second one is rotation degree. The PWM selection list is shown below:

PWM Pin of IO expansion board Value of the first parameter
0 0
1 1
2 2
3 3
All Pins board.ALL

Result

The servo connected to PWM P0 will move to 0°, 180°, and then sweep between 0° and 180° repeatedly. The terminal will output the following messages:

  • "Board begin success."
  • "part of servos move to 0°"
  • "part of servos move to 180°"
  • "All servos move between 0°~180°"

Additional Information

The PWM signal of this expansion board is generated by the on-board STM32. PWM0~PWM3 are output via multiple channels of one timer with same frequency. The duty ratio can be set by users. PWM4 adopts another timers to output PWM. The PWM can be powered by Raspberry Pi(5V) or external power(6-12V).

DFR0604 IO Expansion HAT for Raspberry Pi Zero/Zero W PWM
  • When the VP port is not power by external power, the voltage of PWM ⊕ is 5V.
  • When the VP port is powered by external power, the voltage of PWM ⊕ is equal to that of the VP external power (6-12V).

Was this article helpful?

TOP