Problem with external powered Teensy 4.0

donald

Member
Hi I am using a Teensy 4.0 to run my robot (motors, LIDAR and Limit sensors). It works fine when I am connected to the PC (usb cord) but when I only use external power (not connected via usb from PC).

I removed all reference to the Serial like Serial.begin and Serial.print but it stop at the Delay() command. weird.

Can anyone point me in the right direction

Thank you in advance

Donald
 
Hard to say without taking a look under the hood. Post the source code.

Here is my testing code, just for the motor

/*
L298N Motor Demonstration
L298N-Motor-Demo.ino
Demonstrates functions of L298N Motor Controller

DroneBot Workshop 2017
http://dronebotworkshop.com
*/


// Motor A
int minspeed = 200;
int enA = 8;
int in1 = 6;
int in2 = 7;

// Motor B

int enB = 10;
int in3 = 12;
int in4 = 11;

void setup()

{
//Serial.begin(9600);
// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

}

void demoOne()

{

// This function will run the motors in both directions at a fixed speed

// SET FORWARD

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

// Turn on motor A and B
analogWrite(enA, minspeed);
analogWrite(enB, minspeed);

//Serial.println("Forward ... HIGH-LOW ...");
delay(1000);

// Now change motor directions
//Serial.println("Reverse ... HIGH-LOW ...");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

delay(1000);

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

void demoTwo()

{

// This function will run the motors across the range of possible speeds
// Note that maximum speed is determined by the motor itself and the operating voltage

// Turn on motors

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

// Accelerate from zero to maximum speed

for (int i = 150; i < 250; i++)

{

analogWrite(enA, i);
analogWrite(enB, i);

//delay(40);

}

// Decelerate from maximum speed to zero

for (int i = 255; i >= 150; --i)

{

analogWrite(enA, i);
analogWrite(enB, i);

delay(40);

}

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

void loop()

{

//demoOne();
//delay(1000);
demoTwo();
//delay(1000);
demoOne();
//Serial.println("Demo One done...");
//delay(1000);

//demoTwo();

//Serial.println("DemoTwo done...");
//delay(1000);

}
 
It appears all serial code is disabled, so I don't see a problem there.

I have never used analogWrite() on Teensy 4, but from some cursory research, it appears that its range on that chip is 0 - 1023. 255 is only 25% of maximum pulse width. I don't know if you will get enough torque to break loose from fully stopped.

There are several delay() calls in this code. Where does it appear to be stopping?
 
No matter where I put a Delay() and you can see that I did a lot of testing, it stop. In that test, it stops at the delay(40) in the DemoTwo()
 
How do you know it's stopping?

If you put a delay(1000) and then some code to turn an LED on, just in the setup() function, with no other code in there, does the LED turn on?
 
Have you validated that your external power source is providing enough power? I could imagine that motors starting and stopping might provide a bump in the power, and the Teensy may be in a brownout state. This is just a guess mind you.

If possible, hook up a graphing power meter between the external power source and the Teensy, and see if the power dips. I like this meter, since it takes a variety of inputs and outputs (5.5mm x 2.1mm, USB B-micro, USB-C, etc.). You can run an app on a smartphone and it will produce a graph over time of the volts, amps, and watts. I tend to forget that on my Samsung phone, I have to enable the GPS support before it will connect to the device. I wish there was a way to record the power stats to a file on the phone, rather than just using the graph:

Another thing in terms of motors, I tend to see the recommendation that you should some de-coupling capacitors near the power connection to the Teensy. The capacitors will fill up as you use power, and discharge if is there is a momentary shutdown of the power.
 
Last edited:
The blink test does work. Bizarre. I have Serial and Delay commands in the code. Must be something in my code somewhere.

CODE for BLINK

/* LED Blink, Teensyduino Tutorial #1
http://www.pjrc.com/teensy/tutorial.html

This example code is in the public domain.
*/

// Teensy 2.0 has the LED on pin 11
// Teensy++ 2.0 has the LED on pin 6
// Teensy 3.x / Teensy LC have the LED on pin 13
const int ledPin = 13;

// the setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 4000 );
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
}

// the loop() methor runs over and over again,
// as long as the board has power

void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
Serial.print(" ON ... ");
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
Serial.println(" OFF ... ");
delay(1000); // wait for a second
}
 
I recode my motors test and while DemoTwo which has Delay() in it works, the program always jam at the delay() in the DemoOne after the forward part. I mark by *********************** where it blocks.

// Motor A
int minspeed = 200;
int enA = 8;
int in1 = 6;
int in2 = 7;


// Motor B
int enB = 10;
int in3 = 12;
int in4 = 11;


void setup()
{
Serial.begin(9600);
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}


void demoOne()
{
// This function will run the motors in both directions at a fixed speed
// SET FORWARD


digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);


// Turn on motor A and B
analogWrite(enA, minspeed);
analogWrite(enB, minspeed);


Serial.println("Forward ... HIGH-LOW ...");
delay(2000);
*********************************************************
// Now change motor directions
Serial.println("Reverse ... HIGH-LOW ...");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

// Turn on motor A and B
analogWrite(enA, minspeed);
analogWrite(enB, minspeed);


delay(1000);

// Now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);



}


void demoTwo()
{
// This function will run the motors across the range of possible speeds
// Note that maximum speed is determined by the motor itself and the operating voltage


// Turn on motors


digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);


// Accelerate from zero to maximum speed


for (int i = 150; i < 250; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);


delay(10);
}


// Decelerate from maximum speed to zero


for (int i = 255; i >= 150; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);


delay(10);
}


// Now turn off motors


digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}


void loop()
{
demoTwo();
delay(1000);
demoOne();
Serial.println("Demo One done...");
delay(1000);
}
 
Problem solved many thanks

It turns out that it was the change in dorection that cause the problem, like an overflow from HIGH-LOW.

I turn off the motors before a change in direction and it works.

Thank you again for the help in debugging. Now the program of the robot works

Donald

// Motor A
int minspeed = 200;
int enA = 8;
int in1 = 6;
int in2 = 7;

// Motor B
int enB = 10;
int in3 = 12;
int in4 = 11;

void setup()
{
Serial.begin(9600);
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void demoOne()
{
// This function will run the motors in both directions at a fixed speed
// SET FORWARD

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

// Turn on motor A and B
analogWrite(enA, minspeed);
analogWrite(enB, minspeed);

Serial.println("Forward ... HIGH-LOW ...");
delay(500);

// stop motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(50);

// Now change motor directions
Serial.println("Reverse ... HIGH-LOW ...");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

// Turn on motor A and B
analogWrite(enA, minspeed);
analogWrite(enB, minspeed);

delay(500);

// Now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);


}

void demoTwo()
{
// This function will run the motors across the range of possible speeds
// Note that maximum speed is determined by the motor itself and the operating voltage

// Turn on motors

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

// Accelerate from zero to maximum speed

for (int i = 150; i < 255; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
Serial.print("Speed at : ");Serial.println(i);
delay(10);
}

// Decelerate from maximum speed to zero

for (int i = 255; i >= 150; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);

delay(10);
}

// Now turn off motors

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

void loop()
{
//demoTwo();
//delay(1000);
demoOne();
Serial.println("Demo One done...");
delay(100);
}
 
Back
Top