Sensor de Luz

En este caso, vamos a ver un ejemplo de como usar un sensor de luz con la Raspberry Pi.
Una fotorresistencia, sensor de luz, o LDR (Light Dependent Resistor), es una resistencia que varía dependiendo de la cantidad de luz que incida sobre ella. A mayor luz, menor es su resistencia, debido al efecto fotoeléctrico. El LDR absorbe los fotones de la luz, proporcionando a sus electrones energía. De esta manera se facilita la circulación de corriente, y por tanto disminuye la resistencia. Es decir, se relacionan inversamente. Uno de los componentes más utilizados para fabricar fotorresistencias es el sulfuro de cadmio.
El simbolo eléctrico de un LDR es el siguiente:
El que vamos a usar nosotros es el BH1750FVI. Utiliza comunicación I2C, se alimenta a 3.3V, su rango de detección va desde 0.11 lx hasta como máximo 100000 lx, y su resolución ( 1 - 65535 lx ).
Conectamos el sensor a la Raspberry Pi de la siguiente manera:
Mirando el datasheet del sensor, vemos una tabla con los distintos comandos que pueden usarse, el tiempo que tarda en hacer una medida, según la resolución indicada, y otros muchos datos.
Los comandos para obtener la luz del ambiente son los siguientes:
- CONTINUOUS_LOW_RES_MODE = 0x13
- CONTINUOUS_HIGH_RES_MODE_1 = 0x10
- CONTINUOUS_HIGH_RES_MODE_2 = 0x11
- ONE_TIME_HIGH_RES_MODE_1 = 0x20
- ONE_TIME_HIGH_RES_MODE_2 = 0x21
- ONE_TIME_LOW_RES_MODE = 0x23
#!/usr/bin/python
# -*- coding: utf-8 -*-
#################################################
# #
# Light sensor BH1750 control module #
# Authors: Ismael Tobar and Raquel Muñoz #
# #
#################################################
import smbus
from time import sleep
#Constants
TIME_MEASURE = 0.12 #It's the time that the sensor takes to do a measure: 120 ms in the worst case (High resolution mode)
#----i2c addresses------
LDR_ADDRESS = 0x23 #i2c address of the sensor
#-----Commands of ultrasonics (Low resolution = 1 lx, High resolution: 0.5 lx)
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Power on
CONTINUOUS_LOW_RES_MODE = 0x13
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
ONE_TIME_HIGH_RES_MODE_1 = 0x20
ONE_TIME_HIGH_RES_MODE_2 = 0x21
ONE_TIME_LOW_RES_MODE = 0x23
#After one time measure, the sensor set to Power Down
i2c = 0
light = 0 #measure of the sensor
#sensitivity=69 #Between 31 (lowest) and 254 (highest)
def setup():
global i2c
try:
i2c = smbus.SMBus(1) #The i2c bus is declared, 1 or 0 depends of the Raspberry Rev
except Exception as e:
print("\n Error in Setup")
print(e)
def read_light(int m):
global light
aux = i2c.read_i2c_block_data(LDR_ADDRESS,m)
sleep(TIME_MEASURE)
light=(aux[1] + (256 * aux[0])) / 1.2
if __name__ == "__main__":
terminate = False #finish the while()
seconds = 0
int mode = 0
c = 0x00
setup()
while terminate == False:
sleep(0.1)
println('Enter what mode do you want to take measures: ')
println('0 : Exit')
println('1: CONTINUOUS_LOW_RES_MODE')
println('2: CONTINUOUS_HIGH_RES_MODE_1')
println('3: CONTINUOUS_HIGH_RES_MODE_2')
println('4: ONE_TIME_HIGH_RES_MODE_1')
println('5: ONE_TIME_HIGH_RES_MODE_2')
println('6: ONE_TIME_LOW_RES_MODE')
mode = input('Enter what mode do you want to take measures?: ')
switch (mode)
case 1:
c = 0x13; #CONTINUOUS_LOW_RES_MODE
break;
case 2:
c = 0x10; #CONTINUOUS_HIGH_RES_MODE_1
break;
case 3:
c = 0x11; #CONTINUOUS_HIGH_RES_MODE_2
break;
case 4:
c = 0x20; #ONE_TIME_HIGH_RES_MODE_1
break;
case 5:
c = 0x21; #ONE_TIME_HIGH_RES_MODE_2
break;
case 6:
c = 0x23; #ONE_TIME_LOW_RES_MODE
break;
case 0:
c = 0x00; #EXIT
terminate = True
break;
default:
break;
if c != 0x00:
if ((mode >= 1) && (mode <= 3)):
seconds = input('Enter how many seconds do you want to take measures? (0 to exit, 255 max): ')
if seconds > 0 and seconds < 256:
for i in range (0,seconds):
read_light(c)
sleep(0.85)
print('\nLight: ', light)
else: #exit
seconds = 0
else:
read_light(c)
print('\nLight: ', light)
else
terminate = True
break;
En nuestro caso este sensor forma parte del vehículo robotizado que hemos desarrollado.