GPIO & Analog I/O
Hardware GPIO API reference for pin control, 12-bit ADC analog conversions, software PWM, and microsecond pulse measurement in KryonOS.
Hardware GPIO & Analog I/O
The System.gpio (or global GPIO) object provides direct hardware control over the microcontroller's general-purpose input/output pins and analog converters.
Board pinouts and schematics can be found in the KryonOS GitHub Repository.
Constants
| Constant | Value | Description |
|---|---|---|
System.gpio.INPUT | 0 | High-impedance digital input pin mode |
System.gpio.OUTPUT | 1 | Push-pull digital output pin mode |
System.gpio.INPUT_PULLUP | 2 | Input pin mode with internal pull-up resistor enabled |
System.gpio.HIGH | 1 | Logic High (3.3V) |
System.gpio.LOW | 0 | Logic Low (0V) |
Digital I/O Functions
System.gpio.pinMode(pin, mode)
Configures the physical operating mode of a GPIO pin.
- Parameters:
pin(Integer): Microcontroller GPIO number.mode(Integer):INPUT,OUTPUT, orINPUT_PULLUP.
- Returns:
undefined
System.gpio.digitalWrite(pin, state)
Sets the voltage output state of a configured output pin.
- Parameters:
pin(Integer)state(Integer):HIGH(3.3V) orLOW(0V).
- Returns:
undefined
System.gpio.digitalRead(pin)
Reads the digital logic state of a pin.
- Parameters:
pin(Integer) - Returns:
Integer(1for HIGH,0for LOW).
Analog & Pulse Measurement
System.gpio.analogRead(pin)
Reads the voltage on an ADC-capable pin using the ESP32's 12-bit Successive Approximation Register (SAR) ADC.
- Parameters:
pin(Integer): ADC1 or ADC2 pin number (e.g. GPIO 32, 33, 34, 35, 36, 39). - Returns:
Integer(0to4095, corresponding to 0V to 3.3V).
System.gpio.analogWrite(pin, duty)
Initiates software pulse-width modulation on a digital pin.
- Parameters:
pin(Integer)duty(Integer,0to255)
- Returns:
undefined
For higher resolution (up to 16-bit), custom frequencies, and servo motor control, use the dedicated Hardware PWM API.
System.gpio.pulseIn(pin, state, timeout)
Measures the duration of an incoming pulse with microsecond precision. The JavaScript thread is suspended while native C++ hardware timers measure the pulse width.
- Parameters:
pin(Integer): GPIO pin connected to signal.state(Integer):HIGHorLOWto measure.timeout(Integer, optional): Timeout in microseconds (default1000000).
- Returns:
Integer: Duration in microseconds (or0if timed out).
Code Example: HC-SR04 Ultrasonic Distance Sensor
var TRIG_PIN = 12;
var ECHO_PIN = 13;
System.gpio.pinMode(TRIG_PIN, System.gpio.OUTPUT);
System.gpio.pinMode(ECHO_PIN, System.gpio.INPUT);
function readDistanceCm() {
// Send 10us trigger pulse
System.gpio.digitalWrite(TRIG_PIN, System.gpio.LOW);
System.delayMicroseconds(2);
System.gpio.digitalWrite(TRIG_PIN, System.gpio.HIGH);
System.delayMicroseconds(10);
System.gpio.digitalWrite(TRIG_PIN, System.gpio.LOW);
// Measure return echo pulse in microseconds
var durationUs = System.gpio.pulseIn(ECHO_PIN, System.gpio.HIGH, 30000);
if (durationUs === 0) {
return -1; // Out of range or timeout
}
// Sound travels at ~0.0343 cm/us (divide by 2 for round-trip)
var distanceCm = (durationUs * 0.0343) / 2;
return distanceCm;
}
// Polling loop
var distance = readDistanceCm();
System.print("Measured Distance: " + distance.toFixed(2) + " cm");