Download PDF Parts List View on YouTube Download Code

Introduction

Rotary encoders are electronic devices that can measure mechanical rotation. They can be used in two fashions – As a control or as a device to measure the rotation of a shaft.

When used as a control, rotary encoders can be much more versatile than a potentiometer.  They can be used when you need a very precision control, and they are not subject to drift due to temperature.

As a device to measure mechanical rotation rotary encoders have several uses. One of them is to measure the rotation of a DC gearmotor.

Rotary Encoders

Today we will look at both types of rotary encoders. I will show you how to hook them up to an Arduino and I’ll give you some demonstration sketches that you can run yourself.

So let’s get started!

Rotary Encoders

A rotary encoder, which can also be referred to as a shaft encoder, is an electro-mechanical device that can convert the angular position (rotation) of a shaft to either an analog or digital output signals.  We will be focusing on digital devices today.

There are two main types of rotary encoder: absolute and incremental. The difference is the absolute encoder gives the precise position of the shaft in degrees, whereas the incremental encoder reports how many increments the shaft has moved, but not its actual position.

Rotary Encoders - 2 Types

Rotary encoders are used in many different applications including industrial controls, robotics, optomechanical mice and trackballs, CNC machines and printers.

Rotary Encoder Operation

There are several different types of rotary encoders. I’m restricting this discussion to the simpler “incremental” encoders. These are sometimes called quadrature or relative rotary encoders.

These encoders have two sensors and output two sets of pulses. The sensors, which can be magnetic (hall effect) or light (LED or Laser), produce pulses when the encoder shaft is rotated.

As there are two sensors in two different positions they will both produce the same pulses, however, they will be out of phase as one sensor will pulse before the other one. Which sensor goes first is determined by the direction of rotation.

Rotary Encoder Operation - CW

In the above example the encoder shaft is spinning clockwise. The sensor on the top is triggered before the bottom one, so the top set of pulses precedes the bottom set.

Rotary Encoder Operation - CCW

If the encoder shaft is rotated counterclockwise then the bottom set of pulses will be delivered before the top set.

In both cases the pulses can be counted to determine how much the shaft has rotated. By checking to see which pulse comes first we can determine the direction of rotation.

Control Encoder

The control encoder we are going to use is a very common device, and it’s available from several sources. It’s also included in the infamous “37 in one” sensor kit that is available at many electronic stores and online.

Control Encoder Pinout

The encoder can be mounted exactly like a potentiometer, and it has a D-shaft to accept a knob. It also has its own push button momentary contact switch that can be activated by pressing down upon the shaft.

The pinouts of the control encoder are as follows:

  • GND – The Ground connection.
  • +V – The VCC or positive supply voltage, usually 3.3 or 5-volts.
  • SW – The pushbutton switch output. When the shaft is depressed this goes to ground level.
  • DT – The Output A connection.
  • CLK – The output B connection.

I will show you how to use this control encoder in a moment, but first, let’s look at the other rotary encoder we will be discussing today.

Motor Encoder

Motor encoders are mounted on the shaft of a DC motor and can count the amount that the motor shaft has moved. Coupled with a motor driver, this will allow you to create a feedback mechanism that can permit you to specify how much you would like the shaft to turn.

You can also use these encoders as a tachometer to measure the speed that the shaft is rotating.

Motor Encoder Pinout

The encoder I’m illustrating here is the one that is attached to the gear motor I am using in my DB1 Robot from my Build a REAL Robot series of articles and videos. It is a goBILDA 5201 Series Spur Gear Motor with a built-in encoder.

Other motor encoders should have similar connections.

The connections are very basic:

  • VCC – the 5-volt power supply input.
  • GND – The Ground connection.
  • CH A – Output A.
  • CH B – Output B.

We will see how to use this with an Arduino to measure the motor RPM very soon.

Reading Control Encoders

We will begin our rotary encoder experiments using the control encoder.

Reading a control encoder with an Arduino is actually fairly straightforward. We just need to read input pulses and count them. We also need to determine which set of pulses is occurring first, so that we can determine the direction of rotation.

Arduino Control Encoder Test Hookup

Here is how we will hook up our first rotary encoder experiment.

Arduino Encoder Test Hookup

You will notice that in addition to the rotary encoder I have added a couple of LEDs. These will indicate the direction that we are spinning the encoder shaft.

The dropping resistors for the two LEDs are any value from 150 to 470 ohms, in the illustration I show 220 ohm resistors.

Incidentally, none of the pins used on the Arduino are critical. You can change them around, provided that you alter the sketch accordingly.

Arduino Control Encoder Test Sketch

Now that you have your encoder and LEDs hooked up you’ll need a sketch to make it all work. Here is the one I came up with:

We start the sketch by defining some constants to represent the inputs from the encoder and the outputs to the two LEDs.

Next, a few integer variables are defined. The counter variable represents the count that will be modified by turning the controller, it can be a positive or negative number..

The currentStateCLK and previousStateCLK variables hold the state of the CLK output (Output B), these are used as part of the system to determine the direction of rotation.

A string called encdir is defined, it will be used when we print the current direction of rotation to the serial monitor.

Now on the the Setup section.

The Setup is pretty straightforward, we setup the serial monitor, define the connections to the encoder as inputs and the connections to the two LEDs as outputs.

At the end of the Setup we read the current value of the CLK pin and assign it to the previousStateCLK variable.

And now the Loop, where all the action takes place!

We check the CLK input and compare it to the previousStateCLK value. If it has changed then a pulse has occurred.

Next the logic to determine the direction of rotation. We now look at the DT pin on the encoder module and compare it to the current state of the CLK pin.

If it is different then we are rotating counterclockwise. We then decrement the counter variable value, set encdir to hold a value of “CCW” and turn on the red (CCW) LED.

If, on the other hand, the two values are the same then we are moving clockwise. We do the opposite – increment the counter, write “CW” to the encdir variable and turn on the Green (CW) LED.

After we print our results to the serial monitor we update previousStateCLK with the current state of CLK.

Then we do it all over again.

Arduino Encoder Demo

Load the sketch and observe both the serial monitor and the two LEDs. When the CW LED is lit the value in the serial monitor should increment. Otherwise, the CCW LED will be lit and the value will decrement.

You may use this sketch as the basis for your own encoder projects. In fact, the next demo does exactly that!

Control Encoder with Servo Motor

The next experiment we will perform is to use a rotary encoder to control the position of a servo motor.

This is a great application for a rotary encoder as it will let you position the servo motor very precisely. It would be a great way to operate a robot arm, for example, as it would let you precisely position the arm and its grip. Of course, you would need to add more encoders and more servo motors to the design.

Arduino Servo Hookup

The hookup for the servo motor controller is illustrated below:

Arduino Servo Hookup

One thing to note is that I have used a separate power supply for the servo motor. I always recommend doing this as the servo can induce electrical noise onto the 5-volt line that the Arduino uses. But, if you really must, you can eliminate the extra supply and use the Arduino 5-volt output.

Again the pinouts are not critical but if you do change them you’ll need to make sure to use a digital I/O pin on the Arduino that supports PWM for the servo control lead.

Arduino Servo Control Sketch

Here is the sketch you will need to use to control the servo motor with the rotary encoder.

If you compare this sketch to the previous one you’ll undoubtedly notice many similarities. It starts out by defining many of the same variables, without the LEDs of course as they are not used here.

We also include the built-in Arduino Servo library and define a myservo object to represent our servo motor.

The counter, currentStateCLK and previousStateCLK variables are used again.

In the Setup we attach the myservo object to pin 9, which is where the control lead of the servo motor is connected.

The Loop is also very much like the previous sketch, with some notable exceptions.

Since a servo motor only accepts a value between 0 and 180 we limit our results to this range.

After getting the counter value we use it to position the servo motor. The value should represent the position of the servo arm in degrees.

We print this position to the serial monitor, reset the previousStateCLK variable and do it all over again.

Arduino Servo Demo

When you run the sketch observe both the arm on the servo motor and your serial monitor. The arm position should correspond to the reading on the serial monitor.

This is a very accurate method of positioning a servo motor.

Using Gearmotor Encoders

Now that we have seen how to use control encoders let’s turn our attention to encoders used with gearmotors.

As with the control encoders, the motor encoders have two outputs. This will allow you to determine the direction that the motor is spinning.

I am actually NOT going to decode the motor direction as I don’t really see the point – after all I am the one spinning the motor in a specific direction so I already know which way it is going!

Motor Encoder Output

Before I hook up the encoder to an Arduino thought it might be a good opportunity to take a look at the pulses that it produces. So I hooked the two outputs up to my oscilloscope.

Motor Encoder Output

As you can see from the scope traces the two outputs are nice square waves, offset from each other.  

You could also use the two square waves to make a more accurate encoder, reading the combination of pulses to get much finer resolution. However, as my encoder gives out 374 pulses per rotation it is already accurate to less than a degree. That is accurate enough for my purposes.

Arduino Motor Encoder Hookup

Now let’s test the encoder with an Arduino. We will hook it up, along with a motor driver and a potentiometer to control speed and read the RPM of the motor.

Arduino Motor Encoder Hookup

The motor driver I am using is the Cytron MD10C, which I have used in the article Controlling Large DC Gearmotors. You can read more details about it there if you like. It is very simple to use, requiring only a power supply for the motor to power its internal logic circuits.

I also used a 12-volt power supply, as that is the recommended voltage for my motor.  

The potentiometer I used was a 10k linear-taper. Any value from 5k or higher will work.

The connections from the Arduino to the encoder are as follows:

Arduino Pin Encoder Pin
5V (5-volt output) VCC
Ground GND
3 CH A (Output A)

Actually, you could have used Output B instead, as I’m not measuring the direction of rotation, just the position.

The input connections to the Cytron MD10C motor driver are as follows:

Arduino Pin Cytron MD10C Input Pin
12 DIR (Direction)
10 PWM
Ground GND

I’m not actually using the DIR pin as in this sketch I’m not controlling the motor direction, so you can leave it out if you wish.

The output of the motor controller is connected to, what else, the motor! And the power supply is connected to the power inputs, be sure to observe the proper polarity.

The potentiometer is hooked to analog input A0, so we can control the motor speed.

Arduino Motor Encoder Sketch

Now that we have our motor hooked up it’s time to run the sketch to control and read its speed.

In this sketch we will need to use interrupts to count pulses from our encoder, this is because our pulses will be arriving pretty quickly as compared to reading an encoder control.

Before we can use the sketch we need to know how many pulses our encoder will produce for one rotation of the motor. You can determine this from the specification sheet for your motor.   Without this value, you won’t be able to get an accurate RPM reading.

My motor produces 374 pulses per rotation, so I set the constant ENC_COUNT_REV to this value.

The next few lines define the pins that connect to the motor encoder, motor driver and to the potentiometer.

We will use the long encoderValue to keep track of the pulses we receive from the encoder. Then we will count how many of these occur in one second. By multiplying this by 60 we’ll know how many pulses we should get in a minute. If we divide this value by the ENC_COUNT_REV then we can determine the RPM of the motor shaft.

We will measure the one-second interval by keeping track of the number of milliseconds that have elapsed. The Arduino millis() function counts the number of milliseconds since the Arduino was last reset or powered on, so we can get out interval using this.

At the bottom of the sketch is an interrupt handler, it simply increments the value of encoderValue when it is triggered by a pulse from the encoder.

In the Setup we attach the interrupt handler to the pin connected to the encoder (pin 3), we trigger on a rising pulse.

The Loop starts with reading the value of the analog input connected to the potentiometer.  We use the Arduino Map Function to change its range to 0-255 and an analogWrite command to send a PWM signal with this value to the motor controller. This results in the motor turning at the desired speed.

We see if 1000 milliseconds have elapsed, if so we read the count and do the math to determine the RPM. We then print the values to the serial monitor. Note the use of the tab character to format these results in nice columns.

After printing the value we reset the encoderValue variable to zero so we can begin counting again.

Arduino Motor Encoder Demo

Load the sketch and power everything up. Turn the potentiometer and observe the motor turning, as well as the value on the serial monitor.

Conclusion

Rotary encoders are pretty versatile, they can be used as both controls and as sensors.

As controls, they have much greater precision than a potentiometer, and they cost just a little bit more. I know that I’m planning to create many of my future designs using them.

I also will be building a complete motor controller using the rotary encoder included with my gear motor.  Being able to position my robot to within less than a millimeter is an extremely attractive proposition.

I hope this article, and its associated video, have opened your eyes to some tasks you can perform with a rotary encoder.

 

Parts List

Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.

COMING SOON!

 

Resources

Sketches – ZIP file with all of the sketches used in this article.

 

 

Using Rotary Encoders with Arduino
Summary
Using Rotary Encoders with Arduino
Article Name
Using Rotary Encoders with Arduino
Description
Rotary Encoders can be used in two fashions - as a control with more versatility than a potentiometer, and as a device to measure mechanical rotation. Today I will show you how to use both types of rotary encoders.
Author
Publisher Name
DroneBot Workshop
Publisher Logo
Tagged on:
Subscribe
Notify of

80 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Jesper S. Nielsen
4 years ago

Hi Bill,
I have just built the Control Encoder Test Hookup. It’s like every time I turn the encoder one tick it counts two in value on the serial monitor. Is that normal ?

/Jesper

Jesper S. Nielsen
4 years ago

Hi Bill,
I think the rotary encoder I have from e-bay KY-040 is supposed to have double count per tick.
/Jesper

Don
2 years ago

In your video when you demo the output on the serial monitor it looks like your encoder also increments by two. Maybe you could make an encoder update using a 16×2 led display for output, would be a very useful sketch 🙂 Great channel content, thanks for all your dedicated work!

Jurgen
4 years ago

Hi Jesper,
see my comment made above…

Daniel
3 years ago

The encoder might be experiencing some mechanical noise. To help alleviate this you can innroduce a debouncing method. Initialise a currentTick value by assigning the current millis() value to it. In the first if statement include the && operation that the statement can only execute once a certain amount of milliseconds have passed. I think 50 – 100 should be fine. At the end of each execution stage assign the currentTick value to a new variable previousTick value. This will ensure that the conditional execution is only exectued after a certain amount of time has passed after mechanical bounce has… Read more »

1 year ago

Hello Jasper. I have a Chinese encoder and have the same problem like you. All the time increment twice. To solve the problem i add in software Serial.println(counter >> 1); I am not sure if this work for Arduino, because i use another compiler for Microchip PIC.

Abdulrahman Mostafa
4 years ago

If i bought motors with encoders built in but i don’t have any specification sheet to know how many pulses in one rotation.
So what can i do to know without specification sheet ?

Abdulrahman Mostafa
4 years ago

can you help me in my project

Parnnet
4 years ago

You make all this look like very simple. Thank you.

leonard HOYLAND
4 years ago

Hi Bill
I had trouble compiling the sketch to control the servo motor with the rotary encoder.
I subsequently found that the “{” is missing from line 43 at start of loop.
Hope this helps others!

robert isringhausen
4 years ago

rotary Encoder with servo motor when i try to to upload the sketch to the ardino uno it gives me a error on line 46 expected Arduino: 1.8.9 (Windows 10), Board: “Arduino/Genuino Uno”Arduino: 1.8.9 (Windows 10), Board: “Arduino/Genuino Uno” boat_control:46:2: error: expected initializer before ‘currentStateCLK’ currentStateCLK = digitalRead(inputCLk); ^ boat_control:49:1: error: expected unqualified-id before ‘if’ if (currentStateCLK != previousStateCLK);{ ^ boat_control:49:42: error: expected unqualified-id before ‘{‘ token if (currentStateCLK != previousStateCLK);{ ^ exit status 1 expected initializer before ‘currentStateCLK’ This report would have more information with “Show verbose output during compilation” option enabled in File -> Preferences. boat_control:46:2: error: expected… Read more »

Leonardo
4 years ago

Hi Bill
I had trouble compiling the sketch to control the servo motor with the rotary encoder.
I subsequently found that the “{” is missing from line 43 at start of loop.
Hope this helps others!

Roy Locke
3 years ago
Reply to  Leonardo

THANK YOU! IT’S ALIVE!

Brad Voltero
3 years ago
Reply to  Leonardo

Thanks for that correction!

Leonardo
4 years ago

This is the answer
Hi Bill
I had trouble compiling the sketch to control the servo motor with the rotary encoder.
I subsequently found that the “{” is missing from line 43 at start of loop.
Hope this helps others!

Jurgen
4 years ago

Hi Bill, Thank you very much for your excellent lessons and how you made them, great fun! My rotary encoder also counts 2 counts with 1 click using your sketch. I think it has to do with the fact that in the sketch you DO check if there is a transition in the clock ((currentStateCLK != previousStateCLK)) but you do NOT check if that signal is a high->low or a low-> high transition as with one click/turn on the encoder you both have a 1->0 (falling edge) as well as a 0->1 (rising edge). What I added is after the… Read more »

Mark Hache
3 years ago
Reply to  Jurgen

Hi Bill and Jurgen, building on the original and Jurgen’s improvement, here is how I addressed the double count problem.
from:
 if (currentStateCLK != previousStateCLK){ 
to:
 if (currentStateCLK == 0 && currentStateCLK != previousStateCLK){ 

rest remains unchanged.
This change only counts the falling edges of CLK.

Hope that helps someone

Regards

Brad Voltero
3 years ago
Reply to  Mark Hache

Hey all – like many others, I had the same issue (count seemed to increment by 2 instead of one).

I used the solution here, but I also saw Bill’s recommendation about eliminating possible noise using a capacitor wired to GND… is this an either or solution-scenario or is the capacitor route a red herring (I haven’t tried it)?

Thanks for the assistance.

Andrew McGill
1 year ago
Reply to  Mark Hache

And it did! Thanks

ahmed
4 years ago

hi bill, I was testing my encoder gear DC motor. i uploaded your code as above. On serial monitor it just shows counter value only 0 or 1 or -1 . Moreover when i rotate it alternates between CW and CCW . I even tried with encoder library but still same problem

ahmed
4 years ago
Reply to  ahmed

Hi bill now w this problem is solved but another problem is that whether i rotate it CW or CCW it always run either “if ” or either “else” in both type of rotations.

Shengming Hu
4 years ago

Can you do the same operation with 3 V DC coin vibrating motors? I am working on a project that requires 16 – 32 motors to vibrate uniformly between 40 – 60 Hz. Thank you!

4 years ago

Thank you for the lesson on rotary encoders! Long time viewer, first time caller. Not sure if you enjoy reading stories from fans, but it couldn’t hurt to leave this here if you didn’t: I run a small PC gaming community, and enjoy flight simulators. I wanted to create a project I could share with our little group that others could make at home. A week ago, I had a little idea that I could make a super simple USB game controller for a PC flight simulator that consisted of just two rotary encoders, and in a matter of an… Read more »

muhammad
4 years ago

Sir the … second program where you attach the encoder with the servo is not working when uploaded …
the error they gave is….. in this line.

// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);

error is :
exit status 1
expected initializer before ‘currentStateCLK’

what should i do?

Julio Oliveira
4 years ago
Reply to  muhammad

Line 43
void loop() is missing one “{” so it should be “void loop() {“

Wilfrido Tamayo
4 years ago

Hi Bill, with the system you set up or similar is it possible to control the motor to a precise constant speed? precise I mean keep it a earth sidereal speed (star tracking motor)?

Bon
4 years ago

Hi! Which specific rotary encoder is suitable for measuring number of rotations of a rotating shaft

Ar Dhoub
4 years ago

do you ever test detect rotation position and then using this control the rotation location? that would be very useful.

4 years ago

It registers two pulses per click.
The ALPS encoder (EC11J1524413) has two variants: One with two dents per cycle and the other with one dent per cycle.
The one on my devkit has one dent and goes trough all 4 states (quadrature/gray code) in one click.
Your code uses both the falling and rising edges of the clk(pin a) so it detects two transitions per click.
I changed the code so it only samples data (pin b) on the falling edge of clk (pin a).

Paul Castro
4 years ago

Hello Bill!!! How´s going???
Can I show the angle in a 16×2 display instead of a serial monitor???
Thanks

Miklos Antal
4 years ago

Hi Bill,
I have just built the Encoder tester circuit and load your sketch.
I’ve used Grayhill 61BY22050 optical encoder with push button for my menu system.
I turn the encoder, and only for two steps with encoder counted the value on the serial monitor.
Is that normal?
Thx Mike

Santiago
4 years ago

Hi Bill,
how would I implement a PID controller in the encoder hookup engine with arduino, because it helps me to control my autonomous robot. Thank you for your work.

4 years ago

Hello, I am from an “old Europe” country. I have an idea: It should be possible to control the stepping motors driving a cnc, to detect lost steps. I ordered encoders to be coupled to the stepping motors with 600 pulses per revolution, and two phases output signals, so I have to gain the direction signal myself; as far as I remember there are special chips for this purpose on the market. The program than has to compare the number of step pulses from the steering program with the number of encoder pulses and has to stop the cnc when… Read more »

Ray H
4 years ago

Hello Bill, LOVE your methodical approach, clear presentations too.! how ‘hard’ is it to ‘map’ 2 x 600 line encoder outputs so that the arduino stores the mapped positions of these X-Y encoders, in 10 memory slots, on an SD card, then be able to read back & output those stored X-Y postions as step & dir o/p’s to 2 x stepper drivers? the rate of output pulses would be the same rate as the collected X-Y coordinates data.(at this point). each memory slot would have to store multiple “passes” of the encoder positions to not cause the steppers to… Read more »

4 years ago

Line 43 of the servo demo is missing a curly brace.

Julio Oliveira
4 years ago
Reply to  fortney

Yeah, got it too today. “void loop() {“

Ahmd
3 years ago

Hi
I am working on a project to generate square signal with a 50% duty cycle.
I want to use Encoder to increase or decrease frequency.
Is there any idea and code
Thanks

Steve Paschke
3 years ago

I love the way you explain the circuits and code. It makes it very easy to understand.

Andrew McMahon
3 years ago

Could you add some code to keep the motor at a constant speed under load, im doing a project with a wire feeder and when the wire reel is full it labours the motor opposed to when the reel is empty then the wire feeds at different rates which i need a constant wire rate.
Thank you

Josh Kindhart
3 years ago

I don’t understand why resistors aren’t included in the circuit between the rotary encoders and the 5v supplied by the Arduino. Isn’t the potential difference infinite when their is no resistor in the circuit? Does the rotary encoder have an included resistor?

Tarek
3 years ago

Thank you so much for sharing your knowledge but I need your help with my rotary encoder it has a 600 ppr and I cannot get it to work properly with an arduino I need to measure distance at a 9M a minute
Thank you for your time
Much appreciated

Pedro A. Vazquez
3 years ago

You can write a very important book about any Arduino and all of the information about all the topics you publish here.
 

TERRENCE THOMPSON
3 years ago

First off ,thank you for the great website and body of work that you share. yours is my “Go to” site. Your presentation is the best!   I have been attempting to make a liquid mirror telescope over the past couple of years and my present iteration is using a rotary encoded geared motor to drive my dish. Of course my first port of call for information was this site.   I have since used your sketch as the basis of mine with a couple of differences which I thought you may like to make a future video of.  … Read more »

Eric
3 years ago

Wow it amazing how I find the blog more interesting and it is good that I came by. Very excellent tutorials

Torsten
3 years ago

Hi Bill,
thank you so much for the servo control sketch. finally I can use it on my model train for rotate the track at the engine shed. greetings from germany

Dave White
3 years ago

Erm, *expected initializer*
You missing { just after the void loop…
Just thought I’d share this or it will not work as shown.

This thing sucks
3 years ago

Can anyone please get the shitty subscribe and comments buttons away?

Neil Barnett
3 years ago

The double count is a fault in the code. You test for change in clock, but not which edge it is, then you test for change in the data, but not which direction it is. I’m surprised it ever worked right. This example code has been used by many writers showing how to use these encoders and it always does the same thing. Jurgen’s comment below shows how to get just one count at a time. Or you can divide by 2 if you don’t want to change the code. There are now libraries that simplify encoders and debounce them… Read more »

Robert June Tria
3 years ago

Hi Bill, Great tutorials! Your channel is one of my favorite. I came here to solve my problem using rotary encoders.. Rotating a rotary encoder, there are clicks and i found that in 1 click of rotary encoders there are 4 states for CLK and DT… CLC | DT | Problem Code | My Idea 0 | 0 | Increment | (no change) 1 | 0 | Decrement | Decrement 0 | 1 | Decrement | (no change) 1 | 1 | Increment | Increment My problem code is like your code which increment during CLK==0&DT==0 and CLK==1&DT==1 and decrements… Read more »

Roy Locke
3 years ago

Just saying thanks. I was looking for an Arduino sketch to adapt, to use some rotary encoders to control the servos on a robotic arm. I’ve had a lot of trouble with noise on the potentiometers. Plus, they’re just not very accurate, especially when you use a bunch of different values, which is what I had. It looks like I can also use this to encode the shafts on my balancing robot. Thanks again, I enjoy your videos.

Roy Locke
3 years ago

A few years ago I bought one of those 6DOF robotic arms from China and put it together. I wired together a controller using two joysticks (with the detent springs remove harvested from a used toy drone controller, plus two discrete potentiometers to control the gripper and the rotating base. The results were, shall we say, not good. An inadvertent slight bump of the joysticks would send the arm wildly flinging about, and it about tore itself apart. You can control a servo with an Arduino and a pot, but precision is another matter, with the tiny joysticks I had.… Read more »

Robert
3 years ago

Hi, the sketch is very useful but in my case I had to remove the capacitors because they are delaying too much the response of the encoder.

Denis Law
3 years ago

will there be any tutorial for Leo Bohnar boards?

Denis Law
3 years ago

for the servo sketch, i have the following message

Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: “Arduino Uno”

RotaryEncoder_ServoRotate:46:4: error: expected initializer before ‘currentStateCLK’

  currentStateCLK = digitalRead(inputCLK);

  ^~~~~~~~~~~~~~~

RotaryEncoder_ServoRotate:49:4: error: expected unqualified-id before ‘if’

  if (currentStateCLK != previousStateCLK){

  ^~

RotaryEncoder_ServoRotate:75:4: error: ‘previousStateCLK’ does not name a type

  previousStateCLK = currentStateCLK;

  ^~~~~~~~~~~~~~~~

RotaryEncoder_ServoRotate:76:2: error: expected declaration before ‘}’ token

 }

 ^

exit status 1

expected initializer before ‘currentStateCLK’

This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.

YUNUS EMRE ACAR
3 years ago

Servo Motorlu Kontrol Kodlayıcı devresinin baskı devresine nasıl ulaşabilirim yardımcı olurmusunuz

YUNUS EMRE ACAR
3 years ago

Arduino Servo Hookup devresinin baskı devresini bana atabilme imkanınız var mı

Juri Grittner
3 years ago

Hello, I’m trying to connect a rotary encoder to an atmega32u4 and was wondering, if it is required, to connect it to the SDA and SCL Pins. Or could I use different pins as well?

Erik Hoffman
3 years ago

Hi DroneBot, I wonder if you can help me to create something for my old project – l can see that you are an expert! I am very happy to Paypal you as a matter of contribution so let me explain, I have a project of an old radio which l converted to an internet radio and l am using the oldvariable capacitor to move the tuning dial so it looks real old radio, l want to use the variable capacitor like a rotary encoder connected to a Nano with about 20-30 nepixels which are all illuminated one color apart… Read more »

Jack
3 years ago

Excellent tutorial- very professional and informative. Thanks!

Mike
3 years ago

I’ve learned a lot from your presentations and accompany sketches. Thank you and please continue to post new Arduino tutorials.

Ray Williams
3 years ago

Hi
Thanks for producing such clear and helpful articles. I am looking to see if it is possible to use the built in rotary encoder in the motors that I have bought to i) control the speed of the motor (by changing the “speed” parameter via software) AND use the encoder output to measure the distance travelled by the vehicle (by counting wheel revolutions). I don’t think this article deals with carrying out those two tasks at the same time. I would not be surprised if it was impossible. I shall look at your other contributions just to be sure.

bogdan
3 years ago

Hi,
The code can be write using registers without pinMode and digitalRead ?

Eduardo
2 years ago

Es formidable esta página de internet
¡ Muchas gracias !
¡ Felicidades !

Robert M
2 years ago

These tutorials are always good. I’m envious of the workshop space.

Willem
2 years ago

fantastica explanation en documentation
thank you very much