Meet your sumobot.
Before you write your first program, get to know the robot you're coding for the Sumo arena — what it can feel around itself, how it moves, and which pin every part is wired to. Your code talks to the robot through these exact pins.
The wiring is fixed. The sensors and motors are joined to set pins — your code must use these same pin numbers. (This page describes the robot used in the Sumo arena.)
Main board: ESP32 Motor driver: L298N
How it moves
The simulator uses an L298N motor driver. The robot has four motors, but you don't control them one by one. The two left motors are joined together and move as one, and the two right motors are joined together and move as one. So you drive the robot by side: a left side and a right side.
Any other driver that uses the same control method — two direction pins plus one PWM speed pin per side — works the same way with no code changes (for example, the TB6612FNG). Just wire it to the same pins.
Each side uses three pins: two direction pins (forward and backward) and one speed pin (PWM, a number from 0 to 255).
| Side (drives 2 motors) | Forward | Backward | Speed (PWM) |
|---|---|---|---|
| Right | 18 | 19 | 21 |
| Left | 17 | 5 | 16 |
To move a side, set one direction pin HIGH, the other LOW, and pick a speed. Here are small helper functions you can copy and use:
void motor_right_forward(uint8_t speed) {
analogWrite(PIN_MOTOR_RIGHT_PWM, speed);
digitalWrite(PIN_MOTOR_RIGHT_FWD, HIGH);
digitalWrite(PIN_MOTOR_RIGHT_BWD, LOW);
}
void motor_right_backward(uint8_t speed) {
analogWrite(PIN_MOTOR_RIGHT_PWM, speed);
digitalWrite(PIN_MOTOR_RIGHT_FWD, LOW);
digitalWrite(PIN_MOTOR_RIGHT_BWD, HIGH);
}
void motor_right_stop() {
analogWrite(PIN_MOTOR_RIGHT_PWM, 0);
digitalWrite(PIN_MOTOR_RIGHT_FWD, LOW);
digitalWrite(PIN_MOTOR_RIGHT_BWD, LOW);
}
void motor_left_forward(uint8_t speed) {
analogWrite(PIN_MOTOR_LEFT_PWM, speed);
digitalWrite(PIN_MOTOR_LEFT_FWD, HIGH);
digitalWrite(PIN_MOTOR_LEFT_BWD, LOW);
}
void motor_left_backward(uint8_t speed) {
analogWrite(PIN_MOTOR_LEFT_PWM, speed);
digitalWrite(PIN_MOTOR_LEFT_FWD, LOW);
digitalWrite(PIN_MOTOR_LEFT_BWD, HIGH);
}
void motor_left_stop() {
analogWrite(PIN_MOTOR_LEFT_PWM, 0);
digitalWrite(PIN_MOTOR_LEFT_FWD, LOW);
digitalWrite(PIN_MOTOR_LEFT_BWD, LOW);
}
- To go forward: forward pin
HIGH, backward pinLOW. To go back, swap them. - Speed
0is stopped,255is full speed. - To turn, run one side forward and the other side slower, stopped, or backward.
What it can feel
The robot has two kinds of sensors. Both work the same way: a pin reads LOW when the sensor finds something, and HIGH when there is nothing.
Edge sensors (find the edge of the arena)
The robot has four sensors that look down at the floor. They tell you when the robot is close to the edge of the arena, so it doesn't drive out. Each one reads LOW at the edge and HIGH on the open floor.
| Where it is | Pin | At the edge when… |
|---|---|---|
| Front-right | 39 | digitalRead(39) == LOW |
| Front-left | 34 | digitalRead(34) == LOW |
| Back-right | 22 | digitalRead(22) == LOW |
| Back-left | 23 | digitalRead(23) == LOW |
if (digitalRead(39) == LOW) {
// the front-right sensor is at the edge — back away!
}
Front sensors (find the enemy)
The robot has three sensors on the front that look for the enemy in front of it. They work the same way: LOW means an enemy is there, HIGH means the way is clear.
| Where it is | Pin | Enemy found when… |
|---|---|---|
| Front-left | 32 | digitalRead(32) == LOW |
| Front-center | 33 | digitalRead(33) == LOW |
| Front-right | 35 | digitalRead(35) == LOW |
Remember: an enemy in front is LOW, not HIGH. If you read it the wrong way, your robot will run away from the enemy.
All pins in one place
Copy these to the top of your program to use easy pin names:
// Edge sensors (LOW = at the edge)
#define PIN_IR_FRONT_RIGHT 39
#define PIN_IR_FRONT_LEFT 34
#define PIN_IR_BACK_RIGHT 22
#define PIN_IR_BACK_LEFT 23
// Front sensors / enemy (LOW = enemy ahead)
#define PIN_PROX_FRONT_RIGHT 35
#define PIN_PROX_FRONT_CENTER 33
#define PIN_PROX_FRONT_LEFT 32
// Motors — one set of pins per SIDE (FWD, BWD, PWM)
#define PIN_MOTOR_RIGHT_FWD 18
#define PIN_MOTOR_RIGHT_BWD 19
#define PIN_MOTOR_RIGHT_PWM 21
#define PIN_MOTOR_LEFT_FWD 17
#define PIN_MOTOR_LEFT_BWD 5
#define PIN_MOTOR_LEFT_PWM 16
Your first sketch — and how to run it
Every new account starts with the sketch below. It already knows the robot's pins and gives you ready-made helpers to move each side and read every sensor. It does not fight yet — on purpose. It only shows the two things you need: how to move and how to read the sensors. The strategy is yours to write.
Run it in four steps:
- Open the IDE — this exact sketch is already loaded.
- Hit Build & Run. Your robot appears in the arena.
- Open the Serial monitor — you'll see the front-sensor readings printing live as an opponent moves in front of the bot.
- Uncomment the two
motor_*_forward(255)lines insideloop(), run again, and watch it drive.
From there, fill in the YOUR STRATEGY GOES HERE block: read the sensors, decide, and call the motor helpers.
#include <Arduino.h>
// ---------------------------------------------------------------------------
// Sumobot starter — the basics only.
//
// This sketch does NOT fight yet. On purpose. It gives you the two building
// blocks and nothing more:
// 1. how to MOVE the robot -> the motor_* helpers at the bottom
// 2. how to READ the sensors -> in loop(), printed to the Serial monitor
//
// The actual strategy — when to charge, when to back off, how to turn — is
// yours to write. Look for "YOUR STRATEGY GOES HERE" in loop().
// ---------------------------------------------------------------------------
// ---- Pinout (from the "Know Your Robot" docs) -----------------------------
// Edge sensors (LOW = that corner is over the white edge)
#define PIN_IR_FRONT_RIGHT 39
#define PIN_IR_FRONT_LEFT 34
#define PIN_IR_BACK_RIGHT 22
#define PIN_IR_BACK_LEFT 23
// Front sensors (LOW = enemy ahead)
#define PIN_PROX_FRONT_RIGHT 35
#define PIN_PROX_FRONT_CENTER 33
#define PIN_PROX_FRONT_LEFT 32
// Motors — one set of pins per SIDE (FWD, BWD, PWM speed 0-255)
#define PIN_MOTOR_RIGHT_FWD 18
#define PIN_MOTOR_RIGHT_BWD 19
#define PIN_MOTOR_RIGHT_PWM 21
#define PIN_MOTOR_LEFT_FWD 17
#define PIN_MOTOR_LEFT_BWD 5
#define PIN_MOTOR_LEFT_PWM 16
// ---- Function declarations (defined at the bottom of the file) ------------
void motor_driver_init();
void motor_right_forward (uint8_t speed);
void motor_right_backward(uint8_t speed);
void motor_right_stop ();
void motor_left_forward (uint8_t speed);
void motor_left_backward (uint8_t speed);
void motor_left_stop ();
void sensors_init();
void setup() {
Serial.begin(115200);
motor_driver_init();
sensors_init();
Serial.println("Start Simulator");
}
void loop() {
// --- 1. MOVE -----------------------------------------------------------
// Uncomment these two lines to drive both sides forward at full speed,
// then comment them back out. Speed is 0 (stop) to 255 (full).
// motor_left_forward(255);
// motor_right_forward(255);
// --- 2. READ THE FRONT SENSORS (find the enemy) ------------------------
// LOW = enemy in front of that sensor, HIGH = clear.
int enemy_right = digitalRead(PIN_PROX_FRONT_RIGHT);
int enemy_center = digitalRead(PIN_PROX_FRONT_CENTER);
int enemy_left = digitalRead(PIN_PROX_FRONT_LEFT);
Serial.print("[ENEMY] FR:"); Serial.print(enemy_right);
Serial.print(" FC:"); Serial.print(enemy_center);
Serial.print(" FL:"); Serial.println(enemy_left);
// --- 3. READ THE EDGE SENSORS (stay in the arena) ----------------------
// LOW = that corner is over the edge, HIGH = on the floor.
int edge_front_right = digitalRead(PIN_IR_FRONT_RIGHT);
int edge_front_left = digitalRead(PIN_IR_FRONT_LEFT);
int edge_back_right = digitalRead(PIN_IR_BACK_RIGHT);
int edge_back_left = digitalRead(PIN_IR_BACK_LEFT);
// Uncomment to also print the edge sensors:
// Serial.print("[EDGE] FR:"); Serial.print(edge_front_right);
// Serial.print(" FL:"); Serial.print(edge_front_left);
// Serial.print(" BR:"); Serial.print(edge_back_right);
// Serial.print(" BL:"); Serial.println(edge_back_left);
// --- 4. YOUR STRATEGY GOES HERE ----------------------------------------
// Use the readings above to decide how to move. For example:
// * an enemy in front -> charge forward
// * an edge sensor LOW -> back away and turn
// Combine the motor helpers below to make it happen.
delay(100);
}
// ---- Motor helpers --------------------------------------------------------
void motor_driver_init() {
pinMode(PIN_MOTOR_RIGHT_FWD, OUTPUT);
pinMode(PIN_MOTOR_RIGHT_BWD, OUTPUT);
pinMode(PIN_MOTOR_RIGHT_PWM, OUTPUT);
pinMode(PIN_MOTOR_LEFT_FWD, OUTPUT);
pinMode(PIN_MOTOR_LEFT_BWD, OUTPUT);
pinMode(PIN_MOTOR_LEFT_PWM, OUTPUT);
}
void motor_right_forward(uint8_t speed) {
analogWrite(PIN_MOTOR_RIGHT_PWM, speed);
digitalWrite(PIN_MOTOR_RIGHT_FWD, HIGH);
digitalWrite(PIN_MOTOR_RIGHT_BWD, LOW);
}
void motor_right_backward(uint8_t speed) {
analogWrite(PIN_MOTOR_RIGHT_PWM, speed);
digitalWrite(PIN_MOTOR_RIGHT_FWD, LOW);
digitalWrite(PIN_MOTOR_RIGHT_BWD, HIGH);
}
void motor_right_stop() {
analogWrite(PIN_MOTOR_RIGHT_PWM, 0);
digitalWrite(PIN_MOTOR_RIGHT_FWD, LOW);
digitalWrite(PIN_MOTOR_RIGHT_BWD, LOW);
}
void motor_left_forward(uint8_t speed) {
analogWrite(PIN_MOTOR_LEFT_PWM, speed);
digitalWrite(PIN_MOTOR_LEFT_FWD, HIGH);
digitalWrite(PIN_MOTOR_LEFT_BWD, LOW);
}
void motor_left_backward(uint8_t speed) {
analogWrite(PIN_MOTOR_LEFT_PWM, speed);
digitalWrite(PIN_MOTOR_LEFT_FWD, LOW);
digitalWrite(PIN_MOTOR_LEFT_BWD, HIGH);
}
void motor_left_stop() {
analogWrite(PIN_MOTOR_LEFT_PWM, 0);
digitalWrite(PIN_MOTOR_LEFT_FWD, LOW);
digitalWrite(PIN_MOTOR_LEFT_BWD, LOW);
}
// ---- Sensor setup ---------------------------------------------------------
void sensors_init() {
pinMode(PIN_IR_FRONT_LEFT, INPUT);
pinMode(PIN_IR_FRONT_RIGHT, INPUT);
pinMode(PIN_IR_BACK_LEFT, INPUT);
pinMode(PIN_IR_BACK_RIGHT, INPUT);
pinMode(PIN_PROX_FRONT_CENTER, INPUT);
pinMode(PIN_PROX_FRONT_LEFT, INPUT);
pinMode(PIN_PROX_FRONT_RIGHT, INPUT);
}