Monday, March 28, 2016

Sending and recieving commands from the Pololu Trex Jr. Motor Contoller

The more I try to do with 3d printing + robotics + arduino, the more I realize what a capable little microcontroller the Trex Jr. from Pololu is.

One of the big challenges that I faced when I first started messing around with the controller is sending and receiving command bytes to and from the controller using the Arduino. The following very simple code sends a command from the arduino to the motoe controller to turn one motor at full speed.

The code also receives information about the current draw from the controller and prints it to the serial monitor. As it says in the Trex jr. documentation, you need multiple the current received by 40 to get how much milliamps the motor is drawing.

#include

#define TXPIN 4
#define RXPIN 5

SoftwareSerial pololu(RXPIN, TXPIN);
int incomingByte = 0;

void setup() {
  pinMode(RXPIN, INPUT);
  pinMode(TXPIN, OUTPUT);
 
  Serial.begin(9600);
  pololu.begin(19200);

}

void loop() {

pololu.write(0xC2);
pololu.write(127);
pololu.write(0x8D);

          if (pololu.available()<68 br="">        
                // read the incoming byte:
                incomingByte = pololu.read();
                delay(2);              
               Serial.println(incomingByte);
             }
 
}