Arduino - Rotary Encoder - Servo Motor
In deze tutorial leren we hoe u Arduino programmeert om een servo motor te laten draaien op basis van de outputwaarde van een rotary encoder.
Hardware Vereist
Of u kunt de volgende kits kopen:
| 1 | × | DIYables Sensorkit (30 sensoren/displays) | |
| 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 Servo Motor en Rotary Encoder
Als u nog niet bekend bent met servo motor en rotary encoder (pinout, werking, programmeren, ...), leer er dan meer over in de volgende tutorials:
Bedradingsschema

This image is created using Fritzing. Click to enlarge image
Arduino Code
/*
* 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-rotary-encoder-servo-motor
*/
#include <Servo.h>
#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4
#define SERVO_PIN 9
#define DIRECTION_CW 0 // clockwise direction
#define DIRECTION_CCW 1 // counter-clockwise direction
int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
Servo servo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
// configure encoder pins as inputs
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
// read the initial state of the rotary encoder's CLK pin
prev_CLK_state = digitalRead(CLK_PIN);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(0);
}
void loop() {
// read the current state of the rotary encoder's CLK pin
CLK_state = digitalRead(CLK_PIN);
// If the state of CLK is changed, then pulse occurred
// React to only the rising edge (from LOW to HIGH) to avoid double count
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
// if the DT state is HIGH
// the encoder is rotating in counter-clockwise direction => decrease the counter
if (digitalRead(DT_PIN) == HIGH) {
counter--;
direction = DIRECTION_CCW;
} else {
// the encoder is rotating in clockwise direction => increase the counter
counter++;
direction = DIRECTION_CW;
}
Serial.print("DIRECTION: ");
if (direction == DIRECTION_CW)
Serial.print("Clockwise");
else
Serial.print("Counter-clockwise");
Serial.print(" | COUNTER: ");
Serial.println(counter);
if (counter < 0)
counter = 0;
else if (counter > 180)
counter = 180;
// sets the servo angle according to the counter
servo.write(counter);
}
// save last CLK state
prev_CLK_state = CLK_state;
}
Snel aan de slag
- Verbind Arduino met uw pc via een USB-kabel
- Open de Arduino IDE, selecteer het juiste board en de juiste poort
- Kopieer bovenstaande code en open deze met de Arduino IDE
- Klik op de Upload-knop in de Arduino IDE om de code naar de Arduino te uploaden

- Open de Seriële Monitor
- Draai aan de rotary encoder
- Bekijk de beweging van de servo motor
- Zie het resultaat in de Seriële Monitor
COM6
DIRECTION: Clockwise | COUNTER/ANGLE: 19
DIRECTION: Clockwise | COUNTER/ANGLE: 26
DIRECTION: Clockwise | COUNTER/ANGLE: 34
DIRECTION: Clockwise | COUNTER/ANGLE: 46
DIRECTION: Clockwise | COUNTER/ANGLE: 53
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 46
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 34
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 26
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 16
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 06
Autoscroll
Clear output
9600 baud
Newline
Code Uitleg
Lees de regel-voor-regel uitleg in de commentaarregels van de broncode!
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.