Arduino

DIY Control PC Volume, Photoshop, Lightroom, Premiere Pro & more using Arduino and Rotary Encoder [Volume knob]6 min read

February 15, 2021 4 min read

DIY Control PC Volume, Photoshop, Lightroom, Premiere Pro & more using Arduino and Rotary Encoder [Volume knob]6 min read

Reading Time: 4 minutes

Want to know how to create a physical knob to control your PC’s volume? and also easily control apps like Photoshop, Lightroom, Premiere Pro etc with it?

In this quick tutorial, I’ll show you how you can create a knob using an Arduino and Rotary encoder, to control your PC’s volume and other apps. That too under a budget of $15!

Things You’ll Need:

  1. Rotary Encoder
  2. Arduino Leonardo
  3. Jumper Cables

You can buy each of these products from the links above.

Video Tutorial

https://youtu.be/_-_Q7BhXLiw

All the steps are covered in detail with graphics for better explanation in the video. But if your are more of a reader than a watcher then let’s continue with the article.

Control PC volume with Arduino

Step 1: Arduino Sketch & Hardware Assembly

  1. Firstly, we’ll connect the Arduino with the Rotary Encoder using some Jumper cables. The table below shows the digital pins of Arduino I used to connect it to the encoder:
Arduino PinRotary Encoder Pin
Ground (GND)Ground (GND)
5v Power (VCC)Power (+)
D1CLK
D2DT
D3SW
yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - DIY Control PC Volume, Photoshop, Lightroom, Premiere Pro & more using Arduino and Rotary Encoder [Volume knob]
  1. Next, Connect the Arduino IDE to the PC and also Download the Arduino IDE.
  2. In the Arduino IDE, Goto Tools > Port and make sure an appropriate port is selected. In my case it was COM11. Next, Goto Tools > Board and select the type of Arduino board that you have, in my case it was Arduino Leonardo.
  3. Goto Sketch > Include library > Manage libraries. In the window that opens up, search for ‘HID-Project’ and install the HID-project library by Nicohood.
  4. Next, open this link to open the GitHub page of this project and copy the code from there to the Arduino IDE.

NOTE: Please watch the video above for a detailed walkthrough and explanation of the code.

Currently, in the repository, you will find the code to increase or decrease the volume. For that, I have used the Nicohood library that we installed earlier. I have used Consumer.write(MEDIA_VOLUME_UP) to increase the volume. Similarly, MEDIA_VOLUME_DOWN to decrease the volume. You can find a list of all the constants that you can use in the documentation of NicoHood library.

Let’s not limit our setup to just increase or decrease the volume, let’s also do some other cool stuff with it.

Step 2: Configuring different modes.

The knob of the rotary encoder can also be pushed like a button. We can read the value of the button from the SW pin of the encoder.

So let’s define different modes for our encoder. Mode 1 will increase and decrease the volume, Mode 2 will change the tabs of the browser, Mode 3 will control Lightroom sliders and PowerPoint presentations and Mode 4 will increase or decrease the size of the brush in photoshop.

So in our code, let’s declare 2 variables called mode and maxModes. The mode variable will store the currently selected mode, which by default would be mode 1. While the maxModes variable will store the total number of modes we want to configure, which in my case would be 4.

int mode = 1;
int maxModes = 4;

Next, let’s define a method called changeMode which will increment the currently selected mode when the button of rotary encoder is pressed.

void changeMode() {
    mode = (mode % maxModes) + 1;
}

Here, when this method is called, it will increase the mode by 1. When the mode is equal to maxModes and this method is called again, the currently selected mode will jump back to 1.

Next, let’s call this changeMode method when the button is pressed. So at the end of loop method, paste this code:

if(!digitalRead(inputSW)) {
    changeMode();
    delay(300);
}

So, in the end the loop method will look like this:

void loop() { 
	// Read the current state of inputCLK
	currentStateCLK = digitalRead(inputCLK);

	// If the previous and the current state of the inputCLK are different then a pulse has occured
	if (currentStateCLK != previousStateCLK){ 
		// If the inputDT state is different than the inputCLK state then 
		// the encoder is rotating counterclockwise
		if (digitalRead(inputDT) != currentStateCLK) { 
			rotateRight();
		} else {
			rotateLeft();
		}
	} 
	// Update previousStateCLK with the current state
	previousStateCLK = currentStateCLK; 

        if(!digitalRead(inputSW)) {
            changeMode();
            delay(300);
        }
}

Next, in the rotateRight method let’s write a switch case to handle all the cases and configure the modes:

void rotateLeft() {
  switch(mode) {
    case 1:
      // Increase the volume.
      Consumer.write(MEDIA_VOLUME_UP);
      break;
    case 2: 
      //Cltr + TAB
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_TAB);
      Keyboard.releaseAll();
      break;
    case 3:
      // Lightroom, PowerPoint
      Keyboard.press(KEY_UP_ARROW);
      break;
    case 4:
      // Photoshop Brush size
      Keyboard.press('[');
      break;
  }
}

Here, for case 1, we are simply increase the volume as we did earlier.

In case 2, we want to cycle between the browser tabs. We can cycle the browser tabs using the key combination Control + Tab. So in the code as well, I have sent Control + tab as well. To configure that, I have used the arduino Keyboard library. You can find all the key definitions used here and more in the official documentation of keyboard library.

In case 3, to control the sliders of lightroom and also to change the slides of a PowerPoint presentation, we can use the up/down arrow keys. Similarly, in case 4, to increase the size of the brush we can user the shortcut to increase brush size i.e. by pressing [

We can configure the rotateRight method in a similar way:

void rotateRight() {
  switch(mode) {
    case 1:
      // Decrease the volume.
      Consumer.write(MEDIA_VOLUME_DOWN);
      break;
    case 2: 
      //Cltr + SHIFT + TAB
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_SHIFT);
      Keyboard.press(KEY_TAB);
      Keyboard.releaseAll();
      break;
    case 3:
      // Lightroom, PowerPoint
      Keyboard.press(KEY_DOWN_ARROW);
      break;
    case 4:
      // Photoshop Brush size
      Keyboard.press(']');
      break;
  }
}

Now you can upload this code and test it out. It should work fine.

More articles like this:

Blogger, Web Developer, Software Developer and Indie Game Developer.
Leave a comment

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