Controlling itunes using Arduino and a Proximity Sensor

Using arduino,proximity sensor and python i was able to make a my itunes play and pause. It’s pretty cool! so check it out!

Things you are going to need:

Hardware required:

  • Arduino Uno
  • IR receiver and emitters
  • 1 resistor (1M ohm)
  • few jumper wires

Softwares required:

  • You should have python installed in your laptop
  • You are going to need itunes
  • A text editor (I am using Sublime Text 2. You can use which you feel comfortable with.)

Step 1 : Running itunes through shell

I found a shell script online to do this. Copy paste the code in a text file and save it as itunes.sh where you have your Arduino code saved.

for me it was: /Users/shabadlamba/Documents/Arduino/

#!/bin/sh
#
####################################
# iTunes Command Line Control v1.0
# written by David Schlosnagle
# created 2001.11.08
####################################
showHelp () {
echo “ — — — — — — — — — — — — — — -”;
echo “iTunes Command Line Interface”;
echo “ — — — — — — — — — — — — — — -”;
echo “Usage: `basename $0` <option>”;
echo;
echo “Options:”;
echo “ status = Shows iTunes’ status, current artist and track.”;
echo “ play = Start playing iTunes.”;
echo “ pause = Pause iTunes.”;
echo “ next = Go to the next track.”;
echo “ prev = Go to the previous track.”;
echo “ mute = Mute iTunes’ volume.”;
echo “ unmute = Unmute iTunes’ volume.”;
echo “ vol up = Increase iTunes’ volume by 10%”;
echo “ vol down = Increase iTunes’ volume by 10%”;
echo “ vol # = Set iTunes’ volume to # [0–100]”;
echo “ stop = Stop iTunes.”;
echo “ quit = Quit iTunes.”;
}
if [ $# = 0 ]; then
showHelp;
fi
while [ $# -gt 0 ]; do
arg=$1;
case $arg in
“status” ) state=`osascript -e ‘tell application “iTunes” to player state as string’`;
echo “iTunes is currently $state.”;
if [ $state = “playing” ]; then
artist=`osascript -e ‘tell application “iTunes” to artist of current track as string’`;
track=`osascript -e ‘tell application “iTunes” to name of current track as string’`;
echo “Current track $artist: $track”;
fi
break ;;
“play” ) echo “Playing iTunes.”;
osascript -e ‘tell application “iTunes” to play’;
break ;;
“pause” ) echo “Pausing iTunes.”;
osascript -e ‘tell application “iTunes” to pause’;
break ;;
“next” ) echo “Going to next track.” ;
osascript -e ‘tell application “iTunes” to next track’;
break ;;
“prev” ) echo “Going to previous track.”;
osascript -e ‘tell application “iTunes” to previous track’;
break ;;
“mute” ) echo “Muting iTunes volume level.”;
osascript -e ‘tell application “iTunes” to set mute to true’;
break ;;
“unmute” ) echo “Unmuting iTunes volume level.”;
osascript -e ‘tell application “iTunes” to set mute to false’;
break ;;
“vol” ) echo “Changing iTunes volume level.”;
vol=`osascript -e ‘tell application “iTunes” to sound volume as integer’`;
if [ $2 = “up” ]; then
newvol=$(( vol+10 ));
fi
if [ $2 = “down” ]; then
newvol=$(( vol-10 ));
fi
if [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e “tell application \”iTunes\” to set sound volume to $newvol”;
break ;;
“stop” ) echo “Stopping iTunes.”;
osascript -e ‘tell application “iTunes” to stop’;
break ;;

“quit” ) echo “Quitting iTunes.”;
osascript -e ‘tell application “iTunes” to quit’;
exit 1 ;;
“help” | * ) echo “help:”;
showHelp;
break ;;
esac
done

After you have done this go to your terminal and get into the directory where you have saved the itunes.sh. Now to test if it is working do the following:

Step 2: Writing the Python code

In the same directory :

/Users/shabadlamba/Documents/Arduino/

create a text file itunesControl.py with the following code:

####################################
# Arduino iTunes Controler v1.0
# written by Shabad Lamba
# created 2016.01.07
####################################
import serial
import os
connected = False
ser = serial.Serial(“/dev/cu.usbmodem1421” ,9600) #change the  arg0 accordingly with port number your arduino is connected too
print ser
flag=1
while not connected:
serin = ser.readline() #reading a line from the serial port
serinNew = serin.split(‘\r’)[0]
print serinNew
if serinNew == “Detected!”:
if flag == 1:
os.system(‘/Users/shabadlamba/Documents/Arduino/./itunes.sh play’)
flag=0
else:
os.system(‘/Users/shabadlamba/Documents/Arduino/./itunes.sh pause’)
flag=1
if serin == “exit”:
connected = True
ser.close()

Also if you wanna learn how to use arduino with python using serial library check out my blog:

As you can see that this programme with just plays and pauses itunes. You can add whatever features you can based on; in how many ways your sensor can take input.

Step 3: Writing the Arduino Code!

Last set of code you’ll have to write. Be Patient! Once it’s done you’ll feel awesome about yourself and you won’t be able to stop using it!

Now open you Arduino ide and write the following code:

// Controlling itunes using Arduino and a Proximity Sensor
// Author: Shabad Lamba
// Date: 07/01/2016
// Version: 1.0
int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 8; // IR emitter LED on digital pin 2
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the object
int value[10]; // variable to store the IR values
int distance; // variable that will tell if there is an obstacle or not
void setup(){
Serial.begin(9600); // initializing Serial monitor
pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter,LOW);// setup IR LED as off
pinMode(11,OUTPUT); // buzzer in digital pin 11
}
void loop(){
distance = readIR(5); // calling the function that will read the distance and passing the “accuracy” to it
//Serial.println(distance); // writing the read value on Serial monitor
itunes(); // uncomment to activate the buzzer function
}
int readIR(int times){
for(int x=0;x<times;x++){
digitalWrite(IRemitter,LOW); // turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
}

for(int x=0;x<times;x++){ // calculating the average based on the “accuracy”
distance+=value[x];
}
return(distance/times); // return the final value
}
void itunes(){
if (distance>50){
Serial.println(“Detected!”);
delay(1000);
}
}

Upload the code to your Arduino!

Almost there!

Step 4: Setting up the circuit

check “Connecting the Circuit” section in the following blog:

Remember to connect your IR emitter to digital pin 8 not 2 (as in the blog) or make a change in your Arduino code and set variable IRemitter to 2.

Once you are done with setting up the circuit do the following

Open up your terminal and go to the directory where you saved itunesControl.py

And type the following

$ python itunesControl.py

There you go. You did it!

Just move your hand over you sensor and listen to the magic happening!

Have fun playing with Arduino!

Show your support

Clapping shows how much you appreciated Shabad Lamba’s story.