Arduino - DC Motor - Eindschakelaar

In deze Arduino-handleiding leert u hoe u een Arduino gebruikt om een DC motor te besturen met een eindschakelaar (limit switch) en de L298N driver. Concreet leert u:

Over DC Motor en Eindschakelaar

Als u nog niet bekend bent met DC motor en eindschakelaar (pinout, werking, programmeren ...), bekijk dan de volgende tutorials:

Aansluitschema

Deze handleiding bevat Arduino code voor twee situaties: één DC motor + één eindschakelaar, én één DC motor + twee eindschakelaars.

  • Aansluitschema voor de DC motor en één eindschakelaar
Arduino DC motor en eindschakelaar aansluitdiagram

This image is created using Fritzing. Click to enlarge image

  • Aansluitschema voor de DC motor en twee eindschakelaars
Arduino DC motor en twee eindschakelaars aansluitdiagram

This image is created using Fritzing. Click to enlarge image

Arduino Code - DC Motor Stoppen met een Eindschakelaar

De onderstaande code laat een DC motor continu draaien en stopt deze direct zodra een eindschakelaar wordt ingedrukt

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-dc-motor-limit-switch */ #include <ezButton.h> #define ENA_PIN 7 // The Arduino pin connected to the EN1 pin L298N #define IN1_PIN 6 // The Arduino pin connected to the IN1 pin L298N #define IN2_PIN 5 // The Arduino pin connected to the IN2 pin L298N ezButton limitSwitch(A1); // create ezButton object that attach to pin A1 void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds // initialize digital pins as outputs. pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); // max speed digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); digitalWrite(IN1_PIN, LOW); // stop motor digitalWrite(IN2_PIN, LOW); // stop motor } }

Snelle Stappen

  • Verbind de Arduino met de pc via de USB kabel
  • Open de Arduino IDE, selecteer het juiste board en de juiste poort
  • Ga naar het Libraries-icoon in de linker balk van de Arduino IDE
  • Zoek op “ezButton”, en vind de knopbibliotheek van ArduinoGetStarted.com
  • Klik op de Installeren-knop om de ezButton bibliotheek te installeren
Arduino knop bibliotheek
  • Kopieer de bovengenoemde code en open deze in de Arduino IDE
  • Klik op de Upload-knop in de Arduino IDE om de code te uploaden naar de Arduino
  • Als de bedrading correct is, draait de motor in klokwijzerzin
  • Druk de eindschakelaar in
  • U zult zien dat de motor direct stopt
  • Het resultaat op de Seriële Monitor ziet er als volgt uit:
COM6
Send
The limit switch: TOUCHED The DC motor is STOPPED The DC motor is STOPPED The DC motor is STOPPED The DC motor is STOPPED
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Uitleg

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

Arduino Code - Draairichting DC Motor Veranderen met een Eindschakelaar

De onderstaande code laat een DC motor continu draaien en verandert de draairichting zodra een eindschakelaar wordt ingedrukt

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 // constants won't change #define ENA_PIN 7 // The Arduino pin connected to the EN1 pin L298N #define IN1_PIN 6 // The Arduino pin connected to the IN1 pin L298N #define IN2_PIN 5 // The Arduino pin connected to the IN2 pin L298N ezButton limitSwitch(A1); // create ezButton object that attach to pin A1 int direction = DIRECTION_CW; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds // initialize digital pins as outputs. pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); // max speed digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } 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); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise } } }

Snelle Stappen

  • Kopieer de bovengenoemde code en open deze in de Arduino IDE
  • Klik op de Upload-knop om de code naar de Arduino te uploaden
  • Indien de bedrading correct is, draait de motor in klokwijzerzin
  • Druk de eindschakelaar in
  • U zult zien dat de draairichting van de DC motor verandert naar tegen de klok in
  • Druk de eindschakelaar opnieuw in
  • U zult zien dat de draairichting verandert naar klokwijzerzin
  • Het resultaat op de Seriële Monitor ziet er als volgt uit:
COM6
Send
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Code - Draairichting DC Motor Veranderen met twee Eindschakelaars

De onderstaande code laat een DC motor continu draaien en verandert de draairichting zodra één van twee eindschakelaars wordt ingedrukt

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-dc-motor-limit-switch */ #include <ezButton.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define ENA_PIN 7 // The Arduino pin connected to the EN1 pin L298N #define IN1_PIN 6 // The Arduino pin connected to the IN1 pin L298N #define IN2_PIN 5 // The Arduino pin connected to the IN2 pin L298N ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0 ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1 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(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); // max speed digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } 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); // control motor A spins clockwise digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise } else { Serial.println(F("ANTI-CLOCKWISE")); digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise } prev_direction = direction; } }

Snelle Stappen

  • Kopieer de bovengenoemde code en open deze in de Arduino IDE
  • Klik op de Upload-knop om de code naar de Arduino te uploaden
  • Indien de bedrading correct is, draait de motor in klokwijzerzin
  • Druk eindschakelaar 1 in
  • U zult zien dat de draairichting van de DC motor verandert naar tegen de klok in
  • Druk eindschakelaar 2 in
  • U zult zien dat de draairichting verandert naar klokwijzerzin
  • Het resultaat op de Seriële Monitor ziet er als volgt uit:
COM6
Send
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten en helpt u sneller aan de slag te gaan.

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