# NI digital write task

> Write command channel values to the digital output lines of an NI device.

An NI digital write task writes the values of command channels to the digital output lines of one NI device. For example, a valve on a schematic can switch `port0/line0` of a USB-6343 through a relay.

Each line has a command channel and a [state channel](https://docs.synnaxlabs.com/reference/hardware/tasks#write-tasks). Send 1 to turn a line on and 0 to turn it off.

Before you start, you need a [configured device](https://docs.synnaxlabs.com/reference/hardware/ni/configure-device).

## Create an NI digital write task

When you create command channels with a client library, give each one its own index channel. A writer that holds several commands on one index must write all of them in every frame.

[Enterprise](https://docs.synnaxlabs.com/reference/get-started/editions "Needs Synnax Enterprise")

**Console**

1. In the devices toolbar, right-click the device and select **Create digital write task**.
2. Set **State update rate**.
3. Click **Add channel**, then enter the port and the line in **Port/Line**.
4. Click (**Start**).

The Console creates the channels. For a device with the identifier `dev1`, port 0, line 0 gives the command channel `dev1_do_0_0_cmd` with the index `dev1_do_0_0_cmd_time`, and the state channel `dev1_do_0_0_state` with the index `dev1_do_state_time`.

[](https://synnax.nyc3.cdn.digitaloceanspaces.com/docs/hardware/ni/digital-write-task-dark.mp4#t=0.001)

**Python**

Python does not create channels. Create them first.

```python
import synnax as sy
from synnax import ni

dev = client.devices.retrieve(key="<device key>")
state_time = client.channels.create(
    name="dev1_do_state_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
state = client.channels.create(
    name="valve_1_state", index=state_time.key, data_type=sy.DataType.UINT8
)
cmd_time = client.channels.create(
    name="valve_1_cmd_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
cmd = client.channels.create(
    name="valve_1_cmd", index=cmd_time.key, data_type=sy.DataType.UINT8
)

task = ni.DigitalWriteTask(
    name="Valves",
    device=dev.key,
    channels=[
        ni.DOChannel(port=0, line=0, cmd_channel=cmd.key, state_channel=state.key)
    ],
)
client.tasks.configure(task)
task.start()
```

Python creates the task on the embedded Driver of the Core. For a device on another Driver, use the Console or TypeScript.

**TypeScript**

TypeScript does not create channels. Create them first, as in the Python example.

```typescript
import { ni } from "@synnaxlabs/client";

const dev = await client.devices.retrieve({ key: "<device key>" });
const task = await client.tasks.create({
  name: "Valves",
  rack: dev.rack,
  type: "ni_digital_write",
  config: ni.digitalWriteConfigZ.parse({
    device: dev.key,
    channels: [
      {
        port: 0,
        line: 0,
        cmdChannel: cmd.key,
        stateChannel: state.key,
      },
    ],
  }),
});
await task.start();
```

## Task options

| Console label     | Python field           | Default | Notes                                                                                |
| ----------------- | ---------------------- | ------- | ------------------------------------------------------------------------------------ |
| Device            | `device`               |         | The key of the device.                                                               |
| State update rate | `state_rate`           | 10 Hz   | How often the task writes the state channels.                                        |
| Data saving       | `data_saving_disabled` | On      | See [data saving](https://docs.synnaxlabs.com/reference/hardware/tasks#data-saving). |
| Auto start        | `auto_start`           | Off     | Starts the task each time the Driver starts.                                         |

## Channel reference

Each channel has a `cmd_channel` and a `state_channel`. The table shows the Console label of each setting, with its Python field under it.

| Setting                  | Default | Notes                         |
| ------------------------ | ------- | ----------------------------- |
| Port/Line `port`, `line` | `0`/`0` | The port, and the line in it. |

## Troubleshooting

* [Port ..., line ... has already been used on another channel](https://docs.synnaxlabs.com/reference/hardware/ni/digital-write-task#port-line-has-already-been-used-on-another-channel)

  * Cause

    Two channels use the same line.

  * Fix

    Change the port or the line of one of them.

For other problems, see [Configure an NI device](https://docs.synnaxlabs.com/reference/hardware/ni/configure-device#troubleshooting) and [Troubleshoot hardware](https://docs.synnaxlabs.com/reference/hardware/troubleshooting).
