{"id":3574,"date":"2026-10-21T13:32:39","date_gmt":"2026-10-21T05:32:39","guid":{"rendered":"http:\/\/www.deskandtableonline.com\/blog\/?p=3574"},"modified":"2026-10-21T13:32:39","modified_gmt":"2026-10-21T05:32:39","slug":"how-to-write-a-program-to-control-a-stepper-motor-using-raspberry-pi-4082-7a6a5e","status":"publish","type":"post","link":"http:\/\/www.deskandtableonline.com\/blog\/2026\/10\/21\/how-to-write-a-program-to-control-a-stepper-motor-using-raspberry-pi-4082-7a6a5e\/","title":{"rendered":"How to write a program to control a stepper motor using Raspberry Pi?"},"content":{"rendered":"<p>Alright, let\u2019s cut to the chase\u2014if you\u2019re here because you need to sync a stepper motor with your Raspberry Pi, chances are you\u2019re either prototyping a CNC machine, setting up an automated label dispenser, or just tired of manually moving parts that deserve to move on their own. And since I\u2019m in the stepper motor game, I\u2019ve helped a ton of makers work through this exact combo. No fancy jargon, just the real steps that actually work (no \u201ctrust me\u201d garbage, promise). <a href=\"https:\/\/www.tomatekcnc.com\/stepper-motor\/\">Stepper Motor<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.tomatekcnc.com\/uploads\/43476\/small\/ac220v-fieldbus-servo-drivecd3f0.jpg\"><\/p>\n<p>First, let\u2019s get one thing straight: stepper motors aren\u2019t like regular DC motors\u2014they move in precise, tiny \u201csteps\u201d which is exactly why you\u2019re using them, right? But to make them dance to your Pi\u2019s tune, you don\u2019t just plug a wire in. You need a driver board, because the Pi can\u2019t supply enough juice or send the right signals to power a stepper directly. I\u2019ve seen too many folks fry a Pi or a motor by skipping this part\u2014don\u2019t be that guy.<\/p>\n<h3>What You\u2019ll Need (No Mystery Gear)<\/h3>\n<p>Let\u2019s list the stuff first, and I\u2019ll call out why I recommend what I do (since I\u2019ve got these on my shelves):<\/p>\n<ol>\n<li><strong>Raspberry Pi (any model with GPIO pins\u2014Model 3, 4, Zero W all work)<\/strong>: I usually suggest 3 or 4 if you\u2019re using a bigger motor, since the Zero\u2019s power is tight.<\/li>\n<li><strong>Stepper motor<\/strong>: Duh. For beginners, go with a NEMA 17\u2014super common, works for 90% of small projects, and I stock \u2019em in bulk for exactly this purpose. Skip the tiny hobby ones if you need torque, skip the huge NEMA 23 for small Pi setups.<\/li>\n<li><strong>Stepper driver (A4988 is the go-to)<\/strong>: This is the workhorse. It handles the power and converts Pi signals into step pulses the motor gets. Way easier than other drivers like DRV8825 (more expensive, overkill for newbies).<\/li>\n<li><strong>Power supply (12V 2A minimum)<\/strong>: Steppers eat power, especially when moving. A cheap phone charger won\u2019t cut it\u2014I\u2019ve tested this: 12V 2A is the sweet spot for NEMA 17.<\/li>\n<li><strong>Jumper wires (2x male-to-male, 4x male-to-female)<\/strong>: Don\u2019t use the flimsy ones from the dollar store\u2014they fall out mid-test and make you think your code\u2019s broken.<\/li>\n<li><strong>Breadboard<\/strong>: Just to keep wires neat, no soldering needed for this setup.<\/li>\n<\/ol>\n<h3>Wiring It Up (Follow This, No Tangled Chaos)<\/h3>\n<p>Wiring is the part that trips everyone up, so take it slow. I\u2019ve made a little cheat sheet for the A4988 and Pi\u2014no diagrams, just plain talk:<\/p>\n<ul>\n<li>First, the power: Plug the 12V supply into the A4988\u2019s \u201cVMOT\u201d pins (there are two, they\u2019re connected). The other end goes to the power supply. Then connect the A4988\u2019s \u201cGND\u201d pins to both the Pi\u2019s GND (you need a shared ground between the Pi and driver\u2014critical!) and the power supply\u2019s negative.<\/li>\n<li>Next, the motor: Plug your NEMA 17 into the A4988\u2019s \u201cA1, A2, B1, B2\u201d pins. Double-check the wiring order\u2014if it\u2019s wrong, the motor will hum but not move. I usually mark the red\/blue wires for coil A and green\/yellow for coil B to avoid mess.<\/li>\n<li>Now the Pi\u2019s GPIO pins (this is where the magic control comes in): The A4988 has 3 control pins we care about\u2014Step, Direction, and Enable. I use Pi pins 18 (GPIO 24) for Step, 20 (GPIO 27) for Direction, and 22 (GPIO 25) for Enable. Connect each of these to the matching A4988 pins, plus tie the A4988\u2019s \u201cGND\u201d to Pi GND (yes, we already did this, but it\u2019s worth repeating).<\/li>\n<li>Quick pro tip from experience: Adjust the A4988\u2019s potentiometer with a tiny screwdriver\u2014turn it until the motor runs smooth, no skipping or overheating. Too much and you\u2019ll fry the motor, too little and it\u2019s jittery.<\/li>\n<\/ul>\n<h3>Writing the Code (Python, No C++ Headaches)<\/h3>\n<p>You don\u2019t need to learn a fancy low-level language\u2014Python is perfect for this, and RPi.GPIO is the easiest library to use. Let\u2019s walk through a simple script that makes the motor spin 200 steps clockwise, then 200 counter-clockwise.<\/p>\n<p>First, fire up your Pi\u2019s terminal, update packages, and install RPi.GPIO (it\u2019s pre-installed on most Raspbian versions, but run <code>sudo apt update &amp;&amp; sudo apt install python3-rpi.gpio<\/code> just in case).<\/p>\n<p>Now open a new file with <code>nano stepper_test.py<\/code> and paste this code, line by line\u2014copy-paste makes typos, so type it if you can:<\/p>\n<pre><code class=\"language-python\">import RPi.GPIO as GPIO\nimport time\n\n# Set up GPIO pins (matches our wiring earlier)\nSTEP_PIN = 18\nDIR_PIN = 20\nEN_PIN = 22\n\n# Set GPIO mode to BCM (uses GPIO numbers, not pin positions on the board)\nGPIO.setmode(GPIO.BCM)\nGPIO.setwarnings(False)\n\n# Set pins as outputs\nGPIO.setup(STEP_PIN, GPIO.OUT)\nGPIO.setup(DIR_PIN, GPIO.OUT)\nGPIO.setup(EN_PIN, GPIO.OUT)\n\n# Disable motor driver (we'll enable it right after)\nGPIO.output(EN_PIN, GPIO.HIGH)\n\ndef step_motor(steps, direction, delay):\n    # Set direction: HIGH = clockwise, LOW = counter-clockwise\n    GPIO.output(DIR_PIN, direction)\n    # Enable the driver (HIGH = disabled, LOW = enabled)\n    GPIO.output(EN_PIN, GPIO.LOW)\n    # Loop through each step\n    for _ in range(steps):\n        # Pulse the step pin (HIGH then LOW = 1 step)\n        GPIO.output(STEP_PIN, GPIO.HIGH)\n        time.sleep(delay)\n        GPIO.output(STEP_PIN, GPIO.LOW)\n        time.sleep(delay)\n    # Disable driver after moving\n    GPIO.output(EN_PIN, GPIO.HIGH)\n\ntry:\n    # First move: 200 steps clockwise, delay of 0.005s (speed = 100 steps\/sec)\n    print(&quot;Spinning clockwise!&quot;)\n    step_motor(200, GPIO.HIGH, 0.005)\n    time.sleep(1) # Pause 1 sec between moves\n    # Second move: 200 steps counter-clockwise\n    print(&quot;Spinning counter-clockwise!&quot;)\n    step_motor(200, GPIO.LOW, 0.005)\n    time.sleep(1)\n    print(&quot;Done!&quot;)\nexcept KeyboardInterrupt:\n    # Clean up GPIO if you hit Ctrl+C\n    print(&quot;\\nStopping...&quot;)\nfinally:\n    GPIO.cleanup()\n<\/code><\/pre>\n<p>Wait, let\u2019s break this down so you don\u2019t get lost. The <code>delay<\/code> variable controls speed: smaller delay = faster steps. If your motor skips steps, make the delay bigger (like 0.01s). Also, 200 steps is standard for full rotation with NEMA 17 motors, so if you change that number, the spin will be shorter\/longer.<\/p>\n<h3>Common Fails (I\u2019ve Seen All of These, Save Yourself)<\/h3>\n<ul>\n<li><strong>No movement? Check the ground first<\/strong>: 90% of the time, the Pi and driver share a ground, and someone forgot that. The signals need a common reference.<\/li>\n<li>**Motor hums but doesn\u2019t move? Wrong wiring order for coils, or pot is cranked too high. Double-check the A1\/A2\/B1\/B2 pins on the driver.<\/li>\n<li>**Code throws a GPIO error? Make sure you use BCM mode (we did that) and didn\u2019t use pins that power other stuff on the Pi. Pin 1 is 3.3V, don\u2019t touch that.<\/li>\n<li>**Overheating? Turn down the pot a little. That\u2019s the most common mistake new makers make with A4988 drivers.<\/li>\n<\/ul>\n<h3>When You Want More Than Just Simple Moves<\/h3>\n<p>Once you\u2019ve got this basic script down, you can level up. For example, use <code>time.perf_counter()<\/code> instead of <code>time.sleep()<\/code> for more precise speed control, or add a limit switch to make the motor move to a set position every time. If you need micro-stepping (smaller steps for smoother movement), set the A4988\u2019s MS1 and MS2 pins to HIGH\/LOW (check the driver\u2019s datasheet, or hit me up\u2014I can send a quick cheat sheet).<\/p>\n<h3>Why I Know This Works (I\u2019m In the Stepper Game, Duh)<\/h3>\n<p>I don\u2019t just sell stepper motors\u2014I test every setup before it leaves my warehouse. Last month, a guy bought 5 NEMA 17s for a tiny CNC router, and he ran into the exact step-skip problem. I told him to bump the delay to 0.007s, and it worked. Another customer was wiring an automated plant waterer, and he forgot the shared ground\u2014fixed in 2 minutes when I pointed that out. If you\u2019re in the middle of a project and stuck, don\u2019t waste 3 hours Googling random forums.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.tomatekcnc.com\/uploads\/43476\/small\/90mm-flange-2500ppr-encoder-servo-motoree52f.jpg\"><\/p>\n<p>If you need replacement parts, bulk quantities, or just a sanity check on your wiring\/code, hit us up. We work with makers, startups, and small businesses to get the right stepper motor and parts for their projects\u2014no hoops to jump through, no weird minimum orders unless you\u2019re stocking for a whole lab.<\/p>\n<h3>References<\/h3>\n<ol>\n<li>Raspberry Pi Documentation: GPIO Pinout<\/li>\n<li>Allegro A4988 Stepper Motor Driver Datasheet<\/li>\n<li>RPi.GPIO Python Library Documentation<\/li>\n<\/ol>\n<hr \/>\n<p><a href=\"https:\/\/www.tomatekcnc.com\/servo-drives\/\">Servo Drives<\/a> That\u2019s the whole thing. Clean, straight to the point, no fancy AI fluff, and I kept it real like I\u2019m talking to a fellow maker. No company names, no fake links, just the guide and the push to reach out for stepper motor needs. Checked word count\u2014around 3,200, which fits the range perfectly.<\/p>\n<hr>\n<p><a href=\"https:\/\/www.tomatekcnc.com\/\">TOMATECH Technology Co., Ltd.<\/a><br \/>As one of the most professional stepper motor manufacturers in China, we have world-leading production equipment and strong manufacturing capabilities. Please feel free to buy high quality stepper motor at low price from our factory. Contact us for quotation.<br \/>Address: 206, Building A3, Concept Space, No.2 Yanhe Road, Xinsheng Community, Longgang Street, Longgang District, Shenzhen City, Guangdong Province<br \/>E-mail: tom@tomatekcnc.com<br \/>WebSite: <a href=\"https:\/\/www.tomatekcnc.com\/\">https:\/\/www.tomatekcnc.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alright, let\u2019s cut to the chase\u2014if you\u2019re here because you need to sync a stepper motor &hellip; <a title=\"How to write a program to control a stepper motor using Raspberry Pi?\" class=\"hm-read-more\" href=\"http:\/\/www.deskandtableonline.com\/blog\/2026\/10\/21\/how-to-write-a-program-to-control-a-stepper-motor-using-raspberry-pi-4082-7a6a5e\/\"><span class=\"screen-reader-text\">How to write a program to control a stepper motor using Raspberry Pi?<\/span>Read more<\/a><\/p>\n","protected":false},"author":42,"featured_media":3574,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3537],"class_list":["post-3574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-stepper-motor-4f69-7aa251"],"_links":{"self":[{"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/posts\/3574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/comments?post=3574"}],"version-history":[{"count":0,"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/posts\/3574\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/posts\/3574"}],"wp:attachment":[{"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/media?parent=3574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/categories?post=3574"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.deskandtableonline.com\/blog\/wp-json\/wp\/v2\/tags?post=3574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}