Alternative to Tone() that allows duty cycle adjustment

Hi all,

I’m working on a project that uses an arduino to control a microfluidic pump. Here is the schematic that I am using to control Amplitude & Frequency (attached)

Currently I am using a DAC BOB from Sparkfun to provide the amplitude pin with a DC voltage, however for the Clock signal, I am using the Tone() library. This works well, but the datasheet for the micropump recommends that I increase the duty cycle to 95% when working at frequencies ~40Hz (lower flow rate is what i need). I know that the Tone() library doesn’t allow duty cycle adjustments (just stays a square wave at 50% duty) so I was wondering what other options I had that allow me to have variable frequency and duty cycle? I’m still learning the environment so any help is really appreciated!

Please let me know if you need additional details!

Thanks :slight_smile:

There are some PWM libraries that will very likely be useful.

However, AVR timers are actually fairly simple things. Armed with the datasheet I suspect you could make a good attempt then finish off with some help from the forum.

Assuming Arduino UNO, Nano or Mini (AtMega328), here’s one simple idea: (untested)
And a timer tutorial if you would rather go that route (probably better).

int Hz = 40, dutyCycle = 90;
uint32_t tStart, tCycle = 1000000UL / Hz,
         onTime = tCycle / 100 * dutyCycle;
const byte outPin = 13;

void setup()
{
  //Serial.begin(9600);
  pinMode(outPin,OUTPUT);
  tStart = micros();
}
void loop()
{
  digitalWrite(outPin,micros() - tStart < onTime);
  if(micros() - tStart > tCycle)
    tStart += tCycle;
}

edgemoron:
Assuming Arduino UNO, Nano or Mini (AtMega328), here’s one simple idea: (untested)

Thank you, I will give this a shot on Monday and let you know how it goes!