Many Ways to Blink 2: Light Sensor to LED

This tutorial shows you how to switch out the potentiometer for a light sensor. We will go through how to change the electronics and code to make this new system give you the same behavior as the Potentiometer to LED. The trick about this code is that the Light Sensor does not have the same input range as the potentiometer. As you saw before with the potentiometer, sensorValue can range from 0 to 1023. The light sensor resistance does not change as much as the potentiometer and so the range of . We will show you how to find this new range and update the code so it will have the same behavior as before

#Block Diagram Light Sensor to LED System


Code Steps

int maxValue = 1023;
int minValue = 0;
delayTime = scaling*sensorValue;


delayTime = map (sensorValue, minValue, maxValue, 0, 1023);

Full Code

  Adapted From: Analog Input by David Cuartielles and Tom Igoe
  Author: Malcolm Knapp
  Project: Light Sensor to LED
  Date: 4/10/14
  Version: 0.1
  Description: This code shows how to use a light sensor to control
               the blink rate of a LED. 

// ---------- included libraries ------------ 
// None - include new libraries here 

// ---------- hardware pin defines  ----------- 
int sensorPin = A0;    // select the input pin for the light sensor
int ledPin = 13;      // select the pin for the LED

// ---------- variable initialization  ----------- 
int sensorValue = 0;  // variable to store the value coming from the sensor
int delayTime = 0; //variable that holds the delay time in milliseconds
int scaling = 1;
int maxValue = 300;
int minValue = 750;

// ---------- library initialization  ----------- 
// None -  initialize new libraries here 

void setup() {
  // ---------- declare hardware connections ----------
  pinMode(ledPin, OUTPUT);  

void loop() {
  // Input
  sensorValue = analogRead(sensorPin); 
  // Debugging
  Serial.print("Sensor value: ");  Serial.println(sensorValue);

  // Processing
  delayTime = map (sensorValue, minValue, maxValue, 0, 1023);
  Serial.print ("Delay in milliseconds: "); Serial.println (delayTime);

  // Output   
  digitalWrite(ledPin, HIGH); // turn the ledPin on
  digitalWrite(ledPin, LOW);   // turn the ledPin off: