Adjustable LED Tutorial

This is a quick tutorial on how to use BlueJ on the Raspberry Pi to create a Soft PWM (Software Pulse Width Modulation) pin and use it to control a LED brightness. It is not intended to be a complete description of how to use the pins: for that, see the sections called Under the Hood.

This tutorial will make use of the project AdjustableLED, which should be downloaded and open in your BlueJ running on the Raspberry Pi.

We use the same LED from the previous section. If you already have it assembled to your Raspberry Pi, (and downloaded the AdjustableLED project), you can go directly to the Code section of this tutorial.


For this experiment, we will need:

  • 1 LED
  • 1 Resistor (any value between 270Ω to 330Ω)
  • 2 wires
  • a breadboard *optional


Without a breadboard

An LED is a component that emits light whenever there is a current flowing through it. It will have one long leg and one shorter one. The short leg (ground terminal) should be connected directly to the black wire and the longer leg should be connected to your resistor. The resistor is used to limit the current flowing and prevent the LED from burning out.

The other leg of the resistor should be connected to the other wire (the red one), as we can see in the following picture:

Figure 1: connecting the LED to the resistor (click for larger picture).

The Black wire should then be connected to the pin marked 20 (Ground) on your Pi and the red wire should be connected to the pin marked 22 (GPIO6):

Figure 2: The Raspberry Pi Pins.

Note: If you have a Raspberry Pi model B+, you will have more than 26 pins. However, the assignment for those pins present on both models (B and B+) will still same we are describing here and the projects should work without change.

Here is how our our circuit must looks like:

Figure 3: Connection to the Raspberry Pi (click for larger picture).

With a breadboard

If you have a breadboard of any size available, we recommend using it to build the circuit. Don't forget to observe the LED polarity! The circuit is shown in Figure 4, below:
Figure 4: Using a breadboard (click for larger picture).


In BlueJ, open the project AdjustableLED. Your screen should look like this:

Figure 5: The AdjustableLED project open in BlueJ.

Just like in the previous tutorial, each of the yellow boxes in the BlueJ screen above is a Java class. The LED class and the AdjustableLED class represents a real LED connected to the Raspberry Pi.

However, the LED class can only turns LEDs ON and OFF, and the AdjustableLED class, can not only turn an LED on and OFF, but also adjust its level of brightness. The LED class was covered in the GPIO: LED tutorial.

The Controller class is a skeleton class which we will be using in this tutorial to contain code which will control AdjustableLED objects.

Creating a new LED object

Before we start to write code, we'll see how the AdjustableLED class affects the real LED by using BlueJ to control it directly.

To start, right-click on the AdjustableLED class and from the pop-up menu, choose:

new AdjustableLED()
Figure 6: Creating an instance of AdjustableLED.

BlueJ will ask for the "name of the instance": the suggested name is good for now. You will see a red rectangle on the bottom left of the BlueJ window named "adjustab1":

Figure 7: An instance of AdjustableLED on the object bench.

This rectangular red icon represents the "adjustab1" object. This object is the Java representation of the real AdjustableLED connected to the Raspberry Pi.

Changing the brightness level

To set the AdjustableLed to half its maximum brightness level, right-click on the "adjustab1" instance and select:
void setValue(int desiredValue)
Figure 8: The pop-up with the list of operations on "adjustab1".

The brightness level are within the range of 0 (LED off) to 100 (full brightness). Let's set the brightness level to 50:
Figure 9: setting the level of brightness.

This should set the LED to half its maximum brightness. (It might take a moment the first time, as all the behind-the-scenes connections are made).

Tip: The AdjustableLED Class contains its own documentation showing all the available methods and a brief description of each of them. To see them just double click in the AdjustableLED class (yellow box) and its documentation (javadoc) will show up:

Figure 10: The Javadoc for the AdjustableLED class.


  • Exercise 3.1: You have set the AdjustableLED to half its brightness. Can you:
    • Set it to full brightness?
    • Turn it off?

  • Exercise 3.2: You can write code in the Controller class to do what you have just done interactively. In the Controller class, change the body of the method "void setFullBrightness()" to call the method which will set the AdjustableLED to full brightness, and likewise change the method "void turnLEDOff()" in the same class to turn the LED off.


    • To edit the Controller class, double click on the "Controller" yellow box and you will see the source code for the Controller class.

    • Important: before testing the changes you make to the Controller class, do not forget to compile your project by clicking on "Compile" on the top left of your editor or on the left panel of the BlueJ main screen:
      Figure 11: Controller class: Click on the Compile button before testing.

    • To test the changes in your Controller class, make an instance of the Controller by right-clicking on the class, just like we did with the AdjustableLED class, and then right-clicking on the red Controller instance to call the methods you have just changed.
  • Exercise 3.3: Change the body of the method "void dimToZero()" in the Controller class to slowly lower the brightness of the AdjustableLED to zero, creating the effect that the Adjustable LED is slowly turning itself off.


    The Controller class has a method called

    sleepMillisec(int time)
    This method can be used in order to make your program wait a given number of milliseconds.

    1 millisecond is a very short time.

  • Exercise 3.4: Like you did in the previous exercise, can you change the body of the method "void smoothLighthenUp()" in the Controller class to slowly raise the brightness of the AdjustableLED to full brightness (100), creating the opposite effect?
  • Exercise 3.5: Change the body of the method "void internetLight()" to change the brightness level of the LED accordingly to a number we recive from the internet.


    • Your Controller class has a method called
      readFromURL(String urlString)
      This method can be used to return a String from any Internet address.
    • There is a url in the internet where you can get random numbers from 0 to 100 (the exact range which our LED operates.).

      To try it out, copy the address below and paste it in your browser: 
      Every time you reload the page, a different random number will be sent to you.

      You can use that address as an input to the readFromURL(String urlString) method.

    • Be aware that readFromURL(String urlString) returns a String and you will need to convert it to int in order to change the brightness level of the LED.

Next tutorial

Previous tutorial

Back to the index