I am struggling and cannot get this to work all (stepper motor doesn't do anything, not convinced even energising as can turn manually with no resistance)
Pins used from a Raspberry Pi 4B to a Sparkfun L6470
Simple code tried:
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(STBY_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(STBY_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 runtime errors: the channel is already in use, continue anyway for:
GPIO.setup(CS_PIN, GPIO.OUT)
GPIO.setup(SCK_PIN, GPIO.OUT)
GPIO.setup(MOSI_PIN, GPIO.OUT)
GPIO.setup(MISO_PIN, GPIO.IN)
on reruns I get just: program exits with code 0
I get no errors re: spidev or rPi.GPIO - appear to be installed correctly. Sparkfun on board power set to external and set to send data to prev board (on board jumpers) I have tried different stepper motors - so know the stepper works Sparkfun board getting power ok and separate motor power ok
Any help welcomed!, I have toiled over this and no luck so far. Any obvious mistakes or oversights I have made?