Basics of the Raspberry Pi – Turning a light on and off

Basics of the Raspberry Pi – Part 3
Part 1 – Basic Setup
Part 2 – How it all works 

Turning a light on and off – Part 3


In this blog we will make a small Java application that turns our light on and off. We will need a few things before we get started.

  1. Eclipse
  2. sFTP Client (Filezilla is a great on that is free)
  3. SSH Client/Terminal
    • If you are on windows that I would recommend downloading PUTTY

The first thing that we will do is Open Eclipse and create a new Java Project.  We will need to add one dependency to this project; the easiest way to do that is to convert your Java Project to a Maven Project.  This only takes a second, you simply right click your project, go to the configuration option and then click “Convert to Maven Project”.

After you convert your project to a Maven Project you will notice a pom.xml file is automatically added to your project. All you have left to do is add the following element to this pom.xml file under the Project element.

<dependencies>
  <dependency>
     <groupId>com.pi4j</groupId>
     <artifactId>pi4j-core</artifactId>
     <version>1.1</version>
  </dependency>
</dependencies>

Now right click your project, go to the Maven menu and then click “Update Project”. Now you have the PI4J dependency in your project.  Now lets write some code!

Lets start by adding our Java class.  Right click your “src” folder and select “new” then select “class”.  We will name this new class “main”.

So the goal of this application will be to build a small console application that will turn the light on, wait 5 seconds and then turn the light off.

Now lets setup our Java app to run as a console application.  All we need to do is add this basic code.

public class main {
	  public static void main(String[] args) throws InterruptedException {
	        System.out.println("This is my console app");
	  }
}

Lets add our dependency to the application.

import com.pi4j.io.gpio.*;

Lets just jump in and add all of our code; I will explain what it all does below.

  public static void main(String[] args) throws InterruptedException {

        // intializing our gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        //Turn the LED on
        System.out.println("Turning the LED ON.");
        // Wire up our GPIO 25 pin to a variable in our code
        // We are also turning on the LED with the PinState.HIGH parameter
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, "LED-25", PinState.HIGH);

        // Lets leave the LED on for 5 seconds
        System.out.println("Waiting for 5 seconds before turning the LED off");
        Thread.sleep(5000);

        // turn off gpio pin #
        System.out.println("Turning the LED off");
        pin.low();
        
        // clean up tasks before we exit thie application
        gpio.shutdown();

        System.out.println("goodbye :)");
  }

We only have about 4 lines that do anything significant in the code above.

1.  final GpioController gpio = GpioFactory.getInstance();
This simply initialized our GPIO library so we can start interacting with the 40 pin connector on the Raspberry Pi

2.  final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, “LED-25”, PinState.HIGH);
This is probably the most important thing to not only get right but understand what is going on.  What we are doing is simple; we are binding the “pin” to one of the 40 pins on the board.  This is key on how to understand which pin you are binding to.  Here is a “cheat sheet” that I found on the pi4j website; note that the GPIO number you need to use is labeled next to the value you actually see on the GPIO Extension Board. GPIO Cheat Sheet I would recommend printing this off for future projects

3.  pin.low();
We are turning the light off by essentially breaking the circuit.

4.  gpio.shutdown();
We need to clean up our resources and let go of any control we have acquired over the board so other processes can control this circuit.

We are done writing our code; now we just need to compile our application, transfer it to our Raspberry Pi and run it.


Lets compile our code!
We want to basically export our code into an executable JAR file.  Lets right click our project and select the “Export” option.
You will want to ensure that you are exporting this as a “Runnable Jar File” as show in the image below

When we click next we just want to select a run configuration and location to output our JAR file.  If you don’t have a configuration to select, the easiest was to create one is to just run the application in debug mode once.  Eclipse will automatically create one for you.

Now just click the “Finish” button and Eclipse will create your Jar file!


Lets transfer our application/JAR file!
Transferring the Jar file should be fairly basic as long as you enabled ssh on part 1 of this blog.  The first thing you need to do is figure out the IP Address of your Raspberry Pi, assuming it is on your network.  The easiest way to do this is to open a terminal and type “ifconfig”; you should be able to easily see the IP of your Pi from there.

Now that we have the IP Address of the Pi, on the device that has the Jar file, you need to transfer the file using sftp/ssh.  There are tons of tools for this; I personally prefer to use FileZilla.  When you launch FileZilla you can perform a “Quick Connect” and enter the following information.

Host:  {the IP you found from the instructions above}
Username: pi
Password: raspberry
Port: 22


Lets run our application!
Now that the Jar file is on the Raspberry Pi, lets hop back onto the Pi (or ssh in).  Open a new terminal and navigate to the directory that you transfered the Jar file to.  Now type the following command

sudo java -jar myjarfile.jar

You should now see your LED turn on and off!  The ONLY reason we had to run this with high level permission (sudo) is because the GPIO library requires that to interact with the hardware.

I hope this blog helped you understand the entire process better!  Honestly once you understand the basics taking it to the next level is a piece of cake/pi. (sorry for the incredibly dumb play on words).  My next Raspberry Pi will show you how to use sensors and the camera (Night vision/IR Camera).  In regards to code it is not much more and the concepts are almost identical to what we went over here.

Basics of the Raspberry Pi – Part 3
Part 1 – Basic Setup
Part 2 – How it all works 

Basics of the Raspberry Pi – How it all Works

Basics of the Raspberry Pi – Part 2
Part 1 – Basic Setup
Part 3 – Turning a light on and off 

How it all works – Part 2


Lets figure out how all of this comes together!  I promise once you do this once then everything will click and you will be off to bigger and better projects with the Raspberry Pi.  For now lets talk about a simple project; below is our 1,000 foot view of what we want to accomplish.  (All of these components where referenced in Part 1 of this blog)

This all seems fairly simple until you get to the breadboard and how/where too plug in your wires and components.   I can tell you that this is a fairly dummy proof process; I have plugged components into the wrong places and have yet to break anything.  I know I did something wrong when the Raspberry Pi immediately resets/reboots.  Just please be sure to understand your voltages and when to use resistors otherwise the components that you use could easily break/pop/die.

Ok, so to recap we have now performed the following steps and are ready to start interacting with the breadboard.

  1. Plugged our 40 Pin Cable into our Raspberry Pi
  2. Plugged the other end of the 40 Pin Cable into our GPIO Extension Board
  3. Seeded our GPIO Extension Board into our BreadBoard

Before we start plugging wires into the breadboard we need to understand how the GPIO Extension Board.  The Breadboard itself is really a super simplistic device; I have outlined how it works below.

This is a device designed to assist in the creation of completing circuits.  Basically every red line that is overlaying the breadboard represents a metal connection that ensures that all pin holes are inter-connected.

When you plug the GPIO Board into this, you really want to plug it in so that one half of the pins are on the right side and the second half are on the left side as shown in the picture below.

You can now start creating full circuits that connect different Pins on the GPIO Extension Board;  which is essentially the same thing as creating circuits directly on the 40 Pins on the Raspberry Pi device.  This is just the safe and easy way to do it.

The only thing left to do is build a complete circuit on the BreadBoard!  To Create a Complete Circuit we will perform the following steps:

  1. Plug one end of your resistor into the breadboard line that the GPIO 25 connector from the GPIO Extension Board aligns to; plug the other end of the resistor into a line on the breadboard that is free.
  2. On the same line the resistor connects to, plug in one end of your LED light
  3. Plug in the second end of the LED light into a line that aligns to a Ground pin “GRND” on the GPIO Extension Board.

When you are done then your breadboard should be similar to the image below.  There are infinite ways to create this circuit, just be sure to get that resistor into the circuit so you do not damage up your LED light.

Below is the same implementation as above.

We are ready to turn on our Raspberry Pi and start creating some code to control our circuit.  We will be using Java in this demo to control our LED light.  I am choosing Java not because it is the more suitable language for interacting with the Raspberry Pi but because it is the most popular language being used with this device.

Part 3 – Turning a light on and off (using Java)

Side note about using Java with the Raspberry Pi: I can tell you that I have already ran into instances that exposes the Java flaws with the Raspberry Pi but don’t fret too much; all shortcomings have easy work arounds.  I plan on creating a blog focused solely on this topic.

Basics of the Raspberry Pi – Part 2
Part 1 – Basic Setup
Part 3 – Turning a light on and off 

Basics of the Raspberry Pi – Setup

Basics of the Raspberry Pi – Part 1
Part 2 – How it all works 
Part 3 – Turning a light on and off 

Part 1: Raspberry Pi – Prototyping, Simple Lights and some Java


I kept hearing about the Raspberry Pi and all of the wonderful things you can do with it. It was time for me to roll up my sleeves and dig in with my first real project.  I can honestly say that I seriously struggled to find all of the information and documentation out there that I needed put together in a way that I could easily take it and run with it.  Well after many hours of tinkering I decided to write my own guide in words that makes sense to how my mind works.

In this blog I will go over everything you need to know about the Raspberry Pi from what to buy down to running some basic java code to turn on an led.

Here is what you need and more importantly; why you need it!  The total cost of all of this should not exceed $65 unless you cannot find #9 at a good price.

  1. Raspberry Pi – This is basically your computer
  2. Micro SD Card  – This is your hard drive that you will install your Operating System on
  3. 40 Pin Cable – This is used to connect to the Breakout Board (#4)
  4. GPIO Extension Board (Breakout Board) – This is placed on the BreadBoard and basically allows you to create controllable circuits with the Raspberry Pi
  5. BreadBoard – This is nothing more than a bunch of metal wires and pin holes for you to create circuits on using Jumper Cables that connect pins from the Breakout Board (I will explain in detail later)
  6. Resistor – This is here to reduce the voltage that will run through your LED; if not it may melt or pop!
  7. LED – This is your LED that basically will tell us if the circuit that we created is active or not.
  8. Ambition

Lets get started!  The first thing that we need to do is setup our Raspberry Pi.  This is probably the easiest part of the entire thing.  If your Raspberry Pi came with an SD card then it probably already has the OS installer setup.  You will need to download and install NOOBS onto your SD Card. NOOBS is an easy operating system installer which contains Raspbian which will be the Operating System that you interface with when running your code.

Once you download this file; simply unzip it onto your SD Card.  Now lets pop the SD Card in your Raspberry Pi and fire it up.  Your goal is to see a screen like this one. 

The next part is quiet easy; all you have to do is click the CheckBox for Raspbian and click install.  This process takes about 10-15 minutes or so.  
Once the install is finished the system will reboot and load up your brand new Raspbian GUI.

This base install is all loaded up with some of the tools you will need to start programming.  Most notably it already has Java SDK, VNC Server, SSH Server, and other countless developer tools.  Some of which you will need to enable through the rasp-config tool.  The easiest way to enable some of these tools is to open a new Terminal and type “sudo raspi-config”.  Once this menu appears, navigate to “Advanced Options”, this is where you can drill down into SSH and/or VNC and enable/disable these services.

Now that we have a working Raspberry Pi we can start adding all of the other components to it.   In part 2 of this Blog we will go over all of the components and how it all comes together.

Basics of the Raspberry Pi – Part 1
Part 2 – How it all works 
Part 3 – Turning a light on and off