GPIO Pins to control volume

I connected rotary encoders to control tracks and playback/pause. Used PEC11 without detents & with 24detents/24pulse (Bourns) and encoder from minidisc deck Sony MDS-S39 (ALPS). Does not work correctly not one. The script does not have time for short pulses. Tracks switch is not correct. Or no response to the command. Or randomly switching. It is possible to somehow fix at the program level ? Or look self-return rotary encoder or another switch ?
alps.com/prod/info/E/HTML/En … 2010H.html

Just installed sudo dpkg -i python-rpi.gpio_0.5.9-1_armhf.deb
But i need your Help, want to switch GPIO Outs, best over Webinterface, any ideas how to do it?

Hi !
@solaar1974 : Have you tried to change the time.sleep value ? Not sure it will change your problem, but…
@buckler and anyone else
I wrote a script to manage my buttons and it works perfecltly :wink: but now I want to add some features :
A short press on NEXT button -> Next song with the command " subprocess.call(['mpc', 'next' ])
A long press on the NEXT button -> Seek forward (by 8 sec) with the command subprocess.call(['mpc', 'seek', '+00:00:08' ])
And the same thing for the previous button. I just try to wrote it for the next button.
It’s quite similar to what you have done. But I don’t use it with the rotary encoder. And there something wrong in my code I don’t understand… I need help to debug it…
Thank you
I know my code is dirty, but I did plenty of test…

[code]#!/usr/bin/python

button-rasp

December 28, 2014

Remix by Balbuze (balbuze@gmail.com)2014 December

written for Raspberry B+ :for other model please re-assign gpio port!!!

add physical buttons for a mdp player system (used with Volumio)

Provide : -previous

-next

-stop

-play

-volume control through rotary coder

My first python script !!! Surely not perfect…To be improve

To do : add code to manage lcd display

inspirated from :

Connecting a Push Switch http://razzpisampler.oreilly.com/ch07.html

Tutorial: Raspberry Pi GPIO PINS AND PYTHON http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/

radio.py

January 23, 2013

Written by Sheldon Hartling for Usual Panic.

MIT license, all text above must be included in any redistribution

based on code from Kyle Prier (http://wwww.youtube.com/meistervision)

and AdaFruit Industries (https://www.adafruit.com)

Kyle Prier - https://www.dropbox.com/s/w2y8xx7t6gkq8yz/radio.py

AdaFruit - https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git, Adafruit_CharLCD

Rotary class part from : Author : Bob Rathbone

Site : http://www.bobrathbone.com

import RPi.GPIO as GPIO
import time
import sys,os
#import mpd
#import random
from rotary_class import RotaryEncoder
import subprocess
#tglobal time_stamp
#from datetime import timedelta,datetime
#time_stamp = time.time()
#short_press = 1
#long_press = 3
time_now = ‘’
#button_press_timer = 0

#import pdb
#-----------------------------------------------------------

GPIO.setmode(GPIO.BCM)

Define GPIO output pins for Radio Controls

SW_PREV = 12 # Pin 32
SW_NEXT = 16 # Pin 36
SW_STOP = 13 # Pin 33
SW_SHTDWN = 6 # Pin 31
SW_PLAY = 26 # Pin 37

Define GPIO for rotary encoder + button

PIN_A = 23 # Pin 16
PIN_B = 24 # Pin 18
BUTTON = 25 # Pin 22
#------------------------------------------------------------
#As I have a PI-amp from Iqaudio I need to unmute it at startup

#A piece of code will be used to mute it at shutdown
#------------------------------------------------------------

#Code to manage Rotary encoder + switch

This is the event callback routine to handle events

#pdb.set_trace()
def switch_event(event):
if event == RotaryEncoder.CLOCKWISE:
subprocess.call([‘mpc’, ‘volume’, ‘+2’ ])
time.sleep(.2)
elif event == RotaryEncoder.ANTICLOCKWISE:
subprocess.call([‘mpc’, ‘volume’, ‘-2’ ])
time.sleep(.2)
elif event == RotaryEncoder.BUTTONDOWN:
subprocess.call([‘mpc’, ‘toggle’ ])
print ‘toggle’

#elif event == RotaryEncoder.BUTTONUP:

#print “Button up”

    return

#pdb.set_trace()

Define the switch

rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)
#----------------------------------------------------------

#code to manage BUTTONS previous,next,stop,play,shutdown
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_STOP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_SHTDWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
time_stamp = time.time()
button_press_timer = 0
delta = ‘’
while True:
try:
prev_switch = GPIO.input(SW_PREV)
next_switch = GPIO.input(SW_NEXT)
stop_switch = GPIO.input(SW_STOP)
shtdwn_switch = GPIO.input(SW_SHTDWN)
play_switch = GPIO.input(SW_PLAY)
#global time_stamp
if prev_switch == False:
subprocess.call([‘mpc’, ‘prev’ ])
print “PRECED”
elif next_switch == False:
global time_stamp
time_stamp=time.time()
time.sleep(.1)
#else next_switch != False
if next_switch != False:
time_now=time.time()
delta=int(time_now-time_stamp)

print button_press_timer

           #print "Button Up"
         			if delta <= 1:
			#print time_stamp
				print delta
              			subprocess.call(['mpc', 'next' ])
 				elif delta > 2:
				subprocess.call(['mpc', 'seek', '+00:00:08' ])
            elif stop_switch == False:
		subprocess.call(['mpc', 'stop' ])
            	print "stop"
	elif play_switch == False:
                    subprocess.call(['mpc', 'toggle' ])

print “play”

            elif shtdwn_switch == False:
		os.system("shutdown now -h")
		print "shutdown in progress : wait 20 sec before unplug"
	time.sleep(0.5)

except KeyboardInterrupt:
	print "\nExit"
	GPIO.setwarnings(False)
	GPIO.cleanup()
	sys.exit(0)

End of program[/code]

Hi

I try this script and it works great in my project, but I have one question if it possible to do this:

I need to program another button to toggle through playlists. Is this possible? How?

Hi guys

I was wondering if is there any way to program a single button to do 3 different action.

Something like, 1 press = pause, 2 press = next, 3 press = shutdown.

I read something on the internet about a pulse counter to introduce in the script but i have no idea how to do that !!!

Thanks in advance guys.

Hi,
I totally would appreciate that too:
1 press = pause, 2 press = next, 3 press = shutdown.

Could anybody tell us, how to achieve this?

Many thanks and best regards
muhackl

Hi,

I have developed something similar featuring a rotary encoder with an embedded switch. The code is doing more than controlling mpd with encoder but this part could be easily extracted. I have not implemented the double click. What this does is : short click -> mute, long click -> off.
Double click should be possible to implement without changing the existing logic.

For more information :
github.com/PhilippeMeyer/ampCtl

Best

Hi,

Just a quick update : the double click is definitely a good idea, so good that I could not wait any longer to have it!
Now implemented see update on github

Best,

Hello guys,

I’ve been playing around with my raspberry and volumio since a couple of days, and I’ve got a lot of what was mentioned in this thread working. Thank you guys for that!

Now I have a question. I’d like to use one button for play and pause simultaneously. That means, when the button is pressed, I need to check whether the mpc is currently on play or pause, and then do the opposite.
Does anyone now how this would be manageable using mpc commands?

I can not find a way to read anything from mpc in the documentation.

linux.die.net/man/1/mpc

Alright guys, I didn’t read the documentation correctly. There actually is a command called ‘toggle’ that switches between play and pause. Problem solved. Before that, I solved it with a variable that I set either true of false with every push of the button. That way I could switch between the commands ‘play’ and ‘pause’ with every call of the function.

Here’s my (for now) final working code. Maybe it will help someone. I mainly worked with threads, including a while(true) loop only to keep the programm from terminating.

[code]import RPi.GPIO as GPIO
import subprocess
import sys, os
import time
from rotary_class import RotaryEncoder

GPIO.setmode(GPIO.BCM)

SHUTDWN = 22
SW_NEXT = 23
SW_PREV = 24
ROT_PIN_A = 17
ROT_PIN_B = 27
SW_PLAY = 10

GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(SHUTDWN,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def nextsong(channel):
subprocess.call([‘mpc’, ‘next’ ])
def prevsong(channel):
subprocess.call([‘mpc’, ‘prev’ ])
def shutdown(channel):
os.system(“shutdown now -h”)
def switch_event(event):
if event == RotaryEncoder.CLOCKWISE:
subprocess.call([‘mpc’, ‘volume’, ‘+5’ ])
time.sleep(.1)
elif event == RotaryEncoder.ANTICLOCKWISE:
subprocess.call([‘mpc’, ‘volume’, ‘-5’ ])
time.sleep(.1)
elif event == RotaryEncoder.BUTTONDOWN:
subprocess.call([‘mpc’, ‘toggle’ ])
#elif event == RotaryEncoder.BUTTONUP:
#print “Button up”
return

Define the switch

rswitch = RotaryEncoder(ROT_PIN_A,ROT_PIN_B,SW_PLAY,switch_event)

GPIO.add_event_detect(SW_NEXT, GPIO.RISING, callback=nextsong, bouncetime=300)
GPIO.add_event_detect(SW_PREV, GPIO.RISING, callback=prevsong, bouncetime=300)
GPIO.add_event_detect(SHUTDWN, GPIO.RISING, callback=shutdown, bouncetime=300)

while True:
try:
pass
except KeyboardInterrupt:
GPIO.cleanup()[/code]

However, I still do have problems with the software-debouncing of the rotary-encoder. It still seems to randomly do what it wants when turning it quickly. I’ll try do it with hardware for the moment. If I find a solution, I’ll post it here.

Greetings!

I have the same problem. I decided to editing rotary_class.py

GPIO.add_event_detect(self.pinA, GPIO.FALLING, callback=self.switch_event, bouncetime=50) GPIO.add_event_detect(self.pinB, GPIO.FALLING, callback=self.switch_event, bouncetime=50)
and in the script

def right_knob_event(event): if event == RotaryEncoder.CLOCKWISE: subprocess.call(['mpc', 'next' ]) time.sleep(3.0)
Work well.

There is another way to control the encoder github.com/guyc/py-gaugette I installed, test scripts work well. But I’m not a programmer, and correct to control the volume I could not

hello everyone,

i am new to this, so excuse the question. Why is not the following command working

wget https://sourceforge.net/projects/raspberry-gpio-python/files/raspbian-wheezy/python-rpi.gpio_0.5.8-1_armhf.deb
sudo dpkg -i python-rpi.gpio_0.5.8-1_armhf.deb

i get the following message

Resolving sourceforge.net (sourceforge.net)... 216.34.181.60
Connecting to sourceforge.net (sourceforge.net)|216.34.181.60|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2016-08-27 16:51:45 ERROR 404: Not Found.

volumio@volumio:~$ sudo dpkg -i python-rpi.gpio_0.5.8-1_armhf.deb
dpkg: error processing python-rpi.gpio_0.5.8-1_armhf.deb (--install):
 cannot access archive: No such file or directory
Errors were encountered while processing:
 python-rpi.gpio_0.5.8-1_armhf.deb

i connect to volumio (installed via hifiberry.exe) via putty using usrname/pwd volumio/volumio.

any ideas…?

thx in advance

Hi All

I tried to install volume rotary encoder folloving guide posted by Buckler on first page but it doesn’t work on my Raspberry Pi3 with Volumio 2.
In my sd card I can’t find the directory “/home/pi” so I create a directory “Scripts” and put the rotary_encoder file and the VolumeKnob.py script inside it.
It OK?
Another question: for compile the script I tried to respect the space between the line by using tab and space button; it’s right?
Anybody have some suggestion?

Thanks.
Andrea

I can’t really help you for the script, but the guide you followed is for volumio 1.5.
me/pi
In volumio 2 there an user volumio with its home folder /home/volumio and there is no /home/pi

Hi Balbuze

Thanks for your replay.
I’ll try to lock for a compatible version for Volumio 2.

HI Everybody, is there a Chance to get a Tutorial how i can Control the Volume with a Rotary Encoder? I have a Raspberry Pi 3 with volumio 2 and a Hifiberry Amp+. I testet the Plugin GPIO Button Controller (Thanks to tomatepasser) and it works great for me. But a rotary encoder is more interesting for me. I know there a lot Tutorials for Encoder but they are Working under v1.55. I am in the beginning with programming and i hope somebody can help me. Did i have to use this websocket API Things? I Read it but dont understand all thus Things. I am Happy to understand the Python basics a littel bit. Im searching for Days but i cant find a easy step by step solution.

Hi Everybody,

Can somebody help me to install the GPIO Lib for Volumio 2.17? Everytime i got a Error 404 lib not found. I want to Control the volume with a rotary encoder. Can Download the Volumio Version 1.5 somewhere, i find the Tutorials tat with an older Version Everything works fine.

i have a question:

–2017-07-11 17:11:38-- sourceforge.net/projects/raspberry-gpio-python/ files/raspbian-wheezy/python-rpi.gpio_0.5.8-1_armhf.deb
Connecting to sourceforge.net (sourceforge.net)|216.34.181.60|:443… connected.
HTTP request sent, awaiting response… 404 Not Found
2017-07-11 17:11:39 ERROR 404: Not Found.

this is at the end of entering
wget sourceforge.net/projects/raspber … _armhf.deb

how can i get around this? is there a way to get this installed?

Anyone?

I lately stumbled upon this:
github.com/foxey/volumio-buddy/
No idea who made it and if it works, but it looks promising. It includes rotary encoder support.