Raspberry Pi, Node.js and a LED – Emit Morse Code

This post is part 2 of 9 in the series Raspberry Pi
Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

Raspberry Pi, Node.js and a LED – Emit Morse Code

In this post, we will get our hands a bit dirty with Raspberry Pi, Node.js and a LED. We will be writing a simple program in Node.js to turn a LED on and off. Then we will extend this logic to build a simple piece of embedded system that can take a piece of string and emit its morse code.

Interesting right? Below is a quick video as what we will be building

The above LED is emitting the Morse code for SOS. That is (… ___ …) [Dot Dot Dot, Dash Dash Dash, Dot Dot Dot]. Nice right?

You can get the completed code here.

So, let us get started.

Prerequsites

If you are new to Raspberry pi and have not yet installed Node.js on it, I would recommend going through Getting Started with Raspberry pi and Node.js.

If you are new to electronics devices and circuits, I would recommend going through the video lectures from All About Circuits.

Components needed

  1. 1 – Raspberry pi B+
  2. 1 – Breadboard
  3. 1 – 68 Ohms resistor
  4. 1 – LED
  5. 2 – Female to male wires

What is GPIO?

GPIO stands for General Purpose Input Output pins.

These pins are a physical interface between the Pi and the outside world. At the simplest level, you can think of them as switches that you can turn on or off (input) or that the Pi can turn on or off (output).

Inputs don’t have to come from a physical switch; it could be input from a sensor or a signal from another computer or device, for example. The output can also do anything, from turning on an LED to sending a signal or data to another device. If the Raspberry Pi is on a network, you can control devices that are attached to it from anywhere** and those devices can send data back.

You can read more about Raspberry Pi GPIO here.

Raspberry pi B+ has 26 GPIO pins.

GPIO

The remaining ones are either power or ground pins.

Below is the superimposed image of the pin layout on a pi

pi

Turn On Turn Off

Now that we have a basic understanding of GPIO, we will leverage one of pi’s GPIO pins to turn a LED on and off.

There are 2 parts to getting this task done. Part 1 is building a circuit that has a LED connected to the pi. Part 2 is the Javascript code that triggers the state of the LED.

So, let us focus on Part I. We will build the circuit. For this we will assemble the following schematic on a breadboard. 

Screen Shot 2014-10-03 at 5.33.02 pmThe left end of the above circuit will be connected to Pin 9 – GND pin of pi (Left Column, 5th pin). And the right end of the above circuit will be connected to Pin 11(Left Column, 6th pin).

This completes our circuit. Now to the application logic. Connect the pi to your computer. And ssh from terminal/putty into pi. We will be using command line to tools to write the code.

So, as soon as you ssh into pi, you will be landing inside the /home/pi folder. We will create a new folder here named node_programs. And inside this folder, we will be maintaining all our programs. Run

mkdir node_programs

To step inside that folder, run

cd node_programs

For this post, we will create a new folder named ledBlink and will step inside this folder. Run

mkdir ledBlink && cd ledBlink

Note : You can run multiple commands separated by a &&.

First we will initialize a new node project here. Run

npm init

Fill it up as applicable.

Now, we will use a node module named onoff to interact with the GPIO pins from inside our Node.js code. This is the simplest way to interact with the pi from our node program. Run

npm install onoff --save

Next, we will create a new file named index.js inside the ledBlink folder. To create a new file, you can follow any of the 2 ways. First

touch index.js

This will create an empty file named index.js. Second

nano index.js

This will create a new file named index.js and will open the file for editing in the built in nano editor. Even though you are following the first approach, to edit the file, you need to run step 2.

Once the file is opened in the nano editor, paste the below.

Things to notice

Line 1 : We include the GPIO module of onoff to interact with pi’s GPIO pins

Line 2 : We tell our program that we are going to use a GPIO pin and it is of the type output. That is pi provides the input to that pin based on the logic we write. We will name the output of this pin as led, as logically we have a led placed here.

Line 4 : We start a new setInterval()  to run the code inside it every 500 milliseconds.

Line 5 : For every Gpio pin, we have a method named  writeSync(). This method takes in a 1 or a 0. If it is 1, it will command the pi to supply 3.3v to this pin and if it is 0, it will command the pi to stop the supply. This way, we control the pin.

And  led.readSync() tells us if this pin is in state 1 or 0. depending on that, we will toggle the values. That is if the value is 0, we set the value to 1 and if 1, we set the value to 0. Thus making the LED “blink” every 500ms. Simple right?

Line 13 : After 5 seconds, we terminate the   setInterval() and clear the state of the pin and release all the resources.

Let us save the file and run the program. To save the program, press (ctrl+x or cmd+x). This will ask you to save the file. Press Y and press enter key to complete the operation.

To run the program, execute

sudo node index.js

And you should see the LED blinking for 5 seconds before the program exits. Simple right. Now you can see the power of pi. How it can interact with the outside world and control stuff.

Morse Code

Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment.

If you are new to Morse code, checkout learnmorsecode.com.

morse_code

This is how you read the above diagram

Place your pencil where it says START and listen to morse code.
Move down and to the right every time you hear a DIT (a dot).
Move down and to the left every time you hear a DAH (a dash).

So this will create a unique code for each of the alphabets, numbers and a few special characters. Like

morse_code_final

A simple and efficient language!

Now, we will write a program that will emit the Morse Code for a given string. To keep the blinking at a constant rate and in our control, we will use a node module named sleep. Run

npm install sleep --save

Now, we will create a new file named morseCode.js, inside the ledBlink folder. Run

nano morseCode.js

Now, paste the below code to morseCode.js

Things to notice

Line 1,3 : require node modules

Line 2 : Declare the pin to be a out.

Line 4-8 : A base time, based on which the speed of transmission/blinking can be controlled.

sleepTime – Time before which we reset the state of the LED to 0

btwCodes – Time between 2 dits or dahs or a dit and a dah for a given letter.

btwLetters – Time between each letter

btwWords – Time between each word

This way, we can control the speed of transmission/LED blinking.

 Line 11 : We read the text from command line

Line 14-50 : We build an object with the mapping of character and its morse code.

Line 53 : A helper function to make the the LED active

Line 56 : A helper function to make the the LED inactive

Line 60/61 : We will split the input string and iterate over it

Line 65/66 : If the current character is a space, we will sleep for “ btwWords

Line 69 : We split the Morse code for a character and check if it is a dit or a dah.

Line 76 : If the code is a dit, we sleep for “ sleepTime”. This is time that the LED stays lit.

Line 81 : If the code is a dah, we sleep for “ sleepTime * 3”. This is time that the LED stays lit.

Line 77/82 : We reset the state of the LED

Line 78/83 : We wait for “ btwCodes” before we start processing the next code.

Simple and easy right! Save the file by pressing ctrl + x and then Y and then enter key.

To run the program execute

sudo node morseCode.js "Hello World"

And you should see the below log in the console.

And the LED will blink according to this.

Below is a recording of the program with “Morse Code” as input

Hope this post gave you a decent idea on GPIO and how you can control them.


Thanks for reading! Do comment.
@arvindr21

Series Navigation<< Getting Started with Raspberry pi and Node.jsRaspberry pi, a Buzzer and Node.js – An IoT Doorbell >>
Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • vishal uppin

    After giving npm init, it is showing license is asking, what i have to do. I’m new to raspberry pi,
    please guide me.
    Thank you.

  • Aaron

    Can some kind soul please tell me why the led is coded as pin 17?

  • TJ

    Sleep is an anti-pattern in Node. :/

    I’ve refactored the code to use promises instead.
    * http://pastebin.com/QjbFaEk0

    If timing is VERY important, then using a separate thread with sleep would be necessary. Look into Cluster for more information.
    * https://nodejs.org/api/cluster.html

  • Martin McNeela

    This is awesome, just got my Pi GPIO up and running and all I had to write was Javascript :-) What to build now…???…

  • Tmhrtwg

    Thank you for this blog post! I just made my first Raspberry Pi based ‘web to hardware’ connection 😀

  • Jason

    Hi,Arvind Ravulavaru . first thx for share this. i want to know is this tutorial can use on Raspberry-Pi A+ ? thx

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks Jason. Technically yes. All you need is a couple of GPIO pins. I have not tried it on A+ though. Do let me know if you try it. Thanks.

  • dennyhiu

    So basically you can control anything remotely just using a web browser + Node Server + Raspberry Pi. Never thought it’s possible, but it is, and he did! Thank you for your article.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks dennyhiu!