Firstly, this topic was my help request. Since I found solution on my own, I will post both scripts with short instructions here in first post, maybe it would be useful for someone. Also, I changed the topic subject from "Could someone help......" to "[How to]....."
You will need:
- RPI 3 model B (I guess any other Pi can do this with possible slightly modification of wiring/code, depending of model)
- Stepper motor 28BYJ-48 + Motor Driver Board: ULN2003APG https://www.aliexpress.com/item/3304067 ... hweb201603_ I am pretty sure that this Aliexpress link will be dead after some time, but this is very common kit and they are usually selling this motor and this board in bundle
GPIO 23 (connected to ULN2003APG IN1)
GPIO 24 (connected to ULN2003APG IN2)
GPIO 18 (connected to ULN2003APG IN3)
GPIO 17 (connected to ULN2003APG IN4)
You can supply power to motor driver board either from RPI 5V+ and GND or from external battery pack (5-6V). I think that external is recommended.
Script for controlling stepper with keyboard - key LEFT is rotating motor anti clockwise until is pressed down, key RIGHT is rotating motor clockwise until is pressed down.
Code: Select all
import sys
import time
import pygame
import RPi.GPIO as GPIO
pygame.init()
# define variables
chan_list = [23,24,18,17] # GPIO ports to use
delay=.001 # delay between each sequence step
# Use BCM GPIO references
# instead of physical pin numbers
pygame.display.set_mode((100, 100))
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set all pins as output
for pin in chan_list:
print ("Setup pins")
GPIO.setup(pin,GPIO.OUT)
#initialize array for sequence shift
arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
def clockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[3:]+arr1[:3] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
def counterclockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
class rightKey:
def out(self):
print("it works")
class leftKey:
def out(self):
print("it works")
rightKey = None
leftKey = None
# Start main loop
try:
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
rightKey = True
elif event.key == pygame.K_LEFT:
leftKey = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
rightKey = False
elif event.key == pygame.K_LEFT:
leftKey = False
if rightKey:
print ("Run clockwise")
clockwise()
elif leftKey:
print ("Run counterclockwise")
counterclockwise()
except KeyboardInterrupt:
GPIO.output(chan_list, (0,0,0,0))
sys.exit()
GPIO.cleanup()
Code: Select all
import sys
import time
import pygame
import RPi.GPIO as GPIO
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
# define variables
chan_list = [23,24,18,17] # GPIO ports to use
delay=.001 # delay between each sequence step
# Use BCM GPIO references
# instead of physical pin numbers
pygame.display.set_mode((100, 100))
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set all pins as output
for pin in chan_list:
print ("Setup pins")
GPIO.setup(pin,GPIO.OUT)
#initialize array for sequence shift
arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
def clockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[3:]+arr1[:3] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
def counterclockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
class rightKey:
def out(self):
print("it works")
class leftKey:
def out(self):
print("it works")
rightKey = None
leftKey = None
# Start main loop
try:
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 1:
rightKey = True
elif event.button == 2:
leftKey = True
elif event.type == pygame.JOYBUTTONUP:
if event.button == 1:
rightKey = False
elif event.button == 2:
leftKey = False
if rightKey:
print ("Run clockwise")
clockwise()
elif leftKey:
print ("Run counterclockwise")
counterclockwise()
except KeyboardInterrupt:
GPIO.output(chan_list, (0,0,0,0))
sys.exit()
GPIO.cleanup()My programming skill is 0, I guess there is maybe better way, but this is mine and it is working. And, of course, it wouldn't work without great people who already posted their scripts across internet, so I compile my scripts from their. Many of them are mentioned via their links in posts below - thanks to all.
All enhancements and suggestions are welcome
All below line is old OP post, by time when this was help request topic:
___________________________________________________________________________________________________________________________________________
Ok, am giving up on my own and asking for help
Parts:
- RPI 3 model B
- Stepper motor 28BYJ-48, data sheet: https://datasheetspdf.com/pdf/1006817/K ... 28BYJ-48/1 (not sure is this really needed, since this is very common motor)
- Motor Driver Board: ULN2003APG, cannot find data sheet for assembled board, found only for ULN2003APG IC: https://datasheet.lcsc.com/szlcsc/ULN2004APG_C14336.pdf and this is the board (also, very common): https://www.aliexpress.com/item/3304067 ... hweb201603_
I connected ULN2003APG pins and RPI pins in following order:
GPIO 23 (connected to ULN2003APG IN1)
GPIO 24 (connected to ULN2003APG IN2)
GPIO 18 (connected to ULN2003APG IN3)
GPIO 17 (connected to ULN2003APG IN4)
This is the code I downloaded from internet and upon start it will rotate motor counterclockwise forever :
Code: Select all
import sys
import time
import RPi.GPIO as GPIO
# define variables
chan_list = [23,24,18,17] # GPIO ports to use
delay=.001 # delay between each sequence step
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Set all pins as output
for pin in chan_list:
print "Setup pins"
GPIO.setup(pin,GPIO.OUT)
#initialize array for sequence shift
arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
def move():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
# Start main loop
while True:
try:
move()
except KeyboardInterrupt:
GPIO.output(chan_list, (0,0,0,0))
sys.exit()arr1[1:]+arr1[:1] will rotate motor counterclockwise and arr1[3:]+arr1[:3] will rotate motor clockwise.arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
From my understanding move() will execute action.
So I added one more block of code, changed variable name and this would be last part of file now:
Code: Select all
#initialize array for sequence shift
arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
def clockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[3:]+arr1[:3] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
def counterclockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
# Start main loop
while True:
try:
clockwise()
except KeyboardInterrupt:
GPIO.output(chan_list, (0,0,0,0))
sys.exit()I want to make following:
- When script is started, motor does nothing, it is stopped.
- If I press certain keyboard key, e.g. right arrow, motor should rotate clockwise, but only while key is held down, when I release key motor should stop
- If I press another keyboard key, e.g. left arrow, motor should rotate counterclockwise, but only while key is held down, when I release key motor should stop
Code: Select all
import sys
import time
import pygame
import RPi.GPIO as GPIO
pygame.init()
screen = pygame.display.set_mode((100,100))
# define variables
chan_list = [23,24,18,17] # GPIO ports to use
delay=.001 # delay between each sequence step
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set all pins as output
for pin in chan_list:
print ("Setup pins")
GPIO.setup(pin,GPIO.OUT)
#initialize array for sequence shift
arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
def clockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[3:]+arr1[:3] # rotates array values of 3 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
def counterclockwise():
global arr1 # enables the edit of arr1 var inside a function
global arr2 # enables the edit of arr2 var inside a function
arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
arr1 = arr2
arr2 = arrOUT
GPIO.output(chan_list, arrOUT)
time.sleep(delay)
# Start main loop
try:
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
clockwise()
elif event.key == pygame.K_LEFT:
counterclockwise()
except KeyboardInterrupt:
GPIO.output(chan_list, (0,0,0,0))
sys.exit()Sorry if this post is confused long, I was try to give as many details.
Thanks in advance.