简体   繁体   中英

Changing Raspberry Pi volume with Python

Hardware

  • Arduino Uno
  • Potentiometer
  • Raspberry Pi 3
  • Raspberry Pi Os
  • Hifiberry DAC Pro2

Setup

Potentiometer is running on the Arduino, I'm sending the signal through the USB Serial port to the Raspberry Pi. The Code is complete and working on the Arduino and I'm able to get the signal and print it using a python script on the Pi.

I have remapped the signal range from 0-100 on the Arduino side and I'm looking to use this value to control the volume on the raspberry Pi for a music box I am creating.

Issue

I'm currently using this answer found here as a guide. However, when I run the python script I get the following error in the terminal.

" amixer: Unable to find simple control 'Master',0 "

But, when I run the command (amixer scontrols) I show a Master, 0 listed. See below

" pi@raspberrypi:~ $ amixer scontrols Simple mixer control 'Master',0 Simple mixer control 'Capture',0 "

Why is the script not seeing the hardware or what am I doing wrong? Here is my script in it's current form. Once I can get the volume to change I'll work on making the loop, I'm new to python and trying to learn something, but I've been stuck on this for a few weeks. Any help is greatly appreciated.

#import serial adapter library and asla audio library
import subprocess
import serial
import alsaaudio

#Variables
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.flush()

volume = ser
command = ["amixer", "sset", "Master", "{}%".format(volume)]
subprocess.Popen(command)


#Imports serial value and prints to monitor
while True:  
    read_serial=ser.readline()
    print(read_serial)

The problem comes from the 'sudo'

If you try amixer sset Master 100% in you're shell, it's going to work

But if you try sudo amixer sset Master 100% , you will have amixer: Unable to find simple control 'Master',0

My advice: don't write sudo python volcon.py but rather python volcon.py to execute your code

If it doesn't work, or if you really need permissions by sudo, you may use other command. For example amixer cset numid=1 100%

PS: the only thing I'm sure is that amixer sset Master 100% has a different response with a 'sudo', for the solution you can try what I adviced but I'm not sure about the result

Edit:

Please try amixer scontrols and sudo amixer scontrols . If the results are different, select the one from 'sudo...' On my Raspberry, I have

Simple mixer control 'Master',0
Simple mixer control 'Capture',0

for the first one and

Simple mixer control 'Headphone',0

for the second one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM