IRLib Tutorial part 3: Overview Sending IR Codes

Part 3 of this tutorial is so big we have spread it over 4 installments. This blog post is just to pull all of those post together in a single location.

Part 3a: Sending IR Codes
In this segment we create a small Arduino sketch that allows you to type IR codes into the serial monitor and have them transmitted by your IR LED.

satb100Part 3b: Creating a Virtual Remote Using Python
In the segment we use a Python script on a PC or Raspberry Pi to communicate over the serial port with the Arduino. By clicking on the virtual buttons on the PC, the Python script transmits the codes to the Arduino which in turn translates into your device by your IR LED.

Part 3c: Python, PySerial and PyGame Installation on Windows
Because Python and the other tools for creating the Virtual Remote may not be on your system, we have included this extra tutorial on how to install the necessary software on a Windows PC. While it isn’t technically connected to IRLib, we thought it would be useful to a variety of people not just users of our library.

Part 3d: Installing the Arduino IDE on a Raspberry Pi
Again this is not strictly related to IRLib but if you want to run the Part 3b example on a Raspberry Pi you will need to install the Arduino IDE. General users of Arduino and Raspberry Pi might be interested in this even if they do not use our libraries. If you do not use a Raspberry Pi then you do not need this part of the tutorial.

IRLib Tutorial part 3d: Installing the Arduino IDE on a Raspberry Pi

As we mentioned in part 3c, this section of the tutorial doesn’t have anything to do with IRLib specifically, however if you have not already installed the software necessary to run the tutorial part 3b then you will have to follow these instructions. In this section we will install the Arduino IDE on a Raspberry Pi. If you’re not using Raspberry Pi as a host machine but are using Windows like we did in part 3c then you do not need this tutorial at all.

Note: This article has only been tested with Arduino IDE version 1.0.x and not with the beta version 1.5.x. We cannot guarantee these procedures will work with the new version which may have a totally different structure since it is a fairly big upgrade.

We are assuming that you are using a plain vanilla Raspbian Wheezy OS on your Raspberry Pi. We cannot guarantee any of these instructions will work on other versions of the operating system. We are also assuming that your Raspberry Pi has Internet access for downloading updates. If your Raspberry Pi is not connected to the Internet you’ll have to figure out how to get the necessary files onto your system by an alternate means.

Raspbian Wheezy is actually a version of Debian Linux and according to the following reference http://playground.arduino.cc/Linux/Debian the Arduino IDE has already been prepackaged for easy installation. Go to a command prompt and type the following command
sudo apt-get install arduino
Breaking this command down for those of you who may not be familiar with Linux commands… The “sudo” stands for “Super User Do”. Access to the root directory and installation on your machine requires superuser privileges. This command temporarily gives you those privileges. And what are you going to do with them? You’re going to run the “apt-get” command. This is a system already set up in most Linux distributions to get various programs from a central repository. And the command that we passed this program is “install arduino”. I take the time to explain all of that because when I was new to Linux and didn’t know what I was doing, I would ask a question on the support forum and they would give me some obscure line of text type and I had absolutely no idea what I was doing or why.

This process not only installs the necessary Arduino components it also installs and/or updates various Java components necessary to run the Arduino IDE. If you do not have Internet access and did not use the “apt-get” command to install the package but simply copied the programs over to your Raspberry Pi, then you do not have the full package. The process of getting those additional Java packages is beyond the scope of this tutorial.

When the installation is completed most of the software is installed in the folder “/usr/share/arduino” and the folders inside that include “hardware”, “lib”, “libraries”, and “tools”. There is also an alias for the “examples” and “reference” folders however these folders are actually stored in “/usr/share/doc/arduino-core/”.

Also upon completion you will discover that your GUI start menu now has a new folder and it called “Electronics” which contains a link to the Arduino IDE as seen here.
startmenu
If you would like a shortstop on your desktop, the easiest way to do it is to collect up on the start but do not actually click on it. Instead do a right-click and there will be an opportunity to “Add to desktop” as seen here.
add2desktop
You now have an Arduino IDE shortcut on your desktop. For future reference your desktop icon is at /usr/share/pixmaps/arduino.xpm
icon
If you double-click on the desktop icon on select it from the start menu the Arduino IDE the first thing you will want to check is the version number. As of this writing the pre-packaged version of Arduino was only at version 1.0.1 however the most recent version was 1.0.4 which among other things added support for other Arduino boards such as the Arduino Esplora and Arduino Micro. If the above installation process did not get you the latest version then you will have to manually updated yourself. We successfully used the procedure below to update from 1.0.1 up to 1.0.4 and a week or so later ran the process again to get up to 1.0.5.

We’re going to assume you are starting out in a directory called “/home/pi” where “pi” is your username. If you’re logged in under a different username that will be different naturally. We recommend that you create a directory for downloading programs. At the command prompt type
mkdir downloads
Which obviously makes a directory called “downloads”. Then type…
cd downloads
Which changes your current directory to the downloads directory or folder. Then you should type the following command
wget http://arduino.googlecode.com/files/arduino-1.0.5-linux32.tgz
The “wget” command gets a file from the web. If you are installing something newer than 1.0.5 you will have to change the filename. If you are uncertain of the filename, call up a web browser in your computer and go to http://arduino.cc/en/Main/Software and then right-click on the 32-bit Linux link and copy shortcut. Then paste that text somewhere so that you can see the proper filename for the most recent version.

We will now use the “tar” utility to unzip the archive that we have downloaded. Issue the following command.
tar zxvf arduino-1.0.5-linux32.tgz
This will create a folder under your downloads folder called “arduino-1.0.5”. Then change to that folder with this command.
cd arduino-1.0.5
We now want to get rid of the “hardware/tools” folder inside the unzipped distribution. That is because the version that we have downloaded is compiled for an Intel x86 or AMD x86 processor and not the ARM chip that is in your Raspberry Pi. We will use the “rm” command to “remove” that folder.
rm -rf hardware/tools
Now we will copy using “cp” to copy the other files into their proper locations. We will go through the subfolders and overwrite any existing files.
cp -ru lib /usr/share/arduino
cp -ru libraries /usr/share/arduino
cp -ru tools /usr/share/arduino
cp -ru hardware /usr/share/arduino
cp -ru examples /usr/share/doc/arduino-core
cp -ru reference /usr/share/doc/arduino-core

Note some readers of this blog have reported that they needed to pre-append “sudo” to these commands above. If you are having difficulty with these commands you should try using “sudo cp -ru… etc.” instead.
Now if you launch the Arduino IDE you will see that the version has been updated. In this image we had only updated it to 1.0.4 but you can see it does have the additional boards that were missing from the 1.0.1 installation.
boards
One last item… If you were doing this in order to run the IRLib tutorial part 3b on your Raspberry Pi then you will also need Python, PySerial, and Pygame. Fortunately Python and Pygame are already installed on your system. Be sure to use the Python 2 and not Python 3. You will however have to install the PySerial module with the following command.
sudo apt-get install python-serial
That should be all you need.

IRLib Tutorial part 3c: Python, PySerial and PyGame Installation on Windows

Although technically this installment of our tutorial isn’t directly related to IRLib, we are including it here as part 3c because it may be necessary to do these steps if you want to use the example in part 3b of our tutorials.

We’re going to take you step-by-step through installing Python and other necessary modules on a Windows system. We used this method on a Windows 7 64-bit home premium PC. Other versions of Windows will have extremely similar installations.

In order to use the previous examples, we highly recommend that you use Python 2 rather than Python 3 and we recommend that you use the 32-bit version even if you are on a 64-bit Windows machine. You might be able to get our examples to work under 64 bits or Python 3 but we were warned there might be problems and we did encounter them and chose not to try to come up with workarounds.

 Installing the Python Interpreter

Python is an interpreted language rather than a compiled language. The Python interpreter is a program that runs on your PC, reads the text script of your Python program and executes the script rather than compiling it into native machine language for your type of hardware. You can download the Windows version of Python 2 or 3 at…

http://www.python.org/download/releases/

As of this writing the most recent version of Python 2 is 2.7.5 dated May 15, 2013. Download the file named “Windows x86 MSI Installer (2.7.5) (sig)” you can save the program and run it from the same location or run it immediately.

01 downloadInstall the program for all users.

02 all usersChoose your directory. The default will work.03 directory

The default for customization is install everything. That will work fine.

04 customize

And complete the installation

04 finishWhen you completed the installation the following items will be in your start menu.

06 start menuThere is a command line version that can run scripts directly or there is an IDE called “IDLE”. In our previous installment of the tutorial we use the IDE.

There is one more item we need to do to complete this installation. We need to add the Python interpreter to the default system path so that when you try to execute a Python script it will know where to find the interpreter. The process is slightly different on Windows XP versus Windows Vista, 7, or 8.

 Adjusting the path on Windows Vista, 7, or 8…

Go to your Control Panel and click on “System”. If you are in icon view you can click on “System” directly. If you are in category view you should click on “System and Security” and then click on “System”. You should next click on “Change Settings” as shown here

07 change settingsYou will get a system properties dialog that looks like this. You should click on the “Advanced” tab.

08 system propertiesNext click on environment variables.

09 advancedYou’ll get the Environment Variables dialog which looks like this.

In the upper section look for the item “Path”. Select it and click on “Edit” just below it.

11 pathThen very carefully go to the end of the variable value and add a semicolon followed by the path where you installed Python. Make sure do not erase any other text that was previously there. On my system I added “;C:\Python22” to the end of the value. Then click on okay and continue out of all of the open dialogues. You can now proceed to the next section where we will install pySerial.

Adjusting the path on Windows XP

Go to your Control Panel and click on “System”. If you are in classic icon view you can click on “System” directly. If you are in category view you should click on “Performance and Maintenance” and then click on “System”. Unlike later versions of Windows, this will take you directly to the “System Properties” dialog. You do not need to click on any “Change Settings” link. It will look like this…

08 system properties XPClick on the tab labeled “Advanced” and it will look like this

09 advanced XPClick on the “Environment Variables” button and you will see the following dialogue.

10 environment variables XPIn the lower section look for the item “Path”. Select it and click on “Edit” just below it.

11 path XPThen very carefully go to the end of the variable value and add a semicolon followed by the path where you installed Python. Make sure you do not erase any other text that was previously there. On my system I added “;C:\Python22” to the end of the value. Then click on okay and continue out of all of the open dialogues. You can now proceed to the next section where we will install pySerial.

Installing PySerial

Next we will install the PySerial module which will allow us to do serial communication through Python. The main page for the PySerial code is…

http://pyserial.sourceforge.net/

Click on the “Download Page” link as shown here
20 pySerialAt click on the big green download button. Save the file to a convenient location. Note that the version “2.6” refers to the pySerial version and not to the version of Python which you are running.

21 downloadUnzip the file. If you do not have a utility that can handle .tar.gz compressed files we recommend using “7 Zip” which can be downloaded here…

http://www.7-zip.org/

Now we will have to do something that Windows users rarely do. We need to type some command lines to install the package. Click on your start button and choose the “run” command and then type “cmd”.
22 run command
23 run dialog cmd

Or alternatively you may have a command prompt shortcut in your start menu under accessories as shown here.
24 command prompt

When the command line window comes up you need to change directories to where you unzipped the pySerial package. In my case it was in the c:\downloads\pyserial-2.6 so the command was…

cd c:\downloads\pyserial- 2.6

Then type the following command…

python setup.py install

25 set up install

When the installation is complete type “exit”
26 exit

 Instructions for Installing Pygame

Our final step is to install the Pygame package. You can download it at

http://www.pygame.org/

Click on the download link in the upper left menu

30 pygame

Scroll down to the section for windows and click on the link labeled “pygame-1.9.1.win32-py2.7.msi”
31 download

Or whatever the most recent 32-bit Windows installer link is. Simply run the installer and use all of the default settings to complete installation.

That’s everything you need to know to install Python, PySerial and Pygame on a Windows machine.

If you are using a Raspberry Pi you will already have Python and Pygame installed with the standard version of Raspbian. To install PySerial simply go to a command prompt and type

sudo apt-get install python-serial

This tells it to issue a superuser command to get the package and install it. In the next installment of this tutorial we will talk about how to install the Arduino IDE on a Raspberry Pi.

A Modern-Day Carnac the Magnificent: Bob the Robot

Last night on the weekly Adafruit show and tell video chat on Google+ hangouts maker Mike Barela showed off an ongoing project called “Bob the Robot”. The new feature he added for this week’s presentation was an RFID chip reader he had put on top of the robot’s head. He would then hold up a small card with an RFID chip in it and the robot would do his impersonation of the old Johnny Carson Tonight Show character Carnac the Magnificent. By holding the RFID card up to the head robot, the robot would speak an answer to the question written on the card. Here is the video from last night hangout…

Mike had attempted to recycle some old original Carnac jokes but expressed his frustration that many of them were outdated. That inspired me to write a more current script for the sketch…

Ed iMac Mann: We are honored tonight to have a famed visitor from the East! That famed sage, soothsayer, and unemployed Zilog chip designer Bob-nac the Magnificent.

Bob-nac: [walks on stage tripping over a step and sits down] Buzz click whir.

Ed: “buzz, click, whir”? Is that some sort of computer code?

Bob: No, it’s the names of my kids Buzz, Click, and Whir. Whir is my newborn. I was give them a shout out.

Ed: I have here in my hand the questions… They have been hermetically sealed in a rainbow colored Raspberry Pi enclosure and kept on the steps of Adafruit industry since noon today! No one… Absolutely no one knows the contents of these envelopes! But you in your borderline psychic and magical mystical ways will ascertain the answers to these questions having never before seen the questions themselves is that correct sir?

Bob: Affirmative!
[One by one Bob holds the cards up to his head and proclaims the answer]

Bob: Adafruit
[after each answer Ed opens the envelope and read the question]

Ed: What do you call a charity fundraiser for a sick banana?

Bob: Arduino Uno

Ed: What card game makers play when they aren’t making?

Bob: maker faire

Ed: What did the baseball coach tell the batter who keeps hitting foul balls?

Bob: Google hangout

Ed: Where do peeping toms go for a beer?

Bob: I hold in my hand the last envelope…
[Audience cheers]
Bob: May you trip and fall in a giant vat of Lady Ada’s pink hair dye.
[Audience laughs and groans]
Bob: The Harlem Shuffle, Eagle circuit design software, and Angelina Jolie’s husband

Ed: Name a fad, a CAD, and a Brad


In that same video I also showed off a couple of my projects. On the right is the Show and Tell badge that I earned and on the left the Python badge awarded to me for my first Python script despite the fact that I’ve been a programmer for 40 years. It made me feel like a kid again

Python programming patch awarded to me by Adafruit Industries

Python programming patch awarded to me by Adafruit Industries


Adafruit Show-and-Tell sticker I earned for this presentation.

Adafruit Show-and-Tell sticker I earned for this presentation.

IRLib Tutorial part 3b: Creating a Virtual Remote Using Python

satb100In the previous lesson we demonstrated how to send IR codes to a TV, DVD, DVR/cable box or any other device that uses an infrared remote. In order to tell the Arduino what code to send, we typed codes into the serial monitor of the Arduino IDE. While that’s great for demonstration purposes, it’s not very practical. In this continuation of the lesson, we will create a small program using Python which will create a “virtual remote”. This Python script will run on your PC, laptop or even Raspberry Pi which is connected to the Arduino. As you click on the buttons on the app, the script will send the proper codes over the serial link to the Arduino. The Arduino in turn will send the IR signals to your device. In addition to the items listed in part 3a will also need…

  • a PC, laptop or even a Raspberry Pi with the Arduino connected to a USB port.
  • Python 2.x programming environment installed
  • PySerial add-on for Python that enables serial communication
  • PyGame add-on for Python that makes it easy to use graphics, mouse, keyboard
  • IRLib version 1.1 or later which includes the sample code

  • At the end of this tutorial we will post links to the Python, PySerial and PyGame packages. In part 3c of this tutorial we will give detailed instructions on how to install these passages on a Windows PC and a Raspberry Pi. When using Raspberry Pi it will probably already have Python and probably PyGame installed or you can find instructions on how to install them elsewhere online. We will include instructions on installing PySerial, the Arduino IDE and the IRLib packages on Raspberry Pi.

    This example assumes you are using Python 2.7.x or later. Python 3 has some problems with serial communication especially in the Windows version. We also highly recommend installing the 32-bit version even if you have a 64-bit computer. We cannot guarantee this will run on 64 bits. Similarly when installing PySerial we recommend the 32-bit version.

    Presuming you already have these packages installed we can proceed to customize a Python script to create virtual remote. In the previous installment we had you use the dump routine on your Arduino to capture the protocols, codes and number of bits for the functions you want to use. Our Python app can handle up to 40 functions however you can leave some of the buttons empty if you wish. But if you’re not yet done so, go back to the previous lesson and write down a whole bunch of codes you want to put in this new remote.

    The Python script we will use is available in IRLib/examples/IRserial_remote/IRserial_remote.py. You also need the files “click.wav” and “remotebg.png” in the same folder. You will have to edit several lines of the script to customize it for your purposes.

    A few lines into the script we will have to edit the serial port that the script we use to communicate to the Arduino. Look for these lines…


    # You will have to edit this to the proper port and speed
    ser = serial.Serial('COM4', 9600)

    As distributed it is set to “COM4” but on a PC it might be some other COM port or on a Linux machine such as a Raspberry Pi it would be something like

    ser = serial.Serial('/dev/ttyACM0', 9600)

    We also need to specify what font will be used to put the labels on the buttons a few lines down like this…


    # Specify a font. I'm using Arial narrow bold from my Windows
    # font folder. However the default font shown below also works.
    myfont =pygame.font.Font ("c:/windows/fonts/ARIALNB.TTF",30)
    #myfont=pygame.font.Font(None,36)

    By the way if you are not familiar with the Python language it uses a hash tag at the beginning of the line to specify it is a comment. The default is a Windows font that is Arial narrow bold that you can specify your own font of choice or used default font by substituting the word “None” as shown above. Look for the section where we define the label text each button. Here are the values as distributed.

    # These are the text labels that will appear on each button
    label_text=("TVp", "CBp", "P^", "Pv",
    "<<", ">", ">>", "->",
    "Rec", "=", "s", "<-", "Gd", "^", "Fav", "Inf", "<", "sel", ">", "Lis",
    "ret", "v", "Prv", "Mnu",
    "1", "2", "3", "Ch+",

    "4", "5", "6", "Ch-",
    "7", "8", "9", "Vol+",
    "Pip", "0", "Mut", "Vol-",
    )

    These correspond to TV power, cable box power, Page Up, Page Down, rewind, play, fast-forward, jump to live, record, pause stop, jump back, Up Arrow, favorites, info, Left Arrow, Select, Right Arrow, Lists Recordings, Return, Down Arrow, Previous Channel, Menu, numbers 1 through 9 and 0, Channel up, Channel down, Volume up, Volume Down, Mute, and PIP. However you can make them say anything you want.

    Below that we insert the actual codes. We will use the same format we used in the previous lesson when we type codes into the serial monitor. First is a single digit that describes the protocol number, followed by a hex value, followed by the number of bits. Note that when some protocols the number of bits is optional. Here are the codes I used for a Magnavox TV using the “RC5” protocol and 13 bids And a Scientific Atlantic SA 8300 DVR using the PANASONIC_OLD protocols.


    IR_Codes= ("3,180c,13","5,37c107","5,36d924","5,37d904",
    "5,37291a","5,37990c","5,36293a","5,36b129",
    "5,375914","5,374117","5,365934","5,37c906",
    "5,36c127","5,36812f","5,37f101","5,36213b",

    "5,37810f","5,366133","5,364137","5,36c926",
    "5,366932","5,37a10b","5,36e123","5,373918",
    "5,36113d","5,37111d","5,36912d","5,377111",

    "5,37910d","5,365135","5,375115","5,36f121",
    "5,36d125","5,37d105","5,363139","3,1810,13",
    "5,37b908","5,373119","3,180d,13","3,1811,13",
    )

    Once you have everything edited using your codes you should then load the IRserial_remote sketch into your Arduino. You can try typing a few codes into the serial monitor to make sure it’s working but once you are sure the sketch is running properly you need to close the serial monitor and the Arduino IDE so that they do not tie up the serial port. If you do not close at least the serial monitor, your Python script may not be able to connect to the Arduino properly.

    Then run the Python script IRserial_remote.py script. A window should pop up showing a virtual remote control with 40 buttons on it looking something like this…

    Virtual TV Remote using Python

    Virtual TV Remote using Python


    Notice as you hover your mouse over each button, the text will turn red. If you click on the button with the mouse it will make a clicking noise and it will instruct the Arduino send the proper IR codes. If everything works properly you should be able to control your device. Here is a YouTube video demonstrating how it works.

    It’s not our intent to make this a Python tutorial so we will not go through the Python script line by line to explain how it works. Also included in the examples folder under IRserial_remote is a folder called “povray” which contains the source file for the Persistence of Vision Ray Tracer POV-Ray which is a freeware rendering program which was used to create the background image for the buttons on the remote.

    Here is a video which demonstrates this application. This video was shown on the weekly Adafruit Show and Tell video chat on Google+ You can click on the “As seen on Show-and-Tell” logo at the top of this post to see it on the show and tell.

    As promised here are links to various downloads you may need for this tutorial

    In the next installment we will show you how to install these packages in case you haven’t already done so.