Breathing Light LED on Raspberry Pi using Java

  • 20 February 2018
  • ADM

 

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

 

In this article will make a light LED "breathing" using C. The light intensity will start from zero and gradually will increase up to maximum and then will decrease back to zero. To do this will use PWM signals. The PWM signal is a square-wave signal to control the amount of power going to the GPIO pin, in our case to the LED.

If you need more info on LEDs and how can be connected please follow the Blinking LED on Raspberry Pi using Java tutorial. Also, if you want to see the same "breathing" LED 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).

Principle

Pulse Width Modulation (PWM) is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. The on-off pattern can simulate voltages in between full on (3.3 Volts) and off (0 Volts) by changing the portion of the time when the signal is on versus the time that the signal is off. The duration of "on time" is called the pulse width. To get varying analog values, you need to change the pulse width.

Breathing Light LED on Raspberry Pi using Java - /images/breathingLed03.png

In the image above, the green lines represent the regular time interval.

frequency = 1/period = numbers of cycles/time

f = 1 / T = N / t

f = frequency, the cycles in a unit of time

T = period, the time required for one cycle

N = a number of cycles

t = an amount of time

For example, for a PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each.

Schematics

To do the schematics I used Fritzing software. For this tutorial, I will connect the cathode to the Raspberry Pi GPIO, also it is possible to connect the anode to the Raspberry Pi GPIO, check Blinking LED on Raspberry Pi using Java tutorial for details.

Here is the schematic connecting the LED using the cathode of the LED connected to the Raspberry Pi GPIO. Using the Pinout website for reference, we can check the numbering, starting from physical pin 11(BCM pin 17, Wiring Pi pin 0). Because we are using PWM to control the intensity of light we as using pin 1 (BMC_GPIO 18, Phys 12).

Breathing Light LED on Raspberry Pi using Java - /images/breathingLed02.png

And the physical visualization of the circuit.

Breathing Light LED on Raspberry Pi using Java - /images/breathingLed01.png

Hardware

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

Breathing Light LED on Raspberry Pi using Java - /images/breathingLED04.jpg

Software

Key functions

  • Gpio.wiringPiSetup()

    Initialize the wiringPi library, this is needed for PWM.

  • SoftPwm.softPwmCreate(int pin, int value, int range)

    This creates a software-controlled PWM pin. You can use any GPIO pin and the pin numbering will be that of the wiringPiSetup function you used. Use 100 for the pwmRange, then the value can be anything from 0 (off) to 100 (fully on) for the given pin.

  • SoftPwm.softPwmWrite(int pin, int value)

    This updates the PWM value on the given pin. The value is checked to be in-range and pins that haven't previously been initialized via softPwmCreate will be silently ignored.

If you didn't installed already, 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 BreathingLed.java

Copy/paste the following code.

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class BreathingLed {

    private static int PIN_NUMBER = 1;

    public static void main(String[] args) {
	try {
	    /** initialize the wiringPi library, this is needed for PWM */
	    Gpio.wiringPiSetup();
	    SoftPwm.softPwmCreate(PIN_NUMBER, 0, 100);

	    /** keep program running until user aborts (CTRL-C) */
	    while (true) {
		for (int i = 0; i < 1024; i++) {
		    SoftPwm.softPwmWrite(PIN_NUMBER, i);
		    Thread.sleep(200);
		}
		Thread.sleep(1000);
		for (int i = 1023; i >= 0; i--) {
		    SoftPwm.softPwmWrite(PIN_NUMBER, i);
		    Thread.sleep(200);
		}

	    }

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

Save and close vim.

Test

To compile the application run the following command:

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

To run the application execute the following command:

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

You should see the LED light "breathing".

 

References