Everyone dreams of building their own robot. With the Dwengo board, some Lego™ and this tutorial, this becomes easy. Step by step we'll show you how to build a robot that can autonomously drive. Combining this tutorial with a basic explanation on some different kinds of sensors and your own imagination, you can extend your robot into a line-follower, a light-eating robot or even an advanced sumobot.


  1. One Dwenguino board
  2. Enclosed USB cable
  3. Two DC motors for example this Lego NXT motor
  4. Some basic mechanical parts to build a robot

Driving DC-motors using PWM

All devices that operate on direct current or DC require a constant input voltage. This includes several components such as Lego motors and LEDs. The voltage over these components is typically specified in a certain range but can often be reduced. In the case of LEDs a lower voltage results in less light output, while a motor will turn slower.

When a Pulse Width Modulation (PWM) signal with very high frequency is generated, the motor will experience this as a constant voltage. By changing the pulse width or duty cycle of the PWM signal, the amplitude of the average voltage can be controlled. This will in turn speed up or slow down the motor. The same concept can be applied for dimming LEDs. Although to be exact: an LED has a lower capacitance and will therefore do see the individual pulses, and thus turn on and off at high frequency. Our eyes are too slow to see this though, so we will experience a gradual change in brightness.

PWM control of a DC motor

Connecting a DC motor

When we take a close look at the Dwenguino Board, we can see a (blue) connector to connect two motors:
dwenguino motor connector

The schematics of the Dwenguino Board show that these motor are connected to the motor driver. This is a component that converts the PWM signal from the microcontroller into signals that are suitable for driving the motor. This is necessary because motors require a much higher current than what the microcontroller can deliver directly, and often the voltage driving the motor needs to be higher than the +5V that is powering the PIC controller. In the case of the Dwenguino, the motor driver is directly connected to the Dwenguino's input voltage.

A square driving robot

We start by loading the libraries. Apart from the standard libraries we include the DwenguinoMotor library which is responsible for controlling standard DC motors.

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Dwenguino.h>
#include <DwenguinoMotor.h> // the motor library

Next we create two objects called dcMotor1 and dcMotor2 of the class DCMotor. These objects represent the DC motors that we want to control. We attach pins MOTOR_1_0 and MOTOR_1_1 to dcMotor1 and MOTOR_2_0 and MOTOR_2_1 to dcMotor2. Have a look to the summarising Dwenguino pin layout table to see to which digital pins they are connected.

DCMotor dcMotor1(MOTOR_1_0, MOTOR_1_1);
DCMotor dcMotor2(MOTOR_2_0, MOTOR_2_1);

The setup function is easy, we only need to initialise the standard Dwenguino functionality:

void setup() {

Now we can start controlling each motor object by using the function setSpeed The argument is a number which needs to be between -255 and 255. While the sign determines the rotation direction (clock wise or counter clockwise), the value determines the applied voltage to the motor (and thus its rotation speed). Note that the maximal speed depends on the applied battery/adapter voltage through the power connector of the Dwenguino board and the maximal power the motor can have. In the case of the video we used 9.6V battery pack and 6V motors. Consequently we do not set the speed the 255, which would mean that the motors get 9.6V instead of the maximal 6V, but we apply 150 which would result into an applied voltage of 5.6V (9.6V*150/255):

void loop() {

The above code makes our robot following a square. Adapt the speed and the delays to make your robot following a square! The entire program can be found in File > Examples > Dwenguino > SquareServo

Building your robot

While you can start from our robot kit it is much more fun to build your own robot. This can be done quickly and easily with for example the Lego™ parts you may have at home. Apart from the Lego™ technic or Lego™ mindstorms parts, these components are essential:

  • Two DC motors: for instance two Power Functions XL-Motors
  • A battery holder with an output voltage higher than 7V (and smaller than 15 V) which you can connect to the Dwengo Board

If you don't have enough Lego parts, you can order them separately at for instance BrickLink, or try to find a good deal on eBay.

Making your robot intelligent

By adding sensors you can make your robot sense its environment. Try for example to make a light eating robot by using two phototransistors or a maze runner with the sonar sensors. Enjoy!

Related Post