I just hooked up a 10-30v fairly large encoder, fed the encoder 12v @ 70ma or so and worked like a champ. Shaft is a lot hard than yours to turn but works perfect first try. Counts 1-65k and back down no problem. Thanks for simple and clear video.
In some case, it wont work with board's 5v .. if you want to use the board power ONLY, then you can open it up and short the outer pins of LM7805 (78m05).. Remeber its only if you dont want to use any other external power source.
I have a homemade steering wheel project for games, just like Logitec, but I don't have the sketch to use the encoder, and I also can't load the sketches into the Arduino. what to do?
Hi Everyone! I´ve tried this code in arduino mega and works just fine. ( I have the same encoder only 360ppr) but when I try ir in arduino leonardo the counter stays around the same number. It starts working when I modify one of the interrupts to FALLING or if i just unplug uno of the pins. But only increments the Value. Does anyone knows what could be the issue. Thanks.
Hi does it work? With arduino leonardo, i´ve uploaded the code and connected the wired scheme and in the monitor shows 1 2 and when i turn the encoder it doesnt go to 50
Thank you for sharing this video with us. There is one point to enhance the code: add max and minimum counter limits then flip the counter when reaching the limit such as: void countClamp () { // clamp count from QMIN to QMAX: if (counter > QMAX) { counter = QMIN; } else if (counter < QMIN) { counter = QMAX; } This function should be verified whenever updating the counter I think in your case limits would be long temp,counter = 0; //sign should be considered #define QMAX 1199 #define QMIN 0
Hi A R, looking for some help, would this make the code deal with minus numbers as it currently goes to 65000 something when it passes zero? Any elp would be appreciated, been stuck on this too long. Thanks
Make sure that if you're using velocity as a control input that you also account for the rollover. E.g. if the QMAX was 1000 and it goes from 980 to 20 in one dt timestep, you have to do some math. 1000-(prevCount=980) + (newCount=20) to get the 40 counts that the encoder rotated. Do this in reverse if you hit QMIN. If you don't do this, the counts/dt would be 20-980 which would give a speed of -960counts/dt which will throw off any control loop you may have
I have an AC motor which turns 10 RPM . The motor turns a shaft with a chain . I want to measure and stop the motor when to chain travels 4 meters. Is this possible with this encoder.
Hola, espero que no haya llegado tarde pero el encoder funciona, lo que pasa es que el codigo tiene deteccion de interrupciones en HIGH cuando deberia ser CHANGE modifica eso y funcionara
I have exactly the same encoder, it came to me today from Aliexpress and if I don't put pullups on it, it doesn't work for me, I put the outputs on the oscilloscope and I see the signals very very attenuated, with pullups it works very well. What can it be due to, will I have failed? Regards.
That's it, I'm not using it with Arduino, and I can't activate the internal pullups of the microcontroller, because I have opto-isolated input. The output of this encoder in NPN open collector, that's why I need to put external pull ups resistors. Regards.
My homemade Six Digit, 20MHz, CD74HC192 Up-Down counter circuit counts Quadrature Encoder pulses at 20Mhz speeds. And seems to be many times faster than using my PIC16F887 microcontrollers. Can you tell me why?
HI Maker Tutor, i brought Rotary Encoder HQK38H6-600N-G5-24 form china use your Sketch the problem is the encoder reading always increment if i turn forward or backwards the staff.
I have an encoder very similar to this one, having no luck writing a code to do what I need, can you help. I want to use a timing belt and sprocket to measure how far the belt moves (with extreme accuracy) and display on a LCD display.
float temp; int counter = 0; int statePrevious; boolean stateLeft; boolean stateRight; int setLeft; int setRight; int Led7=7; int Led8=8; // volatile unsigned int temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder void setup() { Serial.begin (115200); pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 pinMode(3, INPUT_PULLUP); // internal pullup input pin 3 pinMode(Led7, OUTPUT); pinMode(Led8, OUTPUT); //Setting up interrupt //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino. attachInterrupt(0, ai0, RISING); //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino. attachInterrupt(1, ai1, RISING); } void loop() { // Send the value of counter if( counter != temp ){ temp = counter; } } void ai0() { // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH // Check pin 3 to determine the direction if(digitalRead(3)==LOW) { stateRight = true; stateLeft = false; if (stateRight == true && stateLeft == false) { Serial.println ("Going Right "); setLeft = counter - counter - counter + 1; Serial.println (temp,0); Serial.print (" Set left to "); Serial.println (setLeft); digitalWrite (Led7, HIGH); digitalWrite (Led8, LOW); // send stepper to last (setLeft); } counter--; }else{ counter++; } } void ai1() { // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH // Check with pin 2 to determine the direction if(digitalRead(2)==LOW) { stateRight = false; stateLeft = true; if (stateRight == false && stateLeft == true) { Serial.println ("Going Left "); setRight = counter - counter - counter - 1 ; Serial.println (temp,0); Serial.print (" Set Right to "); Serial.println (setRight); digitalWrite (Led7, LOW); digitalWrite (Led8, HIGH); // send stepper to last (setLeft); } counter++; }else{ counter--; } }/*change detection rotary encoder type LPD3806-360BM-G5-24C J733 purchased on ebay*/
2*pi*radius gives you the length traveled in a whole revolution (radius = outer radius of pulley attached to the axis of the encoder for example). Devide this length by the number of pulses per revolution, in this case 400. Now you now your displacement per pulse.
thank you for this video but i want to read this value and control a DC motor with the same value so the encoder and the motor have the same speed thanks
/*I have been trying to these encoders to read oscillations left to right of a pendulum. I have tried interrupts, switch states and every type of code I can think of. I can get reading from center of zero and - to the left , + to right, but cannot get it to return to zero. It drifts one way or the other. Does anyone have a sketch I can try? Here is an example sketch I pulled out the Stepper code. */ // defines pins numbers const int stepPin = 10; const int dirPin = 11; const int enPin = 12; float pulsesR = 6400; float pulsesL = 6400; const int pulsedelay = 0; int cycledelay= 20; //--------------------ENCODER Variables START------------------------- float counter; int Left; int Right; int leftPin = 2; int rightPin = 3; const int Home = 999; float degree; //--------------------ENCODER Variables FINISH------------------------- void setup() { Serial.begin(115200); // Sets the two pins as Outputs pinMode(6, INPUT_PULLUP); pinMode(7, OUTPUT); pinMode(4, INPUT_PULLUP); pinMode(5, OUTPUT); pinMode(stepPin,OUTPUT); pinMode(dirPin,OUTPUT); pinMode(enPin,OUTPUT); digitalWrite(enPin,LOW); //--------------------ENCODER Setup START------------------------- pinMode(leftPin, INPUT_PULLUP); // internal pullup input pin leftPin pinMode(rightPin, INPUT_PULLUP); // internal pullup input pin rightPin pinMode(Home,INPUT); //Setting up interrupt //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr leftPin on moust Arduino. attachInterrupt(0, SwingRight, RISING); //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr rightPin on moust Arduino. attachInterrupt(1, SwingLeft, RISING); //--------------------ENCODER Setup FINISH------------------------- } void loop() { //--------------------ENCODER Loop START------------------------- degree = counter /2; // Send the value of counter if( digitalRead (Home) == HIGH ){ // temp = counter; counter = 0; Serial.println (degree,0); } digitalRead(leftPin); digitalRead(rightPin); //--------------------ENCODER Loop FINISH------------------------- int sensorVal6 = digitalRead(6); if (sensorVal6 == LOW) { digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction // Makes 200 pulses for making one full cycle rotation for(int x = 0; x < pulsesR; x++) { digitalWrite(stepPin,HIGH); delayMicroseconds(pulsedelay); digitalWrite(stepPin,LOW); delayMicroseconds(pulsedelay); // degree++; // Serial.println(degree / 35.55555555555556 ,0); digitalWrite(7, HIGH); } } else { digitalWrite(7, LOW); delay (50); } delay(cycledelay); delay(cycledelay); // One second delay int sensorVal4 = digitalRead(4); if (sensorVal4 == LOW) { digitalWrite(dirPin,LOW); //Changes the rotations direction // Makes 400 pulses for making two full cycle rotation for(int x = 0; x < pulsesL; x++) { digitalWrite(stepPin,HIGH); delayMicroseconds(pulsedelay); digitalWrite(stepPin,LOW); delayMicroseconds(pulsedelay); // degree--; // Serial.println(degree/ 35.55555555555556,0); digitalWrite(5, HIGH); } } else { digitalWrite(5, LOW); } delay(cycledelay); } //--------------------ENCODER SwingRight Interrupt START------------------------ void SwingRight() { // ai0 is activated if DigitalPin nr leftPin is going from LOW to HIGH // Check pin rightPin to determine the direction if(digitalRead(rightPin)==LOW + LOW + HIGH) { counter--; Serial.print(" SwingingRight at "); Serial.println (degree,0); Right = degree; }else{ counter++; } } //--------------------ENCODER SwingRight Interrupt FINISH------------------------ //--------------------ENCODER SwingLeft Interrupt START------------------------ void SwingLeft() { // ai0 is activated if DigitalPin nr rightPin is going from LOW to HIGH // Check with pin leftPin to determine the direction if(digitalRead(leftPin)==LOW + LOW + HIGH) { counter++; Serial.print("SwingingLeft at "); Serial.println (degree,0); Left = degree; }else{ counter--; } } //--------------------ENCODER Swingleft INterrupt FINISH------------------------
used ur code and the same ecoder, but getting wrong result. with one step i get both signals(+ and _) and result is like 1-0-1-0-1-0-1-0-1..... if i detach one logic pin i get only one direction of counting no matter of direction of rotation. it counts impulses correctly but only pluses or only minuses. cant understand why does it do this way...((
@@andrescarlini2071 you have to change the code, see www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ attachInterrupt(digitalPinToInterrupt(2), ai0, RISING); attachInterrupt(digitalPinToInterrupt(3), ai1, RISING);
@@theragavelan Yeah, I've got that. That encoders should have separate 12V power suplly to work. And that's why I asked that couse that showed some wrong thingies in that screen with defolt 5v from arduino. So was wondered if that a right one. )
yes 0.01 deg means you need 360 * 100 pulses then you need 36000 puls. that's mean you need 36000 / 400 = 90 encoder revelution per one turn of the motor shaft. so use gears to make this posible and you will get 0.01 degree per puls
I just hooked up a 10-30v fairly large encoder, fed the encoder 12v @ 70ma or so and worked like a champ. Shaft is a lot hard than yours to turn but works perfect first try. Counts 1-65k and back down no problem. Thanks for simple and clear video.
In some case, it wont work with board's 5v .. if you want to use the board power ONLY, then you can open it up and short the outer pins of LM7805 (78m05).. Remeber its only if you dont want to use any other external power source.
I have a homemade steering wheel project for games, just like Logitec, but I don't have the sketch to use the encoder, and I also can't load the sketches into the Arduino. what to do?
Hi Everyone! I´ve tried this code in arduino mega and works just fine. ( I have the same encoder only 360ppr) but when I try ir in arduino leonardo the counter stays around the same number. It starts working when I modify one of the interrupts to FALLING or if i just unplug uno of the pins. But only increments the Value. Does anyone knows what could be the issue. Thanks.
I think the encoder you are using (see 0:14) has 600 pulses per revolution, not 400. Yours is type ...-600BM-... Thanks for the video and the code!
Also this code is incrementing counter on both phases, so it 1200 pulses per rotation
is it matter.all is it doing count pulses not revolutions xD
Hi does it work? With arduino leonardo, i´ve uploaded the code and connected the wired scheme and in the monitor shows 1 2 and when i turn the encoder it doesnt go to 50
Thank you for sharing this video with us. There is one point to enhance the code: add max and minimum counter limits then flip the counter when reaching the limit such as:
void countClamp () { // clamp count from QMIN to QMAX:
if (counter > QMAX) {
counter = QMIN;
}
else if (counter < QMIN) {
counter = QMAX;
}
This function should be verified whenever updating the counter
I think in your case limits would be
long temp,counter = 0; //sign should be considered
#define QMAX 1199
#define QMIN 0
Hi A R, looking for some help, would this make the code deal with minus numbers as it currently goes to 65000 something when it passes zero? Any elp would be appreciated, been stuck on this too long. Thanks
how to solve limit counter ? how
Make sure that if you're using velocity as a control input that you also account for the rollover. E.g. if the QMAX was 1000 and it goes from 980 to 20 in one dt timestep, you have to do some math. 1000-(prevCount=980) + (newCount=20) to get the 40 counts that the encoder rotated. Do this in reverse if you hit QMIN. If you don't do this, the counts/dt would be 20-980 which would give a speed of -960counts/dt which will throw off any control loop you may have
I have an AC motor which turns 10 RPM . The motor turns a shaft with a chain . I want to measure and stop the motor when to chain travels 4 meters. Is this possible with this encoder.
Anyone knows the max speed readable? I don't think the arduino can read all values if the encoder reaches 5000 rpm
Is the reading you are showing in RPM?
yo tengo exactamente ese mismo encoder pero mi problema es que solo aumenta, no disminuye, creo que vino con defectos
Hola, espero que no haya llegado tarde pero el encoder funciona, lo que pasa es que el codigo tiene deteccion de interrupciones en HIGH cuando deberia ser CHANGE modifica eso y funcionara
@@angelabreu2676 ya voy para 3 años con mi volante g29 ya que nunca pude resolverlo 😞 pero igual gracias
I have exactly the same encoder, it came to me today from Aliexpress and if I don't put pullups on it, it doesn't work for me, I put the outputs on the oscilloscope and I see the signals very very attenuated, with pullups it works very well. What can it be due to, will I have failed? Regards.
That's it, I'm not using it with Arduino, and I can't activate the internal pullups of the microcontroller, because I have opto-isolated input. The output of this encoder in NPN open collector, that's why I need to put external pull ups resistors. Regards.
can we calculate accurate angle using this??
If the motor does 3.500 rpm, the encoder 1.000 pulses, does the arduino have enough capacity or are pulses lost? Thks
My homemade Six Digit, 20MHz, CD74HC192 Up-Down counter circuit counts Quadrature Encoder pulses at 20Mhz speeds. And seems to be many times faster than using my PIC16F887 microcontrollers.
Can you tell me why?
you programmed it with rising edge on each pins A and B right?
Can you please tell me how would you use this to calculate angle, like the line of code that should be added?
//TRY THIS
// defines pins numbers
const int stepPin = 10;
const int dirPin = 11;
const int enPin = 12;
float pulsesR = 6400;
float pulsesL = 6400;
const int pulsedelay = 0;
int cycledelay= 20;
//--------------------ENCODER Variables START-------------------------
float counter;
int Left;
int Right;
int leftPin = 2;
int rightPin = 3;
const int Home = 999;
float degree;
//--------------------ENCODER Variables FINISH-------------------------
void setup() {
Serial.begin(115200);
// Sets the two pins as Outputs
pinMode(6, INPUT_PULLUP);
pinMode(7, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
//--------------------ENCODER Setup START-------------------------
pinMode(leftPin, INPUT_PULLUP); // internal pullup input pin leftPin
pinMode(rightPin, INPUT_PULLUP); // internal pullup input pin rightPin
pinMode(Home,INPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr leftPin on moust Arduino.
attachInterrupt(0, SwingRight, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr rightPin on moust Arduino.
attachInterrupt(1, SwingLeft, RISING);
//--------------------ENCODER Setup FINISH-------------------------
}
void loop() {
//--------------------ENCODER Loop START-------------------------
degree = counter /2;
// Send the value of counter
if( digitalRead (Home) == HIGH ){
// temp = counter;
counter = 0;
Serial.println (degree,0);
}
digitalRead(leftPin);
digitalRead(rightPin);
//--------------------ENCODER Loop FINISH-------------------------
int sensorVal6 = digitalRead(6);
if (sensorVal6 == LOW) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < pulsesR; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(pulsedelay);
digitalWrite(stepPin,LOW);
delayMicroseconds(pulsedelay);
// degree++;
// Serial.println(degree / 35.55555555555556 ,0);
digitalWrite(7, HIGH);
}
} else {
digitalWrite(7, LOW);
delay (50);
}
delay(cycledelay);
delay(cycledelay); // One second delay
int sensorVal4 = digitalRead(4);
if (sensorVal4 == LOW) {
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < pulsesL; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(pulsedelay);
digitalWrite(stepPin,LOW);
delayMicroseconds(pulsedelay);
// degree--;
// Serial.println(degree/ 35.55555555555556,0);
digitalWrite(5, HIGH);
}
} else {
digitalWrite(5, LOW);
}
delay(cycledelay);
}
//--------------------ENCODER SwingRight Interrupt START------------------------
void SwingRight() {
// ai0 is activated if DigitalPin nr leftPin is going from LOW to HIGH
// Check pin rightPin to determine the direction
if(digitalRead(rightPin)==LOW + LOW + HIGH) {
counter--;
Serial.print(" SwingingRight at ");
Serial.println (degree,0);
Right = degree;
}else{
counter++;
}
}
//--------------------ENCODER SwingRight Interrupt FINISH------------------------
//--------------------ENCODER SwingLeft Interrupt START------------------------
void SwingLeft() {
// ai0 is activated if DigitalPin nr rightPin is going from LOW to HIGH
// Check with pin leftPin to determine the direction
if(digitalRead(leftPin)==LOW + LOW + HIGH) {
counter++;
Serial.print("SwingingLeft at ");
Serial.println (degree,0);
Left = degree;
}else{
counter--;
}
}
//--------------------ENCODER Swingleft INterrupt FINISH------------------------
The encoder detects movement in both directions but loses its reference zero...does anyone knows why?
Is it possible to have a design that will allow the incremental encoder to act like an absolute encoder?
No
@@hurairtariq1 I did it and it worked.
can i use this as a masering wheel for Cut to lenth machine
HI Maker Tutor, i brought Rotary Encoder HQK38H6-600N-G5-24 form china use your Sketch the problem is the encoder reading always increment if i turn forward or backwards the staff.
Hello, what if I want see negative numbers want to see, what should I change in code ? ,thank you.
This is spectacular sketch for Arduino Thanks
I need, can you help. i want to make a long cloth.between 1 meter to 10 meter.so how to do the code and use the suitable rotary encoder?
Thanks. The only one worked codes on youtube.
Hi, Sir. can we use raspberry pi pico to read the incremental rotary encoder?
I have an encoder very similar to this one, having no luck writing a code to do what I need, can you help. I want to use a timing belt and sprocket to measure how far the belt moves (with extreme accuracy) and display on a LCD display.
float temp;
int counter = 0;
int statePrevious;
boolean stateLeft;
boolean stateRight;
int setLeft;
int setRight;
int Led7=7;
int Led8=8;
// volatile unsigned int temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (115200);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(3, INPUT_PULLUP); // internal pullup input pin 3
pinMode(Led7, OUTPUT);
pinMode(Led8, OUTPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop() {
// Send the value of counter
if( counter != temp ){
temp = counter;
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
stateRight = true;
stateLeft = false;
if (stateRight == true && stateLeft == false) {
Serial.println ("Going Right ");
setLeft = counter - counter - counter + 1;
Serial.println (temp,0);
Serial.print (" Set left to ");
Serial.println (setLeft);
digitalWrite (Led7, HIGH);
digitalWrite (Led8, LOW);
// send stepper to last (setLeft);
}
counter--;
}else{
counter++;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
stateRight = false;
stateLeft = true;
if (stateRight == false && stateLeft == true) {
Serial.println ("Going Left ");
setRight = counter - counter - counter - 1 ;
Serial.println (temp,0);
Serial.print (" Set Right to ");
Serial.println (setRight);
digitalWrite (Led7, LOW);
digitalWrite (Led8, HIGH);
// send stepper to last (setLeft);
}
counter++;
}else{
counter--;
}
}/*change detection
rotary encoder type LPD3806-360BM-G5-24C
J733 purchased on ebay*/
@maker tutor , i am having problem that when i rotate it in anti clock wise direction it shows 65535, kindly tell me how to resolve that issue.
I has the same problem. Change variable type from int(int stores a 16-bit) to long (32-bit).
volatile unsigned int temp, counter = 0 ,what should I write to do it ??? thanks
@@muratcanbarut64 You figured it out I myself can’t understand?
hi. i have e38s65 400b g24n rotary encoder 5v-24v it is not work with arduino routary encoder code. how can i solve ?
Can you convert the ENCODER FREQ to RPM Function ?Looking forward to another quick video
What is the software used to view the results mam in your pc
Good job .if I went put stepper motor withe encoder how I do that
how can we measure length travelled using that encoder?
2*pi*radius gives you the length traveled in a whole revolution (radius = outer radius of pulley attached to the axis of the encoder for example).
Devide this length by the number of pulses per revolution, in this case 400. Now you now your displacement per pulse.
May I know what type arduino u used?thanks.GBU
do you have the code to connect to the lcd display with this type of encoder?
thank you for this video but i want to read this value and control a DC motor with the same value so the encoder and the motor have the same speed
thanks
Hi, great project. Do you know about the maximum frecuency reading of arduino
I want to move it to specific number of turn and want to control position of quadrature encoder motor using coding
Hi
Has anyone tried to do the same with a jetson nano or raspberry pi for the same model of the encoder ? If yes then please share the code.
Thank You Very Much ! for ilustration "
Can you control two encoders at once?
/*I have been trying to these encoders to read oscillations left to right of a pendulum. I have tried interrupts, switch states and every type of code I can think of. I can get reading from center of zero and - to the left , + to right, but cannot get it to return to zero. It drifts one way or the other. Does anyone have a sketch I can try? Here is an example sketch I pulled out the Stepper code. */
// defines pins numbers
const int stepPin = 10;
const int dirPin = 11;
const int enPin = 12;
float pulsesR = 6400;
float pulsesL = 6400;
const int pulsedelay = 0;
int cycledelay= 20;
//--------------------ENCODER Variables START-------------------------
float counter;
int Left;
int Right;
int leftPin = 2;
int rightPin = 3;
const int Home = 999;
float degree;
//--------------------ENCODER Variables FINISH-------------------------
void setup() {
Serial.begin(115200);
// Sets the two pins as Outputs
pinMode(6, INPUT_PULLUP);
pinMode(7, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
//--------------------ENCODER Setup START-------------------------
pinMode(leftPin, INPUT_PULLUP); // internal pullup input pin leftPin
pinMode(rightPin, INPUT_PULLUP); // internal pullup input pin rightPin
pinMode(Home,INPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr leftPin on moust Arduino.
attachInterrupt(0, SwingRight, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr rightPin on moust Arduino.
attachInterrupt(1, SwingLeft, RISING);
//--------------------ENCODER Setup FINISH-------------------------
}
void loop() {
//--------------------ENCODER Loop START-------------------------
degree = counter /2;
// Send the value of counter
if( digitalRead (Home) == HIGH ){
// temp = counter;
counter = 0;
Serial.println (degree,0);
}
digitalRead(leftPin);
digitalRead(rightPin);
//--------------------ENCODER Loop FINISH-------------------------
int sensorVal6 = digitalRead(6);
if (sensorVal6 == LOW) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < pulsesR; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(pulsedelay);
digitalWrite(stepPin,LOW);
delayMicroseconds(pulsedelay);
// degree++;
// Serial.println(degree / 35.55555555555556 ,0);
digitalWrite(7, HIGH);
}
} else {
digitalWrite(7, LOW);
delay (50);
}
delay(cycledelay);
delay(cycledelay); // One second delay
int sensorVal4 = digitalRead(4);
if (sensorVal4 == LOW) {
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < pulsesL; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(pulsedelay);
digitalWrite(stepPin,LOW);
delayMicroseconds(pulsedelay);
// degree--;
// Serial.println(degree/ 35.55555555555556,0);
digitalWrite(5, HIGH);
}
} else {
digitalWrite(5, LOW);
}
delay(cycledelay);
}
//--------------------ENCODER SwingRight Interrupt START------------------------
void SwingRight() {
// ai0 is activated if DigitalPin nr leftPin is going from LOW to HIGH
// Check pin rightPin to determine the direction
if(digitalRead(rightPin)==LOW + LOW + HIGH) {
counter--;
Serial.print(" SwingingRight at ");
Serial.println (degree,0);
Right = degree;
}else{
counter++;
}
}
//--------------------ENCODER SwingRight Interrupt FINISH------------------------
//--------------------ENCODER SwingLeft Interrupt START------------------------
void SwingLeft() {
// ai0 is activated if DigitalPin nr rightPin is going from LOW to HIGH
// Check with pin leftPin to determine the direction
if(digitalRead(leftPin)==LOW + LOW + HIGH) {
counter++;
Serial.print("SwingingLeft at ");
Serial.println (degree,0);
Left = degree;
}else{
counter--;
}
}
//--------------------ENCODER Swingleft INterrupt FINISH------------------------
Hello.. I'm having the same issue. Have you find the solucion ? If yes let me know
used ur code and the same ecoder, but getting wrong result. with one step i get both signals(+ and _) and result is like 1-0-1-0-1-0-1-0-1..... if i detach one logic pin i get only one direction of counting no matter of direction of rotation. it counts impulses correctly but only pluses or only minuses. cant understand why does it do this way...((
It happened the same to me but only in Leonardo. In Mega works fine. Dont know why...
@@andrescarlini2071 you have to change the code, see www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
attachInterrupt(digitalPinToInterrupt(2), ai0, RISING);
attachInterrupt(digitalPinToInterrupt(3), ai1, RISING);
Nice little video, code works a treat. Thanks
Can it keep up with a stepper motor running at 30000 mm/min? I would like to use it in high speed gcode application
This rotary encoder can running at 6,000 rpm , convert from rpm to mm/min I don't know sorry
Maker Tutor it's okay :) thanks!
Do you know what encoder this is Brand and Serial Number Please
or a link to where i can buy it =)
Where does the rotary Encoder buy?
how to calculate rpm from encoder pulses? please help me
Did you get an answer ؟
watch my comment
Can I use this code for arduino Leonardo r3? Help please
do you know how to wiring this encoder with cnc ?
Bonjour, j'ai un code de 1200, je veux convertir en 360, puis-je avoir un code
does anyone know how to use two rotary encoders with this code??
can i use this project in unojoy
Can this code stand foe high speed encoder?
Hola,pueden compartir ese codigo para contar pulsos de encoder rotativo?
Like 1.3 K ❤ 👏👏👏
Excellent video
Can the out put of the Arduino Uno be given to an display unit. IF so please make a video on it.
Muchas gracious from India
Wow, until I watched this video, I had no idea how to plug four wires into an Arduino
Ese motor de paso y encodet se puede conectar a un driver híbrido,para motor de paso y encoder.
Hello Bro! What Pin G is used for?
Pin G = Ground or GND
how about this encorder with esp32?
How to open the data inputs information screen please)?
ctr+shift+m
@@theragavelan Yeah, I've got that. That encoders should have separate 12V power suplly to work. And that's why I asked that couse that showed some wrong thingies in that screen with defolt 5v from arduino. So was wondered if that a right one. )
Practico de implementar, gracias.
very good, help a lot, thank u!
is this code limited 65535?
thanks for the video.
but how can i use encoder to meassure angular speed?
any ideas????????
ohhhhhhhhhhhhhhhhhhh 👍👍👍👍👏👏👏👏
how to detect speed of motor by this pls.
Works perfectly! Thanks!!!
its limited 65535 ? and dont show negative numbers?
what details of Arduino please ?
counter limit is 65535 ?! why?
Why do people think it appropriate to make a technical video with hideous music hammering away in the background and no commentary.
Thanks for the video and the code
How to use two encoder in arduino
Muchas gracias por el aporte (Y)
no no your code is working . but how can i have negative numbers?
400P/R is how many can divide angles ? can use for 0.01 degree ?
365/400 = .9 deg/pulse, so no?
yes
0.01 deg means you need 360 * 100 pulses then you need 36000 puls.
that's mean you need 36000 / 400 = 90 encoder revelution per one turn of the motor shaft.
so use gears to make this posible and you will get 0.01 degree per puls
// defines pins numbers
const int stepPin = 10;
const int dirPin = 11;
const int enPin = 12;
float pulsesR = 6400;
float pulsesL = 6400;
const int pulsedelay = 0;
int cycledelay= 20;
//--------------------ENCODER Variables START-------------------------
float counter;
int Left;
int Right;
int leftPin = 2;
int rightPin = 3;
const int Home = 999;
float degree;
//--------------------ENCODER Variables FINISH-------------------------
void setup() {
Serial.begin(115200);
// Sets the two pins as Outputs
pinMode(6, INPUT_PULLUP);
pinMode(7, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
//--------------------ENCODER Setup START-------------------------
pinMode(leftPin, INPUT_PULLUP); // internal pullup input pin leftPin
pinMode(rightPin, INPUT_PULLUP); // internal pullup input pin rightPin
pinMode(Home,INPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr leftPin on moust Arduino.
attachInterrupt(0, SwingRight, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr rightPin on moust Arduino.
attachInterrupt(1, SwingLeft, RISING);
//--------------------ENCODER Setup FINISH-------------------------
}
void loop() {
//--------------------ENCODER Loop START-------------------------
degree = counter /2;
// Send the value of counter
if( digitalRead (Home) == HIGH ){
// temp = counter;
counter = 0;
Serial.println (degree,0);
}
digitalRead(leftPin);
digitalRead(rightPin);
//--------------------ENCODER Loop FINISH-------------------------
int sensorVal6 = digitalRead(6);
if (sensorVal6 == LOW) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < pulsesR; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(pulsedelay);
digitalWrite(stepPin,LOW);
delayMicroseconds(pulsedelay);
// degree++;
// Serial.println(degree / 35.55555555555556 ,0);
digitalWrite(7, HIGH);
}
} else {
digitalWrite(7, LOW);
delay (50);
}
delay(cycledelay);
delay(cycledelay); // One second delay
int sensorVal4 = digitalRead(4);
if (sensorVal4 == LOW) {
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < pulsesL; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(pulsedelay);
digitalWrite(stepPin,LOW);
delayMicroseconds(pulsedelay);
// degree--;
// Serial.println(degree/ 35.55555555555556,0);
digitalWrite(5, HIGH);
}
} else {
digitalWrite(5, LOW);
}
delay(cycledelay);
}
//--------------------ENCODER SwingRight Interrupt START------------------------
void SwingRight() {
// ai0 is activated if DigitalPin nr leftPin is going from LOW to HIGH
// Check pin rightPin to determine the direction
if(digitalRead(rightPin)==LOW + LOW + HIGH) {
counter--;
Serial.print(" SwingingRight at ");
Serial.println (degree,0);
Right = degree;
}else{
counter++;
}
}
//--------------------ENCODER SwingRight Interrupt FINISH------------------------
//--------------------ENCODER SwingLeft Interrupt START------------------------
void SwingLeft() {
// ai0 is activated if DigitalPin nr rightPin is going from LOW to HIGH
// Check with pin leftPin to determine the direction
if(digitalRead(leftPin)==LOW + LOW + HIGH) {
counter++;
Serial.print("SwingingLeft at ");
Serial.println (degree,0);
Left = degree;
}else{
counter--;
}
}
//--------------------ENCODER Swingleft INterrupt FINISH------------------------
i'm confused: that ugly hand is not a girl's hand
How can i contact with mail adress?
Tks, it worked!
my encoder reads 65535 per revolution at 115200 Baud
I has the same problem. Change variable type from int(int stores a 16-bit) to long (32-bit).
Very good
thanks a lot!!!!!!
Hi dear Can you send me Encoder program pictures
Bellissimo progetto , mi aiuti a realizzarlo ? francesco da Palermo
helped a lot, thanks
Круто, работает!
excelente..gracias
thank you very much
Muchas gracias !!
Thanks for the code
Nice.
Thanks ...!!!
oi boa tarde, eu não conseguir
Tutcesni vidya ceksene
O conteúdo é relevante mas veja no mudo porq a música é um lixo
LPD3806-600BM
65535
The enconder has 600 pulses per channel?