MATLAB Examples

digitalWrite

Write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. The width input parameter is optional. Pulse width set on less than 1 will set pin unconditionally, where the values larger than 1 will set a pin for a required time interval in seconds and negate its state after that.

If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor.

NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.

Contents

Syntax

e.digitalWrite(pin,value);
e.digitalWrite(pin,value,width);

Description

e.digitalWrite(pin,value); set the output pin to value HIGH or LOW
e.digitalWrite(pin,value,width); set the output pin to value HIGH or LOW for specific duration then negate its state

Examples

  • set digital pin 13 to output HIGH
  • Connect to Engduino, configure digital pin 13 as output pin, and output HIGH

    % Check if the Engduino object already exists. Otherwise initialize it.
    if (~exist('e', 'var'))
            e = engduino();
    end
    
    e.pinMode(13, e.PIN_TYPE_OUTPUT);
    e.digitalWrite(13,e.PIN_VALUE_HIGH);
    

  • Set digital pin 13 to output LOW for specific duration then negate it
  • Connect to Engduino, configure digital pin 13 as output pin, and output LOW for 1.5s

    % Check if the Engduino object already exists. Otherwise initialize it.
    if (~exist('e', 'var'))
            e = engduino();
    end
    
    e.pinMode(13, e.PIN_TYPE_OUTPUT);
    e.digitalWrite(13,e.PIN_VALUE_HIGH,1.5);
    

    Input Arguments

    pin Pin number
    value e.PIN_VALUE_HIGH or e.PIN_VALUE_LOW state
    width Pulse width in seconds (optional)

    Output Arguments

    res Return status to indicate whether function has been executed (int) 0: OK, <0: Error code