BB2B version 2

Breitenberg-2B with ultrasonic sensors and Arduino

This robot uses a microcontroller to process the data from the sensors and drive the motors. This enables us to use more sophisticated sensors and control. In this case we uses ultrasonic sensors to detect object ahead of the robot. This should make the robot avoid objects before colliding with them.

However, as the beam of ultrasonic sensors is quite small, it is possible that the robot still collides with objects 'between the eyes'. It is left as an exercise for the reader to add another sensor in the middle of the front. 

Alternatively, you can move ahead and look at version 3 of BB2B or look at the more advanced DD2D robot.


(italics components can be re-used from BB2B version 1)

  • 1 2WD car chassis
  • 2 TT motors
  • 1 4x AA battery pack
  • 1 (mini) breadboard
  • 1 TB6612 motor controller
  • 1 Arduino Uno
  • 2 HC-SR04 ultrasonic depth sensors
  • (optional) HC-SR04 mounts on Thingiverse

The robot can run on a 4xAA (4.8v) NiMH battery pack, but that may be convenient for testing :-). Running on a 2-cell 7.2v Li-Ion pack will make it much faster. The use of a 4xAA (6v) Alkaline battery pack has not been tested. When not using NiMH battery pack, make sure you use a voltage regulator (see below).

Instead of an Arduino Uno, most other 5 volt microcontrollers will work. But if you choose to use a 7.4 volt Li-Ion battery pack, the Uno and Mega can run straight from the battery pack using the barrel jack. Also, a Node-MCU (ESP8266) will also be able to run off the battery pack when the Vin pin is used.

Besides the TB6612 driver, an DRV8833 or L293D driver can be used too, which are less efficient but work as well.




All code can be found in our GitLab project.

How it works

The microcontroller code works similar to the BB2B version 1 setup: the left sensor updates the right motor speed and the right sensor updates the left motor speed.

void loop() {
  // update the motors, the distance return value is not used here but can be
  // used in a Serial.println for debugging purposes
  update(sensorLeft, motorRight);
  update(sensorRight, motorLeft);


In the update, you can see that the motor reverses when the distance to the closest object detected by the ultrasonic sensor is small than 0.3 meter.

float update(UltrasonicSensor sensor, Motor motor) {
  // Determine distance
  float distanceLeft = sensor.GetDistance();
  if (distanceLeft > 0) {
    // Determine motor turning direction
    Motor::Direction directionRight = distanceLeft < 0.3
                                          ? Motor::Direction::Reverse
                                          : Motor::Direction::Forward;
    // Set motor direction

  // return the distance for debugging support
  return distanceLeft;

The rest of the code consists of two classes to encapsulate the technical implementation of  the sensors and motor controllers.

The pin assignments can be found just before the (empty) setup function.

Motor motorLeft = Motor(11,  // in1
                        12   // in2
Motor motorRight = Motor(9,  // in1
                         8   // in2

UltrasonicSensor sensorLeft = UltrasonicSensor(3,  // trigger
                                               18  // echo
UltrasonicSensor sensorRight = UltrasonicSensor(2,  // trigger
                                                19  // echo

Previous: basic version without a microcontrollerNext: combining sensors with neurons