Skip to content

Instantly share code, notes, and snippets.

@lianyi
Created February 28, 2019 01:40
Show Gist options
  • Save lianyi/d2f493460251f08078d35b0a0c331610 to your computer and use it in GitHub Desktop.
Save lianyi/d2f493460251f08078d35b0a0c331610 to your computer and use it in GitHub Desktop.
The following code can be downloaded directly to your Raspberry Pi. It will read the trimpot value, translate the reading to a volume range and modify the OS output volume level on your Raspberry Pi. The remap_range() method is being used to convert the 16-bit analog in range 0 - 65,535 to volume 0-100%.
import os
import time
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
# create an analog input channel on pin 0
chan0 = AnalogIn(mcp, MCP.P0)
print('Raw ADC Value: ', chan0.value)
print('ADC Voltage: ' + str(chan0.voltage) + 'V')
last_read = 0 # this keeps track of the last potentiometer value
tolerance = 250 # to keep from being jittery we'll only change
# volume when the pot has moved a significant amount
# on a 16-bit ADC
def remap_range(value, left_min, left_max, right_min, right_max):
# this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is
left_span = left_max - left_min
right_span = right_max - right_min
# Convert the left range into a 0-1 range (int)
valueScaled = int(value - left_min) / int(left_span)
# Convert the 0-1 range into a value in the right range.
return int(right_min + (valueScaled * right_span))
while True:
# we'll assume that the pot didn't move
trim_pot_changed = False
# read the analog pin
trim_pot = chan0.value
# how much has it changed since the last read?
pot_adjust = abs(trim_pot - last_read)
if pot_adjust > tolerance:
trim_pot_changed = True
if trim_pot_changed:
# convert 16bit adc0 (0-65535) trim pot read into 0-100 volume level
set_volume = remap_range(trim_pot, 0, 65535, 0, 100)
# set OS volume playback volume
print('Volume = {volume}%' .format(volume = set_volume))
set_vol_cmd = 'sudo amixer cset numid=1 -- {volume}% > /dev/null' \
.format(volume = set_volume)
os.system(set_vol_cmd)
# save the potentiometer reading for the next loop
last_read = trim_pot
# hang out and do nothing for a half second
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment