0

Trinket pops into "programming mode" at the end of a loop cy
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trinket pops into "programming mode" at the end of a loop cy

by ben_cowden on Tue Oct 10, 2017 8:07 pm

I'm using a Trinket m0 driving a stepper with a cheap L298N-based driver board. My code is basically: push this button, stepper drives for a bit, wait for next button press.

Unfortunately, as soon as the stepper stops, both boards go black (LEDs turn off) for a second, and then they re-start, but the Trinket is now in "programming mode" (DotStar is red) instead of waiting for the next button press. I don't think it's a code issue, but code is below. I'm guessing it's something the stepper driver is doing when it finishes it's run..? Any ideas? Thanks!

Code: Select all | TOGGLE FULL SIZE
#include <Stepper.h>

const int buttonPin = 4;     // the number of the pushbutton pin
const int stepsPerRevolution = 200;  //how many steps

// initialize the stepper library on pins 0 through 3:
Stepper myStepper(stepsPerRevolution, 0, 1, 2, 3);
int rpm = 240; //speed variable
int rot = 20; //how many turns
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    pour();
  }
}

void pour() {
  delay(500);
  myStepper.setSpeed(rpm);
  // turn the desired amount:
  myStepper.step(stepsPerRevolution * rot);
   while (digitalRead(buttonPin) == HIGH) {
  }
}

ben_cowden
 
Posts: 15
Joined: Fri Jun 26, 2015 11:38 am

Re: Trinket pops into "programming mode" at the end of a loo

by adafruit_support_bill on Tue Oct 10, 2017 8:38 pm

How are you powering the stepper? It could be some noise introduced onto the power rails by the motor that is causing a processor reset.

adafruit_support_bill
 
Posts: 62237
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket pops into "programming mode" at the end of a loo

by ben_cowden on Thu Oct 12, 2017 1:33 pm

Thanks adafruit_support_bill! I have a 12v3a adapter running to the stepper driver board. It has an onboard 5v regulator that is running to the Trinket. I agree it is some kind of noise/backlash/pulse that is mucking things up. I tried running from two power sources, but couldn't get it to work at all. Is there something I can just put in between the board's 5v output and the Trinket's BAT input? Thanks again!

ben_cowden
 
Posts: 15
Joined: Fri Jun 26, 2015 11:38 am

Re: Trinket pops into "programming mode" at the end of a loo

by adafruit_support_bill on Thu Oct 12, 2017 1:41 pm

I tried running from two power sources, but couldn't get it to work at all.

If you use 2 power sources, you need to make sure that the grounds are connected.

Is there something I can just put in between the board's 5v output and the Trinket's BAT input?

You can try adding a large-ish capacitor between 5v and GND.

Also, try to keep your motor leads well separated from any other wiring in your system if possible. Wires can act as antennae and couple noise into the system inductively.

adafruit_support_bill
 
Posts: 62237
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket pops into "programming mode" at the end of a loo

by ben_cowden on Thu Oct 12, 2017 2:07 pm

Ok, I'm closer now. I got the 2 power source to work (forgot to connect grounds before, thanks adafruit_support_bill).

With one power source, I tried a 100uF 50V cap between BAT and ground, but that didn't work. I'm really hoping to find a one power source solution. I guess I'll try a bigger cap and see.

ben_cowden
 
Posts: 15
Joined: Fri Jun 26, 2015 11:38 am

Re: Trinket pops into "programming mode" at the end of a loo

by adafruit_support_bill on Thu Oct 12, 2017 2:15 pm

What kind of stepper motor are you driving?

adafruit_support_bill
 
Posts: 62237
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket pops into "programming mode" at the end of a loo

by ben_cowden on Thu Oct 12, 2017 2:43 pm

It's a little 200 step bipolar motor with a peristaltic pump on it. It will take 12-24v, and uses about 1.5a

ben_cowden
 
Posts: 15
Joined: Fri Jun 26, 2015 11:38 am

Re: Trinket pops into "programming mode" at the end of a loo

by adafruit_support_bill on Thu Oct 12, 2017 2:57 pm

It will take 12-24v, and uses about 1.5a

"uses about 1.5a" or the spec sheet rates it for 1.5a per phase? Those statements are not necessarily equivalent.

What is the phase resistance? That is what determines what the actual current draw will be. You really need to read between the lines on some stepper spec sheets.

adafruit_support_bill
 
Posts: 62237
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket pops into "programming mode" at the end of a loo

by ben_cowden on Sun Oct 15, 2017 3:13 pm

Well, I achieved a "good enough" solution by adding 5 (!) 2200uF electrolytic caps in between 5V and ground on the Trinket. It's "good enough" because the Trinket still resets (DotStar goes out), but it stays in operation mode. I added some code so that when the code starts, if the button is being pressed, it doesn't activate the stepper until it gets released and re-pressed (code below).

Sorry I don't have the spec sheet adafruit_support_bill, it's a product from china that came with bare bones info. I know: you get what you pay for. I'm not sure what the spec sheet would tell me that would help. The driver board is also cheap, and I think that's where the issue is. It occured to me to put in my own 5V regulator and bypass the driver board to get power to the Trinket, but I'd have to order a regulator and wait an eternity (2 days!!). I'll try that down the road.

Here's the revised code I used:
Code: Select all | TOGGLE FULL SIZE
#include <Stepper.h>

const int buttonPin = 4;     // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 0 through 3:
Stepper myStepper(stepsPerRevolution, 0, 1, 2, 3);
int rpm = 240; //speed variable
int rot = 20; //how many turns
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
 
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  while (digitalRead(buttonPin) == HIGH) { //if it's high now, that means the code started with button pressed
  } //do nothing
 
  if (buttonState == LOW) { //as soon as button is released,
    wait();                 //start waiting for a new press
  }
}

void wait() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
  delay(500);
  myStepper.setSpeed(rpm);
  // turn the desired amount:
  myStepper.step(stepsPerRevolution * rot);
  }
  else {
    wait(); //stay looping in this cycle until the button is pressed again
  }
}

ben_cowden
 
Posts: 15
Joined: Fri Jun 26, 2015 11:38 am

Re: Trinket pops into "programming mode" at the end of a loo

by adafruit_support_bill on Sun Oct 15, 2017 3:32 pm

I'm not sure what the spec sheet would tell me that would help

It would tell you enough to calculate the actual current draw of the motor at 12v. The L298 does not have current limiting, so if the phase resistance of the motor is lower than 8 ohms, it will be pulling more than 1.5A per phase - over-loading your power supply. Given the symptoms, I suspect that is the case.

adafruit_support_bill
 
Posts: 62237
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket pops into "programming mode" at the end of a loo

by ben_cowden on Sun Oct 15, 2017 8:29 pm

Thanks adafruit_support_bill! Is there something I can try, besides buying better driver boards and stepper motors, to prevent overloading the power supply? I mentioned splitting off the 5V power before the driver board. Could that do it? I tried ramping down the speed of the stepper instead of just cutting it off, but that didn't help. Originally I tried this circuit with the SparkFun Easy Driver board, but it wasn't able to direct enough current to the stepper, hence the cheap L298N board. Thanks again for your patience and help.

ben_cowden
 
Posts: 15
Joined: Fri Jun 26, 2015 11:38 am

Re: Trinket pops into "programming mode" at the end of a loo

by adafruit_support_bill on Sun Oct 15, 2017 9:34 pm

Is there something I can try, besides buying better driver boards and stepper motors,

They don't have to be 'better'. You just need to know the specs so you can tell if they are compatible.
https://learn.adafruit.com/all-about-st ... he-stepper
If your motors are designed for use with current limiting "chopper" drives, you either need to invest in a chopper driver, or a motor that does not need one.

adafruit_support_bill
 
Posts: 62237
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.