Controlling a Photocell with Arduino

Gloria Julien
4 min readJun 10, 2019

Learning how to code an Arduino board

This documentation is part of my studies in the course, SDEG-600–1.

Part 1: Smart Lighting Code

https://openhomeautomation.net/smart-lighting-arduino-photocell/

Required materials

Hardware Configuration

You first have to plug in the LED. Just connect a wire from the Arduino pin number 2 to the 470 ohms resistor, then to the anode of the LED (the long part), then another wire from the cathode to the ground.

I used 2ea of 220Ω resistors instead of the suggested 470Ω resistor for this circuit. Then, put the two 220Ω resistors in series to simulate a 440Ω resistance across the circuit.

Final Schematic

Test Photocell Code:

// Define analog pin
int sensorPin = 0;
// Setup
void setup() {
// Init serial
Serial.begin(9600);
}
// Main loop
void loop() {
// Get temperature
int sensorValue = analogRead(sensorPin);
// Put temperature on the serial port
Serial.println(sensorValue,DEC);
// Wait for 1 sec
delay(1000);
}
Part 1, Figure 1 — Circuit Built
Code1 test outputs

Low lighting is around 300 and less.
High lighting is around 600 and above.

Turn LED On/Off Code:

// Pins
int sensorPin = 0;
int lightPin = 2;
// Variables
int lightState = 0;
int lowThreshold = 300;
int highTreshold = 600;
void setup() {

// Start Serial & set pin to output
Serial.begin(9600);
pinMode(lightPin,OUTPUT);

}
void loop() {// Read the sensor pin
int sensorValue = analogRead(sensorPin);
// If light level is low is detected, switch light on
if (sensorValue < lowThreshold){
digitalWrite(lightPin, HIGH);
}

// If light level goes up again, switch the lights off
if (sensorValue > highTreshold){
digitalWrite(lightPin, LOW);
}
}
Part 1, Figure 2 — Test Video of Turning On/Off LED

Part 2: Brighter Lighting when Display is Bright

Helper code from web: https://forum.arduino.cc/index.php?topic=568260.0

Code Test 1: (using analogWrite( ) )

// Pins
int sensorPin = 0;
int lightPin = 3;
// Variables
int lightState = 0;
int lowThreshold = 300;
int highThreshold = 600;
void setup() {

// Start Serial & set pin to output
Serial.begin(9600);
pinMode(lightPin,OUTPUT);

}
void loop() {// Read the sensor pin
int sensorValue = analogRead(sensorPin);
// If high light is detected, crank up the light slowly
if (sensorValue > highThreshold){
analogWrite(lightPin, 255);
}

// If light level gets dark, crank the light down
if (sensorValue < lowThreshold){
analogWrite(lightPin, 100);
}
}
Part 2, Figure 1 — Using analogWrite( )

Code Test 2: Creating step in brightness

  • Write the current sensorValue to start the LED at before continuing with step increase
  • Set the light value (either moreBright or lessBright) to change when a certain sensorValue is detected
  • Create a delay between loops to view the dimming or brightening effects
// Pins
int sensorPin = 0;
int lightPin = 3;
// Variables
int lightState = 10;
int lowThreshold = 300;
int highThreshold = 600;
int moreBright = 0;
int lessBright = 255;
void setup() {

// Start Serial & set pin to output
Serial.begin(9600);
pinMode(lightPin,OUTPUT);

}
void loop() {// Read the sensor pin
int sensorValue = analogRead(sensorPin);
// If high light is detected, crank up the light slowly
if (sensorValue > highThreshold){
analogWrite(lightPin, sensorValue);
moreBright = moreBright + lightState;
analogWrite(lightPin, moreBright);

}

// If light level gets dark, crank the light down
if (sensorValue < lowThreshold){
analogWrite(lightPin, sensorValue);
lessBright = lessBright - lightState;
analogWrite(lightPin, lessBright);
}
delay(500);
}
Part 2, Figure 2 — Brighten LED when Environment is Bright
Part 2, Figure 3 — Dim LED when Environment is Dark

Part 3: Create Emergency Flashing Lights when Lights Out

Code: (Use “blink”)

  • Modified the variables to create an ON and OFF condition
  • Use the “blink” code as a setup for flashing emergency lights
// Pins
int sensorPin = 0;
int lightPin = 3;
// Variables
int lowThreshold = 300;
int highThreshold = 600;
int lightOff = 0;
int lightOn = 255;
void setup() {

// Start Serial & set pin to output
Serial.begin(9600);
pinMode(lightPin,OUTPUT);

}
void loop() {// Read the sensor pin
int sensorValue = analogRead(sensorPin);
// If high light is detected, keep the lights off
if (sensorValue > highThreshold){
analogWrite(lightPin, lightOff);
}

// If light level gets dark, flash the LED light
if (sensorValue < lowThreshold){
analogWrite(lightPin,lightOn);
delay(500);
analogWrite(lightPin,lightOff);
delay(500);
}
}
Part 3, Figure 1 — Setup
Part 3, Figure 3 — Flashing Lights Circuit

End of Week 4 Assignments

--

--

Gloria Julien

I think a lot about people’s perceptions about life. Oh, and I’m a UX-er, who’s also turning into a Career Coach.