testing pyboard DAC

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
brutor
Posts: 8
Joined: Sun May 04, 2014 7:44 am
Location: Belgium

testing pyboard DAC

Post by brutor » Sun May 04, 2014 12:57 pm

Some testing results on the pyboard DAC output :

some changes compared to the code example on : http://micropython.org/doc/module/pyb/DAC

Code: Select all

from pyb import DAC
from pyb import LED
import math

dac = DAC(1)
led = LED(4)

led.on()

buf = bytearray(100)

for i in range(len(buf)) :
    buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
    
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
An int() around the sinewave calculation seemed necessary, bytearray() only excepts integers.
I've added a LED to see the board is running.

result :
pyboard_DAC_400Hz_100pts_127dig.png
pyboard_DAC_400Hz_100pts_127dig.png (112.78 KiB) Viewed 5723 times
The sinewave is clipped probably because the full DAC range isn't available.

Lowering the range to 100digits and taking a higher time resolution gives better results :

Code: Select all

buf = bytearray(1000)

for i in range(len(buf)) :
    buf[i] = 128 + int(100 * math.sin(2 * math.pi * i / len(buf)))
    
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
result :
pyboard_DAC_400Hz_1000pts_100dig.png
pyboard_DAC_400Hz_1000pts_100dig.png (107.19 KiB) Viewed 5723 times
It's incredible how fast you can get these signals out of this pyboard ! (even faster than on the Raspberry Pi ;) )
Especially for a pure analog designer like myself.

brutor
Posts: 8
Joined: Sun May 04, 2014 7:44 am
Location: Belgium

Re: testing pyboard DAC

Post by brutor » Mon May 05, 2014 9:23 pm

Some more test results on the DAC : trianglewave generator

Code: Select all

from pyb import DAC
from pyb import LED

# turn on a led
led = LED(4)
led.on()

# set the trianglewave frequency (Hz)
# sample frequency needs to be 2048 bigger to get the correct result
frequency = 100
sample_frequency = frequency * 2048

#output to the DAC
dac = DAC(1)
dac.triangle(sample_frequency)
Result :
pyboard_DAC_triangle_100Hz.png
pyboard_DAC_triangle_100Hz.png (109.07 KiB) Viewed 5694 times
- the conversion factor is thus 2048 and not 256 or 1024 as mentioned in the documentation.
- amplitude is about 850mVpp
- tested up to 1200Hz, which seemed strange since the DAC is specified at 1MS/s maximum ??
- there are some noise spikes on the waveform, they stay at the same location at different frequencies,
not sure if this is related to micropython or to the MCU

To be continued ...

pfalcon
Posts: 1153
Joined: Fri Feb 28, 2014 2:05 pm

Re: testing pyboard DAC

Post by pfalcon » Tue May 06, 2014 10:21 pm

Interesting, please keep posting. Can say few words about hardware you use for the capture?
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

brutor
Posts: 8
Joined: Sun May 04, 2014 7:44 am
Location: Belgium

Re: testing pyboard DAC

Post by brutor » Thu May 08, 2014 7:40 pm

It's a Picoscope 5000 series (5444B). An excellent piece of measurement equipment.

I found an interesting little project for the DAC function, will try to work this out over the weekend ...

cjbarnes18
Posts: 5
Joined: Fri May 02, 2014 10:35 pm

Re: testing pyboard DAC

Post by cjbarnes18 » Sat May 10, 2014 9:24 pm

It appears that the triangle method is generating 2 triangles, one modulated by the other.
IMAG001.PNG
triangle modulated by lower frequency triangle.
IMAG001.PNG (3.64 KiB) Viewed 5627 times
This is possibly the cause of the spike artifacts?

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

Re: testing pyboard DAC

Post by blmorris » Wed May 14, 2014 3:26 pm

@cjbarnes18: It's been a few days since you posted this, so you may have moved on.
I can't tell for certain from your screenshot what the sample rate is or the frequency / period of the signal you are measuring, but my first thought upon seeing a waveform like that is to make sure that we aren't looking at an aliasing issue. You can get a plot that looks that way if your sample rate is less than twice the signal frequency. To get a clean and detailed waveform plot you want the sample rate to be more than twice the signal frequency, preferably much more.
You may know this already, but it is the first thing I will check when the plot doesn't look like what I expected.
-Bryan

brutor
Posts: 8
Joined: Sun May 04, 2014 7:44 am
Location: Belgium

Re: testing pyboard DAC

Post by brutor » Thu May 15, 2014 7:52 pm

Hello again,

As said I have a nice little project for testing the DAC functions:

XY generation of the TEK logo on a TEK 2445 oscilloscope
Inspired by w2aew's video : http://www.youtube.com/watch?v=344oEu9vo7w

But it's not working out :

Code: Select all

from pyb import DAC
from pyb import LED
#import math

dac_x = DAC(1)
dac_y = DAC(2)

# Turn on the blue LED
led = LED(4)
led.on()

# TEK Logo from w2aew

x_points = (10, 90, 90, 110, 130, 140, 140, 90, 90, 97, 113, 120, 90, 90, 100,
            150, 150, 170, 170, 195, 220, 190, 220, 195, 170, 170, 100, 85,
            77, 70, 72, 80, 90, 60, 60, 40, 40, 10)
            
y_points = (100, 100, 80, 82, 74, 60, 40, 40, 50, 60, 60, 50, 50, 40, 30, 30,
            100, 100, 55, 80, 80, 45, 10, 10, 35, 10, 10, 13, 23, 40, 60, 72,
            80, 80, 10, 10, 80, 80)

# place the points in a bytearray

buf_x = bytearray(len(x_points))
buf_y = bytearray(len(x_points))

for j in range(len(x_points)) :
    buf_x[j] = x_points[j]
    buf_y[j] = y_points[j]
    
# Write the TEK logo on the dac outputs

dac_x.write_timed(buf_x, 1000, mode=DAC.CIRCULAR)
dac_y.write_timed(buf_y, 1000, mode=DAC.CIRCULAR)
DAC(2) - Y axis isn't working and the pyboard needs to be started in SAFE MODE to recover.

Any ideas ?

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

Re: testing pyboard DAC

Post by blmorris » Fri May 16, 2014 3:57 pm

@brutor- I used REPL to enter the commands from your script relevant to DAC2, and I also saw the hard crash at
dac_y.write_timed(buf_y, 1000, mode=DAC.CIRCULAR)
I don't get the crash when entering just the commands for DAC1, the x buffer. It looks to me like some kind of bug, possibly a typo in the DAC2 initialization. Odd, because it looks like a simple write to DAC2 doesn't cause the crash.
Unfortunately it is hard for me to directly confirm that the DAC outputs are actually working as intended, as I am testing on non-pyboard hardware and the DAC outputs themselves are buried (controlling another chip on the board.)
I will try to investigate further, but it looks like it calls for a bug report. I will post this weekend if you don't get to it first.

Also, thanks for the link to the video of the scope plotting demo. Very cool, gives me some ideas for demos I would like to try.

-Bryan
Last edited by blmorris on Fri May 16, 2014 4:12 pm, edited 1 time in total.

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

Re: testing pyboard DAC

Post by blmorris » Fri May 16, 2014 4:11 pm

@brutor- Regarding your triangle wave post- I wonder if the artifacts are the result of your high sample rate. From page 137 of the data sheet (DM00037051.pdf) the max update rate is 1 MS/sec
Max frequency for a correct DAC_OUT change when small variation in the input code (from code i to i+1LSB)
From this I would presume that a faster update rate could produce inaccurate voltages. Something to check.

brutor
Posts: 8
Joined: Sun May 04, 2014 7:44 am
Location: Belgium

Re: testing pyboard DAC

Post by brutor » Fri May 16, 2014 9:55 pm

@blmorris :
- triangle test :
the sample rate in the triangle waveform test was only 204.8kS/s (100x2048). The artifacts are visible at all frequencies.

- DAC2 problem:
DAC2 is working with the dac.write() function, but not with the dac.write_timed() function
DAC1 works on both functions.

Seems indeed a bug.

Post Reply