• 0

Script Editor: Change duty ratio of a clock pattern every cycle


Hello, I am a new user of Analog Discovery 2 device. I would like to output a pattern at DIO1 (for example) wherein the duty ratio is selected from a look-up table. I am trying to use the Script Editor. However, I am unsure what function/variable I should use to increment the index for the look-up table such that at the beginning of every cycle, the clock has the updated duty ratio. Here is a snapshot of my dummy code:


if(!('Patterns' in this)) throw "Please open a Patterns instrument";

var values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95];


for(var idx = 0; idx < 10; idx++){

	var d = values[idx];

	wait(FOR AN EDGE-TRIGGER OR SOME SIMILAR CLOCK EVENT) // I am unsure if wait is the correct choice. 

	Patterns.Channels.DIO1.Duty.value = d;

	idx = idx+1;


// Assuming that DIO1 has a set frequency of 1kHz, the duty should change from 10% to 95% every 1kHz in steps as listed in variable 'values'.

Any insights would be helpful.


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Hi @gupta

You can create a custom pattern with such duty factors.

With the default configuration of AD2 you have 1ki samples for such custom pattern but with the 4th device configuration up to 16ki.

The pattern can be created in WaveForms Pattern Custom Editor, external spreadsheet editor exported/imported csv, or with the Script tool like this:

var duty = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95];
var data = []
const res = 20
duty.forEach(function(d){ for(var i = 0; i < res; i++) data.push(i>=res*d/100?0:1)})
Patterns.Channels.DIO1.custom = data

if(Patterns.Channels.DIO1.custom.length!=data.length) print("too many samples",data.length)
else print("done", data.length)



Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now