This is an old revision of the document!


Table of Contents

 

Photoresistor

Theory

A photoresistor

A photoresistor is a sensor whose electrical resistance is altered depending on the light intensity falling on it. The more intense the light, the more free carriers are formed and therefore the lower the resistance of the element. Two exterior metal contacts of the photoresistor reach through the ceramic base material to the light-sensitive membrane, which determines the electrical resistance properties with its geometry and material properties. Since photo-sensitive material itself has high resistance, with a narrow, curvy track between the electrodes, low total resistance at average light intensity is gained. Similar to the human eye, the photoresistor is sensitive to a certain range of wavelengths and needs to be considered when selecting a photo element; otherwise, it may not react to the light source used in the application. The following is a simplified list of wavelengths of visible light segmented by colours:

Colour Range of wavelength (nm)
Purple 400 – 450
Blue 450 – 500
Green 500 – 570
Yellow 570 – 590
Orange 590 – 610
Red 610 – 700

A range of working temperatures is set for the photoresistor. Wishing the sensor to work at different temperatures, precise conversions must be executed, because the resisting properties of the sensors depend on the temperature of the ambient. For characterizing light intensity, a physical concept called light intensity (E) is used, which shows the quantity of light reaching any given surface. The measuring unit is lux (lx), where 1 lux represents the even flow of light 1 lumen, falling on a surface of 1 m2. Hardly ever in reality does light (living area) fall on a surface evenly, and therefore, light intensity is generally reached as an average number. Below are a few examples of light intensity for comparison:

Environment Intensity of light (lx)
Full moon 0,1
Dusk 1
Auditorium 10
Class room 30
Sunset or sunrise 400
Operating room (hospital) 500 - 1000
Direct sun light 10000

Practice

The HomeLab is equipped with a VT935G photoresistor. One pin of the photoresistor is connected to the power supply, and the second pin to the analogue-digital converter channel 1. Between this pin and the ground resistor is also connected, which forms a voltage divider with the photoresistor. Since the electrical resistance of the photoresistor decreases as the light intensity falls on it grows, the measured voltage on the pin of the microcontroller increases as the light intensity increases. It is worth taking into account that the photoresistor used in the HomeLab reacts most to orange and yellow light.

The sensor VT935G is not meant to be a specific measuring device. It is meant to be more a device to specify overall lighting conditions – is there a lighted lamp in the room or not. In this case, one has to just measure the resistance of the sensor in a half-dark room, note it in the program, and compare the measured values – is it lighter or darker?

The exercise here is a little bit more complex, as the light intensity is also measured in lux. For doing this, there exists an approximate formula and floating-point variables. In the C language, there are floating-point variables float- and double-type variables, which can be used to represent fractions. Their flaw is a high demand for resources. Computers have special hardware to calculate floating-point variables. In the 8-bit AVR microcontroller, calculations are executed in software, which demands a lot of memory and time. If the flaws are not critical, the floating-point variables are worth using.

The example source code measures the light intensity, calculates it using ADC, and displays the intensity of light on the LCD.

In the example program, variables of voltage, resistance, and intensity are defined using type double of floating-point variables. The variables which should be used as floating-point variables must always contain a decimal point (it can also be just 0, because then the compiler understands it correctly).

// HomeLab photoresistor demonstration
// LCD screen displays the approximate illuminance in lux
#include <stdio.h>
#include <math.h>
#include <homelab/module/lcd_gfx.h>
#include <homelab/adc.h>
#include <homelab/delay.h>
 
// Main program
int main(void)
{
	char text[16];
	unsigned short adc_value;
	double voltage, resistance, illuminance;
 
	// Initializing the LCD
	lcd_gfx_init();
 
        // Setting LCD backlight to work
        lcd_gfx_backlight(true);
 
	// Clearing the LCD.
	lcd_gfx_clear();
 
	//Cursor on the position
	lcd_gfx_goto_char_xy(3, 2);
 
	// Name of the program
	lcd_gfx_write_string("Luxmeter");
 
	// Setting the ADC
	adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
 
	// Endless loop.
	while (1)
	{
		// Reading the average value of the photoresistor
		adc_value = adc_get_average_value(1, 10);
 
		// Calculating the voltage in the input of the ADC
		voltage = 5.0 * ((double)adc_value / 1024.0);
 
		// Calculating the resistance of the photoresistor 
                // in the voltage divider
		resistance = (10.0 * 5.0) / voltage - 10.0;
 
		// Calculating the intensity of light in lux		
		illuminance = 255.84 * pow(resistance, -10/9);
		// Dividing a variable into two integer variables
		// to display it on the screen
		int8_t illu = illuminance;
    		int16_t illudp = trunc((illuminance - illu) * 1000);
 
		// Converting the intensity of light to text		
		sprintf(text, "%3u.%3u lux   ", illu,illudp); 
 
		// Displaying it on the LCD
		lcd_gfx_goto_char_xy(3, 3);
		lcd_gfx_write_string(text);
 
		// Delay 500 ms
		sw_delay_ms(500);
	}
}
en/iot-open/practical/hardware/itt/avr/photoresistor.1756806827.txt.gz · Last modified: 2025/09/02 09:53 by raivo.sell
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0