This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:photoresistor [2015/11/10 08:26] – heikopikner | en:examples:sensor:photoresistor [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Photoresistor ====== | ====== Photoresistor ====== | ||
| - | //Necessary knowledge: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [ELC] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| + | [LIB] [[en:software:homelab:library: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| Line 20: | Line 25: | ||
| A range of working temperature is set for photoresistor. Wishing the sensor to work at different temperatures, | A range of working temperature is set for photoresistor. Wishing the sensor to work at different temperatures, | ||
| - | 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 represents, the even flow of light 1 lumen, falling on a surface of 1m2. Hardly ever in reality falls light (living area) on a surface evenly and therefore light intensity is reached generally as a average number. Below are few examples of light intensity: | + | 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 represents, the even flow of light 1 lumen, falling on a surface of 1 m< |
| - | + | ||
| - | ~~PB~~ | + | |
| - | Values of light intensity for comparison: | + | |
| ^ Environment | ^ Environment | ||
| Line 35: | Line 38: | ||
| ===== Practice ===== | ===== Practice ===== | ||
| - | The Sensor module in the HomeLab is equipped with VT935G photoresistor. One pin of the photoresistor is connected to +5 V power supply and second pin to the channel 1 (pin PF1) of the analogue-digital converter. Between this pin and the ground | + | 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 |
| | | ||
| 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 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. | ||
| Line 78: | Line 81: | ||
| E = 255,84 * R< | E = 255,84 * R< | ||
| - | These formulas help only if the photoresistor on the module | + | 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 presented, which measures and calculates using ADC and displays the intensity of light on the LCD. |
| + | |||
| + | In the example | ||
| - | 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). When using //sprintf// for converting floating-point variable to text, “%f” format must be used, this may be enhanced using integers and decimal places. For example: “%3.2”, which displays always 3 integers and 2 decimal places. | ||
| - | ~~PB~~ | ||
| <code c> | <code c> | ||
| + | // HomeLab photoresistor demonstration | ||
| + | // LCD screen displays the approximate illuminance in lux | ||
| #include < | #include < | ||
| #include < | #include < | ||
| Line 98: | Line 103: | ||
| // Initializing the LCD | // Initializing the LCD | ||
| lcd_gfx_init(); | lcd_gfx_init(); | ||
| + | |||
| + | // Setting LCD backlight to work | ||
| + | lcd_gfx_backlight(true); | ||
| // Clearing the LCD. | // Clearing the LCD. | ||
| lcd_gfx_clear(); | lcd_gfx_clear(); | ||
| - | //Cursor on the position | + | //Cursor on the position |
| - | lcd_gfx_goto_char_xy(3, | + | lcd_gfx_goto_char_xy(3, |
| // Name of the program | // Name of the program | ||
| Line 112: | Line 120: | ||
| // Endless loop. | // Endless loop. | ||
| - | while (true) | + | while (1) |
| { | { | ||
| // Reading the average value of the photoresistor | // Reading the average value of the photoresistor | ||
| - | adc_value = adc_get_average_value(1, | + | adc_value = adc_get_average_value(13, |
| + | // HomeLab II | ||
| + | //adc_value = adc_get_average_value(1, | ||
| // Calculating the voltage in the input of the ADC | // Calculating the voltage in the input of the ADC | ||
| - | voltage = 5.0 * ((double)adc_value / 1024.0); | + | // HomeLab II |
| + | //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 | + | // HomeLab II |
| - | resistance = (10.0 * 5.0) / voltage - 10.0; | + | //resistance = (10.0 * 5.0) / voltage - 10.0; |
| - | + | // HomeLab III | |
| - | // Calculating the intensity of light in lux | + | resistance = (33.0) / voltage - 10.0; |
| + | |||
| + | // Calculating the intensity of light in lux | ||
| illuminance = 255.84 * pow(resistance, | illuminance = 255.84 * pow(resistance, | ||
| + | // Dividing variable into two integer variable | ||
| + | // to display it on the screen | ||
| + | int8_t illu = illuminance; | ||
| + | int16_t illudp = trunc((illuminance - illu) * 1000); | ||
| - | // Converting the intensity of light to text | + | // Converting the intensity of light to text |
| - | sprintf(text, | + | sprintf(text, |
| // Displaying it on the LCD | // Displaying it on the LCD | ||