I do not have expertise in electrical engineering, so I may sound completely naive. I am currently trying to back drive a brushed dc motor. I am reading an analog value from two pressure sensors in a voltage divider circuit that tells the motor which direction to go. Ideally, I want zero mechanical impedance and the motor should drive smoothly and change direction swiftly. For the code, I have taken bits from the code provided by Robojax ("https://www.youtube.com/watch?v=PUL5DZ9TA2o&list=PL0Lbcgzld5acxcoteYK7Ph5EA1f4ShK91&index=17&t=0s") and put it in my sensor reading code. I have got the motor and pressure sensors working but the motor gives jerks when moving and changing directions. I want to smoothen it out. I think my circuitry is alright, maybe changes in the code will be helpful.

Motor>> https://www.aliexpress.com/item/32827922906.html?spm=a2g0s.9042311.0.0. Motor Driver>> https://www.aliexpress.com/item/32819146048.html?spm=a2g0o.productlist.0.0.aadb5f199FbGJd&algo_pvid=0ff3f02d-e74d-4856-83d5-4b424b6b5173&algo_expid=0ff3f02d-e74d-4856-83d5-4b424b6b5173-0&btsid=0ab6fa8115835324922871163ec5a6&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_

code is attached if I am missing out on any info, please let me know.

Thank you.

const int SensorPin = A0; 
int SensorVal;
int PreviousSensorVal;
int change;
const int Middle = 511; // adjusts middle value of potential divider - 0 to 1023 is the range
const int Cushion = 10; // adjusts sensitivity higher number means less sensitive

//*************motor part******************* 
#define RPWM 3 // define pin 3 for RPWM pin (output)
#define R_EN 4 // define pin 2 for R_EN pin (input)
#define R_IS 5 // define pin 5 for R_IS pin (output)

#define LPWM 6 // define pin 6 for LPWM pin (output)
#define L_EN 7 // define pin 7 for L_EN pin (input)
#define L_IS 8 // define pin 8 for L_IS pin (output)
#define CW 1 //do not change
#define CCW 0 //do not change
#define debug 1 //change to 0 to hide serial monitor debugging infornmation or set to 1 to view

#include <RobojaxBTS7960.h>
RobojaxBTS7960 motor(R_EN,RPWM,R_IS, L_EN,LPWM,L_IS,debug);

void setup() {
  //Initiate Serial communication.
  pinMode(SensorPin, INPUT);

void loop() {
  // read the value from the sensors:
  SensorVal = analogRead(SensorPin);
  change = PreviousSensorVal - SensorVal;
  PreviousSensorVal = SensorVal;
  if (SensorVal > (Middle + Cushion)) {
  motor.rotate(80,CW);// run motor with 100% speed in CW direction
  Serial.println("motor is turning CW");
  delay(50);//run for 50 ms seconds
  motor.stop();// stop the motor
  if (SensorVal < (Middle - Cushion)) {
  motor.rotate(80,CCW);// run motor at 100% speed in CCW direction
  Serial.println("motor is turning CCW");
  delay(50);// run for 50 ms seconds
  motor.stop();// stop the motor
  • 2
    \$\begingroup\$ you did not ask a question \$\endgroup\$ – jsotola Mar 6 '20 at 22:26
  • \$\begingroup\$ Is it smoother without all the prints? \$\endgroup\$ – user_1818839 Mar 6 '20 at 22:57
  • \$\begingroup\$ the line delay(50);//run for 5 seconds delays for 50 ms, not 5 seconds. Is this what you intended? If so, please change the comment to match the code. \$\endgroup\$ – Bruce Abbott Mar 6 '20 at 23:41
  • \$\begingroup\$ So the question is that the motor is giving jerks while running and not running smoothly. What changes should I do in the code to make it run smoothly? \$\endgroup\$ – Hamza Warraich Mar 7 '20 at 6:02
  • \$\begingroup\$ Yes, the motor still has a jerky motion without all the prints \$\endgroup\$ – Hamza Warraich Mar 7 '20 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.