Need help with motor braking

Hello, we are currently using a DC motor to power a winch that lifts a heavy object off of the ground. The object can be lifted when the motor is engaged, but the motor does not seem to be strong enough to hold it in place once it reaches the top since the object’s weight seems to pull the motor beyond what the torque can handle. Is there a way to stop the object from falling down and force the motor to have more powerful braking via a current? I will attach our code below.

We are using the following hardware:
30:1 Metal Gearmotor 37Dx68L mm 12V with 64 CPR Encoder (Helical Pinion)
TB9051FTG Single Brushed DC Motor Driver Carrier

const int pwm1 = 5;
const int pwm2 = 6;
const int en = 3;
const int enb = 9;

const int lightSensor = A7;
 
int brightness = 0;
bool shouldTurnOtherWay = false;
bool wasAlreadyOn = false;
bool wasAlreadyOff = false;


void setup() {
  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  pinMode(en, OUTPUT);
  pinMode(enb, OUTPUT);
  pinMode(lightSensor, INPUT);
  Serial.begin(115200);
}

void loop() {
  brightness = analogRead(lightSensor);
  Serial.println(brightness);
  if (brightness < 500 && !wasAlreadyOff) {
        wasAlreadyOff = true;
        wasAlreadyOn = false;
        // motor stops
        analogWrite(pwm1,0);
        analogWrite(pwm2,0);
        analogWrite(en,255);
        analogWrite(enb,0);
        delay(100);
    }
  if (brightness >= 500 && !wasAlreadyOn) {
        wasAlreadyOn = true;
        wasAlreadyOff = false;
        if (shouldTurnOtherWay) {
           //motor spins clockwise
           analogWrite(pwm1,255);
           analogWrite(pwm2,0);
           analogWrite(en,255);
           analogWrite(enb,0);
           delay(100);
        } else {
           //motor spins counterclockwise
           analogWrite(pwm1,0);
           analogWrite(pwm2,255);
           analogWrite(en,255);
           analogWrite(enb,0);
           delay(100);
        }
        shouldTurnOtherWay = !shouldTurnOtherWay;
    }
}

Hello.

You should be able to make your motor hold the load in place by implementing a closed-loop position control routine using your motor’s encoder feedback. This blog post includes links to a good tutorial for that, though please note that it does not use exact same hardware that you have.

Please keep in mind that depending on how much torque your system is putting on the motor, holding it in stall like that could be very stressful on it electrically and mechanically, potentially reducing the life of the motor. If this is a concern, you might look into adding a mechanical brake or latch of some sort. Using a worm drive so it cannot be back-driven might also be an option.

- Patrick