max reasonable timer callback frequency?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
hansel
Posts: 13
Joined: Wed Feb 11, 2015 7:34 pm
Location: Rotterdam, NL
Contact:

max reasonable timer callback frequency?

Post by hansel » Wed Feb 11, 2015 8:19 pm

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

hansel
Posts: 13
Joined: Wed Feb 11, 2015 7:34 pm
Location: Rotterdam, NL
Contact:

Re: max reasonable timer callback frequency?

Post by hansel » Thu Feb 12, 2015 6:12 am

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!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: max reasonable timer callback frequency?

Post by pythoncoder » Thu Feb 12, 2015 8:40 am

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.
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: max reasonable timer callback frequency?

Post by pythoncoder » Thu Feb 12, 2015 4:38 pm

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.

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: max reasonable timer callback frequency?

Post by blmorris » Thu Feb 12, 2015 5:09 pm

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.

hansel
Posts: 13
Joined: Wed Feb 11, 2015 7:34 pm
Location: Rotterdam, NL
Contact:

Re: max reasonable timer callback frequency?

Post by hansel » Thu Feb 12, 2015 6:55 pm

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: max reasonable timer callback frequency?

Post by pythoncoder » Fri Feb 13, 2015 10:46 am

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!
Peter Hinch
Index to my micropython libraries.

hansel
Posts: 13
Joined: Wed Feb 11, 2015 7:34 pm
Location: Rotterdam, NL
Contact:

Re: max reasonable timer callback frequency?

Post by hansel » Fri Feb 13, 2015 2:15 pm

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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: max reasonable timer callback frequency?

Post by dhylands » Fri Feb 13, 2015 3:42 pm

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: max reasonable timer callback frequency?

Post by pythoncoder » Sat Feb 14, 2015 7:46 am

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.

Post Reply