I am new to Sparkfun and am struggling to get the stepper motor to move. I just want to test with a simple python program before I move on. The stepper motor connected correctly, motor power connected ok (12V). Pins connected: On Sparkfun in ‘From Prev Board’ sections. SPIDEV and RPi.GPIO installed ok (I am pretty sure - no error messages on running)
Pi 4B Pi Pin No Autodriver Pin
+5V 4 3-5V POWER (FROM PREV BOARD)
GND 6 GND
GPIO10 (MOSI) 19 SD FROM PREV BOARD (BETWEEN OSCIN & CS
GPIO9 (MISO) 21 SD TO PREV BOARD (BETWEEN GND & SCK)
GPIO11 (SCLK) 23 SCK
GPIO8 (CE0) 24 CS
GPIO25 22 RESET (FROM PREV BOARD) (NEXT TO FLAG) (RESET HAS LINE ABOVE IT)
Simple program used:
import spidev
import time
import RPi.GPIO as GPIO
#GPIO pins
CS_PIN = 8 # GPIO pin for CS (Chip Select)
SCK_PIN = 11 # GPIO pin for SCK (SPI Clock)
MOSI_PIN = 10 # GPIO pin for MOSI (Master Out Slave In)
MISO_PIN = 9 # GPIO pin for MISO (Master In Slave Out)
STBY_PIN = 17 # GPIO pin for STBY/RESET
RST_PIN = 25 # GPIO pin for RST/NSD
Initialize SPI
spi = spidev.SpiDev()
spi.open(0, 0) # Open SPI bus 0, device 0
spi.max_speed_hz = 1000000 # Set SPI speed
Initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(CS_PIN, GPIO.OUT)
GPIO.setup(SCK_PIN, GPIO.OUT)
GPIO.setup(MOSI_PIN, GPIO.OUT)
GPIO.setup(MISO_PIN, GPIO.IN)
GPIO.setup(STBY_PIN, GPIO.OUT)
GPIO.setup(RST_PIN, GPIO.OUT)
def send_command(command):
GPIO.output(CS_PIN, GPIO.LOW) # Enable SPI communication
spi.xfer2([command])
GPIO.output(CS_PIN, GPIO.HIGH) # Disable SPI communication
Reset the L6470
def reset_l6470():
GPIO.output(RST_PIN, GPIO.LOW)
time.sleep(0.01)
GPIO.output(RST_PIN, GPIO.HIGH)
time.sleep(0.01)
Initialize L6470
def initialize_l6470():
send_command(0xC0) # Set parameter command
send_command(0x00) # Set step mode to full step (0x00 for full step, refer to datasheet)
Move the motor
def move_motor(steps, speed):
send_command(0x51) # Move command
send_command(steps & 0xFF) # Number of steps (LSB)
send_command((steps >> 8) & 0xFF) # Number of steps (MSB)
send_command(speed & 0xFF) # Speed (LSB)
send_command((speed >> 8) & 0xFF) # Speed (MSB)
try:
GPIO.output(RST_PIN, GPIO.HIGH) # Enable the motor driver
reset_l6470()
initialize_l6470()
move_motor(400, 200) # Move 400 steps at speed 200 (adjust as needed)
time.sleep(2)
except KeyboardInterrupt:
pass
finally:
GPIO.output(RESET_PIN, GPIO.LOW) # Disable the motor driver
spi.close() # Close the SPI bus
GPIO.cleanup() # Clean up GPIO pins
When I run in Geany for the first time I get line 21,22,23,24 This channel is already in use, continue anyway, then the line about disabling warnings.
Then Program exits with code 0
repeat runs I just get Program exits with code 0
My multimeter says power in to board and motor ok. Stepper does not feel energised at any point, doesn’t do anything.
Please any help greatly welcomed, I have really struggled for days with this, thank you everyone!