arduino light sensors & pulse width modulation

Zach Cusimano
loriscode
Published in
2 min readMar 8, 2017

--

How to create a LED lighting effect that changes based on external lighting conditions.

Given an arduino cannot vary voltage to change an LEDs output, you can utilize Pulse Width Modulation to create a smooth fading effect.

To create the this I’m going to utilize the 6 digital pins set aside for PWM(3,5,6,9,10,11), a tri-color LED, 3 color gels and 3 photoresistors.

The Circuit Setup

Base Setup

Adding in the 3 color gels. Each filter only lets light of a specific wave length through to the sensor. Allowing you to detect the relative color levels in the light that hit the sensors.

Adding Color Gels

The Program

First set constants for each pin and store sensor readings to variables.

Within the loop, read the value of each light sensor and send back the readings.

analogWrite() is what changes the LED’s brightness via PMW. It takes two arguments; the pin and the value between 0–255. Sensor readings are between 0–1023, so to get this number we divide by 4.

  • 255 sets the value to HIGH
  • 0 sets to LOW

Result

Turning off the lights and using an external light source (a flashlight) to show the LED’s smooth transition.

--

--