Roach v2

Work, Work, Work :(. Its been a whole year since i started working on the original Roach robot LINK, with one thing or the other, work was put on hold, but summer is here so the adventure can now continues. Again, the aim of this work is to develop a cheap microBit robot for schools, as part of CAS. Been trying to decide which way to go with this robot i.e. buy off the shelf components or DIY (build the PCBs in house). Couldn't decide so built both, off the shelf first, as i had the parts.

Table of Contents

Version 2.0 : BBC Micro:Bit + off the shelf boards
chassis & drive train
micro-python test code

Version 2.0 : BBC Micro:Bit + off the shelf boards

Advantage of buying in ready made PCBs is its normally a bit cheaper (economy of scale). The down side is this can result in a slightly bigger robots, a lot more wiring and there is always the danger that the boards will reach end-of-line and can no longer be obtained. Searching Amazon and Ebay picked out some standard hobbyist boards to make version 2.0 of the cockroach: motor driver board, boost converter, +3V regulator and a small bread board.

The motor driver is a L298N, a dual H-bridge driver that can be used to control either a single stepper motor or two DC motors, as shown in figure 1. Reading the data-sheet LINK this driver is a little overkill, but its always good to over-spec the driver ICs i.e. just in case you need a little more power (robots always need more drive power than you first guesstimate). This PCB/module also has a built in +5V regulator which can be used to power this driver's internal logic circuits. I therefore decided to use this regulator to also power the robot's interface circuit and +3V regulator for the microBit. The downside of this is that you need at least a +7.2V battery pack i.e. 6 AA cells (you always loose a couple of volts across these linear regs). Alternatively, you can use a boost converter LINK to step-up the voltage from a smaller battery pack up to a larger voltage. This has the advantage of reducing the battery pack size i.e. you can move to 4 AA cells, and therefore, reduces the robot's weight. Searching the joys of Amazon and Ebay, selected the XL6009 module, as shown in figure 2.

Figure 1 : L298 motor driver

Figure 2 : XL6009 boost converter module

This module's minimum voltage input is +3V, which gives some nice flexibility on battery selection i.e. can use NiCads, NiMH, or Alkaline batteries (4.2V - 5V). It may also extend battery life a little i.e. if a 7.2V battery pack was used to directly power the +5V regulator you would enter a brown-out region when the battery voltage drops to below 1.1V per cell, as shown in figure 3. By using the boost converter you can continue to operate beyond this point, for the 4.8V pack, to approximately 0.75V per cell. This may sound significant, but in reality the cliff edge is quite steep in this region of the discharge curve.

Figure 3 : NiMH discharge curve

The output voltage of the boost converter is set to +8V using the modules variable resistor i.e >+7V for the +5V linear reg on the motor driver board (the +8V also powers the motors, discussed later). In addition to these voltage regulators also needed a +3.3V regulator for the microBit. Again searching Amazon and Ebay, selected a board based on the AMS1117 LINK, as shown in figure 4. Could of powered this regulator from the +8V, but as it has a minimum input voltage of +4.8V, decided to power it from the +5V regulator i.e. reduce the voltage dropped across the regulator and therefore reduce its power dissipation. Back of matchbox calculations p=v*i : (8-3.3)*0.1= 0.5W or (5-3.3)*0.1=0.2W.

Figure 4 : AMS1117 regulator

To interface the microBit to the sensors on the robot i.e. 6 x bumper switches, 2 x LDR (light sensors), 1 x microphone (sound sensor) and 1 x ultrasonic (distance sensor), need a small amount of 'glue' circuitry. Normally this would be made on a PCB or Veroboard, however, as this robot is only using off the shelf boards decided to implement these circuits on a small bread board and a breakout board as shown in figure 5.

Figure 5 : BreadBoard (left), Breakout board (right)

The approximate cost of these board is :

  1. Bread board = pack of 2 = £5.50 = £2.75
  2. 3.3V regulator = pack of 5 = £2.99 = 60p
  3. Boost converters = pack of 5 = £7.50 = £1.50
  4. Motor drivers = pack of 5 = £11 = £2.20
  5. MicroBit breakout board = £2.50 = £2.50

Which comes in for approximately £10, not too bad, especially when you consider the reduced construction times i.e. limited soldering, only need to wire.

Chassis & Drive chain

For this version of the Roach moved away from the 'bug-like' chassis to minimise construction time i.e. the robot's chassis and drive chain will be made from off the shelf components, laser cut acrylic sheet and as always 3D printed elements, as shown in figures 6 and 7 (combined.dxf). I think these are the most up to date dxf files/versions, may need some drilling/adjusting etc. Used the same motor/gearbox as the Zergling robots, 200:1 reduction, as shown in figure 8, even though they have increased in price to £1.70, still very good value for money, good torque and speed for your typical general purpose robot. The datasheet for this motor states an input voltage range of 3V to 12V, to be honest i think this a little optimistic i.e. running at 12V for an extended period of time will burn out the brushes. On this robot the motors are powered from the +8V from the boost converter, you will loose 1.4V across the two drive transistors, which gives 6.6V, perhaps still a little high (normally go for 5V), but seems fine. The main chassis is made from 3mm acrylic sheet, the bottom layer contains the battery pack, motors, motor driver board and boost converter. Could of gone for a sealed 4.8V battery pack, but decided on 4 x AA, spring loaded box, as it allows a wider range of batteries to be used (we have a big bucket of old AA NiCads).

Figure 6 : Battery box

Figure 7 : Acrylic sheet

Figure 8 : Drive motor

The motor mounting bracket is a modified version of the Zergling robot's bracket, push fit, secured onto the base using two countersunk M3 bolts, tapped and glued, as shown in figure 9. The front of the battery box (figure 6) fits between the motors, snug fit, back supported by L bracket, again mounted on base using two M3 bolts tapped into the plasic. Wheels are constructed from three parts: top/bottom plate and a drive shaft spacer, these are super-glued together using drive shaft to centralise/align (brackets_wheels.stl). Tyre is a rubber O ring. Also shown in figure 9 is the rear skip, attached to base using M3 bolt. The top acrylic sheet is supported on four M3 metal standoffs (38mm), positioned at the front and back of the battery box, seen in figure 14.

Figure 9 : Brackets and wheels

To give a slight richer set of inputs and therefore software exercises went for 6 bumper switches, 3 front, 3 rear, both having left, middle and right. The left and right switches just use the long lever versions of this micro-switch (to be honest the levers are a little weak, easily bent). Middle switch needs a pressure plate to increase its detection area. To ensure that the plate swings freely went for a 3 lever hinge i.e. reduce twisting when pressed, as shown in figure 10 (bumper.stl). Secured to the base using 2 x M3, Hinge rod is simply an M3 + nylock, microswitch held in place with 2 x M2. Plate glued to levers using super-glue.

Figure 10 : Bumpers

To allow access to the bread board the ultrasonic sensor clips onto the top acrylic sheet using the bracket shown in figure 11, the two sections are glued together using super-glue (us_bracket.stl). This 3D model needs a little more adjustment, a little unstable.

Figure 11 : Ultra-sonic sensor bracket

To hold the bread board on the top layer simple L brackets were used, shown in figure 12, as they do not take any significant load held in place with just glue (supports.stl). The final assembled robot is shown in figure 13.


Figure 12 : Bread board supports

Figure 13 : Assembled robot


The motor driver board connections/wiring are shown in figure 14, as described: bat -> boost -> motor. Added a DPDT to isolate the battery pack and allow a recharging lead, so that the batteries did not have to be removed for recharging, circuit shown in figure 15. At the moment this switch is just loose, need to add a slot to the dxf file so that it can be mounted on the top acrylic sheet, next to the microBit, towards the rear of the robot. Power (+5V/0v from motor drive board), six bumper-micoswitch signals (6 x 2 wires) and the four motor control signals are passed through a hole to the top bread board.

Figure 14 : Battery, boost and motor board wiring.

Figure 15 : Power switch

The 3.3V regulator is soldered directly onto the microBit breakout board, two pins, (vin=5V, vout=3.3v), as shown in figure 16. Connections to the GPIO lines are made using pin strip and male-female jumper leads. The ultrasonic sensor is powered from the +5V supply, the 3.3V control output (Trig) from the microBit falls within the logic '1' range for this sensor, however, the microBit is not 5V tolerant, therefore, a 330 resistor is soldered inline with the echo input signal (blue wire), this drops the required 1.7V across the resistor and microBit input circuit, as shown in figure 17. A slightly better solution would be to use a potential divider to reduce this voltage from 5V to 3.3V.

Figure 16 : microBit 3.3V regulator and wires

Figure 17 : Ultrasonic wiring


The bread board shown in figure 18 is used to construct the required interface circuits and act as a patch board, connecting the robot's various input and output signals to the microBit (using male-female jumper leads). The drive motor control signals are connected to pins 13,14,15 and 16, shown in figure 18 (top right), controlled directly via the microbits GPIO lines (shown in figure 19). The 6 bumper switches could of also been connected to microBit GPIO lines, however, as a number of these lines are already used to drive LEDs etc, it was decided to add an I2C GPIO expander, PCF8574 (LINK), as shown in figure 18 (middle left) ans 20. Micro-switch signals are labelled: RR,RM,RL,FR,FM and FL. The micro-switch signals do not need a pull-up resistor as this can be configured within the I2C expander IC.

Figure 18 : Bread board circuit layout

Figure 19 : microBit

Figure 20 : I2C bumper switch inputs

The analogue sensors are interfaced to the microBit using a LM324 opamp (LINK). The two LDR light sensors are buffered using opamp voltage followers, as shown in figure 21, this is to ensure these signals have a low output impedance, capable of driving the microBits analogue inputs (ADC). The microphone circuit uses a simple inverting amplifier (x470) with a DC offset to allow this signal to oscillate about a virtual ground, set to 1.5V. This signal is then passed to a simple peak detector before being connected to another microBit analogue input, shown in figure 22.

Figure 21 : LDR buffers

Figure 22 : Microphone amplifier / filter

Test code

Test code was written in micro-python (LINK),downloaded and executed using Mu (LINK), debug information is displayed using the REPL interface. Each motor is controlled using two bits: 00=Stop, 01=forwards, 10=backwards, 11=stop. The test code in figure 23 moves the robot forwards and backwards. Speed control of these motors would be possible as the microBit can implement PWM, however, this is only possible on certain pins. An alternative method would be to implement this functionality in software, rapidly pulsing the logical '1' pin to slow down a motor, something around 100Hz-ish seems to work ok. This could be implemented in the main software loop using the microbit.running_time() function which returns the time in ms since boot. However, this is dependent on software timing, so its very approximate speed control.

from microbit import *

while True:
    sleep(3000)              #wait 3 secs
    sleep(3000)              #wait 3 secs
    sleep(3000)              #wait 3 secs 

Figure 23 : Move stop - backwards - forwards - repeat

The I2C GPIO lines use pseudo bi-directional I/O pins i.e. the output can be driven to a logical '0', a logical '1' is implemented using an internal pull-up resistor. Therefore, by setting the outputs to a logic '1' you can use these pins as inputs i.e. connect an external logic '0' or logic '1', overriding the internal pull-up, as shown in figure 24. As a result when used as an output the logical '1' output current is relatively low, therefore, the LED is controlled by switching to a logical '0' state, as shown in figure 25.

from microbit import *

# Switch Data
# None          : 0xFF - 255

# Rear Left     : 0xFE - 254
# Rear Middle   : 0xFD - 253
# Rear Right    : 0xFB - 251

# Front Left    : 0xF7 - 247
# Front Middle  : 0xEF - 239
# Front Right   : 0xDF - 223

addr = 0x38

i2c.write(addr, b'\xFF', repeat=False)

while True:
    data =, 1, repeat=False)
    value = int(ord(data))
    if (value & 1) == False: print("Rear Right")
    if (value & 2) == False: print("Rear Middle")   
    if (value & 4) == False: print("Rear Left")
    if (value & 8) == False: print("Front Right")    
    if (value & 16) == False: print("Front Middle")
    if (value & 32) == False: print("Front Left")   


Figure 24 : Bumper switch I2C test

from microbit import *

addr = 0x38

off = b'\xFF'
on = b'\x00'
data = b'\x00'

while True:
    i2c.write(addr, off, repeat=False) 
    i2c.write(addr, on, repeat=False) 

Figure 25 : Flash I2C LED

The microphone and LDR signals use analogue input pins, converting the 0 - 3.3V input signal into a 10bit representation i.e. a value ranging from 0 - 1023. The code in figure 26 reads the two LDR signals and prints them to the screen. The code in figure 27 implements a simple 'clapper' using the microphone circuit, if you clap your hands twice it will play the Nyan cat sound. The output device is a 'small' piezo-electric speaker/buzzer connected to pin 0, as shown in figure 28. A short video of it in action is available here (Video).

from microbit import *

while True:
    LDR1 = pin10.read_analog()
    LDR2 = pin1.read_analog()
    outputString = "Light Levels: " + str(LDR1) + " - " + str(LDR2)

Figure 26 : LDR test code

from microbit import *
import music

while True:
    while (peak < THRESHOLD):
        peak = pin2.read_analog()
    start_time = running_time()

    while (peak > THRESHOLD):
        peak = pin2.read_analog()
    while (peak < THRESHOLD and ((running_time() - start_time) < 3000)):
        peak = pin2.read_analog()

    stop_time = running_time()
    delay_time = stop_time - start_time
    if ((delay_time > 100) and (delay_time < 2000)):
        clap = True
    if clap:
        clap = False

Figure 27 : Clapper

Figure 28 : Speaker

Creative Commons Licence

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Contact details: email -, telephone - 01904 32(5473)