Introduction: DIY Raspberry Pi Volume Control Knob Using an ADS1115 ADC

About: Here at Progetto, we strive to help simplify your projects so you can spend more time working on the exciting stuff.

DISCLAIMER: CERTAIN COMPONENTS USED IN THIS TUTORIAL ARE FOR SALE BY THE AUTHOR

During this tutorial, you'll create a volume control knob for your Raspberry Pi using a potentiometer, an ADS1115 16-bit ADC, a breadboard, and some jumper wires. This is more of a tutorial module than a project and should serve as a great baseline for any projects where you need analog inputs on your Raspberry Pi. Without further ado, let's start making!

Supplies

To build this volume knob, you'll need:

  • A Raspberry Pi SBC (not a Pico) we used a Model 3, with Raspberry Pi OS installed and autologin enabled if you are running your Pi headless and want to run on startup
  • An ADS1115 ADC: https://www.tindie.com/products/Progetto/ads1115-16-bit-4-channel-adc-programmable-gain/, make sure to solder on some male headers
  • A 10K (Kilohm) Potentiometer, though other values may be used if you tweak the code (bonus points if you have a knob too)
  • Breadboard, I used a full pint
  • Jumper wires
  • A speaker or some other sound output (I used a bluetooth speaker)
  • (Optional) I used a GPIO extender for the Raspberry Pi so the wiring is a bit clearer but it's totally optional, here's one similar to mine: https://www.adafruit.com/product/2028

Step 1: Enable I2C on the Raspberry Pi

Before you do anything else, you'll need to enable the I2C interface on your Raspberry Pi, to do so, type:

sudo raspi-config

Navigate to Interface Options (hit Enter), then I2C then choose <Yes>, <Ok> then navigate to <Finish>.

Alternatively, follow this guide: https://www.raspberrypi-spy.co.uk/2014/11/enabling-the-i2c-interface-on-the-raspberry-pi/

Step 2: Wiring

The wiring is a pretty simple, see the Fritzing diagram above or follow the connections below:

RPI 3V3 -- ADS VDD -- POT LEFT PIN

RPI GND -- ADS GND -- POT RIGHT PIN

RPI SDA1 -- ADS SDA

RPI SCL1 -- ADS SCL

ADS A0 -- POT MIDDLE PIN

Step 3: Python Setup

First, open a terminal and update your system packages:

sudo apt update && sudo apt upgrade

Next, clone or download the code from this project's GitHub repository: https://github.com/ProgettoCompany/Raspberry_Pi_Volume_Knob

Now, navigate to the code's folder set up a python virtual environment which will keep your dependencies isolated for this project:

cd Raspberry_Pi_Volume_Knob
python3 -m venv env
source ./env/bin/activate

To install the dependencies, run:

pip install -r requirements.txt

Step 4: Run the Code and Calibrate the Endpoints

To run the code, use the Terminal you have open and type:

python main.py

As a reminder, you can stop the code at anytime by pressing Ctrl+C.

Make sure to move your potentiometer to the fullest extent of its range and check to see if the volume indicator in the top right of your screen is changing, if it isn't you might need to change the following line:

audio_device = "Master" # You may need to adjust this based on your system configuration

Also, take this time to check to see if the volume printed goes between 0 and 100%.

If the max volume goes over 100% increase the 26250 constant on that same line, and if it doesn't reach 100% you'll want to decrease that number:

volume = int((analog_value / 26250) * max_volume)

If the minimum value doesn't go to 0%, you might need to subtract a constant as shown below with 1000 as an example, if the minimum volume is less than 0%, add a value:

volume = int((analog_value / 26250) * max_volume) - 1000

This will require some trial and error as you restart your code and test both values, move on to the next step when you're happy with it.

Step 5: (Optional) Run the Code on Startup

Now, you probably don't want to have to open a terminal and run the code manually every time you want to use your volume knob, so instead we'll create a systemd user service which will automatically run the code at startup so you don't have to!

Fair warning: this part can be a bit finicky to set up, it took me a couple of hours trying to figure out why things weren't working originally but hopefully I've configured it so yours works first try!

First, edit the ExecStart= line of potentiometer_volume.service to point to the python binary in your virtual environment and also edit the path to your main.py file we ran in the previous step, I have included my service file in the picture above.

Alright, now move potentiometer_volume.service to /etc/systemd/user, you can do this with the command line by running:

mv potentiometer_volume.service /etc/systemd/user

Now you can try it out by running:

systemctl --user daemon-reload # tip: run this file every time you change a systemd service!
systemctl --user start potentiometer_volume.service

You should now see any output in the volume.log file on your Desktop and be able to control the volume! FYI, you can change the log location by editing the path on StandardOutput=.

To run your code after login, type:

systemctl --user enable potentiometer_volume.service

If you ever want to stop it from running on startup, type:

systemctl --user disable potentiometer_volume.service

Right now, this will stop working if you log out, to persist between log in sessions, run:

loginctl enable-linger # Note: this affects all systemd user services

You should be ready for the real test, reboot your Pi and check if you can control the volume after logging in:

sudo reboot


A few notes in case you're interested (or troubleshooting):

ExecStartPre=/bin/sleep 10

Is included to ensure the Pi can actually find the audio device and the Bluetooth to start up (if you're using a Bluetooth speaker like I was).

Also this service will attempt to restart every 10 seconds if it fails, you can disable that by removing these 2 lines:

Restart=on-failure
RestartSec=10

If you want to learn more about systemd user services, check out this excellent guide that helped me out: https://www.baeldung.com/linux/systemd-create-user-services

Step 6: Conclusion

Whew! All done. Now that you have your very own volume knob for your Raspberry Pi, here are a few ideas to expand on this project:

  • Make a 3D printed enclosure
  • Solder everything onto a PCB to make it more permanent
  • Add more knobs by using the other 3 analog channels on your ADC
  • Control something else with the knob
  • Make your own radio, jukebox, or Spotify music player (hint: use the Spotify API)
  • Anything else you can dream of!

If you have any questions, comments or concerns, feel free to email me at progettocompany@gmail.com

Happy hacking!

John Wolf

Progetto Company

Make Some Noise Contest

Participated in the
Make Some Noise Contest