-
Notifications
You must be signed in to change notification settings - Fork 3
Pi Zero LED demo
This how-to isn't really part of the Hydra Cluster but was included in a related presentation entitled Node in small places
This how-to discusses how to use a Raspberry Pi to control an LED light. If you haven't already setup your Pi Zero, see Setting up a Pi Zero for step by step instructions.
The wiring is as basic as it gets. The ground has a 220 Ohm resistor leading to the LED anode terminal, and the power from pin 4 is connected to the cathode terminal.
This demo uses a node package called pi-blaster to assist in controlling an LED light state and brightness. The pi-blaster repo offers several ways to install pi-blaster, but I opted to pull the code down and build it manually, largely because at the time of this writing there was a problem using a straight NPM install.
SSH into your Raspberry Pi:
$ ssh pi@raspberrypi.local
Then...
$ sudo apt-get install git
$ sudo apt-get install autoconf
$ git clone https://github.com/sarfata/pi-blaster.git
$ cd pi-blaster
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install
Next create an led-demo project folder and install the pi-blaster.js
package, so we can control the daemon via Node.js
$ mkdir led-demo
$ cd led-demo
$ npm init
$ npm install pi-blaster.js --save
The following sine.js
program using a sine wave to change the brightness of the LED so that it pulsates based on the iterated value of points on the wave.
const piblaster = require('pi-blaster.js');
const LED_PIN = 4;
let y = 0;
let intervalID = setInterval(() => {
let s = Math.abs(Math.sin(y/10));
piblaster.setPwm(LED_PIN, s);
y += 1;
if (y > 180) {
y = 0;
}
}, 100);
process.on('SIGINT', () => {
clearInterval(intervalID);
piblaster.setPwm(LED_PIN, 0);
setInterval(() => {
process.exit();
}, 1000);
});
The following sos.js
program repeats an SOS morse code destress signal:
const piblaster = require('pi-blaster.js');
const LED_PIN = 4;
const LED_ON = 1;
const LED_OFF = 0;
const SOS_PATTERN = [
LED_ON, LED_ON, LED_ON,
LED_OFF,
LED_ON, LED_OFF, LED_ON, LED_OFF, LED_ON,
LED_OFF, LED_OFF,
LED_ON, LED_ON, LED_ON,
LED_OFF, LED_OFF, LED_OFF, LED_OFF, LED_OFF, LED_OFF];
let i = 0;
let intervalID = setInterval(() => {
piblaster.setPwm(LED_PIN, SOS_PATTERN[i]);
if (i > SOS_PATTERN.length - 1) {
i = 0;
} else {
i += 1;
}
}, 500);
process.on('SIGINT', () => {
clearInterval(intervalID);
piblaster.setPwm(LED_PIN, 0);
setInterval(() => {
process.exit();
}, 1000);
});