Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
| × | 1 | ||||
| × | 1 | ||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
Software apps and online services | ||||||
![]() |
| |||||
Hand tools and fabrication machines | ||||||
![]() |
| |||||
![]() |
|
A client needed a prototype project made so I conceived and designed this platform. It uses a lux sensor placed on the top of the finger to measure the light diffused from an led placed at the bottom of the finger. the led is placed in a hole on the top plate of a load cell.
The system works by putting a finger guard on that holds the lux sensor, then placing your finger on the load cell plate above the hole with the led (trying to make the shortest vector for the light as you can) and pushing down. The harder you push, the more light the lux sensor should pickup. While pressing down, at iterations of.1 KG, the system records the lux data and writes it to an excel sheet held in the SD card logger. Once you're done recording the data, you can copy it from the SD card to your computer and once you place the SD card back in the logger and run the code again, it wipes the excel sheet, making it ready to use again.
The lux sensor has adjustable gain, and can switch from reading the visible spectrum, infrared, and full-spectrum depending on your needs.
#include <SPI.h>
#include <SD.h>
#include <HX711.h>
#include <Wire.h>
#include "Adafruit_TSL2591.h"
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Encoder.h>
Adafruit_SSD1306 oled(-1);
/* For TSL2591 */
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
uint16_t x;
/* For encoder */
Encoder enc(2,3);
long oldPos = -999;
int currentPos;
/* For SD logger */
File dataFile;
const int chipSelect = 10;
/* For Scale use */
HX711 scale;
long CF = 221020; // for 10KG LC
float CurrentScaleRead;
float threshold = 0.0;
/* For averaging data */
float luxArray[4] = {};
int arrayCounter = 0;
float avgLux = 0.0;
int ledPin = 9;
/* Showing general info for the lux sensor. not neccessary but helpful */
void displaySensorDetails(){
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println(F("-------------------------"));
Serial.print(F("Sensor: ")); Serial.println(sensor.name);
Serial.print(F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F(" lux"));
Serial.print(F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F(" lux"));
Serial.print(F("Resolution: ")); Serial.print(sensor.resolution, 4); Serial.println(F(" lux"));
Serial.println("");
delay(500);
}
/* configuring the gain and integration time for lux sensor */
// Uncomment the gain/integration time you want to use
void configureSensor(){
//tsl.setGain(TSL2591_GAIN_LOW);
tsl.setGain(TSL2591_GAIN_MED);
//tsl.setGain(TSL2591_GAIN_HIGH);
//longer integration times are good in low light situations
// tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); //shortest integr. time (bright light)
// tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); //longest integr. time (dim light)
Serial.print(F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch(gain){
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print(F("Timing ")); Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("-------------------------"));
Serial.println(F(""));
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
Serial.println(" ");
pinMode(ledPin, OUTPUT);
Wire.begin();
/* For initializing the lux sensor */
Serial.println(F("Starting Adafruit TSL2591 test"));
if(tsl.begin()){
Serial.println(F("Found a TSL2591 sensor"));
}
else {
Serial.println(F("No sensor found."));
return;
}
/* For initializing the load cell */
Serial.println("Scale initializing, please remove any weight from the scale.");
scale.begin(A0,A1); // (DO, SCK)
scale.set_scale();
scale.tare();
Serial.println("Scale initialized");
/* For initializing the SD card */
if(!SD.begin(chipSelect)){
Serial.println("Card failed or not present");
while(1);
}
/* For erasing all the data held in the csv file */
if(SD.exists("LClog.csv")){ // could try putting at top of setup and using return once lclog is deleted
Serial.println("LClog.csv exists,");
Serial.println("deleting the file");
}
else{
Serial.println("LClog.csv doesnt exist");
}
SD.remove("LClog.csv");
if(SD.exists("LClog.csv")){
Serial.println("LClog.csv exists");
}
else{
Serial.println("LClog.csv doesnt exist");
}
delay(1500);
/* For writing headers in the csv file */
dataFile = SD.open("LClog.csv", FILE_WRITE);
if(dataFile){
dataFile.print("Lux");
dataFile.print(",");
dataFile.print("LC(kg)");
//dataFile.print("");
dataFile.print(",");
dataFile.print("LED Out.");
dataFile.print(",");
dataFile.println("");
dataFile.close();
Serial.println("data file headers written");
}
else{
Serial.println("Error writing data file headers");
while(1);
}
Serial.println("Card initilized");
delay(1000);
/* For showing lux sensor details */
displaySensorDetails();
configureSensor();
//analogWrite(ledPin, 255);
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
scale.set_scale(CF);
EncoderTwo(); //EncoderRead();
if(threshold < 5.0){ // float here is for changing upward limit of reading LC
CurrentScaleRead = scale.get_units();
Serial.print(CurrentScaleRead); Serial.print(" KG ");
Serial.print("Output: "); Serial.println(currentPos);
if(CurrentScaleRead >= threshold){
Serial.print("Threshold met at ");
Serial.print(threshold);
Serial.print(" output = ");
Serial.println(currentPos);
delay(1000);
simpleReadTest();
//averageData();
SDprint();
threshold = threshold + 0.1;
avgLux = 0;
delay(50);
}
}
}
void scaleRead(){ // function for showing load cell reads. Might be null since I just call .get_units in void loop
scale.set_scale(CF);
Serial.print("reading: ");
Serial.print(scale.get_units(),3); Serial.print(" KG");
CurrentScaleRead = scale.get_units(),3;
Serial.print("Calibration factor: "); Serial.println(CF);
scale.power_down();
delay(50);
scale.power_up();
}
void SDprint() {
dataFile = SD.open("LClog.csv", FILE_WRITE);
if(dataFile){
dataFile.print(x); // lux data
dataFile.print(","); //needed to seperate cells in .csv file
dataFile.print(CurrentScaleRead); // load cell event data
dataFile.print(",");
dataFile.println(currentPos);
dataFile.close();
Serial.print("Data written for ");
Serial.print(CurrentScaleRead); //scale read
Serial.println(" KG ");
}
else{
Serial.println("Error opening file");
while(1);
}
}
void simpleReadTest(){
x = 26;
}
void simpleRead () { // function for reading lux sensor. uncomment the type of wavelength you want to measure
x = tsl.getLuminosity(TSL2591_VISIBLE);
// uint16_t x = tsl.get Luminosity(TSL2591_FULLSPECTRUM);
// uint16_t x = tsl.get Luminosity(TSL2591_INFRARED);
Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
Serial.print(F("Luminosity: "));
Serial.println(x, DEC);
}
void advancedRead() { // function for reading advanced lux sensor data
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ]"));
Serial.print(F("IR: ")); Serial.print(ir); Serial.print(F(" "));
Serial.print(F("Full: ")); Serial.print(full); Serial.print(F(" "));
Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F(" "));
Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 6);
}
void averageData(){ // function for reading lux sensor 4 times and averaging data **WIP**
luxArray[arrayCounter] = x;
arrayCounter++;
if(arrayCounter == 4){
for(int i = 0; i < 4; i++){
avgLux = avgLux + luxArray[i];
}
avgLux = avgLux / 4;
Serial.print("The average lux is: ");
Serial.print(avgLux, 2);
Serial.println("===========================");
Serial.print("val 1: "); Serial.println(luxArray[0]);
Serial.print("val 2: "); Serial.println(luxArray[1]);
Serial.print("val 3: "); Serial.println(luxArray[2]);
Serial.print("val 4: "); Serial.println(luxArray[3]);
Serial.println("===========================");
arrayCounter = 0;
//avgLux = 0;
delay(50);
//Serial.println("restarting averaging");
}
}
void EncoderTwo(){
long pos = enc.read();
currentPos = pos/15;
if(currentPos >= 4||currentPos<0){
enc.write(0);
}
analogWrite(ledPin, currentPos*85);
}
Comments
Please log in or sign up to comment.