Back

CreatePwmOutput

The CreatePwmOutput function creates an object which manages Pulse Width Modulation (PWM) output for a particular pin on the IOIO board.

 pwm = ioio.CreatePwmOutput( pinNum, freq, mode );

Put the number of the pin you wish to use in the pinNum parameter (Pins 3-7, 10-14, 27-32, 34-40 and 45-48 can be used as PWM output). The freq parameter should contain your chosen pulse frequency in Hz.

If you leave out the mode parameter the pin will be opened in normal mode where pulses will switch between 0 and 3.3V. Setting the mode parameter to "OpenDrain" will enable a mode where pulses switch between 0V and effectively disconnected.

A maximum of 9 pins can be used as PWM outputs at the same time.

See https://github.com/ytai/ioio/wiki/PWM-Output for more details.

Use the SetDutyCycle function of the PWM Object to control the duty cycle. (pass in a decimal value between 0 and 1).

 pwm.SetDutyCycle( dutyCycle );

The following example makes use of the JavaScript setInterval function to repeatedly change the duty cycle which creates a pulse-fade effect.

Example - Fade LED using PWM

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  app.ShowPopup( "Connected!" );
  pwm = ioio.CreatePwmOutput( 0,100 );
  setInterval( "Fader()",10 );
  count = 0; }

function Fader()
{
  pwm.SetDutyCycle( count/100 );
  if( count++ > 100 ) count = 0;
}
  Copy   Copy All    Run