Notifications
Clear all

How to write code to use TSL237-LF sensor  


CoolongER
(@coolonger)
New Member
Joined: 2 weeks ago
Posts: 4
Topic starter  

Hello everyone!

I'm newish to coding and electronics in general, and I'm having some trouble using a light to frequency sensor.

The TSL237-LF.

I've scoured the internet for code examples and found a couple, but they do not seem to give me good readings. 

Does anyone have experience with this sensor, and could you please help.  I out of my depth on this one.

 

Thank you.

 


Quote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 2 years ago
Posts: 741
 

@coolonger

So what is your circuit layout and what code are you using that does not seem to give you good readings?

I read that,
Power-supply lines must be decoupled by
a 0.01μF to 0.1μF capacitor with short leads placed close to the
TSL237 (Figure 17). A low-noise power supply is required to
minimize jitter on output pulse.

Have you met those requirements?

I also read that the Arduino is not all that great at reading frequencies. I am assuming you are using an Arduino?

 


ReplyQuote
CoolongER
(@coolonger)
New Member
Joined: 2 weeks ago
Posts: 4
Topic starter  

@robotbuilder

Hey, thanks for fielding my question.

I do have a 0.1μF capacitor in the circuit. the leads are about 6" though.

I am using an Arduino Uno, and it is powering the sensor.

Since I last posted; I used a different sensor, and have gotten much better results.  I may have damaged the first one when I soldered the leads onto it. 😬 I am new at this and expect to make mistakes.

the code I am using is:

int TSL237 = 2;
#define READ_TM 1000
int GreenLED = 8;

volatile unsigned long pulse_cnt = 0;

unsigned long cur_tm = millis();
unsigned long pre_tm = cur_tm;

unsigned int tm_diff = 0;




void setup() {

// attach interrupt to pin2, send output pin of TSL230R to arduino 2
// call handler on each rising pulse

attachInterrupt(0, add_pulse, RISING);

pinMode(TSL237, INPUT);
pinMode(GreenLED, OUTPUT);
digitalWrite(GreenLED, HIGH);
Serial.begin(9600);
}

void add_pulse(){
pulse_cnt++;
return;
}

unsigned long get_tsl_freq() {
// copy pulse counter and multiply.
// the multiplication is necessary for the current
// frequency scaling level. Please see the
// OUTPUT SCALING section below for more info

unsigned long freq = pulse_cnt;

Serial.println(freq);
// re-set pulse counter
pulse_cnt = 0;

return(freq);


}
void loop() {
pre_tm = cur_tm;
cur_tm = millis();

if( cur_tm > pre_tm ) {
tm_diff += cur_tm - pre_tm;
}
else if( cur_tm < pre_tm ) {
// handle overflow and rollover (Arduino 011)
tm_diff += ( cur_tm + ( 34359737 - pre_tm ));
}
if( tm_diff >= READ_TM ) {

// re-set the ms counter
tm_diff = 0;

unsigned long frequency = get_tsl_freq();
}
}

 

 

 

Here are a couple of pictures of the circuit. I am using a green LED with a 220kohm resistor for low light, as I don't want high frequencies, because I read that it overloads the Arduino.

https://drive.google.com/file/d/1Q8Kf5fsZpxvhZQg2YJRUy6m9_Ms6v9dL/view?usp=sharing

https://drive.google.com/file/d/1Q25u7HvRWjuyybh3GuHXdKn_qfc6S0W5/view?usp=sharing

 

What other controllers would you suggest? Or maybe another sensor would work better for what I am trying to do.  You can see my other post on measuring translucence to see what I'm trying to do.

Thanks again for the help.


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 2 years ago
Posts: 741
 

@coolonger

You can see my other post on measuring translucence to see what I'm trying to do.

https://forum.dronebotworkshop.com/sensors-modules/reading-the-changing-translucence-of-a-liquid-with-an-led-and-a-light-sensor/#post-17837

Yes I now remember reading the post but didn't really get what you were doing apart from wanting to read some light levels to see how dark/light the color was at a high resolution.  I refrained from asking any questions as a past experience has shown that the poster only becomes more frustrated and angry at how thick I am in understanding rather than them not giving a clear (to me) or complete (for me) explanation as someone who doesn't know anything about their project or in this case mixing dyes or the machinery setup being used. I was told not to keep asking questions about their project as I wasn't any help at all. So you see it is with trepidation I even write this post.

Hopefully someone else will get it and offer some assistance, advice or suggestions.

I am not familiar with the TSL237-LF.

I am using a green LED with a 220kohm resistor for low light, as I don't want high frequencies, because I read that it overloads the Arduino.

You see I have no idea what a green LED has to do with high frequencies overloading the Arduino?  I imagined that it was the output frequency from the TSL237-LF being too high for the Arduino to read it fast enough?  I don't even get why converting a light level to a square wave is useful?  Maybe it provides more resolution than the analog inputs 0 to 1023 range of the Arduino? And so on and so on ...

With posting source code it is good to retain indents. If you are using the Arduino IDE you can Select All with the right mouse button and then Copy as HTML. You can then use the {;} to insert the source code copied.

 

 

int TSL237 = 2;
#define READ_TM 1000
int GreenLED = 8;

volatile unsigned long pulse_cnt = 0;
unsigned long cur_tm = millis();
unsigned long pre_tm = cur_tm;

unsigned int tm_diff = 0;

void setup() {
  // attach interrupt to pin2, send output pin of TSL230R to arduino 2
  // call handler on each rising pulse

  attachInterrupt(0, add_pulse, RISING);

  pinMode(TSL237, INPUT);
  pinMode(GreenLED, OUTPUT);
  digitalWrite(GreenLED, HIGH);
  Serial.begin(9600);
}

void add_pulse(){
  pulse_cnt++;
  return;
}

unsigned long get_tsl_freq() {
  // copy pulse counter and multiply.
  // the multiplication is necessary for the current
  // frequency scaling level. Please see the
  // OUTPUT SCALING section below for more info

  unsigned long freq = pulse_cnt;

  Serial.println(freq);
  // re-set pulse counter
  pulse_cnt = 0;

  return(freq);
}


void loop() {
  pre_tm = cur_tm;
  cur_tm = millis();

  if( cur_tm > pre_tm ) {
    tm_diff += cur_tm - pre_tm;
  }
  else if( cur_tm < pre_tm ) {
    // handle overflow and rollover (Arduino 011)
    tm_diff += ( cur_tm + ( 34359737 - pre_tm ));
  } 
  
  if( tm_diff >= READ_TM ) {
    // re-set the ms counter
    tm_diff = 0;
    unsigned long frequency = get_tsl_freq();
  } 
}

 

 


ReplyQuote