IQ Audio DAC+ Rotary Encoder On Raspberry Pi B+
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.
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
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:
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:
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
Play with the volume and it should work famously. You did it. Congrats. Now make it start at boot.
sudo VISUAL=nano crontab -e
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.
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.
# Simple script for shutting down the raspberry Pi at the press of a button.
# by Inderpreet Singh
import RPi.GPIO as GPIO
# Use the Broadcom SOC Pin numbers
# Setup the Pin with Internal pullups enabled and PIN in reading mode.
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Our function on what to do when the button is pressed
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!
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.