rock 4se Tutorial

Last revision 2026/01/23

This tutorial guides users through setting up and controlling DC motors using the Rock 4SE board and a motor driver HAT, with detailed instructions on hardware and software preparation, enabling i2c, and utilizing Python for motor operations.

Hardware Preparation

NOTE: the operating voltage should be within 7~12V, and the motor's current should not be more than 2A.

Software Preparation

rock 4se Operations

install libmraa

step1: Plug the DC Motor Driver HAT in a rock 4se main-board, connect driver and motor, power up.

step2: enable rock 4se i2c

Firstly, modify the /boot/hw_intfc.conf file as following and then reboot to enable i2c7.

# Hardware Interface Config 
   # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays. 
   # Set "on" to enable the optional hardware interfaces while set "off" to disable
   intfc:pwm0=off
   intfc:pwm1=off
   intfc:uart2=off
   intfc:uart4=off
   intfc:spi1=off
   intfc:spi2=off
   intfc:i2c2=off
   intfc:i2c6=off
   intfc:i2c7=on

step3: install smbus

command: pip install smbus

Note: It is advisable to utilize Python 3 when executing Python files on Rock Pi. For the operating system, Ubuntu is recommended.

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)
  • Step3. Set the frequency of PWM signal (1000Hz in demo)
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