Raspberry Pi Tutorial

Last revision 2026/01/23

This tutorial guides users through the process of controlling DC motors using a Raspberry Pi and a Motor Driver HAT. It includes detailed steps on hardware setup, enabling I2C interface, downloading necessary libraries, and executing Python commands for motor control and speed measurement. Users will learn how to operate motors with encoders and understand the significance of motor reduction ratios and PWM signal frequency. The tutorial also covers troubleshooting tips and additional resources for further learning. With practical examples and clear instructions, this guide is designed to empower enthusiasts and educators in exploring the capabilities of Raspberry Pi in robotics and automation.

Hardware Preparation

Raspberry pi Operations

Step1: Plug the DC Motor Driver HAT in a Raspberry Pi's main-board, connect driver and motor, power up.

Step2: Detect if the I2C interface is enabled. Input the command i2cdetec-y 1, if it is disabled, the following interface will appear:

DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi Operations

Step3: Enable I2C interface. (Skip this step if I2C is already enabled.) Input the command sudo raspi-config to enter the configuration interface. Shown as below:

DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi Operations DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi Operations DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi Operations

When finished the configuration, input sudo reboot to restart Raspberry Pi.

Step4: Detect I2C address. Input i2cdetect -y 1 to detect the device I2C address, shown as below:

DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi Operations

Step5: Download the DFRobot_RaspberryPi_Motor Library. Enter sudo git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Motor. Input ls to check commands.

DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi Operations

Example

Enter the python catalogue in DFRobot_RaspberryPi_Motor library.

command: cd ~
command: git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Motor.git
command: unzip DFRobot_RaspberryPi_Motor.zip
command: cd /DFRobot_RaspberryPi_Motor/raspberry

How to drive a DC motor with encoder?

Use the variable M1,M2, ALL for id parameter to represent motor M1, motor M2, M1 and M2.

  • Step1. Enable the Encoder:
set_encoder_enable(self, id)
   eg:
     board = Board(1,0x10) 
     set_encoder_enable([board.M1])#Enable encoder motor M1
     set_encoder_enable([board.M2])#Enable encoder motor M2
     set_encoder_enable([board.M1,board.M2])#Enable encoder Motor M1 and M2
     set_encoder_enable(board.ALL)#Enable encoder motor M1 and M2
  • Step2. Set the motor reduction ratio(43:1 in demo). Related with the reduction of the motor.
set_encoder_reduction_ratio(self, id, reduction_ratio)
```arduino
- Step3. Set the frequency of PWM signal (1000Hz in demo)
```arduino
set_moter_pwm_frequency(self, frequency)
  • Step4. Set the rotating direction and speed of motor M1, M2, M1 and M2. (Duty ratio: 0~100)
motor_movement(self, id, orientation, speed)
  • Step5. Get the speed of the encoder motor
get_encoder_speed(self, id)

NOTE: Different motors have different reduction ratio. Please revise the reduction ratio of the motor before running the codes in the demo.

Click to check more function configuration and usage description.

Run the example DC_Motor_Demo.py. Print the duty ratio and rotating speed of the encoder motor.

command: python3 DC_Motor_Demo.py

  • Program Function: Motor driver generates a signal with 1KHz frequency and a changing duty ratio within 5%~95% to make the encoder motor M1 to rotate clockwise with speed first increasing then decreasing; M2 rotate anti-clockwise with speed first increasing then decreasing, and stop. At the same time, print the rotating speed on the serial port. The whole program is repeatedly executed.
DFR0592 DC Motor Driver HAT(V1.0) for Raspberry Pi How to drive a DC motor with encoder?

How to drive a DC motor?

Connect the DC motor to the interface of M1+, M1- or M2+, M2-. Disable the function of encoder as the above demo, set frequency, duty ratio, rotating direction.

NOTE: For motors without encoder, we cannot get its speed, but only adjust the speed through PWM.

Was this article helpful?

TOP