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
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
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
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
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
Now that the hardware part is in place. We can create a new project in Visual Studio. Select ".NET Gadgeteer Application".
New project
Select the main board.
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
When you 'right-click' you get a pop-up menu with the option to 'Connect all modules'.
Connect automatically
You can also manually connect the modules to the mainboard.
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
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
{
public partial class Program
{
private Distance_US3 _distanceMeterFront;
private DigitalOutput m1a;
private DigitalOutput m1b;
private DigitalOutput m2a;
private DigitalOutput m2b;
void ProgramStarted()
{
int socketNumber = 4;
_distanceMeterFront = new Distance_US3(socketNumber);
m1a = extender.SetupDigitalOutput(Socket.Pin.Nine, false);
m1b = extender.SetupDigitalOutput(Socket.Pin.Eight, false);
m2a = extender.SetupDigitalOutput(Socket.Pin.Seven, false);
m2b = extender.SetupDigitalOutput(Socket.Pin.Six, false);
GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously);
timer.Tick += timer_Tick;
timer.Start();
Debug.Print("Program Started");
}
void timer_Tick(GT.Timer timer)
{
int distance = _distanceMeterFront.GetDistanceInCentimeters();
// based on measured distance you can
// enable / disable m1a, m1b, m2a, m2b
// when both m1a and m1b are low, motor m1 will not run
// when m1a is low and m1b is high, motor m1 will run direction 1
// when m1a is low and m1b is high, motor m1 will run opposite direction 1
// when both m1a and m1b are high, motor m1 will not run
// same story for m2a and m2b and motor 2
if (distance >= 0 && distance <= 25)
{
m1a.Write(true);
m1b.Write(false);
m2a.Write(true);
m2b.Write(false);
}
else
{
m1a.Write(false);
m1b.Write(true);
m2a.Write(false);
m2b.Write(true);
}
}
}
}
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