ESP32 - Stappenmotor en Endschakelaar

In deze ESP32 tutorial bekijken we hoe u de ESP32, een eindschakelaar (limit switch), de DRV8825 stappenmotor driver en een stappenmotor gebruikt. Specifiek behandelen we:

Met deze onderwerpen kunt u gecontroleerde en veelzijdige bewegingstoepassingen maken met uw ESP32.

Over Stappenmotor en Eindschakelaar

Als u niet bekend bent met de stappenmotor en de eindschakelaar (pinout, werking, programmeren ...), leer er meer over in de volgende tutorials:

Dit project gebruikt een DRV8825 module, gemonteerd op zijn uitbreidingsbord, als driver voor de stappenmotor. De ESP32 heeft er maar twee pinnen voor nodig: STEP (één puls = één stap) en DIR (de draairichting). De driver houdt de spoelstroom op een veilig niveau, zodat de NEMA 17 motor koel blijft. Alle drie de DIP-schakelaars op het uitbreidingsbord staan op OFF, dus de motor draait in volledige stap: 200 stappen voor één omwenteling.

DRV8825 Module Pinout

Bedradingsschema

Deze tutorial geeft de ESP32 code voor twee situaties: één stappenmotor + één eindschakelaar, één stappenmotor + twee eindschakelaars.

  • Bedradingsschema tussen de stappenmotor en een eindschakelaar
ESP32 Stappenmotor DRV8825 en Eindschakelaar bedradingsschema

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

Als u niet weet hoe u ESP32 en andere componenten van stroom moet voorzien, vindt u instructies in de volgende tutorial: Hoe ESP32 van stroom te voorzien.

  • Bedradingsschema tussen de stappenmotor en twee eindschakelaars
ESP32 Stappenmotor DRV8825 en Twee Eindschakelaars bedradingsschema

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

Onderdeel Pin Verbinden met
DRV8825 Uitbreidingsbord STEP (S pin) GPIO18 van ESP32
DRV8825 Uitbreidingsbord DIR (S pin) GPIO19 van ESP32
DRV8825 Uitbreidingsbord G pin GND van ESP32
DRV8825 Uitbreidingsbord VIN, GND schroefklem + en - van de 12V adapter (via de DC power jack)
DRV8825 Uitbreidingsbord 1A, 1B één spoel van de stappenmotor
DRV8825 Uitbreidingsbord 2A, 2B de andere spoel van de stappenmotor
Eindschakelaar 1 twee draden GPIO25 en GND van ESP32
Eindschakelaar 2 twee draden GPIO26 en GND van ESP32

De EN pin en de V pinnen van het uitbreidingsbord blijven vrij. Eindschakelaar 2 is alleen voor het laatste voorbeeld.

※ Notiz:

Afhankelijk van de stappenmotor kunnen de draadkleuren van de twee spoelen verschillen. Bekijk deze tutorial ESP32 - Stappenmotor goed om te zien hoe u de spoelen vindt en ze aansluit op 1A/1B en 2A/2B van het uitbreidingsbord.

Bedrading zonder uitbreidingsbord

U kunt het uitbreidingsbord ook weglaten en de losse DRV8825-module aansluiten. De ESP32 kan de module zelf aansturen, pin voor pin bedraad zoals hieronder.

Bedradingsschema ESP32 met DRV8825-module zonder uitbreidingsbord en stappenmotor

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

Hier is de opstelling met twee eindschakelaars:

Bedradingsschema ESP32 met DRV8825-module zonder uitbreidingsbord en stappenmotor en twee eindschakelaars

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

Pin van de DRV8825-module Aansluiten op
STEP ESP32 GPIO18
DIR ESP32 GPIO19
GND (naast FAULT) ESP32 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:

  • Een condensator van 100uF tussen VMOT en GND houdt de 12V-voeding stabiel. Gebruik er een van minstens 25V en houd de pootjes kort.
  • Als er niets gebeurt wanneer de code draait, verbind dan RST en SLP met elkaar.

ESP32 Code - Stappenmotor Stoppen met een Eindschakelaar

Er zijn meerdere manieren om een stappenmotor te stoppen:

  • De functie stepper.stop() aanroepen: Hiermee stopt de stappenmotor NIET direct, maar geleidelijk
  • De functie stepper.run() NIET aanroepen: Hiermee stopt de stappenmotor direct

De onderstaande code laat een stappenmotor oneindig draaien en direct stoppen wanneer een eindschakelaar wordt aangeraakt

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-stepper-motor-and-limit-switch */ // ESP32 + DRV8825 stepper driver (on its expansion board) + limit switch // Full step (all DIP switches OFF): 200 steps per revolution #include <ezButton.h> #include <AccelStepper.h> #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(25); // ESP32 pin GPIO25 connected to the limit switch #define STEP_PIN 18 // ESP32 pin GPIO18 connected to the STEP pin of the DRV8825 #define DIR_PIN 19 // ESP32 pin GPIO19 connected to the DIR pin of the DRV8825 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // DRIVER mode for the DRV8825 bool isStopped = false; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position stepper.moveTo(MAX_POSITION); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); isStopped = true; } if (isStopped == false) { // without this part, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(MAX_POSITION); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function } else { // without calling stepper.run() function, motor stops immediately // NOTE: stepper.stop() function does NOT stops motor immediately Serial.println(F("The stepper motor is STOPPED")); } }

Snelle Stappen

  • Als dit de eerste keer is dat u ESP32 gebruikt, bekijk dan hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de bedrading zoals op bovenstaande afbeelding.
  • Verbind het ESP32 board met uw PC via een micro USB-kabel
  • Open Arduino IDE op uw PC.
  • Selecteer het juiste ESP32 board (bijvoorbeeld ESP32 Dev Module) en de juiste COM-poort.
  • Verbind ESP32 met de PC via USB-kabel
  • Open de Arduino IDE, selecteer het juiste board en de juiste poort
  • Klik op het Libraries icoon in de linkerbalk van de Arduino IDE.
  • Zoek op “ezButton” en vind de button library van ArduinoGetStarted.com
  • Klik op de Install knop om de ezButton library te installeren.
ESP32 button library
  • Zoek op “AccelStepper” en vind de AccelStepper library van Mike McCauley
  • Klik op de Install knop om de AccelStepper library te installeren.
ESP32 AccelStepper library
  • Kopieer bovenstaande code en open deze in Arduino IDE
  • Klik op de Upload knop in Arduino IDE om de code naar de ESP32 te uploaden
  • Steek de 12V adapter in om de stappenmotor van stroom te voorzien.
  • Als de bedrading correct is, ziet u de motor rechtsom draaien.
  • Raak de eindschakelaar aan
  • U zult zien dat de motor direct stopt
  • Het resultaat in de Seriële Monitor ziet er als volgt uit
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Code-uitleg

Lees de regel-voor-regel uitleg in de commentaarregels van de code!

ESP32 Code - Draairichting Stappenmotor Veranderen met een Eindschakelaar

De onderstaande code laat een stappenmotor oneindig draaien en verandert zijn draairichting wanneer een eindschakelaar wordt aangeraakt

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-stepper-motor-and-limit-switch */ // ESP32 + DRV8825 stepper driver (on its expansion board) + limit switch // Full step (all DIP switches OFF): 200 steps per revolution #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(25); // ESP32 pin GPIO25 connected to the limit switch #define STEP_PIN 18 // ESP32 pin GPIO18 connected to the STEP pin of the DRV8825 #define DIR_PIN 19 // ESP32 pin GPIO19 connected to the DIR pin of the DRV8825 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // DRIVER mode for the DRV8825 int direction = DIRECTION_CW; long targetPos = 0; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position targetPos = direction * MAX_POSITION; stepper.moveTo(targetPos); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) Serial.println(F("CLOCKWISE")); else Serial.println(F("ANTI-CLOCKWISE")); targetPos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(targetPos); } // without this part, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(targetPos); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function }

Snelle Stappen

  • Kopieer bovenstaande code en open deze in Arduino IDE
  • Klik op de Upload knop in Arduino IDE om de code naar de ESP32 te uploaden
  • Als de bedrading correct is, ziet u de motor rechtsom draaien.
  • Raak de eindschakelaar aan
  • U zult zien dat de draairichting van de stappenmotor verandert naar linksom
  • Raak de eindschakelaar opnieuw aan
  • U zult zien dat de draairichting van de stappenmotor verandert naar rechtsom
  • Het resultaat in de Seriële Monitor ziet er als volgt uit
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
ESP32 Dev Module on COM15
2

ESP32 Code - Draairichting Stappenmotor Veranderen met twee Eindschakelaars

De onderstaande code laat een stappenmotor oneindig draaien en verandert zijn draairichting wanneer één van de twee eindschakelaars wordt aangeraakt

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-stepper-motor-and-limit-switch */ // ESP32 + DRV8825 stepper driver (on its expansion board) + two limit switches // Full step (all DIP switches OFF): 200 steps per revolution #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define STATE_CHANGE_DIR 1 #define STATE_MOVE 2 #define STATE_MOVING 3 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch_1(25); // ESP32 pin GPIO25 connected to the limit switch 1 ezButton limitSwitch_2(26); // ESP32 pin GPIO26 connected to the limit switch 2 #define STEP_PIN 18 // ESP32 pin GPIO18 connected to the STEP pin of the DRV8825 #define DIR_PIN 19 // ESP32 pin GPIO19 connected to the DIR pin of the DRV8825 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // DRIVER mode for the DRV8825 int stepperState = STATE_MOVE; int direction = DIRECTION_CW; long targetPos = 0; void setup() { Serial.begin(9600); limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position } void loop() { limitSwitch_1.loop(); // MUST call the loop() function first limitSwitch_2.loop(); // MUST call the loop() function first if (limitSwitch_1.isPressed()) { stepperState = STATE_CHANGE_DIR; Serial.println(F("The limit switch 1: TOUCHED")); } if (limitSwitch_2.isPressed()) { stepperState = STATE_CHANGE_DIR; Serial.println(F("The limit switch 2: TOUCHED")); } switch (stepperState) { case STATE_CHANGE_DIR: direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) Serial.println(F("CLOCKWISE")); else Serial.println(F("ANTI-CLOCKWISE")); stepperState = STATE_MOVE; // after changing direction, go to the next state to move the motor break; case STATE_MOVE: targetPos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(targetPos); stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity break; case STATE_MOVING: // without this state, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(targetPos); // move the motor to maximum position again } break; } stepper.run(); // MUST be called in loop() function }

Snelle Stappen

  • Kopieer bovenstaande code en open deze in Arduino IDE
  • Klik op de Upload knop in Arduino IDE om de code naar de ESP32 te uploaden
  • Als de bedrading correct is, ziet u de motor rechtsom draaien.
  • Raak eindschakelaar 1 aan
  • U zult zien dat de draairichting van de stappenmotor verandert naar linksom
  • Raak eindschakelaar 2 aan
  • U zult zien dat de draairichting van de stappenmotor verandert naar rechtsom
  • Het resultaat in de Seriële Monitor ziet er als volgt uit
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ 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!