[Tinyos-help] TinyOS-2.x and Sampling rate

David Gay dgay42 at gmail.com
Mon Apr 12 12:25:19 PDT 2010

From: Omar Bouzid <o.m.bouzid at newcastle.ac.uk>
> Currently I am trying to implement a simple code using nesC language and
> TinyOS-2.x so that a sensor node will be able to capture an acoustic signal
> generated by a PC speaker, for instance a sine wave or a gunshot signal, in
> order to know what is the maximum sampling frequency that MICAz mote can
> achieve. This code is installed on a sensor node which sends the signal
> captured to the base station. However, I only can achieve maximum sampling
> frequencies in the range of 200Hz and 750Hz using Read interface and
> ReadStream interface respectively. But...
> In the literature, it was reported that, in tinyos-1.x, the MICA components
> can normally sample at a frequency up to 200 Hz (in real time transfer mode
> i.e. sampling and sending), while in the  ADC's free running mode (i.e.
> turning off the wireless radio of the MICA components while sampling) the
> highest sampling rate using these components can reach 17.723 KHz.
> After I read the TinyOS Programming book, I decided to use TinyOS-2.x
> instead of TinyOS-1.x in my project because of two reasons:
> 1- it is expected that the sampling frequency can be easily increased since
> TinyOS-2.x supports in addition to a millisecond timer a microsecond timer
> which could be used to improve the sampling rate.

The presence or absence of a microsecond timer is platform specific.
Because timer's run in task context, there will be significant latency
between the timer expiring and your task actually starting to execute,
making microsecond timers of relatively slow platforms like the micaz.
What most platforms *do* provide is a microsecond alarm, whose event
runs in interrupt context and doesn't have the latency associated with
a timer. The micaz supports three microsecond alarms, one of which is
used by the ADC subsystem to implement ReadStream.

> 2- It supports the ReadStream Interface which is equivalent to the ADC's
> free running mode.

No it isn't. ReadStream allows arbitrary sampling intervals (in
microseconds), while the ATmega128 free running mode only supports a
very small number of sampling rates which depend on the microprocessor
frequency and a setting in a clock divider register. As a result,
ReadStream is implemented using the single-sample mode of the ADC in
combination with a microsecond alarm.

> But, as I tried to use the microsecond timer in my code, I surprised that
> this type of timer is actually unknown for the compiler.

See above. You can create an AlarmMicro32C ...

> Also, in ReadStream
> mode I only can reach 750 Hz as a maximum sampling rate by setting
> timer.startperiodic(1300) (below this value the senor node does not send any
> data).

What are you doing with the sampled data? Any significant processing
will impact the maximum sampling rate. See below...

> So, my questions are:
> 1- Is it right that the microsecond timer still not fully supported by the
> MICA components?
> 2- Which commands that can be used to set the sampling rate in the
> ReadStream interface? Or is there an technique that can be used to achieve
> high sampling rates?

Modifying an ADC test application to check the sampling rate on a
micaz, I can obtain a maximum sampling rate of ~4.2kHz (sampling the
built-in voltage sensor). A maximum of 4kHz is thus a reasonable
bound... I'm not sure why you only get 750Hz, but I'd suggest checking
whether the radio (and anything else) is off during sampling... Also
it's suspicious that it fails for small sampling times - it should
just fall behind, not fail. I would check if there's not some other

I've attached the modified apps/tests/TestAdc app which I used to
measure this - it sends a serial port message with the elapsed time
(in microseconds) from start to end of sampling 1000 values from
VoltageC. If you set the sampling interval to something small, you'll
get the maximum sampling rate...

Enabling the radio, doing other stuff at the same time as the
sampling, etc, will lower this result.

Note that at the normal 7.37MHz frequency, and using the ADC prescaler
(64) that gives the full 10-bit precision, the maximum sampling
frequency in free running mode is only ~8.9kHz (7.37 / 64 / 13)
anyway. FWIW, lowering the prescaler to 32 gives a maximum sampling
rate of 4.4kHz, so not much higher (implying the overheads are mostly

If you're really interested in getting the maximum possible sample
rate from a micaz (i.e. 4kHz is not enough), I suggest that:

- you carefully read the section on the ADC in the ATmega128 datasheet
- you look at the TinyOS 1 "HighFrequencySampling" application
(tinyos-1.x/apps/HighFrequencySampling) that reads&samples to flash.
IIRC, it can attain ~8kHz sampling (though that they may be keeping
only 8 bits/sample - the flash write rate was one of the limitations).

Note that HighFrequencySampling doesn't play very nicely with other
abstractions (e.g. it uses the timer registers directly in
MicroTimerM.nc). At a guess, the difference between it's sampling rate
and the TinyOS 2.x one is probably in the overheads of the more
general (but much more practical) abstractions for timer and ADC

-------------- next part --------------
A non-text attachment was scrubbed...
Name: FastAdc.tar.gz
Type: application/x-gzip
Size: 2296 bytes
Desc: not available
Url : https://www.millennium.berkeley.edu/pipermail/tinyos-help/attachments/20100412/32dccca7/attachment.bin 

More information about the Tinyos-help mailing list