HexEngine 2 BS2p Tutorial 03
Two Way Serial Comms + Body Control Using Range Finder

Updated: 24-Feb-2010


One of the easiest micro controllers to program is the basic stamp range. For this reason I have chosen the BS2p stamp to demonstrate how easy it is to communicate with the HexEngine via the serial control port.

This programs demonstrates two way serial communications between the HexEgnine and a basic stamp BS2p device using the SERIN and SEROUT commands. When using SERIN, the maximum baud rate is 19200, and a delay is needed from when the HexEngine receives a packet request to when it returns it. This is because we are not using hardware uarts on the BS2p. There is a setting on the HexEngine that will transmit a number of 0xff bytes to form a transmission delay before the actual reply packet. This setting also sets the UART STOP bits to two, which also helps slow down the comms data rate. Also, it is much easier to receive PIP packets in mode 0, with no Escape code checking.

Note: If you are using a p.Brain-µ24, 5 Volts is not available from the board.

Equipment Used
  • MSR-H01 Hexapod
  • MSR-H01-PH Pan Only Head
  • p.Brain-SMB + HexEngine or p.Brain-µ24 HexEngine
  • BS2p24 + Carrier Board
  • SRF08 Range Finder
HexEngine Configuration

CN18 Jumpers Off Terminal port = RS232 (UART2) , Control port = TTL (UART1).
CBR=4 Control baud rate = 19200
PIP=0 PIP Packet mode = 0
TXD=3 Transmission delay = 3

When running this programme, the debug BS2 debug window will display the light intensity and range of the SRF08 range finder module. In V2 the SRF08 range is reduced to approx 1 metre.

The main loop uses the I2C out command to start the PING on the SRF08, waits 70mS and then reads back from the HexEngine I2C port to gather the SRF08 echo. once it has the data, the light level is used to control the height of the hexapod and the distance from the ranger is used to control the 'Y' translate of the hexapod, if the SRF08 module is mounted on the front of the hexapod this gives the effect of the hexapod moving away from an object in front of it, and cowering if a hand passes of the head.


Only 3 wires are necessary for communication with the BS2p, which can be found on the p.Brain-SMB connector CN17.

Pin 2 = U1RX
Pin 4 = U1TX
Pin 6 = GROUND

U1RX needs to be connected to the BS2p SER_OUT pin and GROUND to the BS2P ground. IF you are using the 5V regulator ON the p.Brain-SMB, You CAN also take a third wire from PIN 9 ON CN17 TO supply the 5V power to the BS2p

Description BS2p
Pin 2 Pin 2 Data In Pin 5 (Can be user configured )
Pin 4 Pin 4 Data Out Pin 6 (Can be user configured )
Pin 6 Pin 6 Ground Pin 4
Pin 9 N/A 5 Volts ( Optional ) Pin 21

Four wires are required to connect the SRF08 to the HexEngine.

p.brain-SMB CN17 Description SRF08 Range Finder
Pin 5 I2C SDA Pin 2
Pin 7 I2C SCL Pin 3
Pin 6 Ground Pin 5
Pin 9 5 Volts Pin 1


BS2p Code

HE_BS2_COMS_3_V2.zip Contains HE_BS2_COMS_3_V2.bs2 ADDED 24/11/08
HE_BS2_COMS_3.zip Contains HE_BS2_COMS_3.bs2
Useful Links & Documents

HexEngine Configuration Guide (PDF)  
HexEngine PIP protocol Guide (PDF)  
p.Brain-SMB user Guide (PDF)  
BS2p24 Schematic (PDF)  
BS2p24 Information (Link)  

Image 01 - Basic Stamp Carrier attached to MSR-H01 Hexapod, HexEngine is mounted on underside of top body plate. HE+BS2

Video 01 - Video demonstrating hexapod control as described in this tutorial.