Driving a TReX Jr with an Arduino

I can drive the TReX just fine if I use a standard RC receiver if I have the TReX set to digital mode. I wrote a little test snippet of code for the Arduino using the AnalogWrite function and I can fade an LED in and out so I know it is putting out a varying analog signal. I also tested it with my DMM and it varies from 1V to 5V appx. When I try and drive the TReX with the Arduino I get bupkis. The motor lights do not light and, of course, the motors don’t operate. I have it jumpered for: no serial, mixed signal, analog mode. I have tried Channel 1 and Channel 2 for testing.

I have also tried using the Servo functions in with the Arduino and setting the mode to digital and that doesn’t work either. I have tried using 6V power supply for everything and I have tried using a 6V PS for the Arduino and a 12V PS for the TReX. Again, works fine if I use a RC receiver but nothing with the Arduino.

Any help will be appreciated.

Jim, K6JMG

Hello.

Why don’t you just use the serial interface to set the motor speed directly? This sounds like a very roundabout way of interfacing the Arduino with the TReX.

AnalogWrite() on the Arduino does not output an analog voltage, it outputs a PWM that rapidly alternates between 0 and 5 V with a specifiable duty cycle. Without using external components to convert this into an actual analog voltage, this will not work as an input to the TReX.

Code to drive servos should work with the TReX in RC mode as the RC inputs of the TReX are designed to accept the standard RC hobby servo pulses output by RC receivers. Can you post your sketch that uses servo functions? (Please cut it down to the simplest sketch that should do something but doesn’t.)

Also, the TReX doesn’t have a “digital mode”; do you mean “RC mode”?

- Ben

Ben,

I did mean RC not digital. I completely misunderstood the analog interface. I thought it was expecting PWM.

I would be open to serial IF you can point me to some good simple clear examples as I am very much a beginner at this and smoke started coming out of my ears when I started reading the serial portion of the TReX manual. I am very good at ~adapting~ code but have some real problems ~creating~ code from scratch. If I can see examples I can usually use that with other instructions to get where I need to be. I use the Serial functions with the Arduino for displays and debugging but have never communicated with it.

Plan B, is still using RC or something to drive the TReX.

Jim

I think serial is definitely the better way to go, though as I said, outputting servo pulses should work just fine if the TReX is in RC mode. If you want some code to adapt from, take a look at the sample Arduino code we have for our new Simple Motor Controllers:

pololu.com/docs/0J44/6.7.1

This code demonstrates sending serial bytes using the NewSoftSerial software serial library. The bytes you need to send to control the TReX are different, but it’s still just a matter of sending bytes. The TReX command documentation explains the bytes required for the different TReX commands.

If you have questions about the sample code or about the specific TReX command bytes, please let me know, and if you come up with a simple Arduino sketch to drive the motors using the TReX, please post it. If it works, it might be helpful to others in your situation, and if it doesn’t work, I can help you get it working.

- Ben

Ben,

Below is the code I am using with the Arduino. It compiles and uploads fine. I had to track down the NewSerial library but not a big deal. I have attached the pin on the TReX that is designated as Logic-level TReX Jr serial in to the PIN 4 on the Arduino. I have attached the GND pin on the TReX to the GND pin on the Arduino. I have not connected the pin on the TReX as Logic-level TReX Jr Serial Out to anything. I have the mode jumper removed entirely which, I think, sets the mode ot serial. I have the mixed jumper in place. I have the BEC jump remoed. I have nothing connected to the Channel pins. Nothing happens? What am I doing wrong?

Here’s the code, it is a direct copy from the link you provied.

#include <SoftwareSerial.h>
#include <NewSoftSerial.h>

#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}

// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}

void setup()
{
// initialize software serial object with baud rate of 38.4 kbps
mySerial.begin(38400);

// the Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms
delay(5);

// if the Simple Motor Controller has automatic baud detection
// enabled, we first need to send it the byte 0xAA (170 in decimal)
// so that it can learn the baud rate
mySerial.print(0xAA, BYTE); // send baud-indicator byte

// next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run
exitSafeStart(); // clear the safe-start violation and let the motor run
}

void loop()
{
setMotorSpeed(3200); // full-speed forward
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
}

As I wrote in my previous post:

The code I pointed you to is written for a different product, so it’s no surprise that it does not work on the TReX. You said you were bad at creating something from scratch but very good at adapting code, so I pointed you to this code as a starting point for adaptation. You will need to read through the TReX documentation and change the bytes you are sending to conform with the TReX serial commands. As I said, if you need help with this, please ask specific questions about what is confusing you. Also, you should start with something very simple (much simpler than what you just posted), such as just sending a single command to set the motor speed. And finally, when you post code, please use the [ code ] [ /code ] tags to make it display in a more readable format (you can do this by pressing the “Code” button when writing your post).

- Ben

Ben,

Thanks for your help but I am just going to change controllers, this one is beyond my abilities. This whole robotics thing is supposed to be fun; when I have to struggle this hard the fun goes out of it.

Jim

I understand where you’re coming from, but note that using an Arduino to send serial bytes is relatively easy stuff compared to most of what you’ll encounter building robots, at least if you plan on doing anything truly interesting. There is a learning curve involved, but understanding things like bits, bytes, and how to send serial data will benefit you if you want to pursue this hobby.

If you are looking for something even easier to use, you could consider our new Simple Motor Controllers. The code I linked to above gives you functions that let you control the Simple Motor Controller with an Arduino without understanding how the code actually works, and the USB connector makes it easy to configure it and monitor its status. They’re designed to be much more beginner-friendly, and the user’s guide should be a lot more approachable to newcomers than the TReX documentation. Also, like the TReX, you have the option of just plugging in an RC receiver to achieve easy manual control.

- Ben