Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:practical:hardware:itt:avr:photoresistor [2025/08/26 08:11] – created ingmar05en:iot-open:practical:hardware:itt:avr:photoresistor [2025/09/02 11:25] (current) raivo.sell
Line 1: Line 1:
 +<pagebreak>
 ====== Photoresistor ====== ====== Photoresistor ======
- 
-//Necessary knowledge:  
-[HW] [[en:hardware:homelab:digi]], 
-[ELC] [[en:electronics:voltage_divider]],  
-[AVR] [[en:avr:adc]],  
-[LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]],  
-[LIB] [[en:software:homelab:library:module:sensor]]// 
- 
 ===== Theory ===== ===== Theory =====
  
-[{{  :examples:sensor:photoresistor:sensor_photoresistor_designator.png?150|Electrical symbol for a photoresistor}}] 
 [{{  :examples:sensor:photoresistor:sensor_photoresistor.jpg?150|A photoresistor}}] [{{  :examples:sensor:photoresistor:sensor_photoresistor.jpg?150|A photoresistor}}]
  
-A photoresistor is a sensor which electrical resistance is altered depending on the light intensity falling on it. The more intense is the light the more free carriers are formed and therefore the lower gets the resistance of the element. Two exterior metal contacts of the photoresistor are reaching 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 narrow, curvy track between the electrodes, low total resistance at average light intensity is gained. Similarly to the human eye, the photoresistor is sensitive at certain range of wavelengths and needs to be considered when selecting a photo elementotherwise it may not react to the light source used in the application. Following is simplified list of wavelengths of visible light segmented by colours:         +A photoresistor is a sensor whose electrical resistance is altered depending on the light intensity falling on it. The more intense the lightthe 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 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 elementotherwiseit may not react to the light source used in the application. The following is simplified list of wavelengths of visible light segmented by colours:         
  
 ^ Colour            ^ Range of wavelength (nm)  ^ ^ Colour            ^ Range of wavelength (nm)  ^
Line 23: Line 15:
 | Red          | 610 – 700                  | | Red          | 610 – 700                  |
  
-A range of working temperature is set for photoresistor. Wishing the sensor to work at different temperatures, precising conversions must be executed, because the resisting properties of the sensors are depending on the temperature of the ambient.  +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 m<sup>2</sup>. Hardly ever in reality does light (living area) fall on a surface evenlyand thereforelight intensity is generally reached as an average number. Below are few examples of light intensity for comparison:   
-For characterizing light intensiveness physical concept called light intensity (E) is used, this shows the quantity of light reaching any given surface. Measuring unit is lux (lx), where 1 lux representsthe even flow of light 1 lumen, falling on a surface of 1 m<sup>2</sup>. Hardly ever in reality falls light (living area) on a surface evenly and therefore light intensity is reached generally as average number. Below are few examples of light intensity for comparison:   +
  
  
Line 38: Line 29:
  
 ===== Practice ===== ===== Practice =====
-The HomeLab is equipped with VT935G photoresistor. One pin of the photoresistor is connected to power supply and second pin to the analogue-digital converter (HomeLab II channel 1, HomeLab III channel 13). 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 is decreasing as the light intensity falling on it grows, the measured voltage on the pin of the microcontroller grows as light intensity grows. It is worth to take into account that the photoresistor used in the HomeLab reacts most on orange and yellow light.+The HomeLab is equipped with VT935G photoresistor. One pin of the photoresistor is connected to the power supplyand 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 the half dark room, note it in the program and compare measured values – is it lighter or darker.+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 caseone has to just measure the resistance of the sensor in half-dark room, note it in the programand compare the measured values – is it lighter or darker?
  
-The exercise here is a little bit more complex as the light intensity is measured also in lux. For doing this, exists an approximate formula and floating-point variables. In the C-language are floating-point variables //float-// and //double//-type variables, which can be used to present fractions. Their flaw  is high demand of 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 exercise here is a little bit more complexas 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 high demand for resources. Computers have special hardware to calculate floating-point variables. In the 8-bit AVR microcontrollercalculations are executed in softwarewhich demands a lot of memory and time. If the flaws are not critical, the floating-point variables are worth using.   
  
  
-[{{  :examples:sensor:photoresistor:sensor_photoresistor_vt935g_slope.png?260|Relationship between resistance (R) of VT935G and intensity of light (E)}}]+The example source code measures the light intensity, calculates it using ADC, and displays the intensity of light on the LCD. 
  
-There is an approximate formula showing the relationship between the intensity of light and electrical resistance in the sensor datasheet. As seen on the graph (on the right), with using logarithm scale, the resistance and intensity of light are almost in linear relationship and form a in-line formula, because following conversion applies:     +In the example program, variables of voltage, resistanceand 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). 
- +
-log(a/b) = log(a) - log(b) +
- +
-The relation is characterised by the ascent of the factor γ (ascend of the line), which is 0,9 on VT935G sensor. We have also data on one of the points on that line:  resistance 18.5 kΩ (R<sub>A</sub>) at 10 lx intensity of light (E<sub>A</sub>). Hence we have the coordinates of one point as well as the ascent of the line and for calculating any other point, we only need one coordinate. Meaning, if sensors' resistance (R<sub>B</sub>) is measured, it is possible to calculate from the equation of line, the intensity of light E<sub>B</sub>) that falls on the sensor. Finding E<sub>B</sub> from the equation of line: +
- +
-log(E<sub>B</sub>) = log(R<sub>A</sub>/R<sub>B</sub>) / γ + log(E<sub>A</sub>) \\ \\ +
-E<sub>B</sub> = 10<sup>log(R<sub>A</sub>/R<sub>B</sub>) / γ + log(E<sub>A</sub>)</sup> +
- +
-This gives the formula for calculating the intensity of light when the resistance is known. The resistance can not be measured directly with microcontroller. For this the photoresistor is in the voltage divider. The output voltage of this voltage divider is converted to a specific variable by the analogue-digital converter (ADC). To find the resistance, the output voltage (U<sub>2</sub>) of the voltage divider must be calculated first, using the ADC value, also comparison voltage (U<sub>ref</sub>) of the converter must be taken into account:  +
-The formula is following: +
- +
-U<sub>2</sub> = U<sub>ref</sub> * (ADC / 1024) +
- +
-From the formula for voltage divider(check the chapter on voltage divider) the resistance of the upper photoresistor (R<sub>1</sub>) can be found: +
- +
-R<sub>1</sub> = (R<sub>2</sub> * U<sub>1</sub>) / U<sub>2</sub> - R<sub>2</sub> +
- +
-In the following calculation of voltage and resistance, the known factors are replaced with numbers and indexes have been removed: +
- +
-U = 5 * (ADC / 1024) \\ \\ +
-R = (10 * 5) / U - 10 \\ \\ +
- +
-For finding the intensity of light, simplifying conversions can be done: +
- +
-E = 10<sup>log(18.5/R) / 0.9 + 1</sup> = 10<sup>log(18.5/R) * 10/9</sup> * 10<sup>1</sup> = \\ \\ +
-= 10<sup>log18.5*10/9 - logR*10/9</sup> * 10 = (10<sup>log18.5*10/9</sup> / 10<sup>logR*10/9</sup>) * 10 = \\ \\ +
-= (18.5<sup>10/9</sup> / R<sup>10/9</sup>) * 10 = 18.5<sup>10/9</sup> * 10 * R<sup>-10/9</sup> +
- +
- +
-By calculating the constant in front of the variable of the field R, the expression remains follows: +
- +
-E = 255,84 * R<sup>-10/9</sup> +
- +
-These formulas help only if the photoresistor on the module of the HomeLab is used. If circuit is used equipped with different components, respective variables need to be changed. Next, source code of the example program is presentedwhich measures and calculates 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 be also just 0, because then the compiler understands it correctly). +
  
 <code c> <code c>
Line 123: Line 78:
  {  {
  // Reading the average value of the photoresistor  // Reading the average value of the photoresistor
- adc_value = adc_get_average_value(13, 10); + adc_value = adc_get_average_value(1, 10);
- // HomeLab II +
- //adc_value = adc_get_average_value(1, 10);+
  
  // Calculating the voltage in the input of the ADC  // Calculating the voltage in the input of the ADC
- // HomeLab II  + voltage = 5.0 * ((double)adc_value / 1024.0); 
- //voltage = 5.0 * ((double)adc_value / 1024.0); +
- // HomeLab III +
- voltage = 2.0625 * ((double)adc_value / 2048.0); +
  // Calculating the resistance of the photoresistor   // Calculating the resistance of the photoresistor 
                 // in the voltage divider                 // in the voltage divider
- // HomeLab II + resistance = (10.0 * 5.0) / voltage - 10.0;
- //resistance = (10.0 * 5.0) / voltage - 10.0; +
- // HomeLab III +
- resistance = (33.0) / voltage - 10.0;+
   
  // Calculating the intensity of light in lux   // Calculating the intensity of light in lux
  illuminance = 255.84 * pow(resistance, -10/9);  illuminance = 255.84 * pow(resistance, -10/9);
- // Dividing variable into two integer variable+ // Dividing variable into two integer variables
  // to display it on the screen  // to display it on the screen
  int8_t illu = illuminance;  int8_t illu = illuminance;
Line 159: Line 106:
 } }
 </code> </code>
 +
 +==== Task to be implemented ====
 +  - Make a three-level light indicator, either dark, normal, or intense light, and show the result with three LEDs (green, yellow, and red).
 +
en/iot-open/practical/hardware/itt/avr/photoresistor.1756195901.txt.gz · Last modified: 2025/08/26 08:11 by ingmar05
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