Now that we know our light sensor works, we can add an LED and create our night-light.
Connect the short leg of your LED to a ground rail using a 330K ohm resistor, and wire the long leg to GPIO #5, also known as P1-29:
Create a file in the same folder as the other files from this chapter, and copy the contents of print-light-sensor-readings.js
into it.
In the start of the board.on('ready')
handler, add a constructor for our LED:
let light = new five.Led('P1-29')
And in the lightSensor.on('change')
function, replace the console.log
statement with the logic that will turn the LED on and off:
if(this.value <= 25) { light.on() } else { light.off() }
And we're ready to run! Load the folder onto your Pi, navigate to the folder in your Pi's SSH session, and run:
sudo node night-light.js
When you cover the light sensor with your thumb, the LED should light up, as shown in the following image: