UniDev Reference

Class for controlling sensors, addressable LEDs (Light Ring), and other peripherals.

Port Definitions

The badges on each port show its available capabilities: analog input (ADC), digital I/O (DIG), and servo/PWM (PWM). Below is the default usage.

P1 GPIO 12 ADC DIG PWM
Traffic light red
P2 GPIO 13 ADC DIG PWM
Traffic light yellow
P3 GPIO 14 ADC DIG PWM
Traffic light green / Side ultrasonic Trig
P4 GPIO 15 ADC DIG PWM
Side ultrasonic Echo
P5 GPIO 17 ADC DIG PWM
Button
P6 GPIO 16 ADC DIG PWM
Front ultrasonic Trig
P7 GPIO 32 ADC DIG PWM
Front ultrasonic Echo
P8 GPIO 23 ADC DIG PWM
Light Ring (in use)

Summary:

  • Analog sensors (analogSensor, lineSensor): only P1–P4 and P7. P5, P6, P8 have no ADC — reads return garbage.
  • Digital I/O (digitalSensor, button): any port P1–P8.
  • Servo / PWM (servo): P1–P7. P8 technically supports PWM but is occupied by the light ring — avoid it.
  • P7 is the most versatile port: the only analog input on ADC1 (no Wi-Fi conflict), with no boot-time quirks.

For hardware nuances of individual pins (strapping pins, ADC2/Wi-Fi conflict) see the Limitations page.

Initialization

begin void begin()

Initializes the UniDev instance, creating the NeoPixel object and setting pin modes. But it is not necessary to call this function, because it is called automatically when the UniDev object is created.

Example

UniDev module;
void setup() {
    module.begin();
}

Sensors

ultraSonic int ultraSonic(int trig, int echo)

Reads distance from an ultrasonic sensor (HC-SR04).

Parameters

  • trig: Trigger pin (P1-P8)
  • echo: Echo pin (P1-P8)

Returns

Distance in millimeters.

Example

int dist = module.ultraSonic(P6, P7);
if (dist < 100) {
    // Obstacle detected
}
lineSensor int lineSensor(int port)

Reads an analog value from a line sensor.

Parameters

  • port: Sensor port definition (P1-P8).

Returns

Analog value (0-4095).

Example

int val = module.lineSensor(P3);
digitalSensor int digitalSensor(int port)

Reads a generic digital value from a port.

Parameters

  • port: Port definition (P1-P8).

Returns

0 (LOW) or 1 (HIGH).

Example

if (module.digitalSensor(P5) == 1) {
    // Sensor Active
}
analogSensor int analogSensor(int port)

Reads a generic analog value from a port.

Parameters

  • port: Port definition (P1-P8).

Returns

Analog value (0-4095).

Example

int lightLevel = module.analogSensor(P4);
getPinMode int getPinMode(uint8_t pin)

Returns the current configured mode of a GPIO pin.

Returns

OUTPUT, INPUT, INPUT_PULLUP, INPUT_PULLDOWN, or -1 on error.

Example

int mode = module.getPinMode(P2);

Inputs

waitButton void waitButton(int port)

Blocking function that halts program execution until the specified button is pressed and released.

Parameters

  • port: Port where button is connected (e.g., P5).

Example

// Wait for button press
module.waitButton(P5);
getButtonState bool getButtonState(int port)

Checks if a button is currently pressed.

Returns

true if pressed, false otherwise.

Example

if (module.getButtonState(P5)) {
    Serial.println("Button Pressed!");
}

Servo

servo void servo(int port, int angle)

Sets the angle of a servo motor connected to the specified port.

Parameters

  • port: The port the servo is connected to (e.g. P1).
  • angle: Target angle in degrees (0–180).

Example

module.servo(P3, 0);   // Home position
module.servo(P3, 90);  // Centre position
module.servo(P3, 180); // End position
servoAttach void servoAttach(int port)

Pre-attaches a servo to a port and reserves a PWM channel without setting an angle. Optional — servo() attaches automatically on first call. Useful when you want to claim the channel before the first movement.

Parameters

  • port: The port the servo is connected to (e.g. P3).

Example

module.servoAttach(P3); // Reserve PWM channel early
module.servo(P3, 90);   // Then use it
servoDetach void servoDetach(int port)

Detaches a servo from its port — PWM pulses stop and the motor goes limp. Use this to protect the servo when the mechanism is forced against a stop or carrying a load. The next servo() call re-attaches it automatically.

Parameters

  • port: The port the servo is on (e.g. P3).

Example

module.servo(P3, 90);    // Move to position
delay(1000);
module.servoDetach(P3);  // Release — servo goes limp

Light Ring (Basic)

pixel void pixel(int index, int r, int g, int b)

Sets the color of a single pixel.

Parameters

  • index: LED index (0-23).
  • r, g, b: Color components (0-255).

Example

// Set 1st LED to Blue
module.pixel(0, 0, 0, 255);
pixelsAll void pixelsAll(int r, int g, int b)

Sets all LEDs in the ring to the specified RGB color. Implicitly calls show().

Example

module.pixelsAll(255, 0, 0); // All Red
pixelsClear void pixelsClear()

Turns off all LEDs.

Example

module.pixelsClear();
pixelsShow void pixelsShow()

Sends the current buffer to the LED strip.

Example

module.pixelsShow();
pixelsBrightness void pixelsBrightness(int brightness)

Sets the global brightness scaling.

Parameters

  • brightness: from 0 to 100.

Example

module.pixelsBrightness(50); // Dim the LEDs

Light Ring (Effects)

These are blocking animations.

pixelsRainbow void pixelsRainbow(int speed, int duration)

Displays a rotating rainbow animation.

Parameters

  • speed: Speed of rotation (0-100). Higher values mean faster rotation.
  • duration: Total duration of the animation in milliseconds.

Example

module.pixelsRainbow(50, 2000); // 2 seconds
pixelsRunning void pixelsRunning(int r, int g, int b, int duration)

Running light effect: a single pixel moves around the ring once.

Parameters

  • r, g, b: Color of the running pixel (0-255).
  • duration: Time in milliseconds to complete one full circle.

Example

module.pixelsRunning(255, 0, 0, 1000); // Red pixel circles in 1s
pixelsBreathing void pixelsBreathing(int r, int g, int b, int duration)

Smoothly fades the LEDs up and down (breathing effect).

Parameters

  • r, g, b: Target color at maximum brightness (0-255).
  • duration: Total duration of one full breath cycle (fade in + fade out) in milliseconds.

Example

module.pixelsBreathing(0, 0, 255, 3000); // Blue breath over 3s
pixelsFill void pixelsFill(int r, int g, int b, int duration)

Gradually fills the ring with color, then gradually clears it.

Parameters

  • r, g, b: Fill color (0-255).
  • duration: Total duration for the fill AND clear sequence in milliseconds.

Example

module.pixelsFill(0, 255, 0, 1000); // Green fill+clear in 1s
pixelsSparkle void pixelsSparkle(int r, int g, int b, int duration, int count)

Randomly flashes individual pixels.

Parameters

  • r, g, b: Color of the sparkles (0-255).
  • duration: Total duration of the effect in milliseconds.
  • count: Number of sparkles to generate within that duration.

Example

module.pixelsSparkle(255, 255, 255, 2000, 20); // 20 white flashes in 2s
pixelsRotating void pixelsRotating(int r, int g, int b, int duration, int segmentLength, int rotations)

Rotates a segment of lit LEDs around the ring multiple times.

Parameters

  • r, g, b: Color of the segment (0-255).
  • duration: Total duration for ALL rotations in milliseconds.
  • segmentLength: Number of LEDs in the rotating segment (e.g., 5).
  • rotations: Number of full 360-degree turns to complete.

Example

// Rotate a segment of 5 red LEDs 3 times over 2 seconds
module.pixelsRotating(255, 0, 0, 2000, 5, 3);
pixelsSpinner void pixelsSpinner(int r, int g, int b, int duration)

A spinner animation that lights up one pixel at a time until the ring is full, then restarts.

Parameters

  • r, g, b: Color of the spinner (0-255).
  • duration: Time in milliseconds for one full rotation.

Example

module.pixelsSpinner(255, 255, 0, 1000);

Traffic Light

setTrafficLight void setTrafficLight(TrafficLightColor color)

Sets the traffic light module state.

Parameters

  • color: TRAFFIC_RED, TRAFFIC_YELLOW, TRAFFIC_GREEN, or TRAFFIC_OFF.

Example

module.setTrafficLight(TRAFFIC_GREEN);
trafficLightSequence void trafficLightSequence()

Runs a standard standard traffic light sequence (Green -> Yellow -> Red -> Yellow -> Green).

Example

module.trafficLightSequence();