repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv_sensor.py 963B

123456789101112131415161718192021222324252627282930313233
  1. import smbus
  2. import time
  3. # Get I2C bus
  4. bus = smbus.SMBus(1)
  5. # TSL2561 address, 0x39(57)
  6. # Select control register, 0x00(00) with command register, 0x80(128)
  7. # 0x03(03) Power ON mode
  8. bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
  9. # TSL2561 address, 0x39(57)
  10. # Select timing register, 0x01(01) with command register, 0x80(128)
  11. # 0x02(02) Nominal integration time = 402ms
  12. bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)
  13. time.sleep(0.5)
  14. # Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
  15. # ch0 LSB, ch0 MSB
  16. data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)
  17. # Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
  18. # ch1 LSB, ch1 MSB
  19. data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)
  20. # Convert the data
  21. ch0 = data[1] * 256 + data[0]
  22. ch1 = data1[1] * 256 + data1[0]
  23. # Output data to screen
  24. print("Full Spectrum(IR + Visible) :%d lux" %ch0)
  25. print("Infrared Value :%d lux" %ch1)
  26. print("Visible Value :%d lux" %(ch0 - ch1))