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.

asked Apr 18, 2017 at 12:22

1 Answer 1

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.

answered Apr 18, 2017 at 13:09
2
  • 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 Commented Apr 18, 2017 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. Commented Apr 18, 2017 at 15:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.