Jump to content

Interfacing Light to Frequency converter TSL235R with Energia, MSP430F5529LP

Recommended Posts



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,


#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() {

  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) {  

    ctr_left_front = 0L;
    ctr_right_front = 0L;
    second_mark = micro_mark; 
    // critical, time-sensitive code here


void intr_left_front() {

void intr_right_front() {

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



#include "CounterLib_t.h"

Counter<> MyCounter;  // setup a counter

void setup()
  MyCounter.start();  // initialize and start counter

void loop()
  MyCounter.reset();  // reset counter to 0
  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:


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.



  • 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?)



  • 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!)


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?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...