Jump to content
Sign in to follow this  
altineller

Interfacing Light to Frequency converter TSL235R with Energia, MSP430F5529LP

Recommended Posts

Hello,

 

I put two of those light sensors, and wrote the following code to count pulses from them using interrupts. Unfortunately, the sensors output can go upto 1Mhz.

So in low light conditions it will work, but in when i turn a light bulb on near, it will just freeze. when i turn of the bulb it will continue, with a high reading, in millions, and then reset to zero again. I am thinking interrupts are coming in too fast for the mcu to count, or they get put in a queue.

 

Any other methods for reading this sensor? Without use of interrupts maybe? One idea I had was to put a divide by 16 counter, and use this output, but it would be nice to handle them with no external parts.

 

Any ideas, recomendations and help greatly appreciated.

 

Best regards,

C.A.

#define LEFT_FRONT P2_0
#define RIGHT_FRONT P2_3

volatile long ctr_left_front = 0L;
volatile long ctr_right_front = 0L;

static volatile long second_mark;
static unsigned long second_interval = 100000L;

void setup() {
  
  Serial.begin(9600);
  Serial.println("hello");

  pinMode(LEFT_FRONT, INPUT_PULLUP);
  pinMode(RIGHT_FRONT, INPUT_PULLUP);
  
  attachInterrupt(LEFT_FRONT, intr_left_front, FALLING);
  attachInterrupt(RIGHT_FRONT, intr_right_front, FALLING);
}

long micro_mark;

void loop() {
  
  micro_mark = micros();
    

  // second interval
  if(micro_mark - second_mark > second_interval) {  
    
    noInterrupts();

    
    Serial.print(ctr_left_front);
    Serial.print('\t');
    Serial.println(ctr_right_front);
    
    ctr_left_front = 0L;
    ctr_right_front = 0L;
    
    second_mark = micro_mark; 
    
    // critical, time-sensitive code here
    interrupts();    
  }    


}


void intr_left_front() {
  ctr_left_front++;
}

void intr_right_front() {
  ctr_right_front++;
}

Share this post


Link to post
Share on other sites

Hello @@altineller

 

According to the datasheet the maximum frequency without saturation is 500 kHz. Figure 1 in the datasheet shows this occurs at less than than 1000 uW/cm2 irradiance. If this is what is happening, a perhaps less than elegant solution might be to place a filter over it.

Share this post


Link to post
Share on other sites

I took the lack of a convenient solution for such a simple problem as a challenge to start a Counter library for Energia.

 

Attached a first version that works for P1.0 on MSP430G2533 and MSP430F5529.

 

This library leverages the Timer_A peripheral and uses P1.0 as external clock input for a 16 bit counter. 

 

It has 5 functions:

start() initializes the timer peripheral and I/O pin and starts the counter

stop() stops the counter

read() reads the current value of the counter

reset() resets the counter to 0, the counter keeps running

readAndReset() reads the current value and resets the counter to 0

 

Example:

#include "CounterLib_t.h"

Counter<> MyCounter;  // setup a counter

void setup()
{
  Serial.begin(9600);
  MyCounter.start();  // initialize and start counter
}

void loop()
{
  MyCounter.reset();  // reset counter to 0
  delay(1000);
  Serial.println(MyCounter.read());  // read counter and print out value
}

Connected to the 1KHz calibration signal output of my oscilloscope (Rigol DS1054Z) I get the following output:

1005
1006
1005
1005
1006
..

I think you should be able to measure 500 KHz by changing the delay between reset and read to 50-100ms. Make sure to add another longer delay after Serial.print to not overwhelm the serial output.

 

Caveats:

  • The counter will restart at 0 after 65536 clock cycles, that's why it's called a 16 bit counter.
  • P1.0 is also used for LED1, you may need to remove the LED1 jumper if the signal source is not strong enough to drive the LED
  • On the MSP430F5529LP, you have to use the upper pin of the LED1 jumper to input the signal
  • Make sure to verify, that the signal source does not exceed 3.3V or you may damage the MSP430
  • This library may not play nicely with other libraries or functionality that uses Timer A (PWM / analogWrite?)

 

Todo:

  • Configurable /2, /4 and /8 clock dividers, currently the counter is increased on each clock
  • Support for other MCUs and LaunchPads

 

Edit: Updated library with basic support for MSP430F5529

Edit2: Uploaded the correct library file (dooh!)

CounterLib_t.h

Share this post


Link to post
Share on other sites

The MSP430F5529 has 4 timer clocks exposed. Though I only implemented it for TimerA0 yet, and one of them (P7.7 IIRC) is not broken out on the LaunchPad.

 

Do you have to measure all sensors all the time? Couldn't you connect them all to the same counter but activate and measure the sensors one at a time, for say 100ms each?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×