circuiTician

Light and Motor Driver for Python

,

Table of Contents

In this article, I will show you how to use DF Robot’s all-new Light and Motor Driver Module for Python in your project. By using this module you can control the speed of a PMDC motor for the brightness of the LED strip.

I use PMDC most of the time in my small projects because it is cheap and very easy to control. I am going to drive my PMDC motor using this Motor Driver Module.

4 Control Modes

There are 4 Control modes in this module

1. Analogue control mode for potentiometer control mode 

2. UART or serial communication control mode 

3. Host PC software control mode 

4. Python Programming control 

The main advantage of this driver module is the driver can be controlled via PC using Python programming language. So, this board can be used very easily for those project which is based on Python programming.

Board Description

This module can be supplied from 5 volts to 24 volts DC. And the onboard motor driving MOSFET can handle 10 ampere current. For connecting the power supply and the motor there is a couple of 2 pin screw connector. 

There is an onboard micro switch to start and stop the motor. Along with this switch, a male header is also present for connecting external NO-type push switch for the same purpose. 

For controlling the speed of the motor manually here is a 3 pin connector where a potentiometer can be connected. 

For your or serial communication purpose a 4 pin JST terminal is used. You will also get a 4 pin JST while purchasing this board. The four pins of this JST terminal are used for 5 volts, Ground, TX, and RX

If you want to communicate with this motor driver via PC there is a USB C type connector on the board. By using a USB C-type data cable you can connect this board to the PC directly. For changing the driving mode is light switch is soldered on the right side of the JST terminal. To put this board into the analog control mode you have to slide the switch in the analog position and for the other three modes f to slide this switch in the communication position.

The heart of this board is the CH552 microcontroller. It is an 8 bit USB microcontroller having 2 PWM outputs and one UART communication port. As this is a USB microcontroller. So the USB C type port is directly connected to the microcontroller without any USB to TTL converter. For more information, you can check the datasheet of this microcontroller.

Analog or Potentiometer Control Mode

To configure this model you will require- 

1. DF robot light and motor driver module for python.

2. A power source (from 5 volts to 24 volts).

3. A motor (LED strip if you want to control the brightness of it).

4. Potentiometer (from 4.7 Kohm to 470 Kohm).

This is the circuit diagram for analog control mode.

1. Analog or Potentiometer Control Mode

After powering up the board the motor will start. Now by rotating the Potentiometer the motor speed will also change according to the position of the Potentiometer. By pressing the onboard micro switch you can stop the motor and by pressing the switch again you can start the motor. Now instead of this motor if a LED strip light is connected the brightness of that LED light also can be controlled very easily by just adjusting the Potentiometer.

UART or Serial Control Mode

In this mode, you can easily communicate with the motor driver with the help of Arduino or any other microcontroller. To configure this mode you will require – 

1. DF robot light and motor driver module for python 

2. A power source (from 5 volts to 24 volts) 

3. A motor (LED strip if you want to control the brightness of it) 

4. Arduino (any other microcontroller will also work)

At first, you have to connect the JST cable to the JST terminal of the motor driver board. Then the other end of the JST terminal will be connected to Arduino as per the circuit diagram.

UART or Serial Control Mode

For coding Arduino, I am using the example code written by DFRobot. 

For serial communication, this board follows the RS485 MODBUS standard.

Code:

#include "SoftwareSerial.h"

#define  PWM_ENABLE           0x01
#define  PWM_DISENABLE        0x00
#define  DEV_ADDR             0x32
#define  DUTY_REG_ADDR        0x0006
#define  FREQ_REG_ADDR        0x0007
#define  PWM_EN_REG_ADDR      0x0008

SoftwareSerial mySoftwareSerial(10, 11); // RX_ARD --> TX LMD, TX_ARD --> RX_LMD

static uint16_t CheckCRC(uint8_t *data, uint8_t len){
  uint16_t crc = 0xFFFF;
  for(uint8_t pos = 0; pos < len; pos++){
    crc ^= (uint16_t)data[pos];
    for(uint8_t i = 8; i != 0; i-- ){
      if((crc & 0x0001) != 0){
        crc >>= 1;
        crc ^= 0xA001;
      }else{
        crc >>= 1;
      }
    }
  }
  crc = ((crc & 0x00FF) << 8) | ((crc & 0xFF00) >> 8);
  return crc;
}

static void WriteRegValue(uint16_t regAddr, uint16_t value){
  uint8_t tempData[8];
  uint16_t crc;
  tempData[0] = DEV_ADDR;
  tempData[1] = 0x06;
  tempData[2] = (regAddr >> 8) & 0xFF;
  tempData[3] = regAddr & 0xFF;
  tempData[4] = (value >> 8) & 0xFF;
  tempData[5] =  value & 0xFF;
  crc = CheckCRC(tempData, 6);
  tempData[6] = (crc >> 8) & 0xFF;
  tempData[7] = crc & 0xFF;
  for(uint8_t i = 0 ;i < 8; i++){
    mySoftwareSerial.print((char)tempData[i]);
  }
  mySoftwareSerial.flush();
}


static void setPwmDuty(uint8_t duty){
  WriteRegValue(DUTY_REG_ADDR, (uint16_t)duty);
}

static void setPwmFreq(uint8_t freq){
  WriteRegValue(FREQ_REG_ADDR, (uint16_t)freq);
}

static void setPwmEnable(uint8_t pwmStatus){
  WriteRegValue(PWM_EN_REG_ADDR, (uint16_t)pwmStatus);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySoftwareSerial.begin(9600);
  delay(1000);
  setPwmFreq(10);
  delay(50);
  setPwmDuty(0);
  delay(50);
  setPwmEnable(PWM_ENABLE);
  delay(50);
}

void loop() {
  for(int i=15; i<=255; i=i+1){
    setPwmDuty(i);
    Serial.println(i);
    delay(30);
  }
  delay(5000);
  for(int i=255; i>=15; i=i-1){
    setPwmDuty(i);
    Serial.println(i);
    delay(30);
  }
  delay(5000);
  
}

In this code, you have to just include the software serial library. I define pin number 10 and 11 as software serial pins. 

For communicating with the board, they have written a function that is sending serial data by the command “mySoftwareSerial.print”, which includes device ID and the value of driver to enable pwm and frequency register with generated CRC (cyclic redundancy check). After sending the serial data every time function clears the serial line by writing the “mySoftwareSerial.flash”. 

What generating the register value for duty cycle frequency and driver enable register there are 3 function – “setPwmDuty”, “setPwmFreq” and “setPwmEnable”

In the void setup part, I started the hardware serial library by writing “Serial.begin(9600)” and the software serial library by writing “mySoftwareSerial.begin(9600)”. 

Then I am enabling the driver with a fixed frequency which is 1 kHz. 

In the void loop part, I have written two for loops in the first for loop I increase the value of the Duty cycle from 0% to 100%. The value of the Duty cycle is in 8-bit format where 0 represents 0% duty cycle and 255 represents 100% duty cycle. In the second for loop, I am decreasing the duty cycle from 100% to 0%. After each for loop, there is a delay of 5 seconds to observe the highest and lowest speed. 

In the for loop, I am setting the value of the Duty cycle by writing this function “setPwmDuty(i)”. ‘i’ stores the value of PWM (in between 0-255). 

Overall this code will increase and decrease the speed of the motor automatically. 

So after making all the connections and powering up the board you will observe the speed of the motor is increasing from zero to maximum and decreasing from maximum to zero.

Host PC Software Control Mode

In this mode, you have to download and install the software provided by DFRobot. 

To configure this mode you will require- 

1. DF robot light and motor driver module for python 

2. A power source (from 5 volt to 24 volt) 

3. A motor (LED strip if you want to control the brightness of it) 

4. USB C type cable 

At first, you have to connect the module to the PC using the USB C-type cable. After connecting it to the PC go to the device manager and check which communication port (COM) it is connected to.

Now you have to launch the software in the software you are to select the come for where the board is connected. By pressing the start switch in the software you can start the motor and by adjusting the slider of the frequency control and the duty cycle control you can control the speed of the motor for the brightness of a LED strip. 

By increasing the frequency you can decrease the speed of the motor and vice versa. And by increasing the duty cycle you can increase the motor speed and by decreasing the duty cycle you can decrease the motor speed. 

In simple words, the motor speed is inversely proportional with frequency and directly proportional to the duty cycle. 

If frequency can be adjusted from 183 Hz to 46.87 KHz and the duty cycle can be adjusted from 0% to 100%. The value you can put in the software is in 8 bit format show the frequency and the duty cycle is mapped between 0 to 255.

 Host PC Software Control Mode

Python Control Mode

I have said earlier this driver module can be controlled via python code. So, I have installed Python on my PC.

To configure this mode you will require- 

1. DF robot light and motor driver module for python 

2. A power source (from 5 volt to 24 volt) 

3. A motor (LED strip if you want to control the brightness of it) 

4. USB C type cable 

I installed the pinpong library by writing this command in the command prompt

pip3 install pinpong

Code:

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_dri0050 import DRI0050 # Import DRI0050 library from libs 


Board("Win").begin() #windows platform


pwmd = DRI0050(port="COM3")  #Windows platform



pwmres = 0.1
pwmD = 0

while True:
        pwmres = 0.1
        pwmD = 0
        
        for i in range (0,10):
            
                pwmd.set_freq(1000) #(183HZ-46875HZ)
                pwmD = pwmD + pwmres
                pwmd.set_duty(pwmD)#(0%-100%)
              
                pwmd.set_enable(1)
                time.sleep(0.5)
            

        for i in range (0,10):
            
                pwmd.set_freq(1000) #(183HZ-46875HZ)
                pwmD = pwmD - pwmres
                pwmd.set_duty(pwmD)#(0%-100%)
                  
                pwmd.set_enable(1)
                time.sleep(0.5)
            
        pwmd.set_enable(0)
        time.sleep(1.5)

In this code at first import time module to put a delay in the code. Then import Board class from Pinpong module to control this motor driver. 

Next, I imported the DRI0050 class of the Pinpong module. It is required for USB communication between the board and the PC.

I am declaring the com port the board connected with, in my case, it is COM3. You can check your com port by just looking into the device manager. 

After this, I started a while true loop which I have written to follow for increasing and decreasing the duty cycle of the PWM signal in the first loop I am increasing the duty cycle from 0% to 100%. In the python code, 0 represents 0% duty cycle and 1 represents 100% duty cycle. In every step of the for loop, I am increasing the duty cycle by 10%. 

In a second for loop, I am decreasing the value of the duty cycle from 100% to 0% with 10% resolution.

As this module interacts with python code directly I have made a fun project using computer vision. To check all the modes and the fun project you can watch my YouTube video on DFRobot’s light and motor driver module for python. You can find all the schematics and code here.

Overall this module is very good to drive motor and LED strips but I found 2 drawbacks in this module. In analog control move if I connect a LED strip for brightness control the light flicker a little bit but in other modes, if I choose a higher frequency for the PWM signal this problem will not happen. 

The second problem is you can only drive the motor in a single direction. As this is the first version of this module I think DFRobot will improve it in the next upcoming versions.

Video Tutorial

Check out my video tutorial on Youtube

Like it !! Share it !!

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest

Related Articles