Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Re: Struggling with GPIOZero for Motor Control Using TB6600 Microstep Driver

$
0
0
I’ve made progress with getting my motor to move using the TB6600 driver! I’ve successfully implemented the clockwise (CW) rotation, but I’m encountering an issue when trying to get the motor to rotate counter-clockwise (CCW).

For CW rotation, everything works perfectly as expected, but for CCW, I’m unable to make it move. I’ve checked the wiring and double-checked the code, and the logic for the direction pin seems correct (using 1 for CCW and 0 for CW). However, despite this, the motor doesn’t rotate when I try to set it to CCW.

Code:

from gpiozero import OutputDevicefrom time import sleep# Define the GPIO pinspulse_pin = 6    # Pulse pindir_pin = 5      # Direction pinenable_pin = 26   # Enable pin# Initialize the pins as output devicespulse = OutputDevice(pulse_pin, active_high=True)direction = OutputDevice(dir_pin, active_high=True)  # Active high to rotate CWenable = OutputDevice(enable_pin, active_high=False)  # Active low to enable the driver# Enable the driver (this must be done to activate the TB6600)enable.on()sleep(0.5)  # Small delay to allow the driver to initialize# Direction values (1 for CCW, 0 for CW)ccw_direction_2 = 1cw_direction_2 = 0def set_direction(direction_value):    """    Sets the direction of the motor.    - direction_value should be ccw_direction_2 (1) for CCW    - direction_value should be cw_direction_2 (0) for CW    """    direction.value = direction_value  # Set the direction pin based on the value (0 or 1)    if direction_value == cw_direction_2:        print("Direction: CW (Active Low)")    else:        print("Direction: CCW (Active High)")def step(steps=1, delay=0.001):  # Adjust pulse delay as needed    for _ in range(steps):        pulse.on()  # Send pulse to TB6600        sleep(delay)        pulse.off()        sleep(delay)# Test the movement with CW (Clockwise)print("Starting CW rotation...")set_direction(cw_direction_2)  # Set direction to CW (0)step(steps=2000, delay=0.001)  # Execute 200 steps with 1ms delaysleep(1)  # Pause for a moment# Test the movement with CCW (Counter-Clockwise)print("Starting CCW rotation...")set_direction(ccw_direction_2)  # Set direction to CCW (1)step(steps=2000, delay=0.001)  # Execute 200 steps with 1ms delayprint("Test complete.")
Has anyone else faced a similar issue or have suggestions on what I might be missing for CCW operation?

Thanks in advance for your help!

Statistics: Posted by XGTFrostKing — Thu Dec 12, 2024 7:36 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles