Menu

Pulsin command to Measure Light with TSL230R

Help
2008-08-23
2013-05-30
  • Mark Twomey

    Mark Twomey - 2008-08-23

    Hello,

    I love GCBASIC !!!

    I am using GCBASIC version 0930.

    I am trying to measure the light level using the TSL230R Light to Frequency Converter chip with the PIC16F887. The TSL230R generates a "pulse train or square wave with frequency directly proportional to light intensity".

    I see that there is a solution using BASIC STAMP (pic basic?) using their PULSIN command:

    http://itp.nyu.edu/physcomp/sensors/Code/TSL230R

    Anybody know how to accomplish this in GCBASIC.

    Thanks,

    Mark Twomey.

     
    • kent_twt4

      kent_twt4 - 2008-08-25

      Have the TSL14S light to voltage converter.  Using it with a ReadAD10 works great, have yet to use it in a project.

      PULSIN could be duplicated by making a pin an input, then checking for your pulse condition, and incrementing a variable/counter to see what you have.  Sort of like:

      #defiine FreqPulse PortA.0
      dir PortA.0 in
      dim FreqCntr as Word

      Main:
      For samples = 1 to xx? ;Some sample/time period
      FreqCntr = 0
      Do While FreqPulse On
      wait 1 10us ;or?
      FreqCntr += 1
      Loop
      Next
      ...
      ...
      ...

      goto Main

      This type of loop is going give sloppy results unless you use a fast clock.  Another situation is the devices frequency bandwidth, from 1 Hz to 1 Mhz, ouch!  The above type of scenario isn't going to do well at all in the very upper frequency range.

       
    • Mark Twomey

      Mark Twomey - 2008-08-26

      Kent,

      Thanks for this information.... forever I am learning.

      Anyway, for now I think I won't use this sensor, too messy.

      I just am trying to detect the light level and I have another sensor I can use, it is an old photo-voltaic sensor that generates voltage in the range from 0.0 to 0.5 V DC.

      I can measure it on my multi-tester without a problem, but I am still trying to hook it into my I6F887. Using PORTA Analog, AN0 I have picked up something (and stored it in the EEPROM) but it seems always to give low numbers, not in the expected range of 0 to 255 even when I shine a bright white LED flashlight at it which in test was pushing the output to 5V.

      I'm not sure if the problem is in the level of voltage that the PIC can sense, that 0.0 to 0.5 V is too low? I tried to use a voltage divider to feed 0.5V into VREF+ and attempted to tell GCBASIC to tell the PIC chip to measure with an external voltage reference. But it didn't make a difference. Also I see that this PIC chip has a built-in VREF of 0.6V, more investigation!

      Anyway, now I am experimenting with amplifying the signal with a transistor, to see if boosting it to 0 to 5V DC will resolve the problem.

      Possibly I will just go and buy another more modern light sensor that is more PIC compatible.

      Mark.

       
    • kent_twt4

      kent_twt4 - 2008-08-27

      "Anyway, for now I think I won't use this sensor, too messy."
      I am sure you could get it to work, just not as easy as the light to voltage sensor previously mentioned (output is 0-4V or 4.9V max).  Supposedly the light to frequency device you've got it is most accurate.

      The ADC default reference voltage will be Vdd , so don't expect a reading over about 25 on the ADC if Vcc is 5V (0.5/5*256).

      For using the Vref+ you would need to set that pin as an input, "dir PortA.3 in".  Also need to tell the ADCCON1 register to set the Vref+ bit like "VCFG0 = 1".  Apply your external voltage divider to the RA3/Vref+ pin, and you should be good to go.  Some devices say Vref+ can't go below a certain voltage, like 2V or ??.  But since this is a newer device, that might not be so.

      Using the FVREN bit could be problematic.   But then again maybe not, as you would not be trying to actually read the value....mmm.

       
    • Mark Twomey

      Mark Twomey - 2008-08-28

      >  The ADC default reference voltage will be Vdd ,
      > so don't expect a reading over about 25 on the ADC if Vcc is 5V
      > (0.5/5*256).

      Aha, that's exactly what was happening, wow good thinking.
      It is good to know that the A/D converter is working and my circuit is good. I thought I was getting random noise.

      I tried using a voltage divider to set VRef+ to be 0.5V which I connected to pin 5 (on the 40 pin DIP). I just noticed that there is a VREF- on pin 4. Possibly I have to connect that to GND instead of leaving it floating. So I need to do more experiments.

      > Also need to tell the ADCCON1 register to set the Vref+ bit like "VCFG0 = 1".

      That's good advice. I have checked the microchip's datasheet and found in figure 9-1 that it uses both "VCFG0 = 1" for VRef+ (AN3) and "VCFG1 = 1" for VRef- (AN2) in the ADC (Analog to Digital Conversion) block diagram.

      Now I just have to figure out how to convert that into GCBASIC....

      So, I have inspected the 16f887.dat file in the GCBASIC chipdata folder, and searched for the ADCON1 register. I see other bits individually identified but nothing for bits 4 and 5, only:

      ADFM,ADCON1,7
      PCFG3,ADCON1,3
      PCFG2,ADCON1,2
      PCFG1,ADCON1,1
      PCFG0,ADCON1,0

      And from the PIC chip datasheet:
      "
         bit 5 VCFG1: Voltage Reference bit
         1 = VREF- pin
         0 = VSS
         bit 4 VCFG0: Voltage Reference bit
         1 = VREF+ pin
         0 = VDD
      "

      So I think I need to do the following in GCBASIC:
         SET ADCON1.4 ON
         SET ADCON1.5 ON

      Does this sound right?

      > Some devices say Vref+ can't go below a certain voltage, like 2V or ??.
      > But since this is a newer device, that might not be so.

      I agree since the PIC16F887 has an "Analog Comparator Module with a Fixed voltage reference (0.6V)" from the specs.

      Well, I am looking forward to trying this out, but I am having a computer problem and waiting for a spare part, hopefully I can get up an running again at the weekend (writing this on my work computer).

       
    • Nobody/Anonymous

      I believe another way to do it is by looking up what register that bit is in (for example, INTCON), and what the bit name is, in your case it looks like to be ADCON1? Check the data sheet.

      For example, when I wanted to disable the Global Interrupts on my 16F690, i disabled the GIE bit in the INTCON register:
      in GCBASIC:

      set INTCON.GIE off

      But I find that GCBASIC has most the bases covered, such as just doing this:

      GIE = 0

      The register can be up to 8 bits wide i believe, with each bit a setting for a certain part of the chip.

      For you it could be set ADCON1.VCFG0 on for vref+ pin? not sure about the register, though.

       
    • Mark Twomey

      Mark Twomey - 2008-08-28

      Again, you are correct!!

      I must have been looking at an old version of the 16f887.dat file, because now when I look at it I see the following:

        VCFG1,ADCON1,5
        VCFG0,ADCON1,4

      This seems to indicate to me that I can do the following in my GCBASIC program:

      SET VCFG0 ON 'ADC Voltage Reference bit, VREF- pin
      SET VCFG1 ON 'ADC Voltage Reference bit, VREF+ pin

      or as you mentioned:

      ADCON1.VCFG0 = 1
      ADCON1.VCFG1 = 1

      I also need to check my GCBASIC folders to ensure that I only have the latest chipdata folder for GCBASIC to find.

      Thanks for your help.

       
    • Mark Twomey

      Mark Twomey - 2008-09-08

      Okay, finally I have it working using the VREF etc.

      But there is a lot of noise in the circuit which is making this a lot harder to get the correct values than it should be.

      So I decided to go with a different light sensor component.

      I used another one I had on hand, an IR detector which I have found detects visible natural light well too. It is the SEN-00241 from SparkFun. I use it in conjunction with a 10K resistor as : 5V to 10K to (ADC reads here) to Sensor to GND.

      One minor strange thing is that the voltage is inverse to the light level, but I can handle this in the code and document it well.

      Also I have some LDR's on order to try them too to see what works best.

      Thanks again.

       
    • Mark Twomey

      Mark Twomey - 2008-09-08

      By the way, here is the core of my testbed code, simple enough:

      SUB measure_light_level (From_data, To_data) #NR

         'Do twenty lightmeter readings
         FOR Data_count = From_data TO To_data

            SET Activity ON 'Red
            WAIT half_sec
            SET Activity OFF 'Red
           
            'Do Measurement
            Light_level = ReadAD(LightMeter)
            EP_mem_addr = Data_count - 1 'eeprom addresses start at zero.
            EPWrite(EP_mem_addr, Light_level)

         NEXT

      END SUB

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.