Matha Goram
Published © GPL3+

Is There Light?

Examining the use of photo resistors, also known as Light Dependent Resistors, as a vehicle to understanding timer interrupts in UNO.

BeginnerProtip15 minutes1,185
Is There Light?

Things used in this project

Hardware components

Elegoo Arduino UNO Rev 3
×1
Elegoo Photoresistor
×1
Elegoo Resistor, 10 Kohms
×1
Elegoo DuPont connecting wires
×1
Elegoo Breadboard, any size
×1
Arduino Uno Mega 2560, Raspberry Pi RAB 5 in 1 Breadboard Holder
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Assembly

Simplest assembly for Photo Resistor use

Schematics

Schematics

Simplest schematic for Photo Resistor Use

Code

Hello Photo (LDR) Resistor

C/C++
Simplest code to read the value, display it and wait for a second.
/*
 * PhotoSensor-01.ino
 * A basic exercise to read the data from a photo-resistor continuously
 * 2018-10-12
 * armw
 * v0.2
 * © 2018 <reza@parkcircus.org> All Rights Reserved
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.  
 * 
 * Reference:
 * https://playground.arduino.cc/Learning/PhotoResistor
 */

/* Simple test of the functionality of the photo resistor

Connect the photoresistor one leg to analog pin 0, and pin to +5V
Connect a resistor (~10K should suffice) from analog pin 0 to GND

----------------------------------------------------

           PhotoR     10K
 +5     o---/\/\/--.--/\/\/---o GND
                   |
 Pin A0 o----------

----------------------------------------------------
*/

// 2019-02-16 v.2 armw pin mnemonic correction suggested by Peter Mansvelder
int pinPHOTOsensor = A0;     // analog pin 0 for the sensor - thx PeterM

void setup()
{
    Serial.begin(115200);  // use serial communcation for data display
    pinMode(pinPHOTOsensor, OUTPUT ); // redundant?
}

void loop()
{
  Serial.println(analogRead(pinPHOTOsensor)); // for use on serial line, stay tuned
  delay(1000);              // nominal expedient way, for now
}

Using a Photo Resistor with a timer interrupt

C/C++
This code illustrates the use of a timer interrupt to handle a static periodic delay which does not interfere with other tasks in the code.
/*
 * PhotoSensor-02.ino
 * Using compare and match timer interrupts to read the data from a photo-resistor efficiently
 * 2018-10-12
 * armw
 * v0.2
 * © 2018 <reza@parkcircus.org> All Rights Reserved
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.  
 * 
 * References:
 * https://playground.arduino.cc/Learning/PhotoResistor
 * https://oscarliang.com/arduino-timer-and-interrupt-tutorial/
 */

/* Simple test of the functionality of the photo resistor

Connect the photoresistor one leg to analog pin 0, and pin to +5V
Connect a resistor (~10K should suffice) from analog pin 0 to GND

----------------------------------------------------

           PhotoR     10K
 +5     o---/\/\/--.--/\/\/---o GND
                   |
 Pin A0 o----------

----------------------------------------------------
*/

// 2019-02-16 v0.2 armw pin mnemonic correction suggested by Peter Mansvelder
int pinPHOTOsensor = A0;     // analog pin 0 for the sensor - thx PeterM
bool interrupted = false;   // interrupt not detected

ISR(TIMER1_COMPA_vect)      // timer compare interrupt service routine
{
  interrupted = true;       // nominal flag
}

void setup()
{
    Serial.begin(115200);  // use serial communcation for data display
    pinMode(pinPHOTOsensor, OUTPUT ); // redundant?

    noInterrupts();         // disable interrupts while handlers are setup
    TCCR1A = 0;             // Timer/Counter Control Registers
    TCCR1B = 0;             // pre-scaler configured with these variables
    TCNT1  = 0;

    OCR1A   = 62500;        // 16000000 / 256 / 1; 16MHz/256/1Hz
    TCCR1B |= (1 << WGM12); // CTC mode
    TCCR1B |= (1 << CS12);  // 256 prescaler
    TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
    interrupts();           // re-enable interrupts for the assignments
}

void loop()
{
  if (interrupted)
  {
    interrupted = false;
    Serial.println(analogRead(pinPHOTOsensor)); // for use on serial line, stay tuned
  }
}

Using a Photo Resistor with a timer interrupt redux

C/C++
For this iteration, the code that was previously performed within the interrupt handler is now performed outside it for efficiency reasons.
/*
 * PhotoSensor-02.ino
 * Using compare and match timer interrupts to read the data from a photo-resistor efficiently
 * 2018-10-12
 * armw
 * v0.2
 * © 2018 <reza@parkcircus.org> All Rights Reserved
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.  
 * 
 * References:
 * https://playground.arduino.cc/Learning/PhotoResistor
 * https://oscarliang.com/arduino-timer-and-interrupt-tutorial/
 */

/* Simple test of the functionality of the photo resistor

Connect the photoresistor one leg to analog pin 0, and pin to +5V
Connect a resistor (~10K should suffice) from analog pin 0 to GND

----------------------------------------------------

           PhotoR     10K
 +5     o---/\/\/--.--/\/\/---o GND
                   |
 Pin A0 o----------

----------------------------------------------------
*/

// 2019-02-16 v0.2 armw pin mnemonic correction suggested by Peter Mansvelder
int pinPHOTOsensor = A0;     // analog pin 0 for the sensor - thx PeterM
volatile bool interrupted = false; // interrupt not detected

ISR(TIMER1_COMPA_vect)      // timer compare interrupt service routine
{
  Serial.println(analogRead(pinPHOTOsensor)); // for use on serial line, stay tuned
}

void setup()
{
    Serial.begin(115200);  // use serial communcation for data display
    pinMode(pinPHOTOsensor, OUTPUT ); // redundant?

    noInterrupts();         // disable interrupts while handlers are setup
    TCCR1A = 0;             // Timer/Counter Control Registers
    TCCR1B = 0;             // pre-scaler configured with these variables
    TCNT1  = 0;

    OCR1A   = 62500;        // 16000000 / 256 / 1; 16MHz/256/1Hz
    TCCR1B |= (1 << WGM12); // CTC mode
    TCCR1B |= (1 << CS12);  // 256 prescaler
    TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
    interrupts();           // re-enable interrupts for the assignments
}

void loop()
{

}

Credits

Matha Goram
27 projects • 23 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.
Contact
Thanks to Oscar Liang.

Comments

Please log in or sign up to comment.