max reasonable timer callback frequency?
max reasonable timer callback frequency?
Hello All,
I would like to control an IR-LED to trigger my camera (remote control). The signal is a serial code modulated on a 40KHz carrier. Do you think this is possible in Python? (the timer callback would have to run at 80 KHz to toggle the LED)
Thanks, Hans.
PS: I'm new to micropyton (just received my pyboards today)...
I would like to control an IR-LED to trigger my camera (remote control). The signal is a serial code modulated on a 40KHz carrier. Do you think this is possible in Python? (the timer callback would have to run at 80 KHz to toggle the LED)
Thanks, Hans.
PS: I'm new to micropyton (just received my pyboards today)...
Re: max reasonable timer callback frequency?
I did some experiments... the max usable timer frequency seems to be around 10KHz, not bad but too low for my purpose
Any ideas how to produce a serial stream ( without start bits, etc) at 40 KHz? I looked into SPI but if I understand the docs there is no way to set the baudrate very precisely?
Any hints are much appreciated!
Any ideas how to produce a serial stream ( without start bits, etc) at 40 KHz? I looked into SPI but if I understand the docs there is no way to set the baudrate very precisely?
Any hints are much appreciated!
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: max reasonable timer callback frequency?
I doubt you'll get a callback running that fast. I think you may be able to achieve results using two timers. If you can configure one to flash the LED at 40KHz without using a callback - look at PWM and timer channels - then use the second to gate it on and off, The second only needs to operate at the modulation frequency and shoud work with a callback.
You will need a resistor in series with the LED - and to achieve reasonable range you may need a transistor to drive the LED at higher currents than the board can supply.
You will need a resistor in series with the LED - and to achieve reasonable range you may need a transistor to drive the LED at higher currents than the board can supply.
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: max reasonable timer callback frequency?
This looks promising as a basis, churning out a 40KHz square wave modulated at 500Hz:
Code: Select all
import pyb
import micropython
micropython.alloc_emergency_exception_buf(100)
toggle = False
timer = pyb.Timer(2, freq=40000)
ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width_percent=50)
def cb(filter):
global toggle
if toggle:
ch2.pulse_width_percent(50)
else:
ch2.pulse_width_percent(0)
toggle = not toggle
modulator = pyb.Timer(1, freq=1000) # 500 Hz modulation
modulator.callback(cb)
while True:
print("Running")
pyb.delay(2000)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: max reasonable timer callback frequency?
I was about to point out that the UARTs all support IrDA, but a bit of Googling saved me the embarrassment - a modulated 40kHz carrier signal is typical of Consumer IR, not IrDA.
Good luck!
Bryan
edit - IrDA is supported by the pyboard hardware, not sure if MicroPython can support it easily yet.
Good luck!
Bryan
edit - IrDA is supported by the pyboard hardware, not sure if MicroPython can support it easily yet.
Re: max reasonable timer callback frequency?
Thanks a lot, pythoncoder!
Plenty room for experimenting. I wish I had an oscilloscope. Maybe I can choose a lower frequency, and listen to it?
PS: not sure when this message will be visible, has to be authorized due to new user policy
Plenty room for experimenting. I wish I had an oscilloscope. Maybe I can choose a lower frequency, and listen to it?
PS: not sure when this message will be visible, has to be authorized due to new user policy
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: max reasonable timer callback frequency?
The biggest challenge with IR stuff is knowing the modulation scheme. If this is documented the job will be easy. Otherwise it could be challenging, especially without a 'scope. You can get IR receiver chips which will receive and demodulate the signal from the remote (you need to get one specific to the 40KHz frequency). You could then probably write code to store the received bit stream timing for replay through your modulator.
There is also information on Wikipedia about various standard modulation schemes. Good luck!
There is also information on Wikipedia about various standard modulation schemes. Good luck!
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: max reasonable timer callback frequency?
I tried the 'modulated time' example. It works, thanks again! I believe decoding and encoding IR signals is feasible. I tested the example with a spare Arduino programmed as 'logic analyzer' with OLS. (unreliable and uncomfortable but cheap setup)
I've done IR stuff with Arduinos: quite easy with the IRremote library , by Ken Shirriff. It supports several protocols and has a lot of interesting information...
I think I'll buy a simple (read affordable) usb scope / logic analyzer soon. Thinking about a Saleae logic 8... Any tips for this kind of instruments are very welcome.
I've done IR stuff with Arduinos: quite easy with the IRremote library , by Ken Shirriff. It supports several protocols and has a lot of interesting information...
I think I'll buy a simple (read affordable) usb scope / logic analyzer soon. Thinking about a Saleae logic 8... Any tips for this kind of instruments are very welcome.
Re: max reasonable timer callback frequency?
I love my Logic. I have an early version of the Logic8, and I bought a Logic16 recently (which has some analog capabilities as well).
But the Logic is what goes in my laptop case with me (the nice little case is also stuffed with a few misc boards and ftdi adapters as well).
I find the logic analyzer to be one of the most useful tools (second only to my multimeter).
But the Logic is what goes in my laptop case with me (the nice little case is also stuffed with a few misc boards and ftdi adapters as well).
I find the logic analyzer to be one of the most useful tools (second only to my multimeter).
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: max reasonable timer callback frequency?
Scope or logic analyser? A tough one and it obviously depends on what you're trying to do. If you're playing with DAC's and ADC's then a scope is pretty well essential, and digital ones like my Owon can do a fair job of handling serial data streams. I couldn't do without a scope, but I'd very much like to get a logic analyser too, especially one with built-in support for standard protocols.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.