User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

[How to] Control stepper motor 28BYJ-48 with keyboard and PS4 controller

Sat Mar 28, 2020 10:46 am

Hi,

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
Wiring:

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()

Script for controlling stepper with PS4 controller - button TRIANGLE is rotating motor anti clockwise until is pressed down, key CIRCLE is rotating motor clockwise until is pressed down. Nothing special is needed, it is enough to pair PS4 controller via RPI UI, i.e. Bluetooth icon in top right corner on panel.

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()
I guess that some dependencies I forgot to mention, sorry I really cannot remember which few packages I installed before of this, but if you missing something it will be very intuitive displayed in error messages, and from other side, I will follow up shortly.

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 :D

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 :D My programming skill is equal to +0, last night I spent few hours to modify script downloaded from internet to do what I want, without success.

Parts:
  • RPI 3 model B
I connected 28BYJ-48 motor with ULN2003APG board via provided 5 pin white socket:

Image

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()
From my understanding (zero programming skill) in line:
arrOUT = arr1[1:]+arr1[:1] # rotates array values of 1 digit
arr1[1:]+arr1[:1] will rotate motor counterclockwise and arr1[3:]+arr1[:3] will rotate motor clockwise.

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()
Of course, in main loop, if I change clockwise() to counterclockwise() , motor will change rotate direction.

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
I tried something like this:

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()
But it is not rotating while button is held down, it only makes very very very small steps when button is pressed (clock for RIGHT key and ccounterclock for LEFT key) (off topic: and I am sure that same way/order of main loop is working with my DC motors, controlled by PS3 joystick)

Sorry if this post is confused long, I was try to give as many details.

Thanks in advance.
Last edited by stiw47 on Sun Mar 29, 2020 11:49 am, edited 3 times in total.

Garvan
Posts: 41
Joined: Sun Jan 05, 2020 9:59 am

Re: Can anybody help me to run stepper motor while keyboard key is pressed?

Sat Mar 28, 2020 2:45 pm

Keep the code to check keyboard state separate from the code to control the motor. Do you understand the sample code below?

Code: Select all

import pygame

pygame.init()
screen = pygame.display.set_mode((100,100))

def clockwise():
	print ("Clockwise")

def anticlockwise():
		print ("AntiClockwise")

def stop():
	print ("stop")

# Start main loop
keystate = 0
try:
	while True:
		events = pygame.event.get()
		for event in events:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_RIGHT:
					keystate = 1
				elif event.key == pygame.K_LEFT:
					keystate = 2
			if event.type == pygame.KEYUP:
					keystate = 0
		if keystate == 0:
			stop()
		if keystate == 1:
			clockwise()
		if keystate == 2:
			anticlockwise()

except KeyboardInterrupt:
	print ("bye")

User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

Re: Can anybody help me to run stepper motor while keyboard key is pressed?

Sat Mar 28, 2020 3:05 pm

Ok, I took little from here: https://stackoverflow.com/questions/298 ... ot-working (and little form here and there on my way to mentioned link :D ), learned some things from here: https://stackoverflow.com/questions/148 ... ot-defined and here: https://www.raspberrypi.org/forums/view ... 3#p1632743 and ended up with following semi working code:

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")
  
# 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()
Things not working as I want:
  • Straight upon starting script, motor is rotating clockwise. This is not what I want, it should be stopped
  • Straight upon starting script while motor is rotating clockwise, if I first press/hold RIGHT, motor will rotate counter while I'm holding RIGHT (wrong direction for right) and continue rotate clock if I release RIGHT. This can be repeated endless until LEFT is pressed. Once when LEFT is pressed and released, motor will stop and will do what should do: rotate clock while RIGHT is held and rotate counter while LEFT is held. I think this is related with first AP.
  • Another case: LEFT pressed first upon script start, while motor is rotating clockwise - no matter how many times LEFT is pressed it will do nothing. Motor will rotate clock all the time. Once when RIGHT is pressed and release (after LEFT), motor will stop and will do what should do: rotate clock while RIGHT is held and rotate counter while LEFT is held. I think this is also related with first AP.
  • In some certain angle, motor will not rotate clock while RIGHT is held - it will vibrate, but not rotate. In this case, I have to change angle little bit counterclockwise with LEFT, then RIGHT clockwise will work again
  • Rotation knows to become choppy and slow with more vibration. This is happen randomly and maybe is not applicable, maybe this is happen because of low quality Aliexpress motor
__
It would be appreciated if someone could help me to resolve firs AP, thanks.

User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

Re: Can anybody help me to run stepper motor while keyboard key is pressed?

Sat Mar 28, 2020 3:08 pm

Garvan wrote:
Sat Mar 28, 2020 2:45 pm
Keep the code to check keyboard state separate from the code to control the motor. Do you understand the sample code below?

Code: Select all

import pygame

pygame.init()
screen = pygame.display.set_mode((100,100))

def clockwise():
	print ("Clockwise")

def anticlockwise():
		print ("AntiClockwise")

def stop():
	print ("stop")

# Start main loop
keystate = 0
try:
	while True:
		events = pygame.event.get()
		for event in events:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_RIGHT:
					keystate = 1
				elif event.key == pygame.K_LEFT:
					keystate = 2
			if event.type == pygame.KEYUP:
					keystate = 0
		if keystate == 0:
			stop()
		if keystate == 1:
			clockwise()
		if keystate == 2:
			anticlockwise()

except KeyboardInterrupt:
	print ("bye")
Hi Gavran, thanks for follow up. You replied while I typed above post. Will check this now and reply.

User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

Re: Can anybody help me to run stepper motor while keyboard key is pressed?

Sat Mar 28, 2020 3:37 pm

@Gavran: Thanks a lot, but I just added following straight before main loop, to clear variables:

Code: Select all

rightKey = None
leftKey = None
Whole code now looks as below and it is working as expected:

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()

Still have a problem with random choppy and slow rotation, but this is maybe because motor is powered from RPI 5V PIN + GND. Will try now to find some external power source.

Garvan
Posts: 41
Joined: Sun Jan 05, 2020 9:59 am

Re: Can anybody help me to run stepper motor while keyboard key is pressed?

Sun Mar 29, 2020 3:29 am

Your clockwise code is correct, but counterclockwise is producing the wrong sequence (as far as I understand).

Code: Select all

arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
  
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
  print (arrOUT)
  
for i in range (20):
	counterclockwise()


Output:
[1, 0, 0, 1]
[1, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 0, 1]
[0, 1, 1, 0]
[0, 0, 1, 0]
[1, 1, 0, 0]
[0, 1, 0, 0]
[1, 0, 0, 1]
[1, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 0, 1]
[0, 1, 1, 0]
[0, 0, 1, 0]
[1, 1, 0, 0]
[0, 1, 0, 0]
[1, 0, 0, 1]
[1, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 0, 1]
Do you have a reference for this code? I would try something like this:

Code: Select all

def counterclockwise():
  global arr1
  global arr2
  arrOUT = arr2[1:]+arr2[:1]
  arr2 = arr1
  arr1 = arrOUT
  print (arrOUT)
But this gives a full step when you change from clockwise to counterclockwise, so it may not be perfect if you need this level of precision.

User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

Re: Can anybody help me to run stepper motor while keyboard key is pressed?

Sun Mar 29, 2020 11:48 am

Garvan wrote:
Sun Mar 29, 2020 3:29 am
Your clockwise code is correct, but counterclockwise is producing the wrong sequence (as far as I understand).

Code: Select all

arr1 = [1,1,0,0]
arr2 = [0,1,0,0]
  
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
  print (arrOUT)
  
for i in range (20):
	counterclockwise()


Output:
[1, 0, 0, 1]
[1, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 0, 1]
[0, 1, 1, 0]
[0, 0, 1, 0]
[1, 1, 0, 0]
[0, 1, 0, 0]
[1, 0, 0, 1]
[1, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 0, 1]
[0, 1, 1, 0]
[0, 0, 1, 0]
[1, 1, 0, 0]
[0, 1, 0, 0]
[1, 0, 0, 1]
[1, 0, 0, 0]
[0, 0, 1, 1]
[0, 0, 0, 1]
Do you have a reference for this code? I would try something like this:

Code: Select all

def counterclockwise():
  global arr1
  global arr2
  arrOUT = arr2[1:]+arr2[:1]
  arr2 = arr1
  arr1 = arrOUT
  print (arrOUT)
But this gives a full step when you change from clockwise to counterclockwise, so it may not be perfect if you need this level of precision.
Hi Raven :D ("Gavran" means "Raven" in my language, Serbian).
I tried your code block for def counterclockwise() , unfortunately it does nothing for me, motor is not rotating. So I reverted back to previously.

For code reference, I stole it from here: https://peppe8o.com/control-a-simple-st ... pberry-pi/

As said, I am not a programmer, just trying to make things work with editing of other people code. Every enhancement and suggestions are welcome.

Garvan
Posts: 41
Joined: Sun Jan 05, 2020 9:59 am

Re: [How to] Control stepper motor 28BYJ-48 with keyboard and PS4 controller

Sun Mar 29, 2020 1:04 pm

You followed the code, as it is presented in the tutorial, but it does not show a working counterclockwise function,.

If it works, it works.

If you motor does not run correctly counterclockwise (I expect vibration), when you have sent up the correct power supply, then post again here.

Raven

User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

Re: [How to] Control stepper motor 28BYJ-48 with keyboard and PS4 controller

Sun Mar 29, 2020 2:51 pm

Yup, counterclockwise wasn't presented in tutorial, but it was explained on web page and reading whole tutorial, it wasn't hard to implement it.

I was already tried with external 5V battery pack, sorry forgot to follow up. It is rotating much smoother, but from time to time, it still knows to stuck and not start rotating clockwise. This is only happens if anti clockwise rotation is stopped in one certain angle. Straight after that, clockwise wont start and I have to rotate little more anti clockwise, then start clockwise. I also assume this is because "not the best" implementation, and also could be poor quality Aliexpress motor. I have no another one currently, so cannot tell for sure. This should rotate camera (and maybe Nerf gun) on my old RPI robo car. Will see how it will work and - if works it works, but if would be annoying will try to make some other implementation (or buy another motor).

Thanks for your help.

User avatar
stiw47
Posts: 14
Joined: Sun Mar 31, 2019 9:26 am
Location: Belgrade, RS

Re: [How to] Control stepper motor 28BYJ-48 with keyboard and PS4 controller

Mon May 04, 2020 1:11 pm

Off topic: I just would like to inform you that above lesson learned was used for below project.
I hope you like it :D

https://www.youtube.com/watch?v=MsK8bgPNYxE&t

Thanks once again to everyone who helped me :lol:

Return to “Python”