Skip to main content

pin.write(value)

Sets the state of the specified pin

Availability

Device

Parameters

Name Type Description
value Float or integer The new state value to which the pin will be set

Returns

Nothing

Description

This method writes the state of an output pin, according to its configured mode.

Digital Outputs

Digital outputs accept a 0 or 1.

Pulse-Width Modulation (PWM) Outputs

PWM outputs accept a duty ratio from 0.0 to 1.0. This is then rounded to the nearest available value.

Analog (DAC) Outputs

Analog (DAC) outputs accept a value from 0.0 to 1.0, covering the range from 0V to the imp power supply voltage, usually 3.3V.

Pin-Triggered Pulse Generator (PTPG) Outputs

PTPG outputs accept a float value in seconds, which is the new value for its delay time: typically, the period in which it pauses between being triggered and actually counting pulses. See pin.configure() for more information. When using PTPG for zero-crossing AC dimming circuits, the value written to the pin using pin.write() should correspond to the duty-cycle of the attached circuit.

The new value is applied at the end of the active pulse, ready for the next transition. Writing a value which exceeds the trigger pin’s interval will mean that the pulse never gets generated. In other words, this can be used to cleanly disable the output.

The requested value may be rounded to the nearest clock period. To retrieve the value actually used in this case, use pin.getdelay().

PTPG Example

// Change imp001 pin 5 duty cycle to 66% @ 60Hz
hardware.pin5.write(0.00275);

Example Code

This code turns an LED on using a GPIO pin configured as a DIGITAL_OUT.

The following code pulses an LED using Pulse Width Modulation (PWM).