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:
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:
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):
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:
In BlueJ, open the project AdjustableLED. Your screen should look like this:
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.
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:
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":
This rectangular red icon represents the "adjustab1" object. This object is the Java representation of the real AdjustableLED connected to the Raspberry Pi.
void setValue(int desiredValue)
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:
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.
readFromURL(String urlString)This method can be used to return a String from any Internet address.
To try it out, copy the address below and paste it in your browser:
http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=newEvery 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.