Jump to content
 

Arduino Applications and Programs


Simond
 Share

Recommended Posts

  • RMweb Gold

I have noticed one inconsistancy the sketch you have based yours on uses SoftwareSerial  however you are using the Hardware Serial Pins there is another example TMC2208 Simple which uses the Hardware pins. I do not know if it is the problem but I will suggest a different code with  some changes on you other point. Will do it later.

 

Don

Link to post
Share on other sites

  • RMweb Gold
3 hours ago, Donw said:

I have noticed one inconsistancy the sketch you have based yours on uses SoftwareSerial  however you are using the Hardware Serial Pins there is another example TMC2208 Simple which uses the Hardware pins. I do not know if it is the problem but I will suggest a different code with  some changes on you other point. Will do it later.

 

Don

 

Here is something I would just like you to try

 

// 

// Define pins
 // Define pins
 #define enable_PIN 5 // LOW: Driver enabled. HIGH: Driver disabled
 #define step_PIN 3 // stop on rising edge
 #define RX_PIN 0 // Serial receive pin
 #define TX_PIN 1 // Serial transmit pin
 #define DIR_PIN_IN 9 // deck_position in from DCC decoder
 #define DIR_PIN_OUT 4 // deck_position out to TMC2208
 #define EndStopPin1 8 // End stop switch 1
 #define EndStopPin2 11 // End stop switch 2
 #define pin 13, OUTPUT, LOW 
 byte val=LOW;

#include <TMC2208Stepper.h>                       // Include library
TMC2208Stepper driver = TMC2208Stepper(&Serial);  // Create driver use
                                                  // HardwareSerial 
void setup() {
  Serial.begin(115200);        // Start hardware serial 
  driver.push();                // Reset registers

  // Prepare pins
  pinMode(enable_PIN, OUTPUT); // Enable status to TMC2208
 pinMode(step_PIN, OUTPUT); // Step output to TMC2208
 pinMode(DIR_PIN_IN, INPUT); // Input from DCC decoder
 pinMode(DIR_PIN_OUT, OUTPUT); // Position to TMC2208
 pinMode(EndStopPin1, INPUT); // End Stop1 switch
 pinMode(EndStopPin2, INPUT); // End Stop2 switch

 digitalWrite(enable_PIN, HIGH);   // Disable driver in hardware
 driver.pdn_disable(true); // Use PDN/UART pin for communication
 driver.I_scale_analog(false); // Use internal voltage reference
 driver.rms_current(250); // Set driver current = 250mA, 0.5 multiplier for hold current and RSenableSE = 0.11.
 driver.toff(2); // Enable driver in software

  
  
   
}

void loop() {
  val = digitalRead (DIR_PIN_IN); // Read direction status from DCC assume LOW go from 1 to 2 High goes from 2 to 1
  digitalWrite (DIR_PIN_OUT, val); // Write direction to TMC2208
  
  if (val == LOW) {
    
  // Read status of end stop switch 2
  if (digitalRead(EndStopPin2==HIGH)) {
    digitalWrite(enable_PIN, LOW);    // Enable driver in hardware

    while (digitalRead (EndStopPin2==HIGH)) {
      digitalWrite(step_PIN, !digitalRead(step_PIN));
      delayMicroseconds(100);
    }
    digitalWrite(enable_PIN, HIGH);
   }
  }
  else if (val==HIGH) {
    // Read status of end stop switch 1
  if (digitalRead(EndStopPin1==HIGH)) {
    digitalWrite(enable_PIN, LOW);    // Enable driver in hardware

    while (digitalRead (EndStopPin1==HIGH)) {
      digitalWrite(step_PIN, !digitalRead(step_PIN));
      delayMicroseconds(100);
    }
    digitalWrite(enable_PIN, HIGH);
   }
  }
 }
 

This compiled for a pro micro so it should be ok for any board. I am just wondering whether something was not happening with the serial because of confusion between software serial and hardware serial. 

Don

Link to post
Share on other sites

  • RMweb Premium

Hi Don

It loads onto the Nano fine.

Unfortunately absolutely nothing happens.

 

I have LEDs on the board to monitor the states of EN & DIR going to the TMC2208

EN is always HIGH and DIR doesn't change when the input to the Nano is changed.

 

Keith

 

 

Link to post
Share on other sites

back to first principles, connect up the serial monitor and add serial lineprints all through your program to trace what it is / isnt doing.

 

I did look at your code, and I thought it should work, so I was interested by Don't suggestion, but my guess is that it's something basic that is well camouflaged!

 

good luck

Simon

 

 

Link to post
Share on other sites

  • RMweb Premium
24 minutes ago, Simond said:

back to first principles, connect up the serial monitor and add serial lineprints all through your program to trace what it is / isnt doing.

 

I did look at your code, and I thought it should work, so I was interested by Don't suggestion, but my guess is that it's something basic that is well camouflaged!

 

good luck

Simon

 

 

It was sort of working a while back but the efforts to make it 100% seem to have made it worse

Something odd I have noticed recently is that sometimes the "L" LED comes on, sometimes flashing at various rates.

Reading on various forums it suggest if this happens that the pin (D13) should be made an output. I'm not sure why.

 

Link to post
Share on other sites

  • RMweb Gold
2 hours ago, melmerby said:

Hi Don

It loads onto the Nano fine.

Unfortunately absolutely nothing happens.

 

I have LEDs on the board to monitor the states of EN & DIR going to the TMC2208

EN is always HIGH and DIR doesn't change when the input to the Nano is changed.

 

Keith

 

 

 

Simon  it uses Serial to set values to the TMC2208 so you have to use different pins to the TX and RX if useing serial monitor.

 

Keith are you saying that the EN and DIR pins do not change on the Nano. I have been assuming the pins were being changed but the TMC2208 wasn't responding. If the DIR pin is not changing    it seems the signal from the DCC is not being passed on to the DIR pin   

 

I have made a change below to ignore the DCC it should now just keep moving one way then the other as we are setting the DIR pin alternately to LOW then HIGH.  This will prove whether the DCC input is the problem or not

Don

 

// 

// Define pins
 // Define pins
 #define enable_PIN 5 // LOW: Driver enabled. HIGH: Driver disabled
 #define step_PIN 3 // stop on rising edge
 #define RX_PIN 0 // Serial receive pin
 #define TX_PIN 1 // Serial transmit pin
 #define DIR_PIN_IN 9 // deck_position in from DCC decoder
 #define DIR_PIN_OUT 4 // deck_position out to TMC2208
 #define EndStopPin1 8 // End stop switch 1
 #define EndStopPin2 11 // End stop switch 2
 #define pin 13, OUTPUT, LOW 
 byte val=LOW;

#include <TMC2208Stepper.h>                       // Include library
TMC2208Stepper driver = TMC2208Stepper(&Serial);  // Create driver use
                                                  // HardwareSerial 
void setup() {
  Serial.begin(115200);        // Start hardware serial 
  driver.push();                // Reset registers

  // Prepare pins
  pinMode(enable_PIN, OUTPUT); // Enable status to TMC2208
 pinMode(step_PIN, OUTPUT); // Step output to TMC2208
 pinMode(DIR_PIN_IN, INPUT); // Input from DCC decoder
 pinMode(DIR_PIN_OUT, OUTPUT); // Position to TMC2208
 pinMode(EndStopPin1, INPUT); // End Stop1 switch
 pinMode(EndStopPin2, INPUT); // End Stop2 switch

 digitalWrite(enable_PIN, HIGH);   // Disable driver in hardware
 driver.pdn_disable(true); // Use PDN/UART pin for communication
 driver.I_scale_analog(false); // Use internal voltage reference
 driver.rms_current(250); // Set driver current = 250mA, 0.5 multiplier for hold current and RSenableSE = 0.11.
 driver.toff(2); // Enable driver in software

  
  
   
}

void loop() {
 // val = digitalRead (DIR_PIN_IN); // Read direction status from DCC assume LOW go from 1 to 2 High goes from 2 to 1
  digitalWrite (DIR_PIN_OUT, val); // Write direction to TMC2208
  
  if (val == LOW) {
    
  // Read status of end stop switch 2
  if (digitalRead(EndStopPin2==HIGH)) {
    digitalWrite(enable_PIN, LOW);    // Enable driver in hardware

    while (digitalRead (EndStopPin2==HIGH)) {
      digitalWrite(step_PIN, !digitalRead(step_PIN));
      delayMicroseconds(100);
    }
    digitalWrite(enable_PIN, HIGH);
   }
  }
  else if (val==HIGH) {
    // Read status of end stop switch 1
  if (digitalRead(EndStopPin1==HIGH)) {
    digitalWrite(enable_PIN, LOW);    // Enable driver in hardware

    while (digitalRead (EndStopPin1==HIGH)) {
      digitalWrite(step_PIN, !digitalRead(step_PIN));
      delayMicroseconds(100);
    }
    digitalWrite(enable_PIN, HIGH);
   }
  }

if (val==LOW) val=HIGH;

else val=LOW;
 }

Link to post
Share on other sites

  • RMweb Premium

Hi Don

On an earlier sketch where I was using if.......else the DIR & EN do change but on the later sketches they don't. (just tried it again with an old sketch and that is so.)

I have not changed any connections, it's all soldered into a stripboard with sockets for the ICs.

The inputs to the Nano are correct, however it is not responding to them as required.

Link to post
Share on other sites

  • RMweb Gold
6 hours ago, melmerby said:

Hi Don

It loads onto the Nano fine.

Unfortunately absolutely nothing happens.

 

I have LEDs on the board to monitor the states of EN & DIR going to the TMC2208

EN is always HIGH and DIR doesn't change when the input to the Nano is changed.

 

Keith

 

 

 

Keith, I have gone back and looked at the earlier sketches I could have got it wrong about the settings of val. Could you try this I have reversed the if (val==   statements  it could be that if so It would do nothing if I had them the wrong way round

 

 

// 

// Define pins
 // Define pins
 #define enable_PIN 5 // LOW: Driver enabled. HIGH: Driver disabled
 #define step_PIN 3 // stop on rising edge
 #define RX_PIN 0 // Serial receive pin
 #define TX_PIN 1 // Serial transmit pin
 #define DIR_PIN_IN 9 // deck_position in from DCC decoder
 #define DIR_PIN_OUT 4 // deck_position out to TMC2208
 #define EndStopPin1 8 // End stop switch 1
 #define EndStopPin2 11 // End stop switch 2
 #define pin 13, OUTPUT, LOW 
 byte val=LOW;

#include <TMC2208Stepper.h>                       // Include library
TMC2208Stepper driver = TMC2208Stepper(&Serial);  // Create driver use
                                                  // HardwareSerial 
void setup() {
  Serial.begin(115200);        // Start hardware serial 
  driver.push();                // Reset registers

  // Prepare pins
  pinMode(enable_PIN, OUTPUT); // Enable status to TMC2208
 pinMode(step_PIN, OUTPUT); // Step output to TMC2208
 pinMode(DIR_PIN_IN, INPUT); // Input from DCC decoder
 pinMode(DIR_PIN_OUT, OUTPUT); // Position to TMC2208
 pinMode(EndStopPin1, INPUT); // End Stop1 switch
 pinMode(EndStopPin2, INPUT); // End Stop2 switch

 digitalWrite(enable_PIN, HIGH);   // Disable driver in hardware
 driver.pdn_disable(true); // Use PDN/UART pin for communication
 driver.I_scale_analog(false); // Use internal voltage reference
 driver.rms_current(250); // Set driver current = 250mA, 0.5 multiplier for hold current and RSenableSE = 0.11.
 driver.toff(2); // Enable driver in software

  
  
   
}

void loop() {
  val = digitalRead (DIR_PIN_IN); // Read direction status from DCC assume LOW go from 1 to 2 High goes from 2 to 1
  digitalWrite (DIR_PIN_OUT, val); // Write direction to TMC2208
  
  if (val == HIGH) {
    
  // Read status of end stop switch 2
  if (digitalRead(EndStopPin2==HIGH)) {
    digitalWrite(enable_PIN, LOW);    // Enable driver in hardware

    while (digitalRead (EndStopPin2==HIGH)) {
      digitalWrite(step_PIN, !digitalRead(step_PIN));
      delayMicroseconds(100);
    }
    digitalWrite(enable_PIN, HIGH);
   }
  }
  else if (val==LOW) {
    // Read status of end stop switch 1
  if (digitalRead(EndStopPin1==HIGH)) {
    digitalWrite(enable_PIN, LOW);    // Enable driver in hardware

    while (digitalRead (EndStopPin1==HIGH)) {
      digitalWrite(step_PIN, !digitalRead(step_PIN));
      delayMicroseconds(100);
    }
    digitalWrite(enable_PIN, HIGH);
   }
  }
 }

 

 

 

ps the 100 Microseconds should make it run 10 times faster the Delay(1)

Edited by Donw
adding post script
Link to post
Share on other sites

  • RMweb Premium

Hi Don

Sorry, doesn't work.

EN ouput is flashing rapidly and motor is buzzing and turning slowly

Direction and end stops have no effect.

 

It seems as if the loop isn't looping properly, as surely changing the direction input pin must change the output pin, but it's not.

 

Link to post
Share on other sites

  • RMweb Gold
40 minutes ago, melmerby said:

Hi Don

Sorry, doesn't work.

EN ouput is flashing rapidly and motor is buzzing and turning slowly

Direction and end stops have no effect.

 

It seems as if the loop isn't looping properly, as surely changing the direction input pin must change the output pin, but it's not.

 

 

Thats is getting somewhere the motor is turning. While it is moving only the end stop it is moving towards will stop it. I will look at the spec about the speeds. What gearing is there i.e how many turns to move from one position to the other?

 

Don

Link to post
Share on other sites

  • RMweb Premium
5 hours ago, Donw said:

 

Thats is getting somewhere the motor is turning. While it is moving only the end stop it is moving towards will stop it. I will look at the spec about the speeds. What gearing is there i.e how many turns to move from one position to the other?

 

Don

The motor when running freely is better than 1rpm

I am operating the end stop switches manually as I dont have any other way to do it at present.

previously I had it running like this:

 

Operating Sequence.

One end switch is high, one is low, changing direction switch starts the motor turning in one direction (I then move the high switch to low)

I allow the motor to turn for a few seconds then I make the other switch high and the motor stops.

I change the direction and the motor turns the other way

I change the switch that went high back to low, allow the motor to turn for while then change the first switch back to high, motor stops and so on, except after a few cycles it stops and you can't get it to start again. ENable will be high and the direction switch doesn't change the DIRection signal to the TMC2208.

 

I tried poking about with a scope and was surprised to find that the EN output from the Arduino, once it stops is not a High 5v or a low 0v but some noise a little way above 0v, (almost as if it's floating) I'm wondering about some resistors across the outputs to enforce the logic.*

EDIT

*There are already LEDs and resistors, so it shouldn't need anything else.

 

 

 

Edited by melmerby
Link to post
Share on other sites

  • RMweb Premium

Hi Don

I've gone back to trying a very basic circuit.

One end stop pin switch (state).

One direction switch (deck).

Code:

 if ((state == LOW) &&  (deck == HIGH)  || (state == HIGH) &&  (deck == LOW))
  {digitalWrite(EN_PIN, LOW)   ;}
  else
  {digitalWrite(EN_PIN, HIGH) ;}

Unfortunately as like the other implementations it works a couple of times then stops, then the DIR doesn't change and the ENable remains high

Full code:

TraverserTest1.ino

 

I wonder whether I could try in the Arduino Sim (installed on my PC) and step through to see where/why it stops?

 

Link to post
Share on other sites

Keith,

 

as I read your snip, if (a and b) (do something) or ( not a and not b) (do the other thing) 

 

is it the case that if your inputs are (a and not b) or (not a and b) it will (do the other thing)?

 

Given that it seems to work for some cycles and then fails, is it an intermittent hardware issue?  Faulty switch, contact, connector, wiring or have you perhaps somehow damaged the Arduino and it fails as a component warms up?

 

Which simulator do you use, and would you recommend it?  I’ve done very little recently, more soldering brass kits, but I have some ideas I want to try.

 

thx

Simon

Edited by Simond
  • Like 1
Link to post
Share on other sites

  • RMweb Gold

No you both misunderstand the logic 

 

The end stop will be low when at that side.

The instruction says if LOW and the direction says Take a step to the other side it will do so

After a few steps the end stop will no longer be LOW so it stops moving,

 

I have two things I would try with my code 

      add a statement driver.microsteps(4);      before the the driver.toff(2)   this should make it take bigger steps.

    I would also set the delay(1);   using Microseconds would have made the loop run faster but possibly too fast for the stepper.

 

Alternatively you could change the if statement.

END1Pin   and END2Pin    DECK HIGH goes 1 to 2   DECK  LOW goes 2 to 1  

the if statement becomes

If ((END2pin==HIGH) && (DECK==HIGH)) || ((END1Pin==HIGH) && (DECK==LOW))

 

You cannot do it sensibly with a single state pin because you have three conditons Deck at end 1, Deck at end 2 DECK in the middle

 

Deck Position      End1   centre area     End2

END1pin                 L             H                   H

END2 pin               H             H                   L

 

Therefore   you need to be testing for a Pin that is high until it goes Low when you reach it. Testing the LOW pin will just stop once it has moved off.

 

Don

Link to post
Share on other sites

Try this:

On your 'pin_Mode' lines where you define the INPUT pins change INPUT to INPUT_PULLUP otherwise you are leaving the inputs floating when not pulled to GND by the end stop switch (unless you have an external pull-up resistor)

 

Ray.

edit: same is probably true for the direction input pin.

Edited by tender
  • Agree 1
Link to post
Share on other sites

  • RMweb Premium
19 minutes ago, tender said:

Try this:

On your 'pin_Mode' lines where you define the INPUT pins change INPUT to INPUT_PULLUP otherwise you are leaving the inputs floating when not pulled to GND by the end stop switch (unless you have an external pull-up resistor)

 

Ray.

edit: same is probably true for the direction input pin.

Hi Ray

The end stop input pins are switched between HIGH (5v) and LOW (0v) by changeover switches

The direction input pin (from DCC) is switched between HIGH & LOW by the output from an opto coupler, presently I am just applying 12v or 0v  to the input, (which is the same as what comes from the decoder )

 

 

Link to post
Share on other sites

  • RMweb Premium
1 hour ago, Donw said:

No you both misunderstand the logic 

 

The end stop will be low when at that side.

The instruction says if LOW and the direction says Take a step to the other side it will do so

After a few steps the end stop will no longer be LOW so it stops moving,

 

 

 

Don

Isn't that wrong?

It starts at position 1 with the decoder also set to position 1

To move it is looking at the state of the switch at the other end; position 2, which is LOW

So if the decoder now changes to position 2 it should step until the switch at position 2 changes to HIGH, at which point it stops stepping.

Meanwhile having left Position 1 that switch is now LOW, so if the decoder now changes back to position 1 it will now step back to position one until that switch goes HIGH

(Position 2 switch is now LOW so it can step to position 2 if the decoder changes to posotion 2 etc. etc.)

 

If for any reason the power goes off an it has stopped midway, both position switches will be LOW so the decoder position will determine which way it goes when power is re-applied.

 

Sounds simple but..........:(

Link to post
Share on other sites

26 minutes ago, melmerby said:

 

The direction input pin (from DCC) is switched between HIGH & LOW by the output from an opto coupler, presently I am just applying 12v or 0v  to the input, (which is the same as what comes from the decoder )

 

 

Ouch, I thought the max input voltage on a digital input pin was 5.5volts.

 

  • Agree 1
Link to post
Share on other sites

  • RMweb Gold

You should be applying 12v to a pin. You would be better off using INPUT_PULLUP and applying  0v when the switch detects the Deck.   I had stated way back that I was assuming the switches went low when the deck was at that end.

 

If you and I are at cross purposes no wonder we are having trouble  please confirm the connections.

 

The other problem is  the TMC2208 is designed to work with sophisticated software for 3D printers and the like MARLIN is frequently mentioned and the use of Serial to control the stepper.  Where the software is giving precise instructions as to how many steps to take.

 

BTW Have you got a heat sink on the TMC2208 one is recommended and for some uses they add a cooling fan due to the high currents the steppers can use.

 

 

Don

Link to post
Share on other sites

  • RMweb Premium
2 hours ago, tender said:

Ouch, I thought the max input voltage on a digital input pin was 5.5volts.

 

That's the input to the isolating opto coupler (6N137) which is approx the voltage the DCC decoder supplies, it is via a dropper resistance to limit the current through the diode, the output logic of the opto is connected to 5v as normal via a 10k pullup.

All supplies and inputs on the Arduino & TMC2208 are using 5v max.

Edited by melmerby
Link to post
Share on other sites

If it were me, i'd be unsoldering the link on the TMC2208 and using it in Legacy mode. Just have one reference micro switch and use the AccelStepper Library. Much easier to code and you get acceleration/deceleration at the start and finish of the run.

 

 

Ray

 

 

 

Link to post
Share on other sites

  • RMweb Premium
52 minutes ago, Donw said:

You should be applying 12v to a pin. You would be better off using INPUT_PULLUP and applying  0v when the switch detects the Deck.   I had stated way back that I was assuming the switches went low when the deck was at that end.

 

If you and I are at cross purposes no wonder we are having trouble  please confirm the connections.

 

The other problem is  the TMC2208 is designed to work with sophisticated software for 3D printers and the like MARLIN is frequently mentioned and the use of Serial to control the stepper.  Where the software is giving precise instructions as to how many steps to take.

 

BTW Have you got a heat sink on the TMC2208 one is recommended and for some uses they add a cooling fan due to the high currents the steppers can use.

 

 

Don

No 12v on the Arduino or TMC2208, they are powered from 5v with 5v logic signals.

With 5v motor supply neither the TMC2208 or the motor gets particularly warm (and there is a heat sink)

 

I have proven that you can control the TMC2208 just by changing the logic on the EN & DIR pins

As long as the step signal is running continuously in the loop,  EN controls whether the motor steps or not.

From reading the specs it seems it just disconnects power from the output transistors.

Changing DIR changes the direction of step.

 

I trialled it for some time just manually switching EN & DIR between LOW to HIGH and it never faltered.

 

I can't understand why, if the Arduino is still being refreshed in the loop that it can get to the state of not changing pin outputs in response to pin inputs

The problem seems to be getting the logic sequence correct, without sending the Arduino into sh*t mode:lol:

 

Link to post
Share on other sites

  • RMweb Premium

This is the original ino for this stepper driver:

(Nano pins)

 

// Define pins
#define EN_PIN    2  // LOW: Driver enabled. HIGH: Driver disabled
#define STEP_PIN  3  // Step on rising edge
#define RX_PIN    0  // SoftwareSerial pins
#define TX_PIN    1  //

#include <TMC2208Stepper.h>

// Create driver that uses SoftwareSerial for communication
TMC2208Stepper driver = TMC2208Stepper(RX_PIN, TX_PIN);

void setup() {
  driver.beginSerial(115200);
  // Push at the start of setting up the driver resets the register to default
  driver.push();
  // Prepare pins
  pinMode(EN_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);

  driver.pdn_disable(true);     // Use PDN/UART pin for communication
  driver.I_scale_analog(false); // Use internal voltage reference
  driver.rms_current(500);      // Set driver current = 500mA, 0.5 multiplier for hold current and RSENSE = 0.11.
  driver.toff(2);               // Enable driver in software

  digitalWrite(EN_PIN, LOW);    // Enable driver in hardware
}

void loop() {
  digitalWrite(STEP_PIN, !digitalRead(STEP_PIN));
  delay(1);
}

Edited by melmerby
Link to post
Share on other sites

  • RMweb Gold

I am sure it is something to do with the serial and the baord is expecting communication via serial. If it was working doing it manually I would be tempted to commen out the serial stuff and just see what happens.

Tender's suggestion of going to the legacy mode is not a bad idea and the serial connection seems to be used with sophisticated 3d printer software.

 

 

Don

  • Like 1
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...