Arduino Nano esp32 media knob

a basic, though oversized, media knob for the Arduino nano esp32.
5
3
0
113
updated December 11, 2024

Description

PDF

 

Assembly:

  1. Print the parts.
  2. Place the Arduino inside the base upside-down with the usb port facing the hole in the base.
  3. Insert five female to female jumper wires into the ground, 3.3 v, D2, D3, and D4. Remember which wire is which.
  4. Screw the lid onto the base with the wires coming out the hole in the lid part. This step is a little tricky. I had to use a screwdriver to press on the Arduino as I screwed the top on so it would stay in place.
  5. Connect the wire from you nano's ground to the rotary encoder. Connect the wire from the nano's 3.3 v to the positive pin on the rotary encoder. Connect the wire from the nano's D2 to the button pin of the encoder. Connect the wire from the nano's D3 pin to the DT pin of the encoder. Connect the wire from the nano's D4 pin to the CLK pin of the encoder.
  6. Place the rotary encoder into the slot on the lid and hot glue it onto the lid with the holder part. (refer to image below)
  7. Push the knob onto the rotary encoder.

Code 1: volume knob

  1. Download https://github.com/bonkmachines/ctrl-arduino library and install the zip to arduino. the other libraries should be pre installed.
  2. Upload this code.

Spinning the knob changes the volume and clicking the knob plays/pauses the music or video.

#if ARDUINO_USB_MODE
#warning This sketch should be used when USB is in OTG mode
void setup() {}
void loop() {}
#else
#include "USB.h"
#include "USBHIDConsumerControl.h"
#include <CtrlEnc.h>

USBHIDConsumerControl ConsumerControl;
// Define an onTurnleft handler.
void onTurnleft() {
  ConsumerControl.press(CONSUMER_CONTROL_VOLUME_DECREMENT);
  ConsumerControl.release();
}
// Define an onTurnRight handler.
void onTurnRight() {
  ConsumerControl.press(CONSUMER_CONTROL_VOLUME_INCREMENT);
  ConsumerControl.release();
}
//-----------------------------------------------------------//
//OTHER CONTROLL OPTIONS
//-----------------------------------------------------------//
/*
// Power Control
#define CONSUMER_CONTROL_POWER                             0x0030
#define CONSUMER_CONTROL_RESET                             0x0031
#define CONSUMER_CONTROL_SLEEP                             0x0032

// Screen Brightness
#define CONSUMER_CONTROL_BRIGHTNESS_INCREMENT              0x006F
#define CONSUMER_CONTROL_BRIGHTNESS_DECREMENT              0x0070

// These HID usages operate only on mobile systems (battery powered) and
// require Windows 8 (build 8302 or greater).
#define CONSUMER_CONTROL_WIRELESS_RADIO_CONTROLS           0x000C
#define CONSUMER_CONTROL_WIRELESS_RADIO_BUTTONS            0x00C6
#define CONSUMER_CONTROL_WIRELESS_RADIO_LED                0x00C7
#define CONSUMER_CONTROL_WIRELESS_RADIO_SLIDER_SWITCH      0x00C8

// Media Control
#define CONSUMER_CONTROL_PLAY_PAUSE                        0x00CD
#define CONSUMER_CONTROL_SCAN_NEXT                         0x00B5
#define CONSUMER_CONTROL_SCAN_PREVIOUS                     0x00B6
#define CONSUMER_CONTROL_STOP                              0x00B7
#define CONSUMER_CONTROL_VOLUME                            0x00E0
#define CONSUMER_CONTROL_MUTE                              0x00E2
#define CONSUMER_CONTROL_BASS                              0x00E3
#define CONSUMER_CONTROL_TREBLE                            0x00E4
#define CONSUMER_CONTROL_BASS_BOOST                        0x00E5
#define CONSUMER_CONTROL_VOLUME_INCREMENT                  0x00E9
#define CONSUMER_CONTROL_VOLUME_DECREMENT                  0x00EA
#define CONSUMER_CONTROL_BASS_INCREMENT                    0x0152
#define CONSUMER_CONTROL_BASS_DECREMENT                    0x0153
#define CONSUMER_CONTROL_TREBLE_INCREMENT                  0x0154
#define CONSUMER_CONTROL_TREBLE_DECREMENT                  0x0155

// Application Launcher
#define CONSUMER_CONTROL_CONFIGURATION                     0x0183
#define CONSUMER_CONTROL_EMAIL_READER                      0x018A
#define CONSUMER_CONTROL_CALCULATOR                        0x0192
#define CONSUMER_CONTROL_LOCAL_BROWSER                     0x0194

// Browser/Explorer Specific
#define CONSUMER_CONTROL_SEARCH                            0x0221
#define CONSUMER_CONTROL_HOME                              0x0223
#define CONSUMER_CONTROL_BACK                              0x0224
#define CONSUMER_CONTROL_FORWARD                           0x0225
#define CONSUMER_CONTROL_BR_STOP                           0x0226
#define CONSUMER_CONTROL_REFRESH                           0x0227
#define CONSUMER_CONTROL_BOOKMARKS                         0x022A
*/
//-----------------------------------------------------------//
CtrlEnc encoder(3, 4, onTurnleft, onTurnRight);
void setup() {
  pinMode(2, INPUT_PULLUP);
  ConsumerControl.begin();
  USB.begin();
}
void loop() {
  encoder.process();
  // Serial.println(digitalRead(2));
  if (digitalRead(2) == LOW) {
    //   Serial.println("if statement");
    ConsumerControl.press(CONSUMER_CONTROL_PLAY_PAUSE);
    ConsumerControl.release();
    while (digitalRead(2) == LOW) {
      //   Serial.println("in while loop");
      delay(300);
    }
  }
}
#endif /* ARDUINO_USB_MODE */

Code 2: scrolling knob

  1. Download https://github.com/bonkmachines/ctrl-arduino library and install the zip to Arduino. the other libraries should be pre installed.
  2. Upload this code.

At first the knob is in vertical mode. In vertical mode spinning the knob moves the page up and down. Clicking the knob changes it to horizontal mode. In horizontal mode spinning the knob moves it left and right. Clicking changes it back to vertical mode.

#if ARDUINO_USB_MODE
#warning This sketch should be used when USB is in OTG mode
void setup() {}
void loop() {}
#else

#include "USB.h"
#include "USBHIDMouse.h"
#include <CtrlEnc.h>
USBHIDMouse Mouse;

bool movestate = 0;
//0 equals up-down 1 equals left-right 
// Define an onTurnleft handler.
void onTurnleft() {
if(movestate == 0){
Mouse.move(0, 0, 1, 0);
}
else{
Mouse.move(0, 0, 0, -1);
}
}
// Define an onTurnRight handler.
void onTurnRight() {
if(movestate == 0){
Mouse.move(0, 0, -1, 0);
}
else{
Mouse.move(0, 0, 0, 1); 
}
}
CtrlEnc encoder(3, 4, onTurnleft, onTurnRight);
void setup() {
  // put your setup code here, to run once:
  Mouse.begin();
  USB.begin();
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  encoder.process();

if (digitalRead(2) == LOW) {
    //   Serial.println("if statement");
    if(movestate == 1){
      movestate = 0;
    }
    else{
      movestate = 1;
    }
    while (digitalRead(2) == LOW) {
      //   Serial.println("in while loop");
      delay(300);
    }
  }
}
#endif /* ARDUINO_USB_MODE */

Enjoy your media knob and customize it to you liking!

Model based off of Container - Screw Cap, 4 Sizes, All Purpose - Tin, Box, Can by guppyk

inspired by https://www.printables.com/model/887330-usb-media-dial by https://www.printables.com/@Adafruit

Tags



Model origin

The author remixed this model.

Differences of the remix compared to the original

i have shortened the container. edited the inside. punched holes through the parts. turned it into a media dial. used it as an electronics case.

License