Raspberry Pi / Python code for Servo Motor

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ceilowens
 
Posts: 13
Joined: Tue Sep 13, 2022 5:45 pm

Raspberry Pi / Python code for Servo Motor

Post by ceilowens »

Hello,

I'm trying to get a Servo Motor to work with Raspberry Pi in tandem with a stepper motor driver.

I've tried this code so far in my experiments:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

# Direction pin from controller
DIR = 10
# Step pin from controller
STEP = 8
# 0/1 used to signify clockwise or counterclockwise.
CW = 1
CCW = 0

# Setup pin layout on PI
GPIO.setmode(GPIO.BOARD)

# Establish Pins in software
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)

# Set the first direction you want it to spin
GPIO.output(DIR, CW)

try:
	# Run forever.
	while True:

		"""Change Direction: Changing direction requires time to switch. The
		time is dictated by the stepper motor and controller. """
		sleep(1.0)
		# Esablish the direction you want to go
		GPIO.output(DIR,CW)

		# Run for 200 steps. This will change based on how you set you controller
		for x in range(200):

			# Set one coil winding to high
			GPIO.output(STEP,GPIO.HIGH)
			# Allow it to get there.
			sleep(.005) # Dictates how fast stepper motor will run
			# Set coil winding to low
			GPIO.output(STEP,GPIO.LOW)
			sleep(.005) # Dictates how fast stepper motor will run

		"""Change Direction: Changing direction requires time to switch. The
		time is dictated by the stepper motor and controller. """
		sleep(1.0)
		GPIO.output(DIR,CCW)
		for x in range(200):
			GPIO.output(STEP,GPIO.HIGH)
			sleep(.005)
			GPIO.output(STEP,GPIO.LOW)
			sleep(.005)

# Once finished clean everything up
except KeyboardInterrupt:
	print("cleanup")
	GPIO.cleanup()

When connected to the GPIO pins, it won't turn.

I've done some research and I'm hopeful that this is correct, and I'm trying to obtain some Python code to get the GPIO pins to output accordingly. Can anyone give me any advice?
Attachments
steppermotor.png
steppermotor.png (87.59 KiB) Viewed 130 times

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: Raspberry Pi / Python code for Servo Motor

Post by bidrohini »

What is the voltage requirement of this stepper motor? Have you checked?

User avatar
adafruit_support_carter
 
Posts: 29167
Joined: Tue Nov 29, 2016 2:45 pm

Re: Raspberry Pi / Python code for Servo Motor

Post by adafruit_support_carter »

Please post a photo of your setup showing how everything is connected.

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”