Controlling LEDs

In this first project of the chapter, we are going to see how to control LEDs using your Raspberry Pi Zero. As an example, here we'll see how to control and dim a single LED that we will place on a breadboard. However, the same code can be applied to any kind of LED lighting in your home, or to LED strips.

Let's first see how to assemble this project. Place the LED on the breadboard in series with the 330 Ohm resistor—the longest pin of the LED in contact with the resistor. Then, connect the other side of the resistor to the GPIO18 pin on the Raspberry Pi and the other end of the LED to a GND pin of the Raspberry Pi.

You can, of course, use a cobbler cable kit to easily connect the Pi to the LED. Here, and for the rest of this chapter, I just used two simple jumper wires so you can really see the connections in the images.

This is the final result:

Controlling LEDs

Now that the project is assembled, we are going to test it. To do so, we'll run a simple code that will basically continuously change the intensity of the LED, from 0 to the maximum brightness.

This is the complete code to test our project:

// Modules
var Gpio = require('pigpio').Gpio;
// Create led instance
var led = new Gpio(18, {mode: Gpio.OUTPUT});
var dutyCycle = 0;

// Go from 0 to maximum brightness
setInterval(function () {
  led.pwmWrite(dutyCycle);

  dutyCycle += 5;
  if (dutyCycle > 255) {
    dutyCycle = 0;
  }
}, 20);

We can already test this code. Make sure to grab the code from this book's GitHub repository, navigate into the folder of this project with a terminal on the Pi, and type the following:

sudo npm install pigpio

This will install the required Node.js module to control the LED. Then, type the following:

sudo node led_test.js

You should immediately see the LED gradually going from completely off to full brightness, meaning we can indeed dim an LED using our Raspberry Pi Zero board!

This is great, but we can do better. We are now going to see how to dim the LED using a graphical interface, in which you'll be able to control the intensity of the LED using a slider.

As for the thermostat project, we are going to use Node.js again here, along with an HTML interface and some JavaScript to link the interface to the Node.js server.

Let's first have a look at the Node.js code. We'll again use the Express module to structure our app, along with the pigpio module we used earlier to dim the LED:

// Modules
var Gpio = require('pigpio').Gpio;
var express = require('express');

// Express app
var app = express();

// Use public directory
app.use(express.static('public'));

Then, we define an object that will allow us to control the state of the LED:

var led = new Gpio(18, {mode: Gpio.OUTPUT});

We can then define the routes of our app. The first one is to serve the interface when we access it via a web browser:

app.get('/', function (req, res) {

  res.sendfile(__dirname + '/public/interface.html');

});

Then, we also create a route to set the intensity of the LED:

app.get('/set', function (req, res) {

  // Set LED
  dutyCycle = req.query.dutyCycle;
  led.pwmWrite(dutyCycle);

  // Answer
  answer = {
    dutyCycle: dutyCycle
  };
  res.json(answer);

});

Finally, we start the Node.js server with the following:

app.listen(3000, function () {
  console.log('Raspberry Pi Zero LED control started!');
});

Let's now have a look at the HTML interface. It starts by including modules such as jQuery and Bootstrap:

<head>
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="js/interface.js"></script>
  <link rel="stylesheet" href="css/style.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

Then, in the body of the interface, we simply define a slider element, which we will use to control the LED:

<body>

<div id="container">

  <h3>LED Control</h3>

  <div class='row'>

    <div class='col-md-4'></div>
    <div class='col-md-4 text-center'>
     <input id="duty-cycle" type="range" value="0" min="0" max="255" step="1">
    </div>
    <div class='col-md-4'></div>

  </div>

</div>

</body>

Finally, in the file called script.js, we link the slider element to the Node.js server so it automatically sets the intensity of the LED whenever we use the slider:

$( "#duty-cycle" ).mouseup(function() {

    // Get value
    var dutyCycle = $('#duty-cycle').val();

    // Set new value
    $.get('/set?dutyCycle=' + dutyCycle);

  });

It's now finally time to test our application! First, grab all the code from this book's GitHub repository and navigate to the folder of the project like before. Then, install Express with the following command:

sudo npm install express

When this is done, start the server with the following command:

sudo node led_control.js

You can now test the project by entering the following command in your browser (replacing the IP address with the one for your Pi):

http://192.168.0.103:3000/set?dutyCycle=20

You should immediately see the LED dimmed and also get the confirmation in your browser:


Controlling LEDs

Then, access the interface directly by typing the following URL:

http://192.168.0.103:3000

You should immediately see a very basic interface with a slider:

Controlling LEDs

You can now try it: as soon as you release the mouse from the slider, you should see that the LED is instantly dimmed to the value you set with the slider! You can now connect this project to any LED-based lighting in your home (that uses DC current), and start controlling it from a nice interface using your Raspberry Pi Zero!

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

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