Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
MauricioV
Published © GPL3+

Lux-Pressure Ratio Shield

Need to measure lux diffusion through tissue relative to pressure? Well here is the system for you!

IntermediateFull instructions providedOver 1 day96
Lux-Pressure Ratio Shield

Things used in this project

Hardware components

Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
SparkFun Load Cell Amplifier - HX711
SparkFun Load Cell Amplifier - HX711
×1
Arduino Nano R3
Arduino Nano R3
×1
Flash Memory Card, SD Card
Flash Memory Card, SD Card
×1
Adafruit SD card logger
×1
Adafruit strain gauge load cell
×1
Resistor 330 ohm
Resistor 330 ohm
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Custom parts and enclosures

gcode file for guards, guides and enclosures

3D print files for finger guards, finger guides, and the load cell enclosures.
(gcode file is for ultimaker cura 3D printers)

Schematics

Schematic of system

Code

Lux_LC_SD.ino

C/C++
The main code for running the whole system
#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);
}

Credits

MauricioV
4 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.