Skip to content

Firmware

We use a microcontroller unit (MCU) to act as a bridge between the robot body and the smartphone. We provide our firmware for the Arduino Nano with an ATmega328P microcontroller as well as for the ESP32 development kit.

Features

The main task of the MCU is to handle the low-level control of the vehicle and provide readings from low-level vehicle-mounted sensors. The MCU receives the vehicle controls and indicator signals via the serial connection. It converts the controls to PWM signals for the motor controller and toggles the LEDs according to the indicator signal. The Arduino program also keeps track of the wheel rotations by counting the interrupts of optical sensors on the left and right front wheels. It calculates the battery voltage by a scaled moving average of measurements at the voltage divider circuit. It can also measure the distance to obstacles in front of the car with an optional ultrasonic sensor. These measurements are sent back to the Android application through the serial link.

Setup

The easiest way is tools.openbot.org/firmware: choose your robot and its parts, then flash the firmware straight from the browser (Chrome or Edge on a computer), or download a settings file for the Arduino IDE. The page sets the battery, the parts and phone or no phone mode on the robot itself, so the ready-made firmware fits your robot without compiling. Later changes need no flashing: Update settings only sends them to the robot. On the ESP32, the ready-made firmware has every part built in that the body has pins for; the ones you don't use stay off.

Firmware setup page

To set up the firmware yourself: all settings are in the SETTINGS section at the top of openbot.ino. You don't need to change anything below it.

To keep your settings when you update the firmware, copy the lines of the SETTINGS section between #else and #endif into a new file openbot_settings.h next to openbot.ino, and change them there. If openbot_settings.h exists, the firmware uses it instead of the section in openbot.ino, and git ignores it, so it isn't pushed by mistake.

Robot body

Set OPENBOT to your robot body, and select a board it supports in the Arduino IDE. The firmware detects the board; with any other board the build stops with an error.

Body OPENBOT Arduino Nano ESP32 Dev Module
DIY build (L298N motor driver) DIY ✓ ✓
Custom PCB V1 / V2 PCB_V1 / PCB_V2 ✓
Ready-to-Run kit, TT motors RTR_TT ✓
Ready-to-Run kit, TT motors, ESP32 RTR_TT2 ✓
Ready-to-Run kit, 520 motors RTR_520 ✓
RC truck RC_CAR ✓
OpenBot Lite LITE ✓
Multi Terrain Vehicle MTV ✓

DIY_ESP32 has been replaced by DIY with an ESP32 board selected.

Driving

By default, you drive the robot with the phone app. For no phone mode, set NO_PHONE_MODE 1; with an ESP32 you can then also drive with a gamepad (HAS_GAMEPAD 1). COAST_MODE sets whether the motors coast (1) or brake (0) when there is no control, and DEBUG 1 prints debug output on the Serial Monitor. With the Bluetooth link to the phone, BLE_NAME sets the name the robot shows in the app, for example #define BLE_NAME "Team 3" (up to 20 letters, digits, spaces, - or _).

Parts

Each body turns on the parts it normally has:

OPENBOT Parts turned on
DIY, PCB_V1, PCB_V2 Voltage divider, indicators, sonar, front speed sensors
RTR_TT, RTR_TT2, RTR_520 Voltage divider, indicators, sonar, bumper, front and back speed sensors, front, back and status LEDs
LITE Indicators
RC_CAR None
MTV Front, middle and back speed sensors

On an ESP32, the Bluetooth link to the phone is also on.

In the settings, every part is set to the body's default, e.g. #define HAS_SONAR DEFAULT_HAS_SONAR, and a table above them shows each body's defaults. To change a part, set it to 1 (on) or 0 (off) instead, e.g. #define HAS_SONAR 0 if you haven't installed the ultrasonic sensor. A part that is on but not installed does no harm, but the app shows readings for it that make no sense, such as a sonar distance of 300 cm.

Setting Part
HAS_VOLTAGE_DIVIDER Battery voltage measurement
HAS_INDICATORS Indicator lights
HAS_SONAR, SONAR_MEDIAN Ultrasonic sensor, and a median filter for its readings
HAS_BUMPER Bumper to detect collisions
HAS_SPEED_SENSORS_FRONT, _BACK, _MIDDLE Wheel speed sensors
HAS_LEDS_FRONT, HAS_LEDS_BACK, HAS_LEDS_STATUS Front lights, rear lights and status LEDs
HAS_OLED OLED display
HAS_BLUETOOTH Bluetooth link to the phone (ESP32)

Battery

The app's battery gauge runs from VOLTAGE_LOW (empty) to VOLTAGE_MAX (full). They are set to the body's defaults; for a different battery, set them in the settings, for example #define VOLTAGE_LOW 6.8f:

Battery VOLTAGE_LOW VOLTAGE_MAX
2S Li-ion / LiPo 6.8f 8.4f
3S Li-ion / LiPo 10.2f 12.6f
6 × AA 6.0f 9.0f
8 × AA 8.0f 12.0f

If your voltage divider differs from the body's, also set VOLTAGE_DIVIDER_FACTOR to (R1 + R2) / R2. VOLTAGE_MIN is the lowest voltage at which the motors still turn; the firmware only uses it in no phone mode.

The robot stops responding at full speed. At high speed, and especially when starting on the floor, the motors draw a lot of current. With weak or alkaline AA batteries the voltage can dip so low that the board restarts and the app or gamepad loses its connection. Use fresh or rechargeable batteries (NiMH or Li-ion), or drive in a slower speed mode. A gamepad may then need pairing again.

Check the configuration

When the robot starts, the Serial Monitor (115200 baud) shows the body and the parts that are on, for example:

OpenBot DIY (ESP32): voltage, indicators, sonar, speed sensors front, Bluetooth
OpenBot settings: openbot_settings.h

The second line shows where the settings came from: openbot_settings.h, or openbot.ino if there is no settings file.

If two parts use the same pin, the build stops with the error "Two parts use the same pin".

Dependencies

On the Arduino Nano, the speed sensors and the ultrasonic sensor need the PinChangeInterrupt library; most bodies have them on by default. The Arduino Nano only has two external interrupt pins (D2 and D3) and D3 is also one of only six pins that support PWM. Fortunately, it also has three port interrupts that cover all pins on the Arduino. This library parses these port interrupts allowing all pins of the Arduino to be used as interrupts.

If you have enabled the OLED, you need to install the libraries Adafruit_SSD1306 and Adafruit_GFX Library.

You can install libraries by following these steps:

  1. Open the Library Manager: Tools :arrow_right: Manage Libraries
  2. Enter the name of the library in the search bar.
  3. Select the latest version and click install. If you have already installed the library it will show and you may be able to update it.

You may need to download the WCH340 drivers from the chip manufacturer (Chinese):

ESP32 development kit

To install the ESP32 board in your Arduino IDE, follow these next instructions:

  1. In your Arduino IDE, go to File> Preferences:

App GUI

  1. Enter https://dl.espressif.com/dl/package_esp32_index.json into the “Additional Board Manager URLs” field as shown in the figure below. Then, click the “OK” button:

App GUI

Note: if you already have the ESP8266 boards URL, you can separate the URLs with a comma as follows:

https://dl.espressif.com/dl/package_esp32_index.json,
http://arduino.esp8266.com/stable/package_esp8266com_index.json
  1. Open the Boards Manager. Go to Tools > Board > Boards Manager:

App GUI

  1. Search for ESP32. Look for “ESP32 by Espressif Systems“ and make sure to select version 2.0.17 from the drop-down. Any version 3.x.x will not work! Click on install:

App GUI

  1. You should now have everything to successfully flash the ESP32 board of your OpenBot using the Arduino development environment.

App GUI

  1. To flash the OpenBot with your new code, simply select ESP32 Dev Module in the menu Tools > Board > ESP32 Arduino. Note that additional content as well as troubleshooting of the ESP32 flashing prcess can be found in the following link.

App GUI

If you have trouble uploading try reducing the baud rate. Go to tools and select the upload speed as 115200.

App GUI

Upload

Settings (Arduino nano setup)

  • Tools :arrow_right: Board :arrow_right: Arduino AVR Boards :arrow_right: Arduino Nano
  • Tools :arrow_right: Processor :arrow_right: ATmega328P (Old Bootloader)
  • Tools :arrow_right: Port :arrow_right: *Select the USB port*

:memo: NOTE: Currently, most cheap Arduino Nano boards come with the Old Bootloader. However, depending on the seller you may also get one with the new bootloader. So if you are unable to upload the firmware, chances are that you need to change the processor to ATmega328P.

Settings (ESP32 setup)

  • Tools :arrow_right: Board :arrow_right: ESP32 Arduino :arrow_right: ESP32 Dev Module
  • Tools :arrow_right: Port :arrow_right: *Select the USB port*

Uploading the firmware

The firmware can now be uploaded through Sketch :arrow_right: Upload or by pressing the upload button (right arrow). Firmware Upload

Testing

This section explains how to test all functionalities of the car after the firmware was flashed successfully.

  1. Confirm that:
  2. the wheels are not connected to the car
  3. the Arduino is connected to the computer
  4. the correct USB port is selected
  5. Open the Serial Monitor: Tools :arrow_right: Serial Monitor

Sending messages to the OpenBot

You can also send messages to the Arduino by typing a command into the input field on the top and then pressing send. The following commands are available (provided the necessary features are supported by the robot):

  • c<left>,<right> where <left> and <right> are both in the range [-255,255]. A value of 0 will stop the motors. A value of 255 applies the maximum voltage driving the motors at the full speed forward. Lower values lead to proportionally lower voltages and speeds. Negative values apply the corresponding voltages in reverse polarity driving the motors in reverse.
  • i<left>,<right> where <left> and <right> are both in the range [0,1] and correspond to the left and right indicator LEDs. For example, i1,0 turns on the left indicator, i0,1 the right indicator and i1,1 both indicators. Enabled indicator lights will flash once per second. A value of i0,0 turns the indicators off. Only one state at a time is possible.
  • l<front>,<back> where <front> and <back> are both in the range [0,255] and correspond to the brightness of the front and back LEDs.
  • s<time_ms> where <time_ms> corresponds to the time in ms between sonar measurements triggered (default = 1000). After the sonar reading is aquired the message is sent to the robot. If it times out, the specified MAX_SONAR_DISTANCE is sent.
  • w<time_ms> where <time_ms> corresponds to the time in ms between wheel odometry measurements sent to the robot (default = 1000). The wheel speed is monitored continuously and and the rpm is computed as average over the specified time interval.
  • v<time_ms> where <time_ms> corresponds to the time in ms between voltage measurements sent to the robot (default = 1000). The voltage is monitored continuously and filtered via a moving average filter of size 10. In addition to setting the time interval for voltage readings, sending this command will also trigger messages that report the minimum voltage to drive the motors (vmin:<value>), minimum battery voltage (vlow:<value>) and maximum battery vollage (vmax:<value>).
  • h<time_ms> where <time_ms> corresponds to the time in ms after which the robot will stop if no new heartbeat message was received (default = -1).
  • b<time_ms> where <time_ms> corresponds to the time in ms after which the bumper trigger will be reset (default = 750).
  • n<color>,<state> where <color> corresponds to a status LED (b = blue, g = green, y = yellow) and state to its value (0 = off, 1 = on).
  • f will send a request to the OpenBot to return a message with the robot type and its features, e.g. voltage measurement (v), indicators (i), sonar (s), bump sensors (b), wheel odometry (wf, wb), LEDs (lf, lb, ls), etc. For example, for the RTR_V1 version of OpenBot the message would look like this: fRTR_V1:v:i:s:b:wf:wb:lf:lb:ls:.

Receiving messages from the OpenBot

Depending on your configuration you may see different messages.

Serial Monitor

  • Messages that start with v report the battery voltage. If you connect the battery to the car (i.e. turn on the switch), it should show the battery voltage. If you disconnect the battery (i.e. turn off the switch), it should show a small value.
  • Messages that start with w report readings of the speed sensors measured in revolutions per second (rpm). Each hole in the encoder disk will increment a counter by plus/minus one depending on the direction. You can set the number of holes with the parameter DISK_HOLES. If you are using the standard disk with 20 holes, there will be 20 counts for each revolution of the wheel.
  • Messages that start with s report the estimated free space in front of the ultrasonic sensor in cm.
  • Messages that start with b report collisions. The codes lf (left front), rf (right front), cf (center front), lb (left back), rb (right back) indicate which sensor triggered the collision.

Test procedure

Before you proceed, make sure the tires are removed. You will need the Serial Monitor open to send commands and you will see the messages received from your OpenBot. If you have the OLED display installed, you will also see the vehicle status displayed there in a more human-readable format. The following test procedure can be used to test all functionalities of the car:

  1. Turn on the car and observe the battery voltage (the number after the v). You can verify the reading with a multimeter and adjust the VOLTAGE_DIVIDER_FACTOR if necessary.
  2. If you have an ultrasonic sensor installed:
  3. Hold your hand in front of the sensor and move it back and forth. You should see the readings (the number after the s) change correspondingly.
  4. We have observed that the ultrasonic sensor is very sensitive to vibrations! So it is advisable to make sure you will get reliable readings during operation by performing the following test:
    1. Place the OpenBot with the ultrasonic sensor installed such that there is at least 200cm of free space in front of it. You should see a reading of 200 or more.
    2. Observe the readings on the serial monitor for some time and then enter the command c128,128.
    3. If the sensor readings change significantly, you will need to dampen the vibrations transmitted to the ultrasonic sensor from the chassis (e.g. add some silicon, adjust the mounting position).
  5. If you have the speed sensors installed:
  6. Make sure, you have plenty of free space in front of the ultrasonic sensor. The reading (the number after the s) needs to be at least above the STOP_DISTANCE which is 10 by default.
  7. Send the command c128,128. The motors will start spinning at slow speed (50% PWM). The speed sensor readings (values after the w) are reported in rpm and should be between 250 and 300 for the RTR_TT version depending on the SOC of the battery. If you are using the DIY version or a weaker battery, values may be lower. Check that all motors are spinning forward and that the speed sensor readings are positive.
  8. Try sending different controls and observe the speed sensor readings. For example, the command c-128,-128 will spin all motors backward at slow speed (50% PWM). The command c255,-255 will spin the left motors forward and the right motors backward at fast speed (100% PWM). The command c-192,192 will spin the left motors backward and the right motors forward at normal speed (75% PWM).
  9. Stop the motors by sending the command c0,0 or by holding your hand in front of the ultrasonic sensor
  10. If you have the indicator LEDs installed, send the command i1,0 and observe the left indicator light flashing. The send the command i0,1 and observe the right indicator light flashing. Finally, turn the indicator off by sending the command i0,0.

No Phone Mode

Before testing the car with a smartphone that has the OpenBot application installed, you can also test the car without a phone first. Simply set NO_PHONE_MODE 1 in the settings. The car will now drive at normal_speed (75% PWM) and slow down as it detects obstacles with the ultrasonic sensor. Once it gets close to the TURN_THRESHOLD (default: 50cm), it will start turning in a random direction and turn on the LED on that side. If the estimated free space in front of the car falls below the TURN_THRESHOLD, it will slowly go backwards and both LEDs will turn on. Note that both the car and the Arduino need to be powered. The Arduino can be powered by connecting the 5V pin to the 5V output of the L298N motor driver, or by connecting the USB cable to a power source (e.g. phone).

Before running the car, we recommend to remove the tires, connect the Arduino to a computer and observe the serial monitor like in the section Testing. The output on the serial monitor is a bit easier to parse (same as OLED) and shows the battery voltage, the rpm for the left and right motors and the estimated free space in front of the car. You can move a large object back and forth in front of ultrasonic sensor and observe the speed of the motors changing.

:warning: WARNING: If you do not have an ultrasonic sensor installed or if it is disabled, the car will just drive forward at normal_speed (75% PWM) and will eventually collide. Even with the sensor installed, the car may collide occasionally due to noisy readings.

Gamepad control

In no phone mode, you can drive an ESP32 body with a Bluetooth gamepad instead of letting it drive by itself.

Setup

  1. In the Arduino IDE, add this URL under Preferences > Additional boards manager URLs: https://raw.githubusercontent.com/ricardoquesada/esp32-arduino-lib-builder/master/bluepad32_files/package_esp32_bluepad32_index.json
  2. In the Boards Manager, install esp32_bluepad32 (4.1.0). It is based on the same esp32 core (2.0.17) as the regular firmware.
  3. Select ESP32 Dev Module from the esp32_bluepad32 boards.
  4. In the settings, set NO_PHONE_MODE 1 and HAS_GAMEPAD 1, and upload.

The gamepad uses the ESP32's Bluetooth, so the Bluetooth link to the phone is off in this mode. To drive with the phone app, keep HAS_GAMEPAD 0 and pair the gamepad to the phone instead.

Pairing

Power on the robot and put the gamepad in pairing mode. It connects within a few seconds, and from then on it reconnects whenever you turn it on. To pair a different gamepad, turn the connected one off first: only one gamepad drives at a time.

Gamepad Pairing mode
PS5 DualSense Hold Create, then hold PS until the light bar blinks.
PS4 DualShock 4 Hold Share, then hold PS until the light bar flashes.
Xbox Wireless (model 1708 or newer) Turn it on, then hold the pair button for 3 s. Update the controller firmware to 5.15 or newer first (e.g. with the Xbox Accessories app).
Switch Pro, Joy-Con, 8BitDo and others Use the gamepad's Bluetooth pairing mode (see its manual).

The Bluepad32 list of supported gamepads has more models, including the PS3 DualShock 3, which needs extra setup. With DEBUG 1, the Serial Monitor shows the name of each gamepad that connects.

If a gamepad that was paired before doesn't take over, for example a PS4 or PS5 controller whose light bar stays blue instead of turning green, orange or red, put it in pairing mode again and restart the robot.

If it still doesn't connect, tick Forget paired gamepads when flashing on the setup page. The robot then forgets all gamepads it was paired with.

Controls

Buttons are named PlayStation / Xbox.

Control Action
R2 / L2 Forward / backward (game mode)
Left stick Steer (game mode), drive and steer (joystick mode), or left side (dual mode)
Right stick Right side (dual mode)
L1 Next drive mode: game → joystick → dual. The gamepad rumbles once: short for game, longer for joystick, longest for dual.
D-pad up / R3 Faster
D-pad down / L3 Slower
Square / X Left indicator on or off
Circle / B Right indicator on or off
Triangle / Y Indicators off
Cross / A Lights on or off
Start Explore on or off
Select / Back Obstacle stop on or off

For Start and Select, a long rumble means on and a short one means off.

Drive modes. The gamepad starts in the mode set by GAMEPAD_DRIVE_MODE (default: game). - Game: R2 drives forward, L2 backward, and the left stick steers. Steering is mirrored when reversing, like a car. - Joystick: the left stick drives and steers. The robot always turns the way the stick points, also when reversing. - Dual: each stick drives one side, like a tank.

Speeds. There are three speeds: slow (50 %), normal (75 %, the start speed) and fast (100 %). The gamepad shows the speed with its player LEDs (one to three) or its light bar (green, orange, red).

Explore. Explore is the no phone mode driving described above: the robot drives around by itself and steers around obstacles. It is off when the robot starts; press Start to turn it on. While explore is on, moving a stick or trigger takes over. When you let go, the robot stops briefly and explore continues. Explore needs a connected sonar sensor with HAS_SONAR 1; without it, the robot just drives straight ahead (see the warning above).

Obstacle stop. While you drive with the gamepad, the robot won't drive forward when the sonar sees an obstacle closer than STOP_DISTANCE. It is on when the robot starts; press Select to turn it off, for example to push something. It only works with a connected sonar sensor and HAS_SONAR 1; otherwise the robot never stops for obstacles.

Safety. The robot stops when the gamepad disconnects. Most gamepads also send data continuously, and if such a gamepad goes silent for 0.5 s (out of range, battery empty), the robot stops right away instead of waiting for the disconnect.

Using other MCUs (requirements)

You can use any other MCU with the following features:

  • 1x USB-to-TTL Serial (communication with the smartphone)
  • 4x PWM output (control the motors)
  • 1x analog pin for battery monitoring
  • 2x digital pin for the speed sensors
  • 1x digital pin for the ultrasonic sensor (optional)
  • 2x digital pin for the indicator LEDs (optional)

Next

Compile and run the Android App