Created
May 21, 2015 12:04
-
-
Save pierre-haessig/48843d5cebc2137d208f to your computer and use it in GitHub Desktop.
2 channels light sensor (Broadband+IR) based on Arduino board + TSL2561 sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_TSL2561_U.h> | |
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); | |
void displaySensorDetails(void) | |
{ | |
sensor_t sensor; | |
tsl.getSensor(&sensor); | |
Serial.println("------------------------------------"); | |
Serial.print ("Sensor: "); Serial.println(sensor.name); | |
Serial.print ("Driver Ver: "); Serial.println(sensor.version); | |
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); | |
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux"); | |
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux"); | |
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux"); | |
Serial.println("------------------------------------"); | |
Serial.println(""); | |
delay(500); | |
} | |
/**************************************************************************/ | |
/* Configures the gain and integration time for the TSL2561 */ | |
/**************************************************************************/ | |
void configureSensor(void) | |
{ | |
/* You can also manually set the gain or enable auto-gain support */ | |
//tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */ | |
//tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */ | |
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */ | |
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */ | |
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */ | |
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */ | |
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */ | |
} | |
void setup(void) | |
{ | |
Serial.begin(9600); | |
Serial.println("Light Sensor Test"); Serial.println(""); | |
/* Initialise the sensor */ | |
if(!tsl.begin()) | |
{ | |
/* There was a problem detecting the ADXL345 ... check your connections */ | |
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"); | |
while(1); | |
} | |
/* Display some basic information on this sensor */ | |
displaySensorDetails(); | |
/* Setup the sensor gain and integration time */ | |
configureSensor(); | |
/* We're ready to go! */ | |
Serial.println(""); | |
} | |
/**************************************************************************/ | |
/* Arduino loop function, called once 'setup' is complete */ | |
/**************************************************************************/ | |
void loop(void) | |
{ | |
/* Get a new sensor event */ | |
sensors_event_t event; | |
tsl.getEvent(&event); | |
/* Display the results (light is measured in lux) */ | |
/* Each line should look like | |
"139.00 lux,906 Broad raw,414 IR raw,14496 Broad,6624 IR" | |
*/ | |
Serial.print(event.light); Serial.print(" lux,"); | |
Serial.print(event.countBroadband); Serial.print(" Broad raw,"); | |
Serial.print(event.countIR); Serial.print(" IR raw,"); | |
Serial.print(event.channelBroadband); Serial.print(" Broad,"); | |
Serial.print(event.channelIR); Serial.println(" IR"); | |
delay(250); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Pierre Haessig — January 2015 | |
""" Read luminosity data from the TSL2561 sensor | |
connected to the Arduino board | |
(with the luminosity_2_channels.ino sketch loaded) | |
""" | |
from __future__ import print_function | |
import serial | |
ser = serial.Serial('/dev/ttyACM0') | |
logfile = 'luminosity_log.csv' | |
print('writing data to "{:s}"'.format(logfile)) | |
with open(logfile, 'w') as out: | |
while True: | |
line = ser.readline().strip() | |
print(line) | |
csv_split = line.split(',') | |
if len(csv_split) != 5: | |
print('skipping line') | |
continue | |
# csv_split should look like | |
# ['238.00 lux', '2224 Broad raw', '1074 IR raw', '35584 Broad', '17184 IR'] | |
lux, broad_raw, ir_raw, broad, ir = csv_split | |
line = lux[:-4] + ',' + broad_raw[:-10] + ',' + ir_raw[:-7] +',' + broad[:-6] + ',' + ir[:-3] | |
out.write(line) | |
out.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment