# Regarding stepper motor wih raspberry pi to press a button

#### Raj Waghmare

Joined May 8, 2019
30
Hello guys,

I want to press a button of a subject in different intervals. For that i made a test bench of a stepper motor to press a button. I have attached a picture as well. The button operating force is 1.6N where the motor torque is 420 mN.m. The button travel length is 2mm. When i am applying my code on it, it sometimes pressing the button but most of the time its skipping the step. I am running my motor on Full step. I did check upto 1/32 but it doesnt provide enough force as compare to Full stepping. Have a look at my code, Button link, Motor link, Picture. Any leads would be wonderful.

Thanks and regards.

Motor = https://www.amazon.de/MVPOWER-Schrittmo ... SP0BK2CKK3

Button = https://www.mouser.de/datasheet/2/15/SKRT-1370725.pdf

Code =

Code:
from time import sleep
import RPi.GPIO as GPIO

DIR = 2   # Direction GPIO Pin
STEP = 3  # Step GPIO Pin
CW = 1     # Clockwise Rotation
CCW = 0    # Counterclockwise Rotation
SPR = 5   # Steps per Revolution (360 / 1.8)

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.output(DIR, CW)

MODE = (14, 15, 18)   # Microstep Resolution GPIO Pins
GPIO.setup(MODE, GPIO.OUT)
RESOLUTION = {'Full': (0, 0, 0),
'Half': (1, 0, 0),
'1/4': (0, 1, 0),
'1/8': (1, 1, 0),
'1/16': (0, 0, 1),
'1/32': (1, 0, 1)}

GPIO.output(MODE, RESOLUTION['Full'])
step_count = SPR
delay = 0.005

for x in range(step_count):
GPIO.output(DIR, CW)
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
sleep(1)

GPIO.output(DIR, CCW)
for x in range(step_count):
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)

GPIO.cleanup()

#### Attachments

• 112.8 KB Views: 13

#### DNA Robotics

Joined Jun 13, 2014
611
If you send steps too fast, a stepper motor will just vibrate. I would try a lot longer delay. You can shorten it after you get it working.

#### Raj Waghmare

Joined May 8, 2019
30
i did everything but its not working efficiently rather giving sequences of pressing once in a while with same configuration

#### djsfantasi

Joined Apr 11, 2010
7,866
If you are having trouble with the force in half-step mode, you might just not have enough power.

Have you tested the stepper standalone, without pressing a button? Is it still skipping steps?

How long is the distance from the stepper motor axle to the center point of the face in the actuating lever? This data is needed to calculate the force required.

One thought I have, have you considered a high-torque hobby RC servo instead of a stepper motor?

#### Raj Waghmare

Joined May 8, 2019
30
If you are having trouble with the force in half-step mode, you might just not have enough power.

Have you tested the stepper standalone, without pressing a button? Is it still skipping steps?

How long is the distance from the stepper motor axle to the center point of the face in the actuating lever? This data is needed to calculate the force required.

One thought I have, have you considered a high-torque hobby RC servo instead of a stepper motor?
Hello,

The operation distance requires for a button to be pressed is 2mm and i am keeping motor at a random small distance and trying to press the button with adjusting the SPR. The distance of lever from the center axis is 2.2cm. It does press the button sometimes but most of the time to fails to retain its initial position and stuck a step or two in between

#### djsfantasi

Joined Apr 11, 2010
7,866
Ahhh, fails to retain its position. I’d definitely go with a servo.

#### Sensacell

Joined Jun 19, 2012
2,904
To press a button you might consider a simple solenoid?

Joined Feb 20, 2016
3,924
Another vote for a servo. I've used a small servo to push a camera release button for time lapse photography. The servo worked well.
A lot easier to use that a stepper too.

#### CharlesWMcDonald

Joined May 16, 2019
233
Instead of using a lever, you may want to use a cam.

#### Raj Waghmare

Joined May 8, 2019
30

#### DNA Robotics

Joined Jun 13, 2014
611
I just noticed that the "tactile feeling" of that switch means the force to operate it is
1.6 N = .4 lb. = 6.4 oz.
That is a lot. Maybe try a different switch.

#### MrChips

Joined Oct 2, 2009
24,409
Why not bridge the switch contacts with a relay?

#### Raj Waghmare

Joined May 8, 2019
30
I cant because i am testing on a company product so I cant make any soldering to it

#### djsfantasi

Joined Apr 11, 2010
7,866
What language are you coding in on the raspberry Pi? Dendad provided a good source, but there are a lot of others available. Search for “raspberry pi servo”. There’s even a library available.

#### MrChips

Joined Oct 2, 2009
24,409
I cant because i am testing on a company product so I cant make any soldering to it
In that case, use a rotating mechanism with a cam or elliptical disc.

#### Raj Waghmare

Joined May 8, 2019
30
I am coding in python. Lets see if I can find something crucial

#### djsfantasi

Joined Apr 11, 2010
7,866
Great! Since your coding in python, the search criteria “raspberry pi servo” results in python coding examples, a python library for servos and several tutorials.

#### Raj Waghmare

Joined May 8, 2019
30
Hello,

I am getting a problem of jittering in servo while running my code which is affecting the position of a servo by millimetres and thus not pressing the button. Please guide me on how to fix it. Here's is my code

Code:
from gpiozero import Servo
from time import sleep

myGPIO=16

myServo = Servo(myGPIO)

print("Using GPIO16")
print("Using GPIO defaults for the servo class")

while True:
myServo.mid()
print("Set to middle position")
sleep(1)
myServo.min()
print("Set to minimum position")
sleep(1)

GPIO.cleanup()

#### DNA Robotics

Joined Jun 13, 2014
611
How long is sleep(1) ?