Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Adventures In Electronics: PC Volume Knob

I have some rather un­usu­al holes in my ed­u­ca­tion. I nev­er, for ex­am­ple, learned much about elec­tron­ic­s. It has al­ways been a mis­tery, a thing oth­er peo­ple knew about.

And then one day I ran in­to this gad­get:

What is it? A vol­ume knob for your PC. It plugs via USB and you can use it to turn vol­ume up or down. I sup­pose if you press it it mutes au­dio or some­thing.

It costs 50 dol­lars. 50 fuck­ing dol­lars. For a di­al.

Sure, it's pret­ty but how hard can it be?

So, I start­ed look­ing at how to do it. Here's what I learned first:

  • The thingie that spins and gives feed­back is called a "ro­tary en­coder"
  • I would need some­thing like a mi­cro­con­troller to ... well, con­trol it.
  • It would prob­a­bly in­volve sol­der­ing.
  • There are a bunch of tu­to­ri­als / in­structa­bles on how to do it.
  • The com­po­nents are damn cheap!

The last item is im­por­tan­t. When I was a kid in the 70s, elec­tron­ics was a thing for wealthy kid­s. I was not wealthy. So, the pos­si­bil­i­ty of do­ing this sort of thing? With cheap stuff? Sign me up!

So, I did what ev­ery­one does to learn stuff in 2019: I jumped in­to youtube and asked to be taught elec­tron­ic­s. And a day lat­er ... well, I know enough to break things and to im­ple­ment this!

The goal is:

  • USB vol­ume knob.
  • Press­ing it lights a LED
  • A but­ton click mutes the speak­ers
  • A longer click en­ables the mi­cro­phone while the di­al is pressed (push-­to-talk)

So, here is the BOM:

  • The cheap­est Ar­duino-­like thing with a USB in­ter­face: Digis­park
  • A ro­tary en­coder. I used a KY-040 be­cause it's cheap and work­s.
  • A gener­ic LED (red)
  • Some bread­board ca­bles.
  • A bread­board
  • A US­B-A male/fe­male ca­ble
  • A 1k re­sis­tor

A sec­ond stage (once I have an­oth­er Digis­park) will in­volve mak­ing it nice, but for now let's make it work.

Here is the wiring, which is prob­a­bly a pile of crap but works for me (sor­ry, don't want to learn how to do it prop­er­ly).

Wiring be­tween the Digis­park and the KY-040:

  • P0 -> CLK
  • P1 -> SW
  • P2 -> DT
  • 5V -> +
  • GND -> GND

I also connected KY-040's SW -> 1K resistor -> LED -> 5V so the LED turns on when the button is pressed, but that's optional.

IM­POR­TANT NOTE In or­der for P1 to work prop­er­ly, I need­ed to scratch off a con­nec­tion to dis­able the on­board LED so, if that's a prob­lem, you may be able to use P5 in­stead but P5 is dis­abled in the cheap Digis­park clones. We can't use P3 and P4 be­cause they are need­ed for US­B. So, your choice.

So, here is all the wiring. If the im­age dif­fers from my de­scrip­tion, trust the im­age be­cause it's work­ing ;-)

Once you have ev­ery­thing wired, we need to work on the soft­ware side of things.

I used a cou­ple of li­braries:

I had to configure a global shortcut to enable/disable the michrophone. I used the F10 key and the command pulseaudio-ctl mute-input but you figure out what you want to do.

I wrote a Sketch that does the fol­low­ing:

  • When the en­coder ro­tates clock­wise: send Vol­ume Up key.
  • When the en­coder ro­tates coun­ter-­clock­wise: send Vol­ume Down key.
  • When the en­coder is clicked less than half a sec­ond: send mute key.
  • When the en­coder is pressed for more than half a sec­ond: send mute-in­put tog­gle short­cut.
  • When the en­coder is re­leased af­ter be­ing clicked more than half a sec­ond: send mute-in­put tog­gle short­cut.

This way, if you want to mute, just click. If you want to talk, make sure you mute in­put when the ses­sion start­s, then click­-and-hold and while it's pressed the mi­cro­phone is en­abled. Nice, is­n't it?

Does it work? Oh yeah! (No, the mu­sic is not com­ing from the PC, just look at the screen to see what changes) and sor­ry this video is so crap­py.

And here's the code (which is my 1st ar­duino sketch, but I have been pro­gram­ming for a long time ;-)

#include "TrinketHidCombo.h"
#include <SimpleRotary.h>  // https://github.com/mprograms/SimpleRotary

// Pin A, Pin B, Button Pin
// Setting the button to 5 because this code handles it manually.
SimpleRotary rotary(0, 2, 5);

void setup() {
  TrinketHidCombo.begin();
  pinMode(1, INPUT);
}


void loop() {
  static unsigned long time_pressed = 0;
  static byte ptt_flag = 0;
  byte i = rotary.rotate();
  if (i == 1) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_UP);
  }
  else if (i == 2) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_DOWN);
  }

  int button = digitalRead(1);
  if (button == LOW) {  // Yes, clicking the button makes it LOW
    if (time_pressed == 0) {  // It's a new click
      time_pressed = millis();
    }
    else {  // Button has been pressed a while
      if ((millis() - time_pressed) > 500 && ptt_flag == 0) {
        // Pressed half a second, switch to push-to-talk
        // I configured my machine to toggle the input muting when F10 is clicked
        TrinketHidCombo.pressKey(0, KEYCODE_F10);
        TrinketHidCombo.pressKey(0, 0);
        ptt_flag = 1;
      }
    }
  }
  else {  // Button not pressed
    if (time_pressed) {// Has been pressed
      time_pressed = 0;
      if (ptt_flag == 0) {  // Was a short click
        // Toggle mute
        TrinketHidCombo.pressMultimediaKey(MMKEY_MUTE);          
      }
      else {  // Was a long click
        // Toggle push-to-talk
        TrinketHidCombo.pressKey(0, KEYCODE_F10);
        TrinketHidCombo.pressKey(0, 0);
        ptt_flag = 0;
      }
    }
  }
  TrinketHidCombo.poll();
}

Contents © 2000-2024 Roberto Alsina