IQ Audio DAC+ Rotary Encoder On Raspberry Pi B+

Stacks Image 1986
This is a quick tutorial on how to add a rotary encoder to a Raspberry Pi with an IQAudio DAC+. There were bits and pieces of this on the web but nobody has a Barney style tutorial and that’s what this is.

The assumption is that you have a Raspberry Pi and IQAudioDac that plays music. And now you want to control the volume manually.

And I’m making the assumption that when you got your IQAudio DAC that you soldered on the header pins they sent with it. You’ll need to do that. Follow the directions on page 30 of their user guide.

First get the rotary encoder and some female to female jumpers from Adafruit.

HARDWARE CONNECTIONS:
Stacks Image 1989
I did my best to depict it there but it is simple. Face the pins on the rotary encoder towards you.

Pin in middle goes to pin 14 on the DAC+
Pin on left goes to pin 16 on the DAC+
Pin on right goes to pin 18 on the DAC+

The outermost row of pins closest to you are even numbered pins. The pins closest to the circuit board are odd numbered ones.



Put it all together and boot and then download this somewhere. I made a directory called rot2

cd

mkdir rot2

cd rot2

git clone https://github.com/iqaudio/tools.git

now install this dependency

sudo apt-get install wiringpi

I could not get IQ_rot to work by running:

sudo IQ_rot&

as the IQAudio user guide suggests. So……delete that file. It’s of no use to you.

sudo rm IQ_rot

now do this:

sudo nano IQ_rot.c

scroll down to this area:
Stacks Image 1993
See where I have “PCM” circled? Change that to “Digital”
Stacks Image 1997
Ctl key + Y to save and exit. Now lets compile IQ_rot.c

sudo gcc IQ_rot.c -oIQ_rot -lwiringPi -lasound

It will make a new file called IQ_rot

You can now launch the file

sudo IQ_rot&

Play with the volume and it should work famously. You did it. Congrats. Now make it start at boot.

sudo VISUAL=nano crontab -e

add this:

@reboot /home/pi/rot2/IQ_rot&
Stacks Image 2001
If this helped you please let me know!
Stacks Image 2006
Now that fancy rotary encoder has a push button. Might as well put it to work too. Let’s use it to issue a shutdown command. Take two more female to female pins. Attach one to pin 11 on the pin (that is GPIO 17) and the other on pin 39 which is a ground.

Now do this.

cd

mkdir shutscript

cd shutscript

sudo nano shutdown_pi.py

paste in this code. Lets be crystal clear. I did not write this code. I snipped it off the internet and changed GPIO 18 to 17 because i was already using 18.



#!/bin/python
# Simple script for shutting down the raspberry Pi at the press of a button.
# by Inderpreet Singh

import RPi.GPIO as GPIO
import time
import os

# Use the Broadcom SOC Pin numbers
# Setup the Pin with Internal pullups enabled and PIN in reading mode.
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Our function on what to do when the button is pressed
def Shutdown(channel):
os.system(
"sudo shutdown -h now")

# Add our function to execute when the button pressed event happens
GPIO.add_event_detect(17, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

# Now wait!
while 1:
time.sleep(1)

Okay now lets start that at boot.

sudo crontab -e

add this line

@reboot python /home/pi/shutscript/shutdown_pi.py &

By Golly, you have done it again. A press of the button should issue a shutdown command.
2,932