ScratchGPIO Sound Problem

I am working on a project to demonstrate some of the things you can do with a Raspberry Pi. I am doing this for our local library. One of the things I thought about building is a drum machine or a piano. The premise is when you touch a switch it causes the Raspberry Pi to play a sound. Very easily done in Python (the language I know best).

However, since this project is to target new users of the Raspberry Pi, I thought it would be nice to use the Scratch programming language. This is a visual programming language that is very powerful and really kid friendly.

The problem with using Scratch though is that it doesn’t natively handle the GPIO interface (General Purpose Input Output). These are 40 pins (or 26 on the older models) coming out of the Raspberry Pi that you can use to control hardware such as robots.


There is a project called ScratchGPIO that gives me the control I need. However, I had a problem with playing sound out of the computer when using the GPIO pins. Each time I would make the program play a sound, Scratch would exit (force quit). Looking through several forum threads did not turn up a solution quickly.

Finally, I found a forum post that gave an answer that solved the problem for me. I wanted to include it here in case it may be a help to others (and because I will probably forget the answer the next time I need it). Hopefully no one will have to dig as long as I did to find a solution.

The steps below assume that you have already read the instructions for getting the program installed. It is also probably a good idea to run the basic first program. Having said that, you probably wouldn’t have come here unless you already had the program up and running and fell into the same problem I had with sound.


  1. Start the ScratchGPIO program. When you install it, it will place an icon right on your desktop. Just double click the icon.
  2. After it is loaded, simply close it.
  3. Open a terminal window. I do this by typing “alt+F2” that will open a start program box. Inside that box type “lxterminal” and hit enter. Don’t actually type any of the quotation marks in this step.
  4. Inside the terminal window type “/usr/bin/scratch.old &” then hit enter. Again, no quotes.
  5. Scratch will re-open. Go to File | Open and select the “rsc” program (which is installed when you install ScratchGPIO).

That should make it so you can now use sound in your programs when using the GPIO interface.

I really don’t know if all those steps are necessary. I know I have opened a program I was working on in step 5 without opening the rsc program. It worked fine without doing that final step.

I would think that some of this can be automated with a script if someone wants to try and think that through. For me, the steps aren’t too cumbersome.

This is another one of the many things I have written up so that I can find the answer for myself when I need it in the future. I hope it is a help to others.

Buy a Raspberry Pi

If you don’t have a Raspberry Pi, head on over to MCM Electronics and buy one from the official US distributor.

New Raspberry Pi 2

A friend of mine asked me about the new Raspberry Pi 2 and what I thought of it. This isn’t intended to be an in-depth explanation of the differences between the old and the new models. But, in summary, I would like to buy one of the new ones for myself. It is 6X faster than the current model B+, runs all the same software and is in the same form factor. There is no reason to buy a B+ at this point unless they drop the price considerably to get rid of old stock.

My Current Stable of Raspberry Pi

Picture of the new Raspberry Pi 2I personally own a Raspberry Pi Model B (original) and a Model A+. I also bought a Model B+ and an A+ for the office.

My Model B (which I bought in the summer of 2013) is used at the house as a web, SSH and VPN server. I also use it to do Python programming. I am not much of a programmer, but when I have taken classes, I did all my homework on the Raspberry Pi through SSH. The web server that I am running with it (which is publicly accessible, but I am not wanting to promote it) is really just to do some basic PHP scripting that my son and I were testing. The VPN server is not used as much as it should be. But, basically this machine has found a permanent home and isn’t easily available for playing around with.

My Model A+ was purchased in December of 2014 and is used for play. Eventually it will be the brains behind a laser engraver that I have been working on. I have all the electronic brains built and working. What I don’t have done is the X/Y motor system. I have all the parts, I just haven’t built it yet. The A+ will power that when I finally get it done. Until then, I have enjoyed having it as an extra toy.

At the office the A+ is in service as a power monitor. It is connected to a UPS and collects info on the line voltage and when the power flickers or stays out for any length of time. I expect that it will just hang out there for a few years before it needs to be used for anything else.

The B+ was supposed to live its life as a video camera for recording and live streaming events at our training center. But, it hasn’t been working out the way I planned. So right now it is used for playing around with the Raspberry Pi camera module on whatever projects I can make up that still looks like I am doing important work.

To Buy Next

I think the Raspberry Pi B+ for the office will end up not being used in any valuable way there, so I will probably buy it off them and find a use for it at home.

I would like a Raspberry Pi 2 B. I just need to find a justifiable use for it. An RPi 2 could take the role of one of my minor servers. Then, if it does well, I may put a few other things on it. Nothing super important to start with. We have an internal instant messaging server that could easily run on a current model. Other small, relatively unimportant, services like this would be great to have running on a piece of hardware that takes almost no electricity compared to a full Linux server.

Where to Buy

MCM Electronics is the source to buy from. They are the official distributor in the US and the only place I know where you get the actual price of $35 that is always advertised. Currently they have the B+ on sale for $30.

Here is a comparison between the new and the old Model B+ (RPi2 B vs  RPi B+). At about 1:20 the speed test starts.

PDF to TIFF to TXT: Bash Script Automation

A few weeks ago I started into a project to take a scanned PDF image and turn it into text. At the time I was doing this manually by taking the PDF file and converting it to a TIFF (image file) and then running that through tesseract-OCR engine to output a TXT file. Then I would do some regular expressions on the file to pull out manual line breaks that the OCR process stuck into the file.

Since I was taking a class in Python at the time I thought I would do this using Python. But when I actually started coding the steps, I realized that a bash script would do everything I needed and I already knew how to do most of it. So that is what I ended up with.

I will walk you through what I understand about it. I did get help with the regular expression (line 12) and mostly understand it. But here is a stab at helping you see what is going on. And, I am certainly willing for anyone to offer suggestions to make the script better.

To use the script you invoke it with the name of the script and then the name of the PDF like this: filename.pdf

  • Line 1: This says that the script is a bash script.
  • Lines 2-5: A failsafe to tell the user that they need to type in the command for the script (my script is called and then the location of the PDF that is to be converted.
  • Lines 6-9: A failsafe that if a non-existent file name is given then the user knows the file does not exist in the location they indicated.
  • Line 10: Converts the input file into a TIFF with a resolution of 300 dpi and an 8-bit color depth. It also outputs the original file name with a .tiff extension.
  • Line 11: Uses the tesseract OCR engine to convert the file from an image to a text file. The .txt file extension is automatically added by tesseract.
  • Line 12: The magical regex line. All carriage returns are converted to asterisks. Then double asterisks are converted to double beginning lines. Single asterisks are converted to spaces. The double beginning lines are converted to double carriage returns. Finally the new file is output as the original file name plus .converted.txt.
  • Lines 13 and 14: Deletes the intermediary files that were created. I don’t ever have need for these so I can safely delete them. This is the section where I think my script could be improved. I would guess there is a way to create the intermediary files as temporary files that get automatically deleted as soon as they are used.

I am currently using this in production and it does exactly what I need. I am very pleased with the output and want to thank my friend Brett for his help with the regex and the failsafe lines.

Imperial March On A Floppy

This is not a new project to the Raspberry Pi world, but since I gave myself a new Raspberry Pi model A+ for Christmas, I wanted to do a simple and fun project. I have actually been engaged in a couple of more complicated projects on the Raspberry Pi that I don’t completely understand yet. So this one is just a learning exercise to better understand how to control physical devices with the Raspberry Pi.

Setup and Code

I got my start in this project by watching a video by XtraPerianer and then reading his writeup about it. I don’t go into any details here about how to accomplish the task, I just wanted to show you my project and a couple of things I learned in the process. You need to visit XtraPerianer’s video and site to get the details.

I did run into one problem that should be noted. When I copied the C++ code for the song from the Raspberry Pi forum, there were 4 extra lines at the beginning of the code block that my compiler choked on. Make sure you don’t included these 4 lines when you make your own .cpp file.

Of course the code author should get credit, but for the purposes of compiling the code you should eliminate these lines. At least it did not work for me to have these included. Admittedly, I am clueless as to proper C++ formatting. There may be something that I did wrong.

UPDATE: The problem is the use of # as a comment marker. Thanks to Tnwheeler for pointing out in the comments below the proper comment notation for C++ code.


Here’s my video of the project with some annotations included.

What I Learned

I have done some GPIO programming with the Pi in the past. It has been a bit over a year and I don’t remember all the details. But, this was a simple refresher to get the software I needed for my new Raspberry Pi. The programming I did in the past (and what I have been studying for a few months) is Python. However, this project uses C++. The code has enough comments in it that I pretty much understand what is happening. Now I want to modify it and have my floppy play other songs.

One of my future projects will make heavy use of stepper motors. This is a good reminder of how they work and how to program them.