Read rotary encoder from Arduino Uno in Simulink

142 views (last 30 days)
I can easily read the counts in Matlab (like this:, but not in Sumulink! Can someone tell me how it is the correct way to read the counts in real-time in Simulink?

Accepted Answer

Pranav Murali
Pranav Murali on 23 Mar 2020
I understand that you are trying to read the counts from a rotary encoder using Simulink. Although Simulink doesn't have a block to interface a rotary encoder at present, it can possibly be achieved in two ways:
  1. Write your code on MATLAB and import the function as a Simulink block. See how that is done here.
  2. Attach External Interrupt block to pins 2 and 3 to call an Interrupt Service Routine (ISR) function. The ISR will then perform bitwise comparison of the two inputs to increment/decrement the counts.
The idea behind the second approach is to implement the rotaryEncoder function through existing Simulink blocks.
Ghani Lawal
Ghani Lawal on 18 Feb 2022
If one doesn't care about the direction of the motor, can one just attach the External Interrupt to call the ISR when the signal at PIN 2 goes from LOW o HIGH?

Sign in to comment.

More Answers (3)

Hasan Falah
Hasan Falah on 30 Jun 2020
Hello friends,
I’m using arduino Due to read incremental rotary encoder by simulink, there is no block in sinulink to do that.
I tried with external interrupt example in matlab 2020a but it is reading only in one direction and just increasing, so it should be readings in clockwise and counterclockwise.
Aslo I tried with encoder S-Function in Device Driver examples but also does not work.
Your help please .....
Anass Dkheila
Anass Dkheila on 21 Sep 2021
Hello Hasan did you solve this problem? if you did can you send it to me at

Sign in to comment.

Daniel Velazquez
Daniel Velazquez on 5 Jul 2021
Hi, thanks for the encoder part.
Attached is a preliminary implementation of a PID Speed Controller for a DC motor with encoder.
The circuit is a low side FET to the pin 5 of the ATMEGA (PWM output) and pins 2 and 3 go to the encoders as input.
The PID parameters and scopes are tuneable.
Daniel Velazquez
  1 Comment
Azlin Afiqah
Azlin Afiqah on 4 Aug 2021
Hi, can you give me the block diagram in version R2014a. Thank you. I'm helpless doing this project. I really really appreciate your support. Thank you again

Sign in to comment.

Daniel Velazquez
Daniel Velazquez on 6 Aug 2021
Hello Azlin, unfortunately I do not have R2014a but you can easily redraw it in your version from the image attached of the Simulink diagram.
Have fun
Omer Faruk
Omer Faruk on 19 May 2022
Daniel could you explain how we obtain response curve?

Sign in to comment.


Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!