ESP32 - Rotary Encoder Servo Motor

In deze tutorial gaan we leren hoe u de ESP32 en een rotary encoder programmeert om de hoek van een servo motor te regelen.

Hardware Benodigd

1×ESP32 ESP-WROOM-32 Ontwikkelingsmodule
1×USB-kabel Type-C
1×Servomotor
1×Draaiencoder
1×Breadboard (experimenteerprint)
1×Jumper Wires (verbindingsdraden)
1×(Aanbevolen) Schroefklem Uitbreidingsboard voor ESP32
1×(Aanbevolen) Breakout Expansion Board for ESP32
1×(Aanbevolen) Stromsplitter voor ESP32

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

Bent u niet vertrouwd met servo motors en rotary encoders, inclusief hun pinout, functionaliteit en programmering? Verken uitgebreide tutorials over deze onderwerpen hieronder:

Aansluitschema

ESP32 Rotary Encoder Servo Motor Aansluitschema

This image is created using Fritzing. Click to enlarge image

ESP32 Code

/* * 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-rotary-encoder-servo-motor */ #include <ESP32Servo.h> #define CLK_PIN 25 // ESP32 pin GPIO25 connected to the rotary encoder's CLK pin #define DT_PIN 26 // ESP32 pin GPIO26 connected to the rotary encoder's DT pin #define SW_PIN 27 // ESP32 pin GPIO27 connected to the rotary encoder's SW pin #define SERVO_PIN 33 // ESP32 pin GPIO33 connected to the servo motor #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; int angle = 90; 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(angle); } 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) { direction = DIRECTION_CCW; counter--; angle -= 2; // you can change this resolution } else { // the encoder is rotating in clockwise direction => increase the counter direction = DIRECTION_CW; counter++; angle += 2; // you can change this resolution } if (angle < 0) angle = 0; else if (angle > 180) angle = 180; // sets the servo angle according to the counter servo.write(angle); Serial.print("COUNTER: "); Serial.print(counter); Serial.print(" | ANGLE: "); Serial.println(angle); } // save last CLK state prev_CLK_state = CLK_state; }

Snelle Stappen

  • Als u ESP32 voor het eerst gebruikt, bekijk dan hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de bedrading volgens bovenstaande afbeelding.
  • Verbind de ESP32 met uw pc via een micro USB-kabel.
  • Open Arduino IDE op uw pc.
  • Selecteer het juiste ESP32 bord (bijv. ESP32 Dev Module) en de juiste COM-poort.
  • Verbind ESP32 met pc via USB-kabel
  • Open Arduino IDE, selecteer het juiste bord en poort
  • Kopieer de bovenstaande code en open deze met Arduino IDE
  • Klik op de Upload knop in Arduino IDE om de code naar de ESP32 te uploaden
Arduino IDE Code Uploaden
  • Open de Seriële Monitor
  • Draai aan de rotary encoder
  • Observeer de rotatie van de servo motor
  • Bekijk de resultaten op de Seriële Monitor
COM6
Send
COUNTER: 0 | ANGLE: 90 COUNTER: 1 | ANGLE: 92 COUNTER: 2 | ANGLE: 94 COUNTER: 3 | ANGLE: 96 COUNTER: 4 | ANGLE: 98 COUNTER: 5 | ANGLE: 100 COUNTER: 6 | ANGLE: 102
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Uitleg

Lees de lijn-voor-lijn 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.

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