Skip to main content

Using Cylon.js with the Intel Edison

Using Cylon.js with the Intel Edison


Using the physical computing framework to read a light sensor and control a servo.

In this tutorial we will take a look at using the Cylon JavaScript framework with the GPIO pins on the Intel Edison. It is assumed that you have your Edison board flashed with the latest Yocto Linux image (1.6 at the time of writing), that you are using the Arduino breakout board and that your Edison is configured on your network for login over SSH.

First we will need to install the MRAA library (as we did in a previous blog post):

$ echo “src mraa-upm" > /etc/opkg/mraa-upm.conf

$ opkg update

$ opkg install libmraa0

Once the MRAA library is installed we can install cylon-intel-iot, the Cylon adaptor for the Intel Edison and Galileo platforms.

$ npm install cylon-intel-iot

Now to test the GPIO functionality is working. We will blink the onboard LED, the same test we did in a previous blog post. This time however we will make use of the Cylon framework rather than the MRAA library directly, so the code is a little different:

  • Create a file called blinkTest.js

  • Copy the sample code from the Cylon tutorial page (under the heading 'Blinking the built-in LED') into your blinkTest.js file

  • Save the file

  • Execute the file on the Edison:

  • $ node blinkTest.js

  • You should see confirmation in the console window and the onboard LED blinking

  • Press ctrl-c to stop

This demonstrates how Cylon allows us to control the GPIO pins, similar to directly using the MRAA library, but with the benefit of added portability. By changing the connections object we can use the rest of our code on other platforms, such as the Arduino, BeagleBone, Raspberry Pi and many more.

Cylon also has many devices objects for common components such as analogue sensors, motors and buttons; meaning you can be up and running quickly with supported hardware.

Reading analogue sensor data


Next let's take a look at getting analog sensor data into Cylon. We will use a light dependent resistor (LDR) for this example – though you may choose to use a potentiometer, analog temperature sensor or a different analog input.

There are useful code examples on the Cylon site to get you started. Looking at the analogue sensor example code one can see that it is written to run on an Arduino board (checking the connections object). We will need to modify this to work with the Edison:

  • Copy the code into a new file (e.g. LDRTest.js) and replace the line:

  • arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }

  • With:

  • edison: {adaptor: 'intel-iot' }

  • Save the file.

Now we need to wire up the LDR to the Edison Arduino board as shown in the photo above. Note that this LDR needs a 10k resistor in the circuit to function correctly.

Execute the file:

$ node LDRTest.js


If the code and wiring is correct you will see analog data values echoed to the console as in the picture above.

Controlling a servo

Once we have got data coming into Cylon we will want to do something with it. We could use the output capability of the Arduino breakout board and control the onboard LED, or try controlling something else, such as a servo for example.

As with building any system it is prudent to test each component part seperately before putting everything together, so we will test servo control before linking it to the LDR data.

As with the analog sensor there is example code detailing how to control a servo. Again the code is written for an Arduino board so a small change must be made to make it run properly on the Edison:

  • Copy the code in the 'How To Use' example into a file, e.g. servoTest.js

  • Change the following line

  • arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }

  • With:

  • edison: {adaptor: 'intel-iot' }

  • Save the file

Now we need to add the servo to the original circuit. We will use a breadboard to make it easier to put together.


Once this is wired up we can run the servo example code:

$ node servoTest.js

If all is well the servo will loop through three different positions.

Note that we are using this servo and powering it solely from the board's 5V supply. With little to no servo load and powered from both USB connectors, this did not give us any trouble. In some cases you may need to use a dedicated power supply.

Putting it all together

Now we have tested the component parts of our project we can put everything together. In this example we want to control the position of a servo based on a light sensor reading.

Our program will:

  • Add LDR

  • Add Servo

  • Define LDR input value range + variable

  • Define servo position value range + variable

  • Read LDR value

  • Convert LDR values to valid servo position values

  • Set servo position

  • Wait

  • Repeat

Below is code for our example project. Create a JavaScript file (e.g. LDRServoControl.js) and paste in the code. Save the file and run it on your Edison with the hardware wired as above.

var Cylon = require('cylon');
connections: {
edison: { adaptor: 'intel-iot'}
//declare and configure the LDR and servo - pins and ranges
devices: {
sensor: { driver: 'analogSensor', pin: 0, lowerLimit: 100, upperLimit: 1000 },
servo: { driver: 'servo', pin: 3 }
work: function(my) {
var analogValue = 0; //variable for LDR input
var servoValue = 0; //variable for servo position
my.servo.angle(servoValue); //servo position
every((1).second(), function() { //determine how often to run
analogValue = my.sensor.analogRead(); //read LDR value
console.log('Analog value => ', analogValue); //write LDR value to terminal
servoValue = ((-analogValue + 1023) * (180/1023)); //map LDR values 0-1023 to servo-friendly 0$
servoValue = Math.round(servoValue); //round servo value to nearest integer
console.log('Servo Value ===> ', servoValue); //write servo value to terminal
my.servo.angle(servoValue); //move servo to new position

To finish off our project we laser cut a simple dial with servo mount and indicator arrow. The laser cutter design files for these are available here.

maker, hacker, doer
DesignSpark Electrical Logolinkedin