Using Raspberry Pi to control stepper motor--python language

Source: Internet
Author: User

The advantage of a stepper motor is that it can be precisely positioned to turn "one step" at a time, either forward or reverse, and can also be rotated continuously.


#!/usr/bin/env python######################################################### #File name:stepmotor.py# Author: Jason dai# date:2015/01/26######################################################## #import RPi.GPIO as GPIOimport Tim eIN1 = one # pin11in2 = 12in3 = 13in4 = 15def setstep (W1, W2, W3, W4): Gpio.output (IN1, W1) gpio.output (IN2, W2) GPIO.OUTPU T (IN3, W3) gpio.output (IN4, W4) def stop (): SetStep (0, 0, 0, 0) def forward (delay, steps): For I in range (0, steps): SetStep (1 , 0, 0, 0) time.sleep (delay) setstep (0, 1, 0, 0) time.sleep (delay) setstep (0, 0, 1, 0) time.sleep (delay) setstep (0, 0, 0, 1) time . Sleep (delay) def backward (delay, steps): For I in range (0, steps): SetStep (0, 0, 0, 1) time.sleep (delay) setstep (0, 0, 1, 0) Time.sleep (delay) setstep (0, 1, 0, 0) time.sleep (delay) setstep (1, 0, 0, 0) time.sleep (delay) def setup (): Gpio.setwarnings (False) Gpio.setmode (GPIO). BOARD) # Numbers GPIOs by physical Locationgpio.setup (IN1, GPIO. Out) # Set Pin ' s mode is Outputgpio.setup (IN2, GPIO. Out) GPIO. Setup (IN3, GPIO. Out) Gpio.setup (IN4, GPIO.                 def loop (): While True:print "Backward ..." backward (0.003, up to) # Steps---angleprint "Stop ..." Stop () # Stoptime.sleep (3) # sleep 3sprint "forward ..." forward (0.005) print "Stop ..." Stop () Time.sleep (3) def Destroy (): Gpio.cleanup () # Release Resourceif __name__ = = ' __main__ ': # program start from Heresetup () Try: Loop () except Keyboardinterrupt: # when ' CTRL + C ' is pressed, the child program Destroy () 'll be Executed.destroy ()

To run the program:

Python stepmotor.py




Using Raspberry Pi to control stepper motor--python language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.