circuiTician
Published

Light & Motor driver Module for python

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.

IntermediateProtip1.5 hours1,080
Light & Motor driver Module for python

Things used in this project

Hardware components

DFRobot Light and motor driver module for python
×1
Arduino Mega 2560
Arduino Mega 2560
×1
USB Cable, USB Type C Plug
USB Cable, USB Type C Plug
×1
DC Motor, 12 V
DC Motor, 12 V
×1
Rotary Potentiometer, 10 kohm
Rotary Potentiometer, 10 kohm
×1
adapter 12 volt
×1

Software apps and online services

Arduino IDE
Arduino IDE
python
DFRobot DRI0050 software version 1

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver
Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires
Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires

Story

Read more

Schematics

Circuit for analog mode

Circuit for UART mode

Code

Automatic Speed control code for arduino

C/C++
#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);
  
}

pwmC.py

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

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

#pwmd = DRI0050(port="/dev/ttyUSB0") #RPi Linux platform
pwmd = DRI0050(port="COM3")  #Windows platform

#print("version=0x{:x}, addr=0x{:x}".format(pwmd.get_version(), pwmd.get_addr()))
#print("pid=0x{:x}, vid=0x{:x}".format(pwmd.get_vid(), pwmd.get_pid()))

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)

Credits

circuiTician

circuiTician

2 projects • 1 follower
Hi, it's SUBHABRATA here, I am a regular Electrical Engineer with irregular ideas! And the most important thing BE_CREATIVE

Comments