Now that everything is connected, type the following program in Cloud9, save it as blinkExternalLED.js
and run. You should be able to see the LED blinking each second:
var b = require('bonescript'), var led = "P8_10"; var state = b.HIGH; var loopTime = 1000; var exitTime = 30000; b.pinMode(led, b.OUTPUT); b.digitalWrite(led, state); var loopTimer = setInterval(blink, loopTime); var exitTimer = setTimeout(exitProgram,exitTime); function blink() { if(state == b.LOW) state = b.HIGH; else state = b.LOW; b.digitalWrite(led, state); } function exitProgram() { b.digitalWrite(led,b.LOW); clearInterval(loopTimer); }
This is the exact same program that we did in the previous chapter except we put LED as string P8_10
here. We declared P8_10
pin direction as output using function pinMode()
. When our program does digitalWrite()
HIGH
on P8_10
pin, the processor puts 3.3V on that pin. The current starts flowing from P8_10
to P8_2
and the LED glows. When the program writes LOW
, the processor puts 0V
on P8_10
. As both ends have no voltage, the current flow stops and our LED goes off. When we do this in a repeating time interval, the external LED blinks. You can replace the LED in the above circuit with a buzzer to get sound notification. You can use a relay switch to ON
/OFF
AC devices in the home by connecting the relay to the GPIO
pin.
P8_2
and P8_10
.GPIO
.13.59.197.213