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:48] – 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 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 represents, the even flow of light 1 lumen, falling on a surface of 1 m< |
| Line 76: | Line 81: | ||
| E = 255,84 * R< | E = 255,84 * R< | ||
| - | 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. But before compiling | + | 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 96: | 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 110: | 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 | ||