Gadgeteer GHI Extender Module, combining functionality: one port for motor drive and distance measurements
One of the things I like about the Gadgeteer is that it plugs like LEGO, but if you want to, you connect your own hardware as well. This blog describes GHI Extender module which is a good break-out option to add other devices. In this blog I will describe combining 'motor drive control' and 'distance measurement' on the same port.
GHI Extender module
GHI Extender module

First we need to known which pins are used by the US3 module. As you can see:
  • pin 1 = Vcc = 3.3V
  • pin 2 = 5V
  • pin 10 = ground
  • pin 4 is the trigger signal
  • pin 3 is the echo signal
  • pins 5...9 are not connected
US3 distance module schema
US3 distance module schema

Instead of using the real module, I used a 'Ultrasonic Module HC-SR04 Distance Measuring Transducer Sensor' which can be found on eBay. I added the two resistors as shown in the design.
Ultrasonic Module HC-SR04 Distance Measuring Transducer Sensor
Ultrasonic Module HC-SR04 Distance Measuring Transducer Sensor

For the motor control I use a 'Dual H-Bridge Motor Drive Controller Board Module Arduino L298N'. It is not arduino only, but I will give you the right result when searching for it in google/ebay/etc. The motor drive inputs are connected to pins 6...9 via transistor switches. Connecting the output pins 6...9 directly did not give the expected result. This might have to do with a too low power situation. I first had everything powered by the usb module which can only source a small current. Driving the motors can take > 1 ampere.
Dual HBridge Motor Drive Controller Board Module Arduino L298N
Dual HBridge Motor Drive Controller Board Module Arduino L298N

For powering the motor controller I use 6 AA rechargeable batteries. Ground signal of the Gadgeteer Extender Module is connected to the ground of the motor controller. For the chassis and motors I use one of the 'Smart Robot'-sets. This set only has a battery holder that can only hold 4 AA batteries so for enough power you either need another battery holder or higher voltage AA batteries.
Smart robot chassis set
Smart robot chassis set

Now that the hardware part is in place. We can create a new project in Visual Studio. Select ".NET Gadgeteer Application".
New project
New project

Select the main board.
Main board selection
Main board selection

Drag the following components to the design view:
  • One of the usb clients. In my case the usbClientSP
  • Extender module
basic setup
basic setup

When you 'right-click' you get a pop-up menu with the option to 'Connect all modules'.
Connect automatically
Connect automatically

You can also manually connect the modules to the mainboard.
Connect manually
Connect manually

This basic setup doesn't have a Distance_US3. This will be added in code. First add a reference to 'GTM.GHIElectronics.Distance_US3'
Add reference
Add reference

In the example code below you can see that socket 4 is used both for the extender (determined in design view) and also for the distance measurement module. Pins 6,7,8 and nine are used for controlling a dual motor driver. Pins 3 and 4 are used for the distance measurement. Every second the distance is measured and the state of the motor driver is set.

using System; using System.Collections; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Presentation; using Microsoft.SPOT.Presentation.Controls; using Microsoft.SPOT.Presentation.Media; using Microsoft.SPOT.Presentation.Shapes; using Microsoft.SPOT.Touch; using Gadgeteer.Networking; using GT = Gadgeteer; using GTM = Gadgeteer.Modules; using Gadgeteer.Modules.GHIElectronics; using Gadgeteer.Interfaces; using Gadgeteer; namespace ExtenderModule { &nbsp;public partial class Program &nbsp;{ &nbsp;&nbsp;private Distance_US3 _distanceMeterFront; &nbsp;&nbsp;private DigitalOutput m1a; &nbsp;&nbsp;private DigitalOutput m1b; &nbsp;&nbsp;private DigitalOutput m2a; &nbsp;&nbsp;private DigitalOutput m2b; &nbsp;&nbsp;void ProgramStarted() &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;int socketNumber = 4; &nbsp;&nbsp;&nbsp;_distanceMeterFront = new Distance_US3(socketNumber); &nbsp;&nbsp;&nbsp;m1a = extender.SetupDigitalOutput(Socket.Pin.Nine, false); &nbsp;&nbsp;&nbsp;m1b = extender.SetupDigitalOutput(Socket.Pin.Eight, false); &nbsp;&nbsp;&nbsp;m2a = extender.SetupDigitalOutput(Socket.Pin.Seven, false); &nbsp;&nbsp;&nbsp;m2b = extender.SetupDigitalOutput(Socket.Pin.Six, false); &nbsp;&nbsp;&nbsp;GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously); &nbsp;&nbsp;&nbsp;timer.Tick += timer_Tick; &nbsp;&nbsp;&nbsp;timer.Start(); &nbsp;&nbsp;&nbsp;Debug.Print("Program Started"); &nbsp;&nbsp;} &nbsp;&nbsp;void timer_Tick(GT.Timer timer) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;int distance = _distanceMeterFront.GetDistanceInCentimeters();&nbsp; &nbsp;&nbsp;&nbsp;// based on measured distance you can &nbsp;&nbsp;&nbsp;// enable / disable m1a, m1b, m2a, m2b &nbsp;&nbsp;&nbsp;// when both m1a and m1b are low, motor m1 will not run &nbsp;&nbsp;&nbsp;// when m1a is low and m1b is high, motor m1 will run direction 1 &nbsp;&nbsp;&nbsp;// when m1a is low and m1b is high, motor m1 will run opposite direction 1 &nbsp;&nbsp;&nbsp;// when both m1a and m1b are high, motor m1 will not run &nbsp;&nbsp;&nbsp;// same story for m2a and m2b and motor 2 &nbsp;&nbsp;&nbsp;if (distance >= 0 && distance <= 25) &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;m1a.Write(true); &nbsp;&nbsp;&nbsp;&nbsp;m1b.Write(false); &nbsp;&nbsp;&nbsp;&nbsp;m2a.Write(true); &nbsp;&nbsp;&nbsp;&nbsp;m2b.Write(false); &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;m1a.Write(false); &nbsp;&nbsp;&nbsp;&nbsp;m1b.Write(true); &nbsp;&nbsp;&nbsp;&nbsp;m2a.Write(false); &nbsp;&nbsp;&nbsp;&nbsp;m2b.Write(true); &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;} }

Important notes:
  • combining US3 module and extender module functionality by using the GHIElectronics.Distance_US3 and GHIElectronics.Extender classes is small risk. It depends on the implementation of GHI. As long as the pin assignments for the modules don't change it isn't a problem. If you want to rule out this risk, you can copy the code, rename the classes and/or namespaces and have your own implementation. For this blog I'll depend on GHI's implementation.
  • measuring every second is not very accurate when driving a robot
  • in this example setup there are plenty free ports left. So it is not necessary to combine functionality. But it saves you an extender module

Back to List

All form fields are required.
A confirmation mail for the comments will be send to you.