Saturday, May 10, 2014

Microphone and speaker set up

Now that you have your camera set up and functional, lets get the setup parts out of the way.

First, I'll walk you through the speakers set up.
This should be simple - simply plugin the speakers (3.5mm) cable in to the pi, and that should be it.

To verify that your speakers work, SSH in to your rPi, and type speaker-test
That should immediately start producing white noise from your speakers.

Just for kicks, I would also try the following command:
aplay /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav

aplay is the default audio player (for playing wav files, mp3s etc), and Singer2 is one of the default files that comes installed on the pi :)

Now that you have your speakers set up (leave a comment if for some reason your speakers aren't working), time to set up the mic.
Shutdown your pi (Remember sudo shutdown now) and plugin the USB microphone. Restart your pi, ssh back in to it, and time to get to work.

First, lets change a setting in alsa (Advanced Linux Sound Architecture).
Type sudo nano /etc/modprobe.d/alsa-base.conf
Change the line that says "options snd-usb-audio index=-2" to "options snd-usb-audio index=0"
Then, Ctrl+O to save, Ctrl+X to exit.
Reload alsa by typing sudo alsa force-reload
Reboot your pi (sudo reboot)

Now, ssh back in to your pi
Enable the USB microphone by typing sudo modprobe snd_bcm2835
Now, try recording by typing arecord -f S16_LE -D hw:0,0 -r 48000 test.wav
And, play it back by typing aplay test.wav

Now, your audio might have recorded but is very very faint/weak. To increase the audio capture volume, type alsamixer
Press F6 so as to select the USB sound card, and increase the "capture gain" using the arrow keys.
Save your settings by typing sudo alsactl store

Note: You may have to try several times before you can actually get the "ideal gain" for your microphone.

Here is a photo of the final set up. To capture your attention and remind you how cool this project will end up being, the next post will deal with setting up the voice recognition using Jasper and will include fun apps and videos :)


The left corner (behind the speakers) is a cardboard box with the pi and USB hub in it (until I can learn and afford to make my own 3D printed packaging). At the top of the box you see the little camera peeking out. On the bottom right corner you can see the USB microphone. This will be the final set up for the voice recognition + motion detection project.

Monday, April 28, 2014

Setting up the pi-camera

So now that you've taken all the steps to set up your pi, lets get cracking! Time to set up the camera and take your first still picture!

Follow along, and you'll be taking photos in no time!





  1. Procure a pi camera. Yes, that is correct, without the camera, you cannot set up the camera!
  2. Plug in the camera into the slot right next to the HDMI port. Note - the silver connecters on the camera wire must face the HDMI port. See the image below:
  3. Power up your already set up pi. Here's a photo of how I powered it up, because I haven't bothered with setting up the pi UPS yet, I'm still using my USB breakout cable. If you remember the earlier post about plugging in the USB cable, you can ignore the RX and TX and just plugin the ground and the 5 volt. This should immediately power up your Pi
  4. SSH in to your Pi. Remember that static IP I had you set up? Open terminal and type - ssh -X pi@IP-ADDRESS-YOU-SETUP-EARLIER
  5. Use the password you created to login to your pi and type - sudo raspi-config
  6. Now, if you had followed the previous posts correctly, you should see an option called "Enable Camera". Using the arrow keys, choose that option, press Enter/Return, enable the camera and you're all set!
  7. Choose "Finish" on the config, and accept to reboot the pi
  8. Dont freak out, you should have been disconnected from the Pi. Give it a minute or so to reboot, and re-ssh in to your Pi
  9. Now, to test your setup, point the camera towards your face and type - raspistill -o selfie.jpg
  10. Start vncserver on your pi by typing - vncserver :0 -geometry 1024x768 -depth 24 (Explained in setting up the pi part II). Now, open screen sharing on your mac by typing open "/System/Library/CoreServices/Screen Sharing.app" (Also explained in setting up part II), in the connect to type pi@IP-ADDRESS, enter your password, and see the desktop for your pi. Now, at the bottom left corner, choose file explorer, and you should see a file as soon as the explorer opens up called "selfie.jpg". VOILA! See below for a screenshot of my desktop where my pi took a selfie of itself :)

Taking it down a notch...

So the entire point of this blog is to walk people with minimal programming experience through setting up an automated home with raspberry pis, and after some initial feedback it looks like some of the parts don't make sense...

So, here, I'll try to explain some of my commonly used terms:


  • Raspberry Pi - The brain of your project. This is the mother ship. Its a 35$ computer created for amateur hobbyists to tinker with
  • Terminal - This is sort of the "DOS" equivalent for Macs and Unix machines. If you're on a mac, click the finder and type "terminal". Press enter, and welcome to a new magical land!
  • Sudo - You see me instructing you to type "sudo" several times. What is sudo? Sudo is a program that lets you run other programs with root privileges. Still confused? Basically, you want to run some things on your "mother ship" with the highest privileges ever. Ever seen those pesky "We need your permission to continue" messages while installing stuff? Sudo basically says - "Hey, you have our permission"
  • Nano - You see me typing nano, or emacs, or vim, or vi - These are all text editors. Think notepad or textedit - thats basically it!
  • Git - Version control. IMPORTANT. What is version control you ask? I love giving this comparison - Have you ever used google drive to do documents/spreadsheets/presentations? If yes, when you open the document, have you seen the "Last edit was made BLAH hours ago" or "last edit was made by BLAH? And clicking that shows you all the edits that have been made, and you can potentially go back a few "revisions" in case you liked an older revision better? Thats it! Thats version control for you. We like to keep a track of all our versions so that in case something goes wrong, we have a revision to go back to. Also similar to SVN
  • You see me using the words +5v, GND, Rx, Tx. What are these you ask? +5V refers to the power for your pi. Have you ever seen the red and black wires in plugs? Or in movies when a bomb squad is trying to diffuse the bomb at the last second? One is a power wire, one is a ground (and usually a trip wire so that in case you cut something the bomb goes off anyway). Thats basically it. +5V is the power, GND is ground. RX and TX are the channels for communicating to/from your pi.
Hope this all clears it out a little for the less tech-oriented people. IF you have any more questions/doubts, please post a comment and I'll get back to you as soon as possible!

Friday, April 18, 2014

Setting up the pi - Part II

Now that you've logged in to the shell, where do we go?
So, here's a few more setup-related things we should get out of the way now.

  1. Install the wifi dongle
    At the shell, type -  sudo nano /etc/network/interfaces
    Once in nano, add your network SSID and password, save the file, exit, and shutdown your pi. Plug in the dongle, restart, and verify connectivity using ping (Simply ping google.com)

    Now, at this point, if you've been reading everything I'm saying, you'll be wondering - shutdown from terminal... How do I do that? Well, here it is - sudo shutdown now VOILA!
  2. Now, lets get git on the pi. GIT? Are you crazy man? If thats what just went through your head, its okay.. I'm only making you install git so you can checkout my repo and automate a lot of the installation steps. Now note, most OSs already come with git. Just type in "git" to check.
    Once you have git, just clone my repo from:
    https://github.com/aabhassharma/raspPi
  3. Now, you wouldn't want to plug in the hulking USB cable everytime right? So, lets get ready for some ssh magic. First, run ifconfig on your pi to get the ip address. Then, try pinging that IP address from your laptop (on the same network). Now that you're getting responses from the pi, you're ready to rock! Lets set up some x11 apps so that we can actually see the magic happen. If you look inside setup_scripts on my github repository, the x11.sh file does just that! Change permissions for this script (chmod +x x11.sh) and then just execute using sudo (sudo ./x11.sh). Easy right? :D
  4. Next step - sudo raspi-config - This step is essential! Set up your raspberry pi correctly! Part 1 - Choose option 1 and expand the file system so that all of the memory card is used. Part 2 - Choose option 2 and change the rpi password! Later, we'll come back here and configure the camera :) For now - hit finish and choose yes to reboot.
  5. Install X11 on your mac - http://xquartz.macosforge.org/landing/
  6. SSH in to your rasp pi (sudo ifconfig on the pi to get the ip address, then ssh -X pi@<IP ADDRESS> from your Mac to ssh in to the pi).
  7. Bonus - So you've made it till here. Good job! Now what if you want to see what your desktop looks like? Lets install vncserver on the pi and then use screen sharing to see the pi desktop on the mac! First, on the pi - sudo apt-get install tightvncserver
    Then, on the pi type - tightvncserver
    Then, reboot the pi, and start your vncserver by typing - vncserver :0 -geometry 1024x768 -depth 24
    And finally, in terminal on the mac, type - open "/System/Library/CoreServices/Screen Sharing.app" - Under hostname, type your pi's IP address, and you're ready to go!
    Pretty cool, hunh? 
  8. Set up a static IP! So basically, this is a great analogy - The postman has to deliver you some mail. If you have a new address everytime, he's gonna hate his life! Instead, set up a static IP for your pi (static address). Follow this - http://www.raspberrypi.org/forums/viewtopic.php?f=26&t=22660

And that ends our raspberry pi headless setup! Stay tuned for more!

Setting up the pi - Part I

Good news - the parts are here!!!! (well most of them... Amazon went out of stock on my mic and couldn't deliver it, so will have to make a trip out to best buy tonight to pick up the microphone).



SO here goes part 1 of this tutorial - Setting up the pi without an externally connected display, keyboard and mouse.

Parts needed:
1. Raspberry pi
2. SD card with your OS of choice (several guidelines on how to mount an OS in case you don't buy a card with an OS on it already)
3. USB - TTL breakout cable.


  1. Download the drivers for the USB breakout cable - http://www.adafruit.com/products/954
    Follow the page to get to the downloads, and then download the driver for your OS.
    To make life easier, IF you're on OS X Mavericks, use this link - http://sourceforge.net/projects/osx-pl2303/
  2. Insert your SD card in to the pi (duh)
  3. Now, without plugging in the USB cable, open up terminal (yes, I'm a mac user) and type in the following and note the output:
    ls /dev/tty.*
    Now, plug in the USB cable and type it again and note the change in the output. That new "device" you see is your USB cable. If you'd like to learn more about /dev/tty, please refer to this link - http://stackoverflow.com/questions/8514735/what-is-special-about-dev-tty
  4. Connect to the cable at 115200 baud using "screen". Here's the command for that
    screen -L /dev/tty.NoZAP-PL2303-00001014 115200
    Please note, the /dev/tty.<something goes here> 115200 varies for every machine, that is jus the example from my machine and will NOT work for you. The -L flag starts logging to the local directory.
  5. Time to connect the pi. PLEASE DONT MESS THIS UP. YOU WILL FRY YOUR BOARD IF YOU ARENT CAREFUL NOW. Sorry for the all caps, but wanted to make sure you read all that. Refer to http://forums.adafruit.com/viewtopic.php?f=50&t=32019 for the wiring of the TTL cable. So here goes. First, the pin connections:
    Cable pin-out is as follows:
    Red = +5V
    Black = gnd
    White = RX
    Green = TX

    To connect to Raspberry Pi:
    Red to pin 2 (+5V)
    Black to pin 6 (GND)
    White to pin 8 (GPIO14, TXD)
    Green to pin 10 (GPIO15, RXD)

    Now, the pin diagram - http://www.hobbytronics.co.uk/raspberry-pi-gpio-pinout
    If you notice, there is a "P1" drawn on your board to indicate the first pin. Take it from there. Also, connect Rx, Tx and Ground first :)
  6. As soon as you plugin the Red cable, you should start seeing output on your screen. If you've followed along so far, it should've worked till here. Here's what it looks like now:
  7. Eventually, you should be prompted for a recovery login and password (if you are using NOOBS). Dont do anything. See that flashing green light on your pi? Its starting its installation magic now. Sit back and enjoy! (takes about ~30 minutes).
    If you're not using Noobs and just installed Raspbian on the SD card, then just login with pi/raspberry and you're good to go!
Part II coming soon :)

Wednesday, April 9, 2014

Christmas comes early part 2 (UPDATE)

Turns out I had a "duh" moment.
I don't need to get the kit to do all that, there's GPIO pins on the RaspPi!!!!

Just buy a USB - breakout cable and thats all you'll need!

The good people at MCM let me redo my order, so here's the final order:



A quick explanation of some of the "not so common parts":


  • Pi USB cable - Just a TTL break out cable - Connect to the GPIO pins (tutorial coming soon), plug the other end in to your computer and you're good to go! Eliminates the need for the keyboard + mouse + video output
  • Pi UPS - What use is a security system, if you don't execute it properly? Imagine a burglar breaks in and disconnects your pi from the power source. BOOM. Your house is emptied out. Thanks to this Pi UPS, if my mains are disconnected, I still have some juice to alert me of the same and to take screenshots of the perp to be analyzed by CSI (cue the music)
  • Kinobo (Screenshot below) - A good quality USB microphone. Why a USB mic you ask? Raspberry pi has no audio input! HOWEVER, do note, Wolfson Audio (the famous audio company) just released a plug and play sound card solely for the rasp pi!!!! That is super exciting news, since it was one of the downsides of the pi. Here's a link for the same http://www.adafruit.com/products/1761?gclid=CIWRkqDs0r0CFeMF7AodnhYAEw . Since I dont need fancy sound processing (right now), I'll pass on that and save me the 20$ :)

Also, note : I added my coupon codes in the screenshot, in case you wanna save some money ;)
Don't know how long they'll be active though!

Lastly, some super exciting news. Shubhro Saha and Charlie Marsh at Princeton have developed an excellent project for audio control on the pi!!! So far I've only browsed through the code, and so far, it looks GREAT! I'll be using it next week, so stay tuned for the details and testing.  Until then, here's the link to their project:
http://jasperproject.github.io/

Goodnight!




Friday, April 4, 2014

Christmas comes early

As promised, I'll be sharing every little detail of this project with you.
As the title suggests - its time to go shopping!!!

Here's a snapshot of my shopping cart for today:



Now for a quick explanation of the parts in my cart:


  • Raspberry Pi Model B Enhanced Kit - Contains 1 model B board, one USB power supply, clear case(that I'll probably never use), 8 gig card preloaded with OS, mini keyboard, mini mouse. Why am I getting a keyboard and mouse you ask? Because, before I can start programming my raspberry pi over SSH through my mac, I must plug it in and get the ssh packages, do the setup etc.
  • Wi-Pi - Just a cheap wireless adapter. A LOT of pi users prefer the Edimax (about the same price)
  • Raspberry Pi Camera Board - I'm excited for this. As the name says, its a camera, on a board
  • 4 port USB hub - Because I need to plugin my wifi, USB keyboard, mouse (and maybe other things, who knows!). And I'm sure I can re-provision the hub to something else in my apartment
Total - 145$

The next post might be closer to when everything is delivered :)