Raspberry Pi: Let's light up the (hello) world

There's a light, that shines, special for you, and me..


Soldering complete and Occidentalis installed, it is time to start learning how all this stuff works. Plus check that my soldering isn't so bad that it has destroyed my Cobbler.

From a quick bit of googling I started with the HelloWorld project:

Hello World: Python

So there are a tonne of hello world pythons tutorials and examples, so I won't dwell on this.
  1. from the shell (I setup wifi connection & then ssh on to the Pi so I can work from my normal desktop - I also installed vim - you can either install vim or use your preferred editor) run: 

        vim helloworld.py

    In the editor enter the following:
        print("Hello World")

    Exit and save the file (:wq)
  2. From the command line run
        sudo python helloworld.py
That's it. You will see the Hello World output in the shell. Yay.


Hello World with Lights On

So, the above was trivially easy and kinda pointless - but I did actually go through that, really just to make sure python was behaving and so I had a working baseline to go from.

So next I wanted to do a Hello World equivalent with hardware - the simplest thing to do seemed like a script that instead of printed hello world, just switched on a light briefly.

This assumes you have followed the same path as me and have opted for Occidentalis, and have soldered your Pi Cobbler (the basics are still really the same even if not using a cobbler, and are just breaking out your pi using a GPIO ribbon cable).



  1. If you opted not to install the Occidentalis OS (mod'd Wheezy OS packaged with stuff if you are planning on doing hardware stuff), then you will need to install some extra bits and pieces (you may already have them installed anyway, but running this won't do any harm - will just print the message saying its already there):
        sudo apt-get install python-dev
        sudo apt-get install python-pip
       
    sudo pip install RPi.GPIO

  2. Connect your Pi to the cobbler using the provided ribbon cable - plug the cobbler into your breadboard so it straddles the break down the middle (See photo below of the final setup of my breadboard for this experiment)
  3. Using a standard jumper lead, connect the ground on the cobbler to the -ve power rail on the breadboard (in this case mine is the purple-ish rail as you can see in the photo, the ground on the cobbler will be labelled GND). This is always best practice.
  4. Connect a resistor from the Pi Cobbler plug 23 to one of the free holes not linked to the cobbler (the resistor is needed to reduce the power - without one of these you will just blow your LEDs)
  5. Connect the +ve leg of the LED (the longer of the two legs) to the -ve power rail that we plugged the GND into, and the shorter LED leg into a hole in line with the resistor we plugged in (again, see photo below for final setup - although ignore the surplus resistor - that's not doing anything, I juts forgot to remove for the photo)



Now that the hardware is all connected, we need to update our simple helloworld script to control the light.  So again, run vim hellolightworld.py and enter the following script:


Now save and exit that script, then go to the shell and run python hellolightworld.py  and watch that little sucker flicker on briefly!









0 comments: