0

I am trying to read an analog sensor value using cylonjs framework.

I used this code for reading values.

var Cylon = require('cylon');

Cylon.robot({
  connections: {
    ras: { adaptor: 'raspi'}
  },

  devices: {
    sensor: { driver: 'analog-sensor', pin: 13, lowerLimit: 0, upperLimit: 1000 }
  },

  work: function(my) {
    var analogValue = 0;

    every((1).second(), function() {
      analogValue = my.sensor.analogRead();
      console.log('Analog value => ', analogValue);
    });

    my.sensor.on('lowerLimit', function(val) {
      console.log("Lower limit reached!");
      console.log('Analog value => ', val);
    });

    my.sensor.on('upperLimit', function(val) {
      console.log("Upper limit reached!");
      console.log('Analog value => ', val);
    });
  }
}).start();

Basically this is default arduino LDR program. When I try to execute this, I am getting this.

my_folder/node_modules/cylon-gpio/lib/analog-sensor.js:94
this.connection.analogRead(this.pin, function(err, readVal) { ^

TypeError: this.connection.analogRead is not a function

I know rpi is not directly supporting analog pins, How I can modify this javascript to read analog values.

I just started cylonjs, so any help or guide would be greatly appreciated.

Thanks in advance.

1

You have, unfortunately, hit the nail on the head. The Raspberry Pi does not have analog pins, therefore it is unwise to expect it to be able to read analog values. You would need an analog-to-digital converter (for example the MCP3008) and then you would need to change the cylonjs source to read values from a different bus (SPI or I2C - there are a2d chips for both). Alternatively, take a look at using Python and the GPIO Zero library instead.

  • Yes, I know that, what I want is to create a custom device driver for doing it. I can not use python in this case, because our project is running with Cylon – Sachith Apr 18 '17 at 14:43
  • 1
    Sounds to me like you need some kind of wrapper that holds within it the GPIO Zero (or native) stuff. Can't help, I'm afraid. Try looking to see how cylonjs does device drivers and see what you can add to support the SPI bus. – recantha Apr 18 '17 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.