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:
In the following image, you will see what a real-life LED actually looks like. Not so straightforward as looking at the symbol, right?
You'll need the following items to supplement your now happily perking BBB:
Now, let's begin with the following steps:
http://192.168.7.2:3000
and create a new file called blink_LED.js
.// 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);
// Setup var b = require('bonescript');
Please keep in mind that anything after the //
are comments, and isn't functional code.
var LED = "P8_15";
0
is equal to off and 1
is equal to on:var state = 0;
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');
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);
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); }
500
millisecond increments.You can find more documentation on the BoneScript library functions at http://beagleboard.org/support/bonescript.
Starting from step 3, here's the Python version of the recipe that uses the Adafruit library:
blink_LED.py
. 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)
You can find more support in these tutorials:
13.58.82.79