Reading data from TSL235R Sensor
Re: Reading data from TSL235R Sensor
Strange, I tested that it worked just as a copy-paste into a fresh REPL. I'll try your version when I get back to my computer.
Re: Reading data from TSL235R Sensor
I notice that it does a bunch of register manipulation and then creates a timer 2 object, which might be undoing some of the register manipulations.
Re: Reading data from TSL235R Sensor
Okay, my apologies. The code as posted doesn't seem work on a pyboard with a fresh REPL. I was testing on a different board, which was already running a fairly long script at startup before dropping into the REPL - something in that script was allowing this to work, so I need to track that down. What that doesn't explain is how @Chemist got it to work. I'm guessing that initializing some other peripheral set a bit somewhere that we need here.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Reading data from TSL235R Sensor
Damien's code runs fine here once you specify a pulsewidth for timer10:
Code: Select all
tim10 = pyb.Timer(10, freq=1000)
tim10_ch1 = tim10.channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width = 21000)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Reading data from TSL235R Sensor
On this subject I should point out that the PWM examples in the documentation don't actually work.
http://docs.micropython.org/en/latest/l ... Timer.html
By my measurements the unit of pulse width is 30nS. In the example given, the repetition rate is 1KHz (1mS). The pulse width is 210000*30nS = 6.3mS which results in DC. The example works if these two values are reduced appropriately.
http://docs.micropython.org/en/latest/l ... Timer.html
By my measurements the unit of pulse width is 30nS. In the example given, the repetition rate is 1KHz (1mS). The pulse width is 210000*30nS = 6.3mS which results in DC. The example works if these two values are reduced appropriately.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Reading data from TSL235R Sensor
Back at the office with my multimeter and the problem(s) become a bit more clear 
It appears that my original code does work, but it works more reliably if external trigger pin X1 is initialized with the pull-down enabled:
I had intended to this initially but overlooked it. As a result, when I tested last night on my pyboard I couldn't increment the counter by connecting a wire from 3.3V to X1, but on my other board it did work - floating inputs are weird that way. With the pull-down enabled it works on both boards. It had nothing to do with any other code that was running on either board.
@pythoncoder already pointed out the other problem: in Damien's code, the PWM output on Y3 doesn't actually start until the pulse width is specified. More accurately, it does start, but pulse_width = 0 so there are no events to count.
Also explains why it worked for @Chemist - he didn't need the pull-down enabled on X1 because his sensor was driving the input with a push-pull signal.
I'm feeling much relieved

It appears that my original code does work, but it works more reliably if external trigger pin X1 is initialized with the pull-down enabled:
Code: Select all
pin_t2 = pyb.Pin(pyb.Pin.board.X1, pull=pyb.Pin.PULL_DOWN, mode=pyb.Pin.AF_OD, af=pyb.Pin.AF1_TIM2)
@pythoncoder already pointed out the other problem: in Damien's code, the PWM output on Y3 doesn't actually start until the pulse width is specified. More accurately, it does start, but pulse_width = 0 so there are no events to count.
Also explains why it worked for @Chemist - he didn't need the pull-down enabled on X1 because his sensor was driving the input with a push-pull signal.
I'm feeling much relieved

Re: Reading data from TSL235R Sensor
I noticed afterward that it could appear to be out of order. I tested again, and the code works with the line 'timer2 = pyb.Timer(2)' either before or after the register manipulation section.dhylands wrote:I notice that it does a bunch of register manipulation and then creates a timer 2 object, which might be undoing some of the register manipulations.
It probably makes more logical sense to put it first.
-Bryan
Re: Reading data from TSL235R Sensor
Of course, I was not setting the pulse_width! Thanks @pythoncoder!
So the following code now works for me, and takes a frequency measurement on X1. For testing I connect X1 to Y3 with a wire and Y3 is outputing a PWM signal at 1.5kHz:
@Chemist: that should be good enough to sample the frequency output of the light sensor.
So the following code now works for me, and takes a frequency measurement on X1. For testing I connect X1 to Y3 with a wire and Y3 is outputing a PWM signal at 1.5kHz:
Code: Select all
import pyb, stm
# setup Timer(2) to count by external trigger on pin X1
def tim2_setup_etr():
# Create a Timer(2) object in Python
tim2 = pyb.Timer(2)
# APB1 clock enable for TIM2
stm.mem32[stm.RCC + stm.RCC_APB1ENR] |= 1
# Set External Clock Enable bit in Slave Mode Control Register
stm.mem32[stm.TIM2 + stm.TIM_SMCR] |= 1 << 14
# Enable TIM2
stm.mem32[stm.TIM2 + stm.TIM_CR1] |= 1
# Initialize pin X1 to its alternate function / TIM2 mode
pyb.Pin(pyb.Pin.board.X1, mode=pyb.Pin.AF_OD, pull=pyb.Pin.PULL_DOWN, af=pyb.Pin.AF1_TIM2)
return tim2
tim2 = tim2_setup_etr()
# measure frequency on pin X1
def measure_freq(ms_delay):
tim2.counter(0)
start = pyb.micros()
pyb.delay(ms_delay)
counter = tim2.counter()
micros = pyb.elapsed_micros(start)
freq_khz = 1e3 * counter / micros
print(counter, '/', micros, '=', freq_khz, 'kHz')
# create a PWM output on pin Y3
tim10 = pyb.Timer(10, freq=1500)
tim10_ch1 = tim10.channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width_percent=10)
# take a few sample using random sampling time to test it
pyb.delay(100)
for i in range(10):
measure_freq(pyb.rng() % 100 + 200)
Re: Reading data from TSL235R Sensor
Hello,
I am sorry for the late reply. I would like to thank you All of you for your time and efforts!!! I really appreciate your help!
Have a great day!
I am sorry for the late reply. I would like to thank you All of you for your time and efforts!!! I really appreciate your help!
Have a great day!