element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs BITS N BYTES – Arduino Encoder Control - How-to
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: SmilinVamp
  • Date Created: 12 Jan 2016 5:55 PM Date Created
  • Views 280 views
  • Likes 0 likes
  • Comments 0 comments
  • userinterface
  • encoders
  • menu
  • arduino
  • navigation
Related
Recommended

BITS N BYTES – Arduino Encoder Control - How-to

SmilinVamp
SmilinVamp
12 Jan 2016

Example Rotary Encoder Picture

Example Rotary Encoder

 

I have recently acquired a piece of hardware from a friend.  The item in question is a really cool, high current triple output power supply.  I have big plans for a remote control head for this unit that will turn it into a really cool bench power supply.  Before I get to that project, I am in the midst of building a few bits and pieces with intentions of adding them all together for the power supply.

In my opinion a simple to use, intuitive user interface is always the best approach when designing an instrument type of project.  This little gizmo may prove useful to you.  Using a simple rotary encoder to adjust values and to navigate menus is just one approach to this goal.  I have a number of CUI Inc. ACZ16 Series mechanical rotary encoders lying around, so I have decided to use this device for my project.  Of course an optical encoder is probably more ideal, but my decision here is purely one of convenience.  Besides they are cheap!

Some Theory on Encoders

Basically, what we are about to do is detect when a major transition step occurs within the encoder.  Depending on what transition occurred, we can tell if the encoder was rotated clockwise or anti-clockwise.  Refer to Figure 1 below for the timing (switching) diagram.  The output of our encoder will produce two square waves.  One signal from the A and one signal from B terminal.  These two signals are produced out of phase by 90 degrees.  When we observe a low-to-high transition on terminal A, we can then look to terminal B to see if the signal is high or low.  If  B is high we know that the encoder was turned clockwise.  If  B is low, we know that the encoder was turned anti-clockwise.  Now, since we are human and by nature slow, this algorithm works for us.  If transitions are missed or any number of strange occurrence happen, we should not care in this application.  Things should happily move along unnoticed.  If you are looking for a more precise control method or your application is accuracy dependent (Like for example, a position encoder on an electric motor) – this bit is probably not for you.

Arduino Encoder - Output Waveform Data

Figure 1: Arduino Encoder – Output Waveform Data

 

Arduino Encoder - Test Circuit

Arduino Encoder – Test Circuit

ARDUINO – CODE EXAMPLE

 

Rotary Encoder Control for Arduino - with Interrupt

 

 

 

 

 

Arduino

 

1

 

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

 

/*

 

Encoder Demo with LCD - Interrupts enabled

Richard Galambos - August 7.2014

 

Demonstrates the use of a CUI ACZ16 Encoder with a 16x2 LCD display.

Interrupts enabled

 

The LiquidCrystal library works with all LCD displays that are

compatible with the Hitachi HD44780 driver with the 16-pin interface.

 

  Connections:

- LCD RS pin to digital pin 13

- LCD Enable pin to digital pin 12

- LCD D4 pin to digital pin 8

- LCD D5 pin to digital pin 7

- LCD D6 pin to digital pin 4

- LCD D7 pin to digital pin 2

- LCD R/W pin to ground

 

- Encoder output A pin 3

- Encoder output B pin 10

 

*/

 

// include the library code:

#include <LiquidCrystal.h>

 

// define encoder pins

#define encoder0PinA 3

#define encoder0PinB 10

   

    // initialize variables for Encoder

    int encoder0Pos = 0; //initialize encoder position

    int encoder0PinALast = LOW; // last state variables

    int n = LOW;

   

    // initialize the library with the numbers of the interface pins

    LiquidCrystal lcd(13, 12, 8, 7, 4, 2);

 

void setup() {

  // set up the LCD

  lcd.begin(16, 2);

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("POSITION: ");

  lcd.setCursor(11,0);

  lcd.print(0);

 

  // Set up I/O Digital Pins 

  pinMode(encoder0PinA, INPUT);

  //digitalWrite(encoder0PinA, HIGH);      // turn on pullup resistor

  pinMode(encoder0PinB, INPUT);

  //digitalWrite(encoder0PinB, HIGH);      // turn on pullup resistor

 

  // Setup the interupt

  attachInterrupt(1, EncoderChanged, CHANGE);  // encoder pin on interrupt 1 - pin 3

}

class Human {

    private int age = 0;

    public void birthday() {

        age++;

        print('Happy Birthday!');

    }

}

//Main code

void loop() {

      delay(500);

      }

//Encoder interupt routine

void EncoderChanged() {

  n = digitalRead(encoder0PinA);    //temporary read value of A on Encoder

  // Check to see if previous value was low and if the current value is high

  if ((encoder0PinALast == LOW) && (n == HIGH)) {   

    // if the condition is true then increment the position counter

    if (digitalRead(encoder0PinB) == LOW) {

      encoder0Pos++;

    }

    // Otherwise decrement the position counter

    else {

      encoder0Pos--;

    }

      // set the cursor to column 5, line 0

      // (note: line 0 is the first row, since counting begins with 0):

      lcd.clear();

      lcd.setCursor(0,0);

      lcd.print("POSITION: ");

      lcd.setCursor(11,0);

      lcd.print(encoder0Pos);

     

  }

  // set the Last Pin State

  encoder0PinALast = n;

 

}

 

ARDUINO - Wiring and Prototypeing

ARDUINO – Wiring and Prototyping

 

SEE MORE AT WWW.TECHMAESTER.COM

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube