adc.read_timed(buf, freq)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

adc.read_timed(buf, freq)

Post by mschulz » Tue Jun 09, 2015 8:18 am

Hello,
I need your help again.

I want to read the ADC with a constant frequenz and I found the adc.read_timed(buf, freq) function and this example

Example: http://docs.micropython.org/en/latest/l ... read_timed

Code: Select all

adc = pyb.ADC(pyb.Pin.board.X19)    # create an ADC on pin X19
buf = bytearray(100)                # create a buffer of 100 bytes
adc.read_timed(buf, 10)             # read analog values into buf at 10Hz
                                    #   this will take 10 seconds to finish
for val in buf:                     # loop over all values
    print(val)                      # print the value out
The Problem is that the bytearray use only 8 Bit and I need 12bit. So I tried the array.arry but there I get only an error
"name array is not defined"

Code: Select all

adc = pyb.ADC(pyb.Pin('X19'))    # create an ADC on pin X19
buf = array.array('H')                # create a buffer of 100 bytes
print(buf)
adc.read_timed(buf, 10)             # read analog values into buf at 10Hz

while not switch():
    print(buf)
    pyb.delay(200)
I hopte to get about 30 measurements in a buffer to calc the average, ( and then I want to write the values to the sd card. )

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: adc.read_timed(buf, freq)

Post by danicampora » Tue Jun 09, 2015 10:46 am

The Problem is that the bytearray use only 8 Bit and I need 12bit. So I tried the array.arry but there I get only an error
"name array is not defined"
Have you tried:

Code: Select all

import array 
?

mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: adc.read_timed(buf, freq)

Post by mschulz » Tue Jun 09, 2015 11:26 am

Thank you, I forget it.

Code: Select all

adc = pyb.ADC(pyb.Pin.board.X19)    # create an ADC on pin X19
buf = array.array('H')                # create a buffer of 100 bytes
adc.read_timed(buf, 10)             # read analog values into buf at 10Hz

while not switch():                                    #   this will take 10 seconds to finish
    for val in buf:                     # loop over all values
        print(val)                      # print the value out
        
How I can define the size of the array?
How can I write and read values to the array?

My code don't work

Can I get an example to use the read timed function?
Thank You

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: adc.read_timed(buf, freq)

Post by danicampora » Tue Jun 09, 2015 11:37 am

When you do:

Code: Select all

buf = array.array('H')                # create a buffer of 100 bytes
You are creating an array of ZERO length.
adc.read_timed() will read until filling the array, since the array is empty, it doesn't do anything.

try:

Code: Select all

buf = array.array('H', bytearray(200))                # create a buffer of 200 bytes (100 16-bit elements)
and then the rest of your code as it is.

mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: adc.read_timed(buf, freq)

Post by mschulz » Tue Jun 09, 2015 12:49 pm

ah, ok, Thank you

Put the read_timned() function the last value at the end and if the buffer is full, it starts at the begin?

When ends the adc with reading? With my code I think it read only one time until the buffer is full?

How I can read a single value from the array?
Which is the best way to calc the average vom all measurements?

Thank you

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

Re: adc.read_timed(buf, freq)

Post by pythoncoder » Tue Jun 09, 2015 7:50 pm

read_timed() will read samples at the rate you specify until the buffer is full when the function returns. You can access individual elements with buf[0] returning the first value to be read and buf[7] the 8th value. The average will be given by

Code: Select all

average = sum(buf)/len(buf)
Peter Hinch
Index to my micropython libraries.

mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: adc.read_timed(buf, freq)

Post by mschulz » Wed Jun 10, 2015 9:29 am

Thank you very much!

Whitch is the easiest was to say the adc to read continuously into the buffer (and when ist is full start at the beginn)

When I read timed to buffer is the controller blocked while this time, or can he calc other thinks?
How I can create a matrix for measurements (fror 8 ADC)?

mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: adc.read_timed(buf, freq)

Post by mschulz » Thu Jun 11, 2015 12:24 pm

When I trie the calculate the average with

Code: Select all

import pyb
import array
self.__buf0=array.array('H',[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0])
self.__Umess  = array.sum(self.__buf0)/30
I get the error "module" object has no attribute "sum".?

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: adc.read_timed(buf, freq)

Post by danicampora » Thu Jun 11, 2015 12:27 pm

Code: Select all

sum
is a built-in function, not a method from array. The code example that was given to you before shows the correct usage.

mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: adc.read_timed(buf, freq)

Post by mschulz » Wed Jun 17, 2015 5:32 pm

correct reading is really very difficult,
Thank you!

Post Reply