Controlling external LEDs

In the last chapter, we looked at a quick blink recipe on how to control our on board LEDs. Now, the objective is to have an external LED on a breadboard blink. First, we will take a look at the circuit symbol of a basic LED so that we can recognize its proper usage, as shown in the following diagram:

Controlling external LEDs

The typical symbol for a light-emitting diode (LED)

In the following image, you will see what a real-life LED actually looks like. Not so straightforward as looking at the symbol, right?

Controlling external LEDs

Note

With LEDs, polarity matters to have a working circuit. Pay attention to the fact that the anode is the longer end, whereas the cathode is the shorter end. Although mixing them up will not cause any damage, the circuit will not work.

Getting ready

You'll need the following items to supplement your now happily perking BBB:

  • LED: Just a plain vanilla, inexpensive LED is fine; this is the type you'll find for pennies at your local hobby store or online. You may already have a bunch in your kit.
  • Resistor: Anything from 700 (700Ω) to 1k is fine. We will use a 700Ω version here (violet/black/brown/gold bands).
  • 2x jumper wires: These are easy to connect to the breadboard.
  • Breadboard.

How to do it...

Now, let's begin with the following steps:

  1. From our fritzing tool, here's the diagram we made for our wiring:
    How to do it...
  2. Now, wire up your breadboard using the following steps:
    1. First, put the Ground wire into GND (P8_2) on the BBB.
    2. Then, insert the other wire to the P8_15 pin on the BBB.
    3. On the breadboard, put the LED's shorter end—the cathode (-) pin—into the GND rail and the longer end—the anode (+) pin—into the sixth slot of the breadboard.
    4. Now, insert your resistor and make sure that the top end is aligned in the slot with the GPIO wire and the bottom end is aligned with the LED's anode.
  3. Open up Cloud9 IDE at http://192.168.7.2:3000 and create a new file called blink_LED.js.
  4. Then, copy and paste the following code into the open IDE window:
    // Setup
    var b = require('bonescript'); // Call library
    var LED = "P8_15"; // Pin choice
    var state = 0; // LED state
    b.pinMode(LED, 'out'); // Pin function 
    
    setInterval(blink, 500);
    function blink() {
      state = !state;
      b.digitalWrite(LED, state);
    
  5. Before running the code, let's take a closer look at the parts. First, as noted in the prior chapter, you need to invoke the BoneScript library so you have access to all its functions:
    // Setup
    var b = require('bonescript');
    

    Please keep in mind that anything after the // are comments, and isn't functional code.

  6. Now, select the pin number where your LED is wired. You can change this option easily; just be sure to choose from an available pin. Then, make the change on your board as well. Refer to the pin layout reference diagram at the beginning of this section for more background on the pin outs, as shown in the following code:
    var LED = "P8_15";
  7. This line defines the variable state of our pin, which in this case has a LED connected to it. Naturally, the state will vary as either on or off, where 0 is equal to off and 1 is equal to on:
    var state = 0;
    
  8. Following this, as the pin has no preassigned value, here, we have to use pinMode to tell the pin (P8_15) what type of pin it's supposed to be, either an INPUT (this would be used with a button) or an OUTPUT. In this case, as we will control a LED, our GPIO will be an output, as follows:
    b.pinMode(LED, b.OUTPUT);

    You can also write it this way:

    b.pinMode(LED, 'out');
  9. From the setup code, we then shift to instructing the LED to evoke the blink function (which we'll define in the next section of code). Then, flash on/off according to whatever interval we specify (in this case, every 500 milliseconds). This line is analogous to the commonly seen loop function in Arduino scripts. However, JavaScript and the event-driven Node.js environment greatly simplifies the code, as follows:
    setInterval(blink, 500);
  10. Finally, we will define the blink function. The ! before the state value inverts the value, and as the LED begins with a 0 (off) state (this isn't the same as 0 changing to 1 (on)). The last line (b.digitalWrite)—which happens to be Arduino-friendly syntax—establishes an output statement for the LED and its on/off state, as shown in the following code:
    function blink() {
      state = !state;
      b.digitalWrite(LED, state);
    }
  11. Finally, let's see what the script does. Click Run in the Cloud9 IDE. Your LED should now be blinking merrily at 500 millisecond increments.

    Note

    You can find more documentation on the BoneScript library functions at http://beagleboard.org/support/bonescript.

Writing an alternative script with Python

Starting from step 3, here's the Python version of the recipe that uses the Adafruit library:

  1. In the Cloud9 IDE terminal window, open a new file window in the editor and name it blink_LED.py.
  2. Copy and paste the following code to the window:
    import Adafruit_BBIO.GPIO as GPIO
    import time
    
    GPIO.setup ("P8_15", GPIO.OUT)
    
    while True:
            GPIO.output("P8_15", GPIO.HIGH)
            time.sleep(1.0)
            GPIO.output("P8_15", GPIO.LOW)
            time.sleep(1.0)
    
  3. Now, save the script.
  4. Click Run in the Cloud9 IDE. Your LED should begin blinking.

There's more…

You can find more support in these tutorials:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.82.79