Controlling a Photocell with Arduino
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/
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.
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);
}
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 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( ) )
- Changed the lightPin location to “PWM 3”
- Changed digitalWrite() to analogWrite() https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
- Set output value when ambient lighting is above highThreshold to 255 (always ON)
- Set output value when ambient light is below lowThreshold to 100 (halfway ON)
// 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);
}
}
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 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);
}
}
End of Week 4 Assignments