RCX Library Reference

Setup

The ESP32 acts as an IR tower. Install the driver once, then import whichever modules you need in each script.

  1. Connect to the ESP32 using the serial terminal in the app.
  2. Click Install RCX Lib — this uploads rcx_driver.py and all module files to the ESP32.
  3. Import the modules you need, write your script, then click Flash to run it.
# Import only what you need
from motion_rcx import move, stop
from sound_rcx  import beep

move(speed=6, duration=2.0)
beep()
How it works: Every command is sent as a direct IR packet to the RCX in real time. The ESP32 encodes each byte as a 12-bit UART frame at 2400 baud over a 38 kHz IR carrier. The RCX executes commands immediately as they arrive.

Motion

motion_rcx.py

High-level two-motor movement. Controls motors A and B together. All duration values are in seconds; omit to run until stop().

motion
move(speed=7, duration=None)

Drive forward on motors A and B.

move(speed=5, duration=2.0)
motion
backward(speed=7, duration=None)

Drive in reverse on motors A and B.

backward(speed=4, duration=1.5)
motion
turn_left(speed=7, duration=None)

Pivot left: motor A forward, motor B reverse.

turn_left(duration=0.5)
motion
turn_right(speed=7, duration=None)

Pivot right: motor A reverse, motor B forward.

turn_right(duration=0.5)
motion
spin_left(speed=7, duration=None)

Spin left in place.

spin_left(speed=5, duration=1.0)
motion
spin_right(speed=7, duration=None)

Spin right in place.

spin_right(speed=5, duration=1.0)
motion
stop()

Coast all three motors to a stop.

stop()
motion
brake()

Hard-brake all three motors immediately.

brake()
motion
wait(seconds)

Pause the script for the given number of seconds.

wait(1.5)

Individual Motors

motors_rcx.py

Fine-grained control of motors A, B, and C individually. Use the constants MOTOR_A, MOTOR_B, MOTOR_C and FORWARD, REVERSE for readable code.

motor
on(motor_id, direction=FORWARD)

Turn on a single motor.

from motors_rcx import on, power, MOTOR_A, FORWARD, REVERSE
power(MOTOR_A, 5)
on(MOTOR_A, FORWARD)
motor
off(motor_id)

Coast a single motor to a stop.

off(MOTOR_A)
motor
brake(motor_id)

Hard-brake a single motor.

brake(MOTOR_B)
motor
power(motor_id, level)

Set power level 0–7 before calling on().

power(MOTOR_C, 3)
motor
all_off()

Coast all three motors at once.

all_off()
motor
all_brake()

Hard-brake all three motors at once.

all_brake()

Sound

sound_rcx.py

Preset sounds and arbitrary tones. play_tone duration is in 1/100ths of a second.

sound
beep(sound=BEEP)

Play one of the RCX's five built-in sounds.

BLIPshort blip
BEEPstandard beep (default)
SWEEPfrequency sweep
PLINGhigh pling
BUZZlow buzz
from sound_rcx import beep, SWEEP
beep()        # default beep
beep(SWEEP)   # sweep sound
sound
play_tone(frequency, duration)

Play a tone at any frequency.

frequency: Hz  |  duration: 1/100ths second

from sound_rcx import play_tone
play_tone(440, 50)   # concert A, 0.5 s
play_tone(880, 25)   # one octave up, 0.25 s

Sensors

sensors_rcx.py

Configure the three RCX sensor ports before reading them. Always call configure() (or set_type + set_mode) once at the start of your script.

sensor
configure(sensor, type_id, mode)

Set both type and mode in one call. Use this first.

from sensors_rcx import configure, SENSOR_1, TYPE_LIGHT, MODE_PERCENT
configure(SENSOR_1, TYPE_LIGHT, MODE_PERCENT)
sensor
set_type(sensor, type_id)

Set the hardware type attached to a sensor port.

TYPE_NONEno sensor
TYPE_SWITCHtouch sensor
TYPE_TEMPERATUREtemp sensor
TYPE_LIGHTlight sensor
TYPE_ROTATIONrotation sensor
sensor
set_mode(sensor, mode)

Set how the RCX interprets the sensor reading.

MODE_RAW0–1023 raw A/D
MODE_BOOLEAN0 or 1
MODE_EDGEcount transitions
MODE_PULSEcount half-transitions
MODE_PERCENT0–100 scaled
MODE_CELSIUS°C (temperature)
MODE_FAHRENHEIT°F (temperature)
MODE_ROTATIONrotation ticks
sensor
clear(sensor)

Reset the sensor counter or accumulator to zero. Useful for edge/pulse counting modes.

from sensors_rcx import clear, SENSOR_2
clear(SENSOR_2)

Display

display_rcx.py

Control what the RCX LCD shows. Only one value is displayed at a time.

display
show_sensor(sensor_id)

Display a sensor reading. sensor_id: 0, 1, or 2.

from display_rcx import show_sensor
show_sensor(0)   # show sensor 1 value
display
show_timer(timer_id)

Display a timer value. timer_id: 0–3.

show_timer(0)
display
show_variable(var_id)

Display an RCX variable. var_id: 0–31.

show_variable(0)
display
show_clock()

Display minutes since the RCX was powered on.

show_clock()
display
show(source, argument=0)

Low-level display control using any RCX source constant.

from display_rcx import show, SOURCE_SENSOR_RAW
show(SOURCE_SENSOR_RAW, 0)

System

system_rcx.py

RCX system control: alive check, power, IR range, messaging between bricks, and timers.

system
ping()

Send an alive check to the RCX. Use this to verify IR is working.

from system_rcx import ping
ping()
system
stop_all()

Stop all running tasks on the RCX immediately.

stop_all()
system
power_off()

Turn off the RCX.

power_off()
system
set_time(hours, minutes)

Set the RCX internal clock.

set_time(0, 0)   # reset clock
system
set_range(long_range=False)

Switch IR transmitter range. Use long_range=True for distance; False (default) for nearby reliable operation.

set_range(long_range=True)
system
set_power_down(minutes)

Set the auto power-off delay. Pass 0 to disable.

set_power_down(0)   # disable auto-off
system
set_message(value)

Broadcast an IR message (0–255) to nearby RCX bricks.

set_message(42)
system
clear_timer(timer_id)

Reset a timer to zero. timer_id: 0–3.

clear_timer(0)

Example Programs

Drive a square

from motion_rcx import move, turn_right, stop
from sound_rcx  import beep

for _ in range(4):
    move(speed=6, duration=1.5)
    turn_right(speed=5, duration=0.6)

stop()
beep()

Tone scale

from sound_rcx import play_tone

notes = [262, 294, 330, 349, 392, 440, 494, 523]  # C4 to C5
for freq in notes:
    play_tone(freq, 30)   # 0.3 s each

Light sensor reaction

from sensors_rcx import configure, SENSOR_1, TYPE_LIGHT, MODE_PERCENT
from display_rcx import show_sensor
from motion_rcx  import move, stop

configure(SENSOR_1, TYPE_LIGHT, MODE_PERCENT)
show_sensor(0)

# Drive while light is on, stop when dark
move(speed=5)

Two-brick messaging

from system_rcx import set_message
from sound_rcx  import beep
import time

# Send three signals, one second apart
for i in range(3):
    set_message(i + 1)
    beep()
    time.sleep(1)

Quick Reference

All modules at a glance.

# motion_rcx
from motion_rcx import *
move(speed, duration)
backward(speed, duration)
turn_left(speed, duration)
turn_right(speed, duration)
spin_left / spin_right
stop() / brake() / wait(s)

# motors_rcx
from motors_rcx import *
power(motor, 0–7)
on(motor, direction)
off(motor) / brake(motor)
all_off() / all_brake()
MOTOR_A/B/C  FORWARD/REVERSE

# sound_rcx
from sound_rcx import *
beep(sound)
play_tone(hz, duration)
BLIP BEEP SWEEP PLING BUZZ

# sensors_rcx
from sensors_rcx import *
configure(sensor, type, mode)
set_type(sensor, type)
set_mode(sensor, mode)
clear(sensor)
SENSOR_1/2/3
TYPE_SWITCH/LIGHT/TEMP/ROT
MODE_BOOLEAN/PERCENT/RAW...

# display_rcx
from display_rcx import *
show_sensor(id)
show_timer(id)
show_variable(id)
show_clock()

# system_rcx
from system_rcx import *
ping() / power_off()
stop_all()
set_time(h, m)
set_range(long_range)
set_power_down(minutes)
set_message(value)
clear_timer(id)
                
Open App
IR pin default
Carrier = pin 2 (38 kHz)
Change in RCX.__init__() if needed.