Attiny2313 & Attiny84 V-USB Media Volume Control

My most rewarding projects are the ones which i actually use. I like my audio gear so, this was the perfect companion project!

On my desk i have a headphone amplifier for my AKG K702’s headphones, below it i have my self built Onixia integrated amplifier for my bookshelf speakers. I have been thinking about making a global volume control for both devices for quite some time… here is the results of my efforts.Attiny2313 & Attiny84 V-USB Media Volume Control

Design Decisions


Encoder Choice….

Although you may think a rotary encoder is just a rotary encoder, when selecting one for a specific purpose such as a volume knob, the feel  and quality can play a large part in your design decision. You only have to look at the vast part numbers in any given range to appreciate this. The different manufacturers cater for detent or no detent, the number of detents or the amount of pulses. Even how much torque it takes to either turn the shaft or depress it for the shaft button. Where quality is concerned, this is really down to build and electrical contact quality and how much debouncing you will end up doing in your design.It really can be a minefield…
On my first prototype PCB, I initially mounted a cheap chinese rotary encoder. Following some hardware debouncing, using capacitors, it was fine for most tasks. However, I always got the feeling there were missed steps on the audio volume scale. I did not go into much testing of the Chinese encoder as I soon switched to a Bourns PEC11 rotary encoder, after installing the Bournes encoder. I noticed the volume scale to be much smoother than before, but with a tiny amount of what felt like lag when rotating quickly. After more experimentation, by rotating the shaft very quickly, the volume scale would shoot to 100%  as if by itself! 

After some thought, it occurred to me that in order to reach 100% volume scale it took 2 full rotations of the shaft. The perceived lag, at speed, was actually the Microcontroller just catching up!  This in turn meant that it was also counting every single step from the encoder, whereas, the cheaper quality encoder halted the scale quicker through missing steps. 

Update to the lag issue:
After further investigation it turns out that the lag was actually caused by Microsofts IntelliType Pro keyboard software. I was initially using the Microsoft keyboard software for the volume scale overlay, but after uninstalling the software the global lag was no longer present!


I will touch on the phenomenon of debouncing for the layman, this usually occurs with mechanical switches. If you imagine you have a simple circuit with a battery, an on/off switch and a light bulb… you press the switch and the lightbulb turns on. This could be described as a simple analog interaction. Now if you imagine a switch with dirty contact points, and the contacts arc, the light bulb will flicker a lot due to being turned on and off many times a second similar to a bad connection on a plug.
This can be used to explain how a microcontroller sees the physical world, especially concerning 2 pieces of metal touching each other in an imprecise way, as in the case of a mechanical switch. This is a concern as the Microcontroller will sense the switch thousands of times a second for a change in state, usually High (1) or Low (0) eg. on/off .
In reality, all switches good quality or not are bad connections to the Microcontroller and sampling it thousands of times a second while a person presses a button, does not help and inevitably errors will occur and the incorrect data will be passed on. With a rotary encoder this will manifest itself as missed steps or even steps back when going forward.
For more detail: Attiny2313 & Attiny84 V-USB Media Volume Control

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top