Page 1 of 8

GPIO Pins to control volume

PostPosted: Tue Dec 09 2014 13:07
by smartroad
Hi I was wondering if it is possible to control the volume of Volumio via GPIO pins? I ask as I am making a WiFi speaker and would like to have control on the speaker for volume up/down and mute - just to make it easy to adjust volume rather than having to get phone out :)

GPIO Pins to control volume?

PostPosted: Tue Dec 09 2014 16:04
by Glop109
Hello

I suggest you to add an ask in this message : interface-with-gpio-and-rs232-spi-full-communication-t2192.html
We are a least 2 guy to ask something like that :).

GPIO Pins to control volume?

PostPosted: Tue Dec 09 2014 20:12
by MobeyDuck
Glop109 wrote:Hello

I suggest you to add an ask in this message : interface-with-gpio-and-rs232-spi-full-communication-t2192.html
We are a least 2 guy to ask something like that :).


I believe what you want isn't the same as what Glop asks, or i just didn't understand what you actually want. Glop wants basic control using a few buttons over GPIO. This isn't hard to achieve.

Get some buttons connect them on 3.3v and a GPIO in.
Create a python script, import bash/shell commands and send mpc commands

At least thats the simplest way i can think of, you could skip the bash import with mpc and use python-mpd but then you'll need to install python-mpd.

You could take a loot at this thread for inspiration: detect-when-something-streaming-t2056.html

GPIO Pins to control volume?

PostPosted: Wed Dec 10 2014 08:44
by Glop109
I think that the volume buttons on GPIO is in the same way that I suggeste to configure via GUI the GPIO actions In or Out. (or more deeply within serial communication).

I think it's posible t do that with script and other but it will be "endUser" to propose that on the config in GUI... :D

GPIO Pins to control volume?

PostPosted: Thu Dec 11 2014 08:38
by Michael20
Buttons shouldn't be a big thing. A volume knob i think is more difficult, but this variant would be nice! :D

GPIO Pins to control volume?

PostPosted: Thu Dec 11 2014 12:26
by MobeyDuck
You could use a rotary encoder to do that. Adafruit sells those they also made a sample script to control windows audio. See http://www.adafruit.com/product/377

GPIO Pins to control volume?

PostPosted: Thu Dec 11 2014 13:19
by Buckler
Hi,

A Volume knob is not that difficult!

Hardware:
You need a rotary encoder and some wires. Dupont cables (male/female) are handy, you can solder one side to the encoder and the other side (female connector) can easily be connected with de GPIO connector.

Rotary.jpg
(I have a Pi-Crust board on top of my Pi so I have used the male-connector)
Rotary.jpg (29.62 KiB) Viewed 38528 times


SOFTWARE:

Gpio
To get access to the GPIO-pins you have to install the RPi.GPIO python module:

Download the package from sourceforge
Code: Select all
wget http://sourceforge.net/projects/raspberry-gpio-python/files/raspbian-wheezy/python-rpi.gpio_0.5.8-1_armhf.deb


Install the package
Code: Select all
sudo dpkg -i python-rpi.gpio_0.5.8-1_armhf.deb



Rotary class
You can use the rotary-class from Bob Rathbone source: http://www.bobrathbone.com/raspberrypi_rotary.htm
(Bob has also a fantastic tuturial for building a radio project!)

Save the rotary class in: /home/pi



Script
Then you need some code to control the volume (based on the test-script from Bob):
Code: Select all
#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
#
# Author : Bob Rathbone
# Site   : http://www.bobrathbone.com
#
# This class uses a standard rotary encoder with push switch
#
import subprocess
import sys
import time
from rotary_class import RotaryEncoder

# Define GPIO inputs (BCM)
PIN_A = 23              # Pin 16
PIN_B = 24             # Pin 18
BUTTON = 25     # Pin 22

# This is the event callback routine to handle events
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' ])
        #elif event == RotaryEncoder.BUTTONUP:
              #print "Button up"
        return

# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)

while True:
        time.sleep(0.5)


Save this script in: /home/pi/VolumeKnob.py

Auto-start the script:
Code: Select all
nano /etc/rc.local


Add this line before the exit 0:

Code: Select all
python /home/pi/VolumioKnob.py &


Reboot the Pi and you can control the volume with the rotary-encoder and with a button press you can pause/play the music!

Succes!

Harry

GPIO Pins to control volume?

PostPosted: Thu Dec 11 2014 17:30
by balbuze
Hi!
great !!! that is exactly what I was looking for !!!
But I've got a problem with Volumio 1.5, I can't install python-rpi.gpio. The message is :
Code: Select all
i@volumio-bureau:~$ sudo apt-get install python-rpi.gpio
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python-rpi.gpio
E: Couldn't find any package by regex 'python-rpi.gpio'

Any idea ?
Thanks !

GPIO Pins to control volume?

PostPosted: Thu Dec 11 2014 18:14
by balbuze
Ok
I did it following this thread : rpi-volumio-gpio-for-python-t1491.html
It works now ! But I change step from 2 to 4 in the VolumeKnob.py file.
Thanks a lot!

GPIO Pins to control volume?

PostPosted: Thu Dec 11 2014 20:22
by balbuze
too bad....
I thought it was ok but after a reboot mpd does not work anymore. No webUI... I can ssh to my pi. Get the following error :
Code: Select all
pi@volumio-bureau:~$ sudo service mpd restart
[ ok ] Stopping Music Player Daemon: mpd.
[....] Starting Music Player Daemon: mpdIllegal instruction
 failed!

What to do to avoid a complete re-install ?
info : At boot time I hear the boot sound of Volumio