ESP32 - DC Motor - Eindschakelaar

In deze ESP32 tutorial bekijken we hoe de ESP32 een DC motor aanstuurt met een eindschakelaar en een DRV8833 motor driver. We behandelen specifiek de volgende onderdelen:

Over DC Motor en Eindschakelaar

Als u nog niets weet over DC motoren en eindschakelaars (pinout, werking, programmeren ...), leer er meer over in de volgende tutorials:

De DC motor krijgt zijn voeding via een DRV8833 module. Dit kleine bord heeft MOSFET-uitgangen, dus de 5V motor krijgt bijna de volle 5V, en het bord blijft koel. De ingangen accepteren de 3.3V signalen van de ESP32. We gebruiken alleen kanaal A van de module: IN1 en IN2 krijgen de signalen van de ESP32, en OUT1 en OUT2 gaan naar de motor. De SLEEP pin (EEP op sommige borden) wordt in de chip LOW getrokken, waardoor de driver gaat slapen, dus hij krijgt een draad naar de 3.3V pin van de ESP32. De FAULT pin blijft vrij.

DRV8833 Motor Driver Pinout

Met de DRV8833 stelt de ESP32 de draairichting van de motor in met maar twee pinnen:

  • IN1 HIGH, IN2 LOW: de motor draait rechtsom
  • IN1 LOW, IN2 HIGH: de motor draait linksom
  • IN1 HIGH, IN2 HIGH: de motor remt en stopt snel
  • IN1 LOW, IN2 LOW: de motor loopt vanzelf uit tot stilstand

Bedradingsschema

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

  • Bedradingsschema tussen de DC motor en een eindschakelaar
ESP32 DC Motor DRV8833 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 DC motor en twee eindschakelaars
ESP32 DC Motor DRV8833 en Twee Eindschakelaars bedradingsschema

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

Onderdeel Pin Verbinden met
DRV8833 Module IN1 GPIO27 van ESP32
DRV8833 Module IN2 GPIO26 van ESP32
DRV8833 Module GND GND van ESP32 en negatieve (-) van de 5V adapter
DRV8833 Module VCC positieve (+) van de 5V adapter
DRV8833 Module SLEEP (EEP) 3.3V van ESP32
DRV8833 Module OUT1, OUT2 de twee draden van de DC motor
Eindschakelaar 1 twee draden GPIO32 en GND van ESP32
Eindschakelaar 2 twee draden GPIO33 en GND van ESP32

De tweede eindschakelaar is alleen nodig voor het laatste voorbeeld. De 5V adapter voedt alleen de motor. De ESP32 krijgt zijn voeding van de USB-kabel. Blijft de motor stilstaan? Controleer dan eerst de SLEEP draad.

ESP32 Code - DC Motor Stoppen met een Eindschakelaar

De onderstaande code laat een DC motor continu draaien en stopt hem direct 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-dc-motor-limit-switch */ #include <ezButton.h> #define IN1_PIN 27 // ESP32 pin GPIO27 connected to the IN1 pin of the DRV8833 #define IN2_PIN 26 // ESP32 pin GPIO26 connected to the IN2 pin of the DRV8833 ezButton limitSwitch(32); // create ezButton object that attach to pin GPIO32 void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); // the DRV8833 has no enable pin: IN1 HIGH and IN2 LOW = full speed, clockwise digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); // brake: IN1 and IN2 both HIGH stop the motor fast digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH); Serial.println(F("The DC motor is STOPPED")); } }

Snelle Stappen

  • Als dit uw eerste keer is met ESP32, zie hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de bedrading volgens bovenstaande afbeelding.
  • Verbind het ESP32 board met uw PC via een USB Type-C kabel
  • Open Arduino IDE op uw PC.
  • Selecteer het juiste ESP32 board (bijv. ESP32 Dev Module) en de juiste COM-poort.
  • Verbind ESP32 met PC via USB-kabel
  • Open 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
  • Kopieer bovenstaande code en open deze met 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 motor direct stopt
  • Het resultaat in de Seriële Monitor ziet er zo 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 DC 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 van DC Motor Veranderen met een Eindschakelaar

De onderstaande code laat een DC motor continu 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-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define IN1_PIN 27 // ESP32 pin GPIO27 connected to the IN1 pin of the DRV8833 #define IN2_PIN 26 // ESP32 pin GPIO26 connected to the IN2 pin of the DRV8833 ezButton limitSwitch(32); // create ezButton object that attach to pin GPIO32 int direction = DIRECTION_CW; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); // full speed, clockwise: IN1 HIGH, IN2 LOW digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); } 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")); digitalWrite(IN1_PIN, HIGH); // IN1 HIGH, IN2 LOW: clockwise digitalWrite(IN2_PIN, LOW); } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // IN1 LOW, IN2 HIGH: anti-clockwise digitalWrite(IN2_PIN, HIGH); } } }

Snelle Stappen

  • Als dit uw eerste keer is met ESP32, zie hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de bedrading volgens bovenstaande afbeelding.
  • Verbind het ESP32 board met uw PC via een USB Type-C kabel
  • Open Arduino IDE op uw PC.
  • Selecteer het juiste ESP32 board (bijv. ESP32 Dev Module) en de juiste COM-poort.
  • Kopieer bovenstaande code en open deze met 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 DC motor verandert naar linksom
  • Raak de eindschakelaar opnieuw aan
  • U zult zien dat de draairichting van de DC motor verandert naar rechtsom
  • Het resultaat in de Seriële Monitor ziet er zo 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 van DC Motor Veranderen met twee Eindschakelaars

De onderstaande code laat een DC motor continu 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-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define IN1_PIN 27 // ESP32 pin GPIO27 connected to the IN1 pin of the DRV8833 #define IN2_PIN 26 // ESP32 pin GPIO26 connected to the IN2 pin of the DRV8833 ezButton limitSwitch_1(32); // create ezButton object that attach to pin GPIO32 ezButton limitSwitch_2(33); // create ezButton object that attach to pin GPIO33 int direction = DIRECTION_CW; int prev_direction = DIRECTION_CW; 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 // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); // full speed, clockwise: IN1 HIGH, IN2 LOW digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); } void loop() { limitSwitch_1.loop(); // MUST call the loop() function first limitSwitch_2.loop(); // MUST call the loop() function first if (limitSwitch_1.isPressed()) { direction *= -1; // change direction Serial.println(F("The limit switch 1: TOUCHED")); } if (limitSwitch_2.isPressed()) { direction *= -1; // change direction Serial.println(F("The limit switch 2: TOUCHED")); } if (prev_direction != direction) { Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) { Serial.println(F("CLOCKWISE")); digitalWrite(IN1_PIN, HIGH); // IN1 HIGH, IN2 LOW: clockwise digitalWrite(IN2_PIN, LOW); } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // IN1 LOW, IN2 HIGH: anti-clockwise digitalWrite(IN2_PIN, HIGH); } prev_direction = direction; } }

Snelle Stappen

  • Als dit uw eerste keer is met ESP32, zie hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de bedrading volgens bovenstaande afbeelding.
  • Verbind het ESP32 board met uw PC via een USB Type-C kabel
  • Open Arduino IDE op uw PC.
  • Selecteer het juiste ESP32 board (bijv. ESP32 Dev Module) en de juiste COM-poort.
  • Kopieer bovenstaande code en open deze met 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 DC motor verandert naar linksom
  • Raak eindschakelaar 2 aan
  • U zult zien dat de draairichting van de DC motor verandert naar rechtsom
  • Het resultaat in de Seriële Monitor ziet er zo 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!