Light-emitting diode

A Light-Emitting Diode (LED) is a particular type of diode that converts electrical energy into light energy. An LED emits visible light when an electric current passes through it. LEDs are very efficient in operation, consume less power, and have a very long life. They are generally used in applications such as indicator lights, LCDs, remote controls, and many such electronic devices.

The following figure shows an LED and an internal circuit as well:

Since an LED is a diode, it allows current to flow only in one direction and, if the opposite polarity is applied it won't break—it just won't work. The positive side is called the anode (+) and the negative side is called the cathode (-). The current always flows from positive to negative. In an LED, the anode side lead/leg is always longer and is a good way to identify the anode and cathode.

We have covered all the components required to complete the project in this chapter; now let's integrate all the components together. Here, we will interface the LED and L293D to the Raspberry Pi directly and then interface the motor to the L293D.

Please follow the circuit diagram shown next:

Connections shown in the figure are explained as follows:

  • The Red LED’s positive terminal (+) is connected to GPIO 17 (pin 11) and the negative terminal (-) is connected to Ground (pin 9) with a 4.7 K Ohm resistor in series
  • The Green LED’s positive terminal (+) is connected to GPIO 05 (pin 29) and the negative terminal (-) is connected to Ground (pin 25) with a 4.7 K Ohm resistor in series
  • L293D’s Enable 1 (pin 1) is connected to GPIO 23 (pin 16)
  • L293D’s Input 1 (pin 1) is connected to GPIO 24 (pin 18)
  • L293D’s Input 2 (pin 7) is connected to GPIO 25 (pin 22)
  • L293D’s Ground (pins 4 and 5) are connected to the Raspberry Pi GND (pin 20)
  • L293D’s Vcc 1 (pin 16) is connected to +5V (pin 04)
  • L293D’s Vcc 2 (pin 8) is connected to the positive (+) terminal of the 9V battery
  • The negative terminal of the 9V battery is connected to the Ground of the Raspberry Pi's pin 30
  • L293D’s Output 1 (pin 3) is connected to one terminal of the motor
  • L293D’s Output 2 (pin 6) is connected to another terminal of the motor

After completing the connections, we will write our code to complete the task. Basically, our code will allow us to start, stop, and rotate the motor in both directions (clockwise and anti-clockwise) remotely over the internet. When the motor rotates in a clockwise direction, the green LED will glow; when the motor rotates in an anti-clockwise direction the red LED will glow.

Let's write the code now. Here, we use the MQTT protocol to communicate with the Raspberry Pi from the server, which we have already learnt about in Chapter 3, Let's Communicate. We will not go into the details here again and directly implement it.

Take a server, which can be any Windows or Linux instance provided by AWS or Azure, and note its public IP address. The server can be your own laptop/desktop as well but it should be accessible over internet via its public IP address. Install the MQTT broker on this server and make sure it is up and running:

  1. Log into the Raspberry Pi, create a directory, and install Node.js in it.
  2. Now create a file with the name Motor_LED.js and include the onoff module, which will give you access to GPIO:
var GPIO= require('onoff').Gpio;
  1. As per the circuit diagram in the previous figure, we need to set GPIO 175, 23, 24, and 25 as the output pin:
var red_LED= new GPIO(17,'out'),
green_LED= new GPIO(5,'out'),
enable= new GPIO(23,'out'),
input_1= new GPIO(24,'out'),
input_2= new GPIO(25,'out');
  1. While explaining L293D, we discussed the logic for clockwise and anti-clockwise rotation; now we write a function for it:
module.exports.rotate_clockwise = function(callback){
enable.writeSync(1);
input_1.writeSync(1);
input_2.writeSync(0);
green_LED.writeSync(1);
red_LED.writeSync(0);
console.log ("Clockwise Rotation")
callback("Clockwise Rotation")
}

module.exports.rotate_anti_clockwise = function(callback){
enable.writeSync(1);
input_1.writeSync(0);
input_2.writeSync(1);
green_LED.writeSync(0);
red_LED.writeSync(1);

console.log ("Anti Clockwise Rotation")
callback ("Anti Clockwise Rotation")
}

module.exports.motor_stop = function(callback){
enable.writeSync(0);
input_1.writeSync(0);
input_2.writeSync(0);
green_LED.writeSync(0);
red_LED.writeSync(0);
console.log ("Motor Stopped")
callback ("Motor Stopped")
}
  1. Now we will write a module for the Raspberry Pi that will connect to the MQTT broker to receive the command from the server in order to run the motor in either direction.
  2. Create a new file in the same directory with the name controlPi.js and add the following code line. Here we include the mqtt module for connecting to MQTT server. Here we include the module Motor_LED.js, in which we have written motor rotation logic functions:
var mqtt = require('mqtt')
var motor_Dir= require('./Motor_LED');
  1. options will have the IP address and port number to connect to the server, where we have the MQTT broker running:
var options = {
port:'1883',
host: “IP Address of Mqtt Broker server”
}
  1. Now we listen on the connect event, which gets fired as soon as the connection to the MQTT broker is established. Once connected, we subscribe to the controlPi/cmd topic as we send the control signal from mqtt client to this topic. We receive all the control commands for the LED and motor operation on the topic controlPi/cmd:

var client = mqtt.connect(options)
client.on('connect', () => {
client.subscribe('controlPi/cmd');
console.log (" Raspberry Pi mqtt client connected to broker ");
})

Now we listen to the event message which occurs whenever a message/data is received on a subscribed topic. Once the message is received, we call the appropriate function for rotating the motor clockwise and anti-clockwise and send the acknowledgment to the client using the publish method of mqtt:

  1. The acknowledgment is sent from the Raspberry Pi on the topic raspbPi/ack, which is subscribed by the client:
client.on ('message', (topic, message) => { 
message= message.toString();
if(message=='clockwise') {
motor_Dir.rotate_clockwise((callback) => {
var Acknowledgement=callback;
client.publish ('raspbPi/ack', Acknowledgement);
});
}
else if(message=='anticlockwise') { motor_Dir.rotate_anti_clockwise((callback) => {
var Acknowledgement=callback; client.publish('raspbPi/ack',Acknowledgement)
});
}
else {
motor_Dir.motor_stop((callbacka)=> {
var Acknowledgement=callback; client.publish('raspbPi/ack',Acknowledgement)
})
}
})
  1. Now let's run the code. Open Terminal and execute the following command:
sudo node controlPi.js
  1. On successful execution, the Raspberry Pi will connect to the MQTT broker and a message will come up on the console, as shown in the following screenshot:

Now the Raspberry Pi is connected to the MQTT broker and subscribed the topic controlPi/cmd.

Here, we will use an open source mqtt client on our laptop/desktop to send the control commands for operating the motor interfaced with the Raspberry Pi in a clockwise and anti-clockwise direction:

  1. For this purpose, we download the mqtt.fx (http://mqttfx.jensd.de/) client tool and make a connection to the MQTT broker running on the server, as shown in the following screenshot.
  1. Provide the MQTT broker details and connect. The Edit Connection Profiles window will pop up after you click on the Settings button, adjacent to the Connect button:
  1. Once successfully connected to the broker, a green signal appears in the top-right corner of the mqtt.fx window, as shown here:
  1. We need to subscribe to the topic raspbPi/ack from mqtt.fx, on which a acknowledgement from the Pi will be received, as shown next:
  1. Now both the Raspberry Pi and the mqtt.fx client are connected to the MQTT broker which is running on the cloud/server, so let's play the game.
  2. Publish a message  clockwise on the topic controlPi/cmd from the mqtt.fx client, as shown here:
  1. Since mqtt client on the Raspberry Pi has already subscribed to the same topic, it receives the message and, as per the logic written in the code, it will start rotating the motor in a clockwise direction and switch on the green LED. Also notice the output on the console, as shown here:
  1. The Raspberry Pi also sends an acknowledgment to the mqtt.fx client as a confirmation that the motor has started moving in a clockwise direction, as shown here:
  1. Similarly, we send a command to rotate the motor in an anticlockwise direction, as shown next:
  1. The motor will start moving in an anticlockwise direction and a red LED will glow. An acknowledgement of this will also be received, as shown next:
  1. To stop the motor, we will send a stop message, as shown next:
  1. The motor stops and both LEDs will be switched off. An acknowledgment of this is also received, as shown here:
  1. Let's look at the architecture of our solution, which will further clarify what we have done in this chapter; it is shown in the following figure:
  1. Now we will look at a diagram showing the execution of our code, which will help us grasp the whole logic; it is shown in the following figure:
..................Content has been hidden....................

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