Blinking LED on Raspberry Pi using Java

  • 21 March 2017
  • ADM

 

Blinking LED on Raspberry Pi using Java - images/logos/raspberrypijava.jpg

 

In this article will make a LED blinking using Java. If you want to see the same circuit in other programming languages, please check the references section.

Components

  • 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
  • 1 * 220Ω Resistor
  • 1 * LED
  • 1 * Breadboard
  • 2 * Jumper wires
  • software: Pi4J

Note: you can easily find all components on Amazon or Ebay (in my case, I bought a "Raspberry Pi starter kit" containing a lot of components).

LED

The LED is the abbreviation of light emitting diode. The LED has two electrodes, a positive electrode and a negative electrode. It lights up only when a forward current passes. The colour of light depends on the materials it is made. In general, the drive current for LED is really low, between 5-20mA. Usually, needs an extra resistor for the current limitation to protect the LED.

The symbol for a LED is:

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED01.png

There are two methods for connecting a LED with Raspberry Pi GPIO:

  • The cathode connected to the Raspberry Pi GPIO.

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED04.png

The anode of the LED is connected to VCC (+3.3V), and the cathode to the Raspberry Pi GPIO. When the GPIO outputs a low level, the LED is on; when it outputs high, the LED is off.

  • The anode connected to the Raspberry Pi GPIO.

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED03.png

The anode of LED is connected to Raspberry Pi GPIO after a resistor, and the cathode is connected to ground (GND). When the GPIO outputs a high level, the LED is on; when it outputs a low level, the LED is off.

Resistor

The main function of the resistor is to limit current. In the circuit, the character "R" represents resistor, and the unit of resistance is ohm(Ω).

The symbol for a resistor is:

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED02.png

A band resistor is used in this tutorial. It is a resistor with a surface coated with some particular colour through which the resistance can be identified directly.

To calculate the resistor value needed for this tutorial will use Ohm law:

R=U/I

We already know that to make a LED on a current between 5~20mA is needed. We also know that Raspberry Pi GPIO is 3.3V. Adding these two pieces of information in Ohm Law we can see that a resistor with a value between 165Ω and 660Ω is needed.

R = U / I = 3.3V / (5~20mA) = 3.3V / (0.005A ~0.020A) = 165Ω~660Ω

I have a 220ohm resistor in hand, so I will use it for this tutorial.

Schematics

To do the schematics I used Fritzing software.

The cathode connected to the Raspberry Pi GPIO

Here is the schematic connecting the cathode of the LED to the Raspberry Pi GPIO. Using the Pinout website for reference, we can see that we connected the LED cathode to the physical pin 11(BCM pin 17, Wiring Pi pin 0)

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED05.png

And the physical visualisation of the circuit.

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED06.png

The anode connected to the Raspberry Pi GPIO

Here is the schematic connecting the anode of the LED to the Raspberry Pi GPIO. As you can see the change is just how the connected are placed.

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED07.png

And the physical visualisation of the circuit.

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED08.png

Hardware

Now it's time to connect everything according to the diagram presented in the section above.

The cathode connected to the Raspberry Pi GPIO

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED10.jpg

The anode connected to the Raspberry Pi GPIO

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED09.jpg

You can see that the only difference is how it is connected to the board.

Software

First, you need to install Pi4j, for this run the following command:

$ curl -s get.pi4j.com | sudo bash

The command will do the following:

  • adds the Pi4J APT repository to the local APT repositories
  • downloads and installs the Pi4J GPG public key for signature validation
  • invokes the 'apt-get update' command on the Pi4J APT repository to update the local package database
  • invokes the 'apt-get install pi4j' command to perform the download and installation

Note: all the code will be written directly on Raspberry PI console using vim editor. I've chosen this method because the example is simple and doesn't require a long time to edit. A better approach is to edit the source code locally, on your PC, using your preferable editor and then upload the code to Raspberry PI.

Login to Raspberry PI using ssh command from Linux and MacOS or using putty from windows, then open the vim editor using the following command:

$ sudo vim BlinkingLed.java
Copy/paste the following code.
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class BlinkingLed {

    public static void main(String[] args) {
	try {
	    /** create gpio controller */
	    final GpioController gpio = GpioFactory.getInstance();

	    final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00);

	    /** Blink every second */
	    ledPin.blink(1000, 15000);

	    /** keep program running until user aborts (CTRL-C) */
	    while (true) {
		Thread.sleep(500);
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

Save and close vim.

Note: The numbering from Pi4j library is different, physical pin 11 is GPIO 00. For reference check the Pi4j website, in my case: http://pi4j.com/pins/model-3b-rev1.html.

Test

To compile the code run the following command:

$ sudo javac BlinkingLed.java  -classpath .:classes:/opt/pi4j/lib/'*'

To run the application execute the following command:

$ sudo java -cp .:/opt/pi4j/lib/'*' BlinkingLed

Regarding how you connect the LED, you should see the LED blinking, one second ON, one second OFF.

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED12.jpg

Blinking LED on Raspberry Pi using Java - /images/BlinkingLED11.jpg

 

References