SonicPi on Raspberry Pi

12 Feb

One of these days, I’m going to do something on the Raspberry Pi that is going to work the first time.  Today . . . is not that day.  Liz and Eben Upton sent me a Raspberry Pi B+ and I was dying to see what’s new on it.  I had some struggles getting NOOBS to work on it, so I downloaded a new version and it worked fine.  I installed the new version of Raspbian and a few things that I noticed that were new are: 1) It has the GPIO version of Scratch already installed  2) It doesn’t have the Midori browser anymore  and 3) There’s a new program called Sonic Pi installed.

Sonic Pi is a program that allows you to use code to create music.  You can generate drum sounds, guitar sounds, sample music clips, loop sound effects, and apply envelopes, fades, etc.  For example, if you type “play 44,” it plays the 44th key on the piano.  If you type “sleep 1,” it waits for one second, and if you type “sample :sound_slow,” it plays a sound called sound_slow.  It’s fun to play with and you really can create some great music.  Here’s an example of a person who coded “Flight of the Bumble Bee” with over 1,200 lines of code!

I played around with Sonic Pi for a while and went through the first few simple tutorials.  It was fun.  Then, I wanted to save my song to use in this blog.  If you click “Save,” it saves your code and not your song.  Then there’s a button that says “Record” that will save the song itself as a wav file . . . or so you’d think.  I couldn’t find any instructions for the record button in the tutorials or even online with a simple google search.  So, I just tried it.  I pressed record and then played the song.  When I clicked record again to stop the recording, it asked me to name the file and I chose to put in on the desktop.  Simple, right?  Then I went to the desktop and the file wasn’t there.

I had to do some research to figure out how to find a file in Linux.  I couldn’t get the “find” feature in the terminal to find the file (or maybe it turns out that it wasn’t there), so I installed a program called “mLocate” that works a little better.  It turned out that the file just wasn’t there.  I tried to save it with a different name, save it in a different folder, and repeated over and over and it just wouldn’t save the file.  In researching how to fix this, I found out that there is a new version of Sonic Pi available and I hoped that this would fix all of my problems.  Once I installed Sonic Pi version 2, the program seemed pretty much the same, so I wrote a quick song and tried to record it.  The process was the same and this time, it seemed to work.  But when I used mLocate to find the file, it didn’t find anything.  This time, though, it was my mistake.  Before you use mLocate, you have to update its database where it searches the Raspberry Pi for files and makes a list.  So, when I type “sudo updatedb”, this   updates mLocate’s list and then it found the file.  Now, you just type “locate word” and it will find files, folders, and programs that contain the letters “word” in them.  It seems as though if I type “locate word” it only looks at the present folder, but if I type “sudo locate word,” it checks the entire computer.

But, the wav file wouldn’t play.  It was using Jack Audio to play the file and it crashed every time I tried.  Then, I found out that Alsa can be used to play wav files.  My file was named practice.wav so I typed “sudo aplay practice.wav” and now it plays fine.  If yours doesn’t play, it might be because you have the Pi set to play from HDMI and you have headphones plugged in or vice versa.

That’s as far as I got with Sonic Pi because it took a while for reinstalling, restarting, and researching.  Overall, it was fun and I will definitely use it again.


Raspberry Pi B+ Starter Kit:

Sonic Pi Software (if your OS doesn’t already have it):

Sensors and Relays, Raspberry Pi, and Python

6 Jan

My school has a Computer Programming/Raspberry Pi club that my dad and I host.  Each time, we teach a lesson about the Pi, Python, Scratch,, binary/hex, etc. and then students break into groups to learn about a topic of their choice.  Some choose to go deeper into Scratch, others work on Raspberry Pi projects, and others use websites like codeacademy or EdX to go learn a different programming language.  We spent a couple of classes talking about what the different languages are used for.  For example, we said that HTML and CSS are good for making websites, Python is good for making MineCraft hacks on the Raspberry Pi, and Javascript is good for making web apps.

We’ve also had several guest speakers who are experts in computer programming come to our school and speak.  For example, we had a Google intern, we went and visited Google, and we had a programmer from JennaSys Engineering speak to us.  We asked each one what programming language they would recommend that we start with.  The Google Intern said “Python or Java,” the Google engineer said “Anything but Python, it’s too far from the hardware,” and the JennaSys engineer said, “Python.”  So, we decided that our lessons would focus on Python.

Below is the script that I wrote for our most recent lesson.  For this lesson, we used a GrovePi to take input from a light sensor and send output to a relay.  The circuit spins a motor if the light level gets too low.  So, this could be used to open and close your window blinds as the sun rises and sets, but that’s not very realistic.  In reality, there are many, many devices that use this exact same structure.  For example, your thermostat turns on the heater if the temperature gets too low and the AC if the temperature gets too high.  Some cars turn on the headlights when it gets too dark.  Your refrigerator compressor kicks in if the fridge gets too warm.  Some cars have windshield wipers that turn on when they detect moisture.  Grocery store doors open when they detect a person nearby.  An alarm goes off when a door is opened.  What these systems all have in common is that there is a sensor that triggers a relay to do something.  There are hundreds of other examples of this, too (3D printer micro switch, printer jam detection, phone overheat protection, laptop turns off when the lid closes, etc.) so we decided to look at an example of code that can make this happen.

I have to put a picture of the code because my spacing in the program gets messed up on WordPress for some reason:


A “relay” is a switch that you can turn on and off with code.  The click that you hear when you turn on a device with a remote control is the relay switching on.  They have one side that is “Normally Open” and you can use code to close it (turn it on) and another side that is “Normally Closed” and you can use code to open it (turn it off).  It gets confusing because generally, when you send a value of “1” to an output, you are turning it on.  But if that device is plugged into the “Normally Closed” side of the relay, then it’s already on and you turn it off by sending the value of “1.”

So, although the circuit that we had that day was only slightly cool, the applications to other areas are powerful.  This project also demonstrates why a Google engineer told us that Python isn’t the best language to learn.  The libraries make it so easy.  If you were writing code for a car’s computer, you wouldn’t be able to take the shortcut by importing the GrovePi library.


Raspberry Pi Starter Kit:  $69.95

GrovePi board and sensors:  $75.00

DC Motor:  $5.00

Variety of batteries, clips, and wires

My GrovePi Adventure

19 Nov

Here we go again . . . another story of following the instructions for a project exactly and failing miserably.  A while back, I got a GrovePi and a bunch of sensors.  It would be perfect for adding inputs to the Raspberry Pi since it has temperature, pressure, flame, light, sound, knock, distance, and more sensors.  So, first, I decided to try the joystick.  I went to the website for GrovePi and found instructions there ( or  Wait . . . what?  The kit didn’t come with any cables.  Huh?  The one in the picture has a white plastic connector with four pins.  Mine has no connector and 5 pins!  What’s going on here?

I found out that the sensors that I bought on Amazon are not GrovePi sensors at all.  They are compatible . . . sort of, but they are not the GrovePi sensors.  They came with some connector wires (that don’t fit) and a ribbon cable (which is pretty useless).  The little instruction book only has a picture of each sensor and no instructions whatsoever.  There is an instruction manual online, but it sounds like it was written in another language and translated by a 4-year old.  Here are a couple of quotes:

“13 comes with digital interfaces of the LED, the shock sensor access number 3 interface, when a sense of shock sensor”

“The reason why we feel that infrared is really a wonderful thing, it is because we are invisible, intangible, but Okay, we do not need that, too, can control it and make it serve us, in fact, we are more magical, Is not?”

“The amount of points we can hand to block receiver module, see also the normal communication between them do? The following is the receive window Ah, looked at the window, and we all know it. . .”

I don’t mean to make fun of anyone and their English ability, but these sentences are so confusing that I cannot figure any of them out.  And it’s only written for Arduino (yuck!), not Raspberry Pi.

The reason for all of this is that I purchased a kit on Amazon called the SunFounder 37 modules Raspberry Pi Sensor Kit.  This kit is no longer available, but there are still Arduino kits like this one.  You can still get the old kit at, but I wouldn’t recommend it.

Bottom line is that the GrovePi is a great product.  It has solved all of the issues that I blogged about before with using analog sensors on the Raspberry Pi (here).  The GrovePi library has all of the code for accessing the analog ports on the GrovePi with simple code.  But I would highly recommend buying actual GrovePi sensors (like this one), not SunFounder sensors.  Support for GrovePi sensors on the Raspberry Pi is pretty good, but the software comes with sample code that can easily be modified to use most of the sensors and there are a few cool projects on their website (here).  (Note: This kit no longer says on the advertisement that it works with GrovePi and it comes with an instruction book.  I don’t have the book, so I cannot tell you if it’s good or not.)

I’ll blog later about how I used the code for the analog temperature sensor to make the SunFounder analog sensor work and then modified that code to make the Photo Resistor Sensor work with very few changes.  I’ve added some error handling that people on a forum told me about, so the program doesn’t crash anymore.  My ultimate goal is that some of the students at my school are planting an experimental citrus grove and we want to use moisture sensors to automatically trigger an irrigation system.  So, it would have about 40 moisture sensors and a couple of relays to trigger water pumps connected to a tank of reclaimed water.  We might also get a water flow meter to measure how much water we’ve used and track whether the system is working.

I did it!!!!

3 Sep

I was selected as a Broadcom MASTERS semi-finalist!  That’s top 300 in the country and two weeks from now, they’ll narrow it down to the top 30.  Those 30 will go to Washington DC and meet the president, inventors, astronauts, and more!  It’s going to be a stressful 2 weeks of waiting.  I AM SOOOOO EXCITED!  To make it even better, my friend Becky Dana also got selected as a semi-finalist.  That’s awesome!  Society for Science and the Public does such a spectacular job with this event that I think everyone should do whatever they can to get nominated.  Check out the announcement page here for more info:

Big Day!

3 Sep

I was nominated for Broadcom MASTERS again this year.  If you haven’t been following for long, it’s like the International Science Fair but for middle school students.  Tomorrow is they day that they announce the Semi-Finalists!  I can’t wait!!!!  The announcement is at noon eastern, so I think that’s 9 am California time.  Wish me luck (wait, I believe that people create their own luck by working hard).  Wish me hard work, I guess.


I’ll let you know.

Broadcom MASTERS/ISEF Day 4

16 May

     Today, I got to go to the Aquarium of the Pacific in Long Beach.  I just went on a field trip to Catalina Island with my school and the aquarium is right around the corner from where the boats leave to take people to Catalina. 

     I love the touch tanks where we can touch moon jellyfish, bat rays, and starfish.  My favorite creature to touch is the sea cucumber.  I don’t know why, but sea cucumbers and sea slugs are my favorite.  I once went out on a boat right next door to the aquarium where they throw out a net and catch lots of different animals.  They caught a sea slug and let me hold it.  I was so happy!  It was like a dream come true.

     Then, we toured the exhibits inside the aquarium.  There were coral reefs, kelp forests, and a giant aquarium filled with California marine life.  There is one display that features incredibly camouflaged creatures as well.  There was also an area with frogs.  I LOVE frogs!  A long time ago, I did a podcast called “Fun Stuff for Kids to do in Southern California.”  I interviewed one of the volunteer SCUBA divers from the aquarium.  Check it out here.  Keith Brush was really, really nice.  He also runs a company called “Future Scientists and Engineers of America.”  Wow, I was really young back then!  My brother in the pictures will be 17 this month!

     We also had the opportunity to have Universal Studios all to ourselves.  They closed the park down and only ISEF and Broadcom students were there.  I got to ride on Dispicable Me, Transformers, The Mummy, and The Simpsons rides.  There was also a sort of museum with props from old movies.  They had one of the dinosaurs from the Jurassic Park movies.  I didn’t get back to the hotel room until after 12:45!  I was so tired that I’ll have to post pictures in this blog some other time.

     One of the most helpful things that we did is that we got together with all of the ISEF students from my region and we practiced their interviews over and over giving them feedback and helping them get better and better each time.  I hope that this helps them with their judging.  It was really fun for me to learn so much about their projects.  We did this with all of the Broadcom students in Washington D.C. and it helped me a lot.

      Broadcom and Society for Science and the Public have done such a wonderful job of making sure that we’re having a great time and learning a lot.

      Oh, and I forgot to mention yesterday that they gave us a Raspberry Pi.  I now have 3 Raspberry Pis.  I’m going to have to find a project that connects all three to do something special.

Broadcom MASTERS/ISEF Day 3

14 May

    Today, we went to Disney Studios in Burbank.  We went to the stages where the shows are made.  We went to the stages for a show called “Intelligence.”  We had lunch with Imagineers.  I sat with an Imagineer named Amy Goodwin and she was inspired to become an Imagineer when she was at Disneyland and saw an interactive display where a cartoon character talks to you and knows things about you (I think that there are cameras and a person behind the scenes doing the voices through a microphone).  This really motivated her.  Then she was an intern at Disney Studios and she met the right people and worked really hard and eventually became an Imagineer herself.  This was her dream come true.  Her sister is a microbiologist and got to name her own protein!

    Then, we went to Disney Archives which is kind of like a museum of old Disney memorabilia like items that they sold at Disneyland when it first opened.  We got to hold the Oscar award that Walt Disney won.  We saw an original Disneyland ticket and some of the costumes that they used in Saving Mr. Banks and costumes from Pirates of the Caribean.  We saw miniature models from A Christmas Carol.  They showed us the camera that they used to film Snow White and ended up using for other movies.  The movie was such a hit that it funded the creation of several more movies after that.  Here are some photos from these two places:

Image ImageImage

    Then we went to the La Brea Tarpits.  I had been there once before, but it’s such a great place, I could go there ten times and it would still be fun.  We had a tour guide explaining things to us and telling us stories.  One of the coolest things that they have is metal plungers that are down in some tar and you get to try to pull it up to see how sticky and strong the tar is.  After trying this, you can really see how animals couldn’t get out.

    I got back from the tar pits too late to hear the panel of Nobel Prize winners speak, but I met two of them in the lobby and got my picture taken with them.  One of them was named Dr. Sir Harold Kroto and he was a Nobel Prize winner in Chemistry 1996.  His work was about proving that Buckyballs exist and can be produced in stars.  Here’s a link to his Wikipedia page.  The other was Dr. J. Michael Bishop who was a Nobel Prize winner in Physiology/Medicine in 1989 for his discovery of the cellular origin of retroviral oncogenes.  Here is his Wikipedia page.


    Then, we went to L.A. Live.  They have music and dancing at the Nokia Club.  They had all different kinds of juices for us.  There’s also a game room, bowling alley, food, and pool tables.  It was a great way to mix with all of the ISEF participants and observers.

Today was another incredible day!  I can’t wait to see what they have in store for us tomorrow.


Get every new post delivered to your Inbox.

Join 94 other followers