Modular Music Box – Quadrature Rotary Encoder

The Rotary Encoder page at SparkFun has a datasheet, tutorials and additional info and advice in the comments:

  • though medecau’s QuadEncoder Arduino library doesn’t compile…
  • tronixstuff’s Chapter 11 section on the rotary encoder – and the code works well enough – though there’s a fair amount of ‘bouncing’…
  • but with a little additional research I managed to get the Reading rotary encoder on Arduino example code to work – and it does seem to provide the most consistent and ultimately useful response to encoder input for my needs.

I had to find out more about Port Registers“Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board” – with it’s associated pros and cons – and then find the necessary implementation for the Arduino Mega – and the Arduino: Forum Pinout for Port Registers on Arduino Mega?? post provided useful links to an online spreadsheet, mega schematic and PIN MAPPING ARDUINO MEGA.jpg as well as the DIYdrones.com Mega internal registers map post which helped me work out that the code on a vintage Atmega168-based Arduino Pro :

]
/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC

becomes – on an Arduino Mega

/* Rotary encoder readRotary encoder read example */
#define ENC_A 97
#define ENC_B 96
#define ENC_PORT PINF

though seemingly it only works on these pins – so I have more to find out here??

There’s further development of this code within the comments – but it’ll take more than a cursory cut and paste to get it working… though I definitely need to work more thoroughly on the various posts tagged ‘Encoder’ at Arduinoos to make the most of this approach and the code examples…

These issues aside I did manage to integrate this code with the 7-segment display example I was working on earlier to create a dual display which displays entries from the charset_7seg.h array when turning the encoder – at least when the Serial Monitor is open – albeit that it behaves a little idiosyncratically…

The Arduino: Playground has a Reading Rotary Encoders page with info and various coding examples – including using interrupts which I’ve implemented before in my Auduino switch – but I can’t seem to get any of the example code to work with my encoder… and likewise an Arduino + rotary encoder post on the SparkFun forum… and likewise code found at the Hifiduino Arduino Code for Rotary Encoder post and in the Comments… hmmm… (a simple analogRead() of a pot on Analog In 2 & 3 shows they are working)…

…but it turns out my total der brain isn’t!… I tried SunboX’s RotaryEncoder v0.9 library posted in the Arduino: Forum max with a rotary encoder post on the Mega PWM – NOT Analog – pins 2,3, & 4 and it works… and later in the post came across Keith Neufeld’s Arduino Quadrature Encoder Library available here to download – and though initially it didn’t compile… reading further in the post I came across instructions to add:

#undef int
#undef abs
#undef double
#undef float
#undef round

after all the #include statements in the Arduino sketch – and bingo!

My Rotary Encoder now works perfectly for my needs using this library – in conjunction with the DebounceButton library for the button press…

I may now revisit some of the code examples above and try them on the PWM pins

Posted January 3rd, 2011

One comment/trackback:

  1. Modular Music Box – MAX7221 | blog.monomatic.net:

    […] of the 74HC595 libraries working either (though I haven’t tried adding the code mentioned in the Modular Music Box – Quadrature Rotary Encoder […]