Raspberry Pi - Stappenmotor en Limietschakelaar

Leer hoe een Raspberry Pi een stappenmotor kan aansturen met een limietschakelaar, met een DRV8825 driver voor de motor. Specifiek behandelen we:

Hardware Benodigd

1×Raspberry Pi 5
1×Eindschakelaar (KW12-3)
1×Eindschakelaar Module
1×Eindschakelaar (V-153-1C25)
1×Eindschakelaar (V-155-1C25)
1×Eindschakelaar (V-156-1C25)
1×Stepper Motor Nema 17
1×DRV8825 Stepper Motor Driver
1×Expansion Board for DRV8825
1×12V Voeding
1×DC-Stroomaansluiting
1×Verbindingsdraden
1×(Aanbevolen) Schroefklem Block Shield voor Raspberry Pi
1×(Aanbevolen) Raspberry Pi Prototyping Basisplaat & Breadboard Kit

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over Stappenmotor en Limietschakelaar

Als u niet vertrouwd bent met de stappenmotor en de limietschakelaar (inclusief pinout, werking, programmeren, enz.), kunnen de volgende tutorials u helpen:

Tussen de Raspberry Pi en de stappenmotor zit een DRV8825 drivermodule op zijn uitbreidingsbord. De Raspberry Pi heeft er maar twee pinnen voor nodig: een puls op STEP laat de motor één stap bewegen, en DIR kiest de draairichting. De driver begrenst ook de spoelstroom (ingesteld met het kleine knopje op de module), zodat de motor koel blijft, zelfs als hij bij een limietschakelaar wacht. De DIP-schakelaars op het uitbreidingsbord blijven op OFF, wat volledige stap betekent: 200 stappen per omwenteling.

DRV8825 Driver pinout

Bekabelingsschema

Deze handleiding geeft het bekabelingsschema voor twee gevallen: één stappenmotor + één limietschakelaar, één stappenmotor + twee limietschakelaars.

  • Bekabelingsschema tussen Raspberry Pi, stappenmotor en een limietschakelaar
Raspberry Pi Stappenmotor Limietschakelaar DRV8825 bekabelingsschema

Deze afbeelding is gemaakt met Fritzing. Klik om de afbeelding te vergroten.

Raspberry Pi pinnen Verbonden met
GPIO23 (pin 16) STEP (S pin) van het DRV8825 uitbreidingsbord
GPIO24 (pin 18) DIR (S pin) van het DRV8825 uitbreidingsbord
GND G pin van het DRV8825 uitbreidingsbord
GPIO17 (pin 11) limietschakelaar (zijn andere pin gaat naar GND)
  • Bekabelingsschema tussen Raspberry Pi, stappenmotor en twee limietschakelaars
Raspberry Pi Stappenmotor Twee Limietschakelaars DRV8825 bekabelingsschema

Deze afbeelding is gemaakt met Fritzing. Klik om de afbeelding te vergroten.

De tweede limietschakelaar gaat naar GPIO27 (pin 13) en GND. Alle andere draden zijn hetzelfde als hierboven.

※ Notiz:

  • Voed het uitbreidingsbord met de 12V adapter. Een DC power jack verbindt de twee, met zijn draden in de VIN en GND schroefklem. De EN en V pinnen blijven vrij.
  • De bedrading tussen de stappenmotor en het uitbreidingsbord (1A, 1B, 2A, 2B) hangt af van het type stappenmotor. Bestudeer zorgvuldig de tutorial Raspberry Pi - Stappenmotor om te leren hoe u de stappenmotor correct aansluit op de DRV8825 driver.

Bedrading zonder uitbreidingsbord

Wilt u een kleinere opbouw? Laat het uitbreidingsbord weg en gebruik de DRV8825-module los. Er zijn wat meer draden nodig en de pinnen zijn klein, dus controleer elke draad twee keer.

Bedradingsschema Raspberry Pi met DRV8825-module zonder uitbreidingsbord en stappenmotor

Deze afbeelding is gemaakt met Fritzing. Klik om de afbeelding te vergroten.

De versie met twee eindschakelaars:

Bedradingsschema Raspberry Pi met DRV8825-module zonder uitbreidingsbord en stappenmotor en twee eindschakelaars

Deze afbeelding is gemaakt met Fritzing. Klik om de afbeelding te vergroten.

Om uw bekabelingsopstelling te vereenvoudigen en te organiseren, raden we het gebruik van een Schroevenklemaansluiting-shield voor Raspberry Pi aan. Deze shield zorgt voor veiligere en beter beheerbare verbindingen, zoals hieronder weergegeven:

Raspberry Pi Schroevenklemaansluiting-Shield
Pin van de DRV8825-module Aansluiten op
STEP Raspberry Pi GPIO23
DIR Raspberry Pi GPIO24
GND (naast FAULT) Raspberry Pi GND
VMOT + van de 12V-adapter
GND (naast VMOT) - van de 12V-adapter
2B en 1B één spoel van de stappenmotor
1A en 2A de andere spoel van de stappenmotor
EN, M0, M1, M2, RST, SLP, FAULT niet aangesloten

※ Notiz:

  • De motorvoeding heeft hier een condensator van 100uF nodig (25V of meer), van VMOT naar de GND ernaast. Let op de polariteit: + gaat naar VMOT.
  • Helemaal geen beweging? Voeg een kort draadje toe van RST naar SLP.

Raspberry Pi Code - Stappenmotor Stoppen met een Limietschakelaar

Met de volgende code draait een stappenmotor continu. Hij stopt direct wanneer een limietschakelaar wordt aangeraakt, en beweegt weer verder als de limietschakelaar wordt losgelaten.

Snel Starten

  • Zorg dat u Raspbian of een ander met de Raspberry Pi compatibel besturingssysteem op uw Pi heeft geïnstalleerd.
  • Zorg dat uw Raspberry Pi verbonden is met hetzelfde lokale netwerk als uw pc.
  • Zorg dat uw Raspberry Pi verbonden is met internet als u bibliotheken moet installeren.
  • Als dit de eerste keer is dat u een Raspberry Pi gebruikt, zie hoe u de Raspberry Pi instelt
  • Verbind uw pc met de Raspberry Pi via SSH, met de ingebouwde SSH-client op Linux en macOS of PuTTY op Windows. Zie hoe u uw pc via SSH met de Raspberry Pi verbindt.
  • Zorg dat de RPi.GPIO bibliotheek geïnstalleerd is. Zo niet, installeer deze dan met het volgende commando:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python-scriptbestand drv8825_stepper_limit_switch_stop.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/tutorials/raspberry-pi/raspberry-pi-stepper-motor-limit-switch import RPi.GPIO as GPIO import time # DRV8825 expansion board and limit switch (BCM numbering) STEP_PIN = 23 # GPIO23 (pin 16) -> STEP (S pin) DIR_PIN = 24 # GPIO24 (pin 18) -> DIR (S pin) LIMIT_SWITCH_PIN = 17 # GPIO17 (pin 11) -> limit switch, other pin to GND # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(STEP_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(DIR_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(LIMIT_SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Define constants (full step, all DIP switches OFF) DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Function to move the stepper motor one step def one_step(delay): GPIO.output(STEP_PIN, GPIO.HIGH) time.sleep(delay) GPIO.output(STEP_PIN, GPIO.LOW) time.sleep(delay) # Function to check the limit switch status def is_limit_switch_pressed(): return GPIO.input(LIMIT_SWITCH_PIN) == GPIO.LOW try: # Set the delay between STEP edges (smaller = faster) delay = 0.002 # One direction only (clockwise) GPIO.output(DIR_PIN, GPIO.HIGH) while True: # Move only while the limit switch is not touched if not is_limit_switch_pressed(): one_step(delay) except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Sla het bestand op en start het Python-script door het volgende commando in de terminal uit te voeren:
python3 drv8825_stepper_limit_switch_stop.py
  • Als de bekabeling correct is, zou de motor rechtsom moeten draaien.
  • Wanneer de limietschakelaar wordt aangeraakt, zou de motor direct moeten stoppen.
  • Als de limietschakelaar daarna wordt losgelaten, draait de motor weer.

Het script draait continu in een oneindige lus totdat u Ctrl + C indrukt in de terminal.

Door de waarde van de variabele delay in de code te veranderen, kunt u de snelheid van de stappenmotor veranderen.

Code Uitleg

Bekijk de regel-voor-regel uitleg in de commentaren van de broncode!

Raspberry Pi Code - Draairichting Stappenmotor Veranderen met een Limietschakelaar

Een stappenmotor draait continu, en zijn draairichting verandert wanneer een limietschakelaar wordt aangeraakt.

Snel Starten

  • Maak een Python-scriptbestand drv8825_stepper_limit_switch_change_direction.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/tutorials/raspberry-pi/raspberry-pi-stepper-motor-limit-switch import RPi.GPIO as GPIO import time # DRV8825 expansion board and limit switch (BCM numbering) STEP_PIN = 23 # GPIO23 (pin 16) -> STEP (S pin) DIR_PIN = 24 # GPIO24 (pin 18) -> DIR (S pin) LIMIT_SWITCH_PIN = 17 # GPIO17 (pin 11) -> limit switch, other pin to GND # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(STEP_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(DIR_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(LIMIT_SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Define constants (full step, all DIP switches OFF) DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Global variable to store the previous state of the limit switch prev_limit_switch_state = GPIO.HIGH # Function to move the stepper motor one step in the given direction def one_step(direction, delay): # DIR HIGH = forward (clockwise), DIR LOW = backward GPIO.output(DIR_PIN, GPIO.HIGH if direction == 'forward' else GPIO.LOW) GPIO.output(STEP_PIN, GPIO.HIGH) time.sleep(delay) GPIO.output(STEP_PIN, GPIO.LOW) time.sleep(delay) try: # Set the delay between STEP edges (smaller = faster) delay = 0.002 # Set the initial direction direction = 'forward' while True: # Check if the limit switch state changes limit_switch_state = GPIO.input(LIMIT_SWITCH_PIN) # Change direction if the limit switch changes from HIGH to LOW if limit_switch_state == GPIO.LOW and prev_limit_switch_state == GPIO.HIGH: direction = 'backward' if direction == 'forward' else 'forward' # Move the stepper motor based on the direction one_step(direction, delay) # Update the previous state of the limit switch prev_limit_switch_state = limit_switch_state except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Sla het bestand op en start het Python-script door het volgende commando in de terminal uit te voeren:
python3 drv8825_stepper_limit_switch_change_direction.py
  • Als de bekabeling correct is, zou de motor rechtsom moeten draaien.
  • Wanneer u de limietschakelaar aanraakt, verandert de draairichting van de stappenmotor naar linksom.
  • Raak de limietschakelaar opnieuw aan en de draairichting van de stappenmotor gaat terug naar rechtsom.

Raspberry Pi Code - Draairichting Stappenmotor Veranderen met twee Limietschakelaars

Laten we de code bekijken die een stappenmotor continu laat draaien, en de draairichting van de motor omdraait wanneer één van de twee limietschakelaars wordt aangeraakt.

Snel Starten

  • Maak een Python-scriptbestand drv8825_stepper_two_limit_switch_change_direction.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/tutorials/raspberry-pi/raspberry-pi-stepper-motor-limit-switch import RPi.GPIO as GPIO import time # DRV8825 expansion board and limit switches (BCM numbering) STEP_PIN = 23 # GPIO23 (pin 16) -> STEP (S pin) DIR_PIN = 24 # GPIO24 (pin 18) -> DIR (S pin) LIMIT_SWITCH1_PIN = 17 # GPIO17 (pin 11) -> limit switch 1, other pin to GND LIMIT_SWITCH2_PIN = 27 # GPIO27 (pin 13) -> limit switch 2, other pin to GND # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(STEP_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(DIR_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(LIMIT_SWITCH1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(LIMIT_SWITCH2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Define constants (full step, all DIP switches OFF) DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Global variables to store the previous state of the limit switches prev_limit_switch_1 = GPIO.HIGH prev_limit_switch_2 = GPIO.HIGH # Function to move the stepper motor one step in the given direction def one_step(direction, delay): # DIR HIGH = forward (clockwise), DIR LOW = backward GPIO.output(DIR_PIN, GPIO.HIGH if direction == 'forward' else GPIO.LOW) GPIO.output(STEP_PIN, GPIO.HIGH) time.sleep(delay) GPIO.output(STEP_PIN, GPIO.LOW) time.sleep(delay) try: # Set the delay between STEP edges (smaller = faster) delay = 0.002 # Set the initial direction direction = 'forward' while True: # Check if limit switch 1 state changes limit_switch_1 = GPIO.input(LIMIT_SWITCH1_PIN) if limit_switch_1 == GPIO.LOW and prev_limit_switch_1 == GPIO.HIGH: direction = 'backward' # Check if limit switch 2 state changes limit_switch_2 = GPIO.input(LIMIT_SWITCH2_PIN) if limit_switch_2 == GPIO.LOW and prev_limit_switch_2 == GPIO.HIGH: direction = 'forward' # Move the stepper motor based on the direction one_step(direction, delay) # Update the previous states of the limit switches prev_limit_switch_1 = limit_switch_1 prev_limit_switch_2 = limit_switch_2 except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Sla het bestand op en start het Python-script door het volgende commando in de terminal uit te voeren:
python3 drv8825_stepper_two_limit_switch_change_direction.py

Het script draait continu in een oneindige lus totdat u Ctrl + C indrukt in de terminal.

  • Als de bekabeling correct is, zou de motor rechtsom moeten draaien.
  • Wanneer u limietschakelaar 1 aanraakt, wordt de draairichting van de stappenmotor omgedraaid naar linksom.
  • Als u limietschakelaar 2 aanraakt, draait de stappenmotor weer rechtsom.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!