LED brightness with NodeJS

Hello!

I just got the GrovePi+ and am trying to make the led_fade program in NodeJS, as I am slightly more comfortable with it than with python. I however cannot find a way to change the LED’s brightness. I did find one thing in the documentation that might help, but I don’t have a clue how to utilize it.

  • write(value) - Write a value on the sensor/component

I think this could be used to set the brightness but I haven’t figured it out yet.

Thanks in advance and have a wonderful day!

Edit: After some research, I discovered what I should’ve already known. The write function takes one parameter: when it’s 0, the led turns off, and when it’s 1, it turns the led off. Any number values below 1 result in it turning itself off and any above 1 result in turning on. So when I put .6 it doesn’t change brightness it just turns off.

2 Likes

Disclaimer: I do not know anything about the GrovePi+ nor NodeJS …

Is there anything useful in the led_blink.js example?

I believe it will be located at /home/pi/Dexter/GrovePi/Software/NodeJS/tests/ on your system if you have installed the software correctly.

It is also on github:

1 Like

If you ramp the interval in that led_blink program or change the ratio of on to off time (modulate the pulse width) you may be able to change the apparent brightness of the led.

2 Likes

Is there a “color” value, or other tuple that can be written to the LED’s?

Are you writing to a single color LED or a multi-cooker “neopixel” LED.

The single-color LED’s (like the “blinkers” on the GoPiGo) may only support on/off.

Like @cyclicalobsessive, I also have zero experience with the Grove Pi.

You could look into the NodeJS libraries to see what’s implemented; or failing that, you could ping the good folks at support@modrobotics.com.

In any event, write back and let everyone know what you found out.

Thanks so much for the help!

I figured it out! I posted the solution below for anyone who might run into a similar problem

@cyclicalobsessive, playing with the ratio turnOn/turnOff does work, but it seems like a bit of a hacky way of doing it :smile: nothing wrong with that, ofc, I was just looking for a different solution.

@jimrh I am working with a single LED. Thanks for the e-mail address, I’ll definitely use that sometime in the future :smile:

now, this is the code that works for me:

const GrovePi = require('../libs').GrovePi;
const Board = GrovePi.board;
const RotaryAngleAnalogSensor = GrovePi.sensors.RotaryAnalog;
const AnalogSensor = require('../libs/').GrovePi.sensors.base.Analog;
const anSens = new AnalogSensor(5);
let board;

function start() {
 console.log('starting')

 board = new Board({
   debug: true,
   onError: function (err) {
     console.log('TEST ERROR')
     console.log(err)
   },
   onInit: function (res) {
     if (res) {

       console.log('GrovePi Version :: ' + board.version());

         // Analog Port 2 Rotary Angle Sensor
         const rotaryAngleSensor = new RotaryAngleAnalogSensor(2);
         
         console.log('Rotary Angle Sensor (start watch)');
         rotaryAngleSensor.start();
         rotaryAngleSensor.on('data', function (res) {
           console.log('Rotary onData value = ' + res);
           // potentiometer value * 2.55 (max value of led = 255)
           anSens.write(res*2.55)
         })
       }
   }
 })
 board.init()
}

// happens whe ctrl+c pressed
function onExit(err) {
 console.log('ending');
 board.close();
 process.removeAllListeners();
 process.exit();
 if (typeof err != 'undefined'){
   console.log(err);
 }
}

// starts the test
start()
// catches ctrl+c event
process.on('SIGINT', onExit)

Have a nice day!

2 Likes

Hacky? Perhaps true. Unfortunately if you want a digital event/signal to act like an analog one, this is the fastest and easiest way to do it, especially for simple things like LED’s.

Additionally, the brightness value and the apparent value are related by a complex ratio called “gamma”.

Translation: a brightness value of “50%” results in a visible brightness of over 90%.

To get a truly apparent 50% brightness, you set the brightness value to something closer to 80 instead of 255 or even 128.

You can use this trick to save huge amounts of battery power and increase your run-time.

1 Like

Congrats with solving in a most elegant way.

2 Likes

Glad it worked for you. If I’m reading that correctly you’re reading a sensor and then scaling the value to analog sensor out. I wonder if behind the scenes if that isn’t actually doing a pulse width modulation in the background (vs. an actual analog current reduction)?
/K

1 Like

This is precisely how that works.

In some fancier cases, they put a resistor and filter capacitor.

1 Like