Divulgación: Algunos de los enlaces proporcionados en esta sección son enlaces de afiliado de Amazon. Podemos recibir una comisión por las compras realizadas a través de estos enlaces sin costo adicional para usted. Apreciamos su apoyo.
Si no sabes sobre el servomotor y el codificador rotatorio (disposición de pines, cómo funciona, cómo programarlo...), aprende sobre ellos en los siguientes tutoriales:
This image is created using Fritzing. Click to enlarge image
Código de Arduino
/* * Este código de Arduino fue desarrollado por es.newbiely.com * Este código de Arduino se proporciona al público sin ninguna restricción. * Para tutoriales completos y diagramas de cableado, visite: * https://es.newbiely.com/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 directionint counter = 0;intdirection = DIRECTION_CW;int CLK_state;int prev_CLK_state;Servo servo; // create servo object to control a servovoidsetup() {Serial.begin(9600);// configure encoder pins as inputspinMode(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);}voidloop() {// 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 countif (CLK_state != prev_CLK_state && CLK_state == HIGH) {// if the DT state is HIGH// the encoder is rotating in counter-clockwise direction => decrease the counterif (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");elseSerial.print("Counter-clockwise");Serial.print(" | COUNTER: ");Serial.println(counter);if (counter < 0) counter = 0;elseif (counter > 180) counter = 180;// sets the servo angle according to the counter servo.write(counter); }// save last CLK state prev_CLK_state = CLK_state;}
Pasos R\u00e1pidos
Conectar Arduino a la PC mediante un cable USB
Abrir Arduino IDE, seleccionar la placa y el puerto correctos
Copiar el código anterior y abrirlo con Arduino IDE
Hacer clic en el botón Subir en Arduino IDE para subir el código al Arduino
Abrir el Monitor Serial
Girar el codificador giratorio
Ver la rotación del servomotor
Ver el resultado en el Monitor Serial
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
¡Lee la explicación línea por línea en las líneas de comentario del código fuente!
Video Tutorial
Estamos considerando crear tutoriales en video. Si considera que los tutoriales en video son importantes, suscríbase a nuestro canal de YouTube para motivarnos a crear los videos.
No dude en compartir el enlace de este tutorial. Sin embargo, por favor no use nuestro contenido en otros sitios web. Hemos invertido mucho esfuerzo y tiempo en crear el contenido, ¡por favor respete nuestro trabajo!