Arduino 101 - Lesson 3

Discussion in 'Welcome to the DARK side' started by paul_l, Nov 17, 2017.

  1. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    looks like you stood up too quickly, and ran out of Tinto :avatar:
     
  2. Brian A

    Brian A Full Member

    Messages:
    91
    Likes Received:
    135
    Joined:
    Jun 12, 2019
    Paul,

    Thanks for the tutorials, got my Arduino starter pack yesterday and did all 3 lessons, good experience.

    I cheated on the last one as I used a button to simulate the home position.

    Now onto new projects, picked up a free e-book "Arduino Development Cookbook" lots of inspiration in there.

    First next steps a potentiometer to select tracks and i2c LCD screen for display.

    Thanks

    Brian
     
    Last edited: Aug 3, 2019
  3. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    Hi Brian

    When using a pot or voltage divider, do a couple of tests with power supplies.

    If you have the power supply that will finally run the arduino, create a sketch where you read the voltage value on the input pin (eg A0), using only the usb connection for power, display the value in com window. Now connect the external power supply to the arduino and do the same again. Check what the value are from both experiments.

    //Analog inputs

    int analogPin =A0;

    void setup()
    {
    Serial.begin(9600);
    }

    void loop()
    {
    int rawReading = analogRead(analogPin);
    Serial.println(rawReading);
    delay(2000);
    }



    On Victoria Roads analog controled traverser, I used a voltage divider via a 6 position rotary switch, I got different sets of voltages depending on the supply voltage, giving inconsistant results. To overcome this I had to build in ranges for each position to accomodate the variation.
    One of the big advantages of the Sparkfun Big Easy Driver, is it's ability to provide a stable 5V dc power supply for the arduino from its main supply (in my cases 20V to 32V power bricks).

    I also incorporated an Authorize push button, so that the act of rotaing the rotary switch, wouldn't start the traverser moving untill I had selected the correct road.
    It also provided a cheat for the startup routine, before powering on I would check the track alignment and adjust if required (no homing routine was in place). Then the rotary switch would be set to the current track position. On power up the switch position was read, and the current position set to that value.

    Paul
     
  4. Brian A

    Brian A Full Member

    Messages:
    91
    Likes Received:
    135
    Joined:
    Jun 12, 2019
    Paul,

    Thanks for the hint, I was playing around today and found variation in the potentiometer and so put in some gaps to ensure I got consistent results. after seeing your Victoria Roads I also implemented the Authorise button concept. I don't have my final power supply yet. So I will look for one that can supply a stable 5V as well as the main supply. I will do a test with that supply for certain.

    Cheers

    Brian
     
  5. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    Up till now I have used the Big Easy Driver, and for Viccky Road an Ex HP printer 32V 4A power supply, and for the Toto Traversers (Mk 2 & 4) I have used ex IBM / Lenovo 19 & 20 V 7A power supplies. The big easy driver supplies the regulated 5V supply for the nano.

    If you have a 9 to 12V external power supply, you could use either a LM7805 voltage regulator or a DC to DC Buck Jumper circuit

    When I try the TB 6600 I will need to change my supply arrangements. I know MERG have now revised most kits to be supplied with 12V instead of 5V, and then use the 7805 to step the voltage down to 5V. They found the voltage drop on long 5v Busses gave stability issues.

    Paul
     
    Andy_Sollis likes this.
  6. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    I think I’m going to have to go back to the beginning. I know once we’ve built the outdoor section of the layout, I’ll be coming back inside and building the yard and turn table. Plan is still to do the Peco OO turntable off a stepper motor and hopefully an Arduino but not sure yet if it will be pot controlled or dcc.
     
  7. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    With Toto's O gauge turntable, he bought a stepper with gearbox attached on to it, and rather than direct drive the plan was to use a GT2 drive belt with a 20T gear on the stepper final drive and a 120T gear on the traverser - the 120T gear was printed on the cTc printer.

    Paul
     
    Andy_Sollis likes this.
  8. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    So, resurrecting an old thread as I’ve recently done the peco turntable with the arduino and the same cheap stepper motor.

    but, on one direction, it never finds the road accurately, due to “backlash” in the gearbox.

    has anyone here seen or written anything in a sketch that can overcome this? In simplicity, it’s more a case of “if your going clockwise, add 10 to the preset number your turning to, but if turning anti clockwise, just stop at the set number”... if that makes sense? Sadly, my sketch writing is not yet up to working this out, despite having fumbled my way to a six button preset, speed up and slow down ramping after finding the zero with a Hall effect sensor..

    Yeah.. I know!

     
  9. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    Also, rather than use USB when on the layout, what do folk recommend for the Arduino to power it? I don’t think with the basic motor I need a separate power feed on this one?
     
  10. redpiperbob

    redpiperbob Full Member

    Messages:
    367
    Likes Received:
    299
    Joined:
    Aug 19, 2016
    Hi Andy
    I never got a gearbox to work without backlash. I changed to belt driven and never had a problem since.
    First I used 3D printed pulleys then I splashed out and bought two aluminium pulleys off Ebay.
    Heres a video of my belt driven unit.



    If you need any help please get in touch.
    Be seeing you
    Bob
     
    Andy_Sollis likes this.
  11. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    Hi Andy,

    is it always the same amount, as you may be skipping steps.

    For the traversers like Bob I use belts and Nema 17 stepper motors.

    Can you feel the backlash on the shaft ?

    Does it remain acurate if you only travel in one direction ?

    Paul
     
    Andy_Sollis likes this.
  12. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    It’s litterally “slop” you may say, each time you go clockwise it’s lines up, move back to one anti clockwise and it will miss, go to the position before zero and then come back to zero and it’s spot on. It’s litterally the slack in changing direction - so it’s just simply something I need to add to the sketch to say “if your going this way, keep to the set number, if you going the other way add 10 to the preset position.
     
  13. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    Very nice. Your using a slightly better stepper than me. I have the very basic one.
     
  14. redpiperbob

    redpiperbob Full Member

    Messages:
    367
    Likes Received:
    299
    Joined:
    Aug 19, 2016
    Hi Andy
    If you PM me I will send you a motor and a driver board I have spare.
    be seeing you
    Bob
     
    Andy_Sollis likes this.
  15. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    Thank you, but I’m up and working apart from adjusting the Sketch. No point changing stuff now as would risk going backwards.
     
  16. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    Just for viewing, I thought you may like to see the sketch I'm running.

    /* Example sketch to control a Peco Turntable in OO using a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO: acceleration and deceleration. By A.Sollis Oct 2020 */

    // Include the AccelStepper library:
    #include <Stepper.h>
    #include <AccelStepper.h>
    #include <Wire.h>

    // Motor pin definitions:
    #define motorPin1 8 // IN1 on the ULN2003 driver
    #define motorPin2 9 // IN2 on the ULN2003 driver
    #define motorPin3 10 // IN3 on the ULN2003 driver
    #define motorPin4 11 // IN4 on the ULN2003 driver

    // Define the AccelStepper interface type; 4 wire motor in half step mode:
    #define MotorInterfaceType 8

    // Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
    AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
    int Pin07Read ; // step Road 1 (head)
    int Pin06Read ; // step Road 2 (head)
    int Pin05Read ; // step Road 3 (head)
    int Pin04Read ; // step Road 1 (tail)
    int Pin03Read ; // step Road 2 (tail)
    int Pin02Read ; // step Road 3 (tail)

    //const long MOTOR_OVERSHOOT = -1; // the amount of overshoot/ backlash correction when approaching from CW
    //int overshootDestination = -20;

    void setup() {
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(2, HIGH);

    Serial.begin(9600); // Start screen print

    // Set the maximum steps per second:
    stepper.setMaxSpeed(300);
    // Set the maximum acceleration in steps per second^2:
    stepper.setAcceleration(15);

    //configure pin13 as an input and enable the internal pull-up resistor
    pinMode(13, INPUT_PULLUP);

    //read the sensor (open collector type) value into a variable

    int sensorVal = digitalRead(13);

    // if near reference point move away

    sensorVal = digitalRead(13);

    Serial.println("Finding Sensor Clockwise"); // Turn clockwise to find sensor
    while (sensorVal == HIGH) {
    sensorVal = digitalRead(13);
    stepper.moveTo(stepper.currentPosition() + 1000);
    stepper.runSpeedToPosition();

    delay(5);
    }

    // step forward to sensor index point
    Serial.println("Finding Sensor anti clockwise"); // turn to find sensor
    while (sensorVal == LOW) {
    sensorVal = digitalRead(13);
    stepper.moveTo(stepper.currentPosition() - 100);
    stepper.runSpeedToPosition();

    delay(50);
    }
    stepper.setCurrentPosition(0);

    }

    void loop() {

    // run to position with set acceleration
    stepper.runToPosition();

    Pin07Read = digitalRead(7) ;
    Pin06Read = digitalRead(6) ;
    Pin05Read = digitalRead(5) ;
    Pin04Read = digitalRead(4) ;
    Pin03Read = digitalRead(3) ;
    Pin02Read = digitalRead(2) ;

    // check which button is being pressed - chooses which road is set

    if (Pin07Read == LOW)
    {
    stepper.moveTo(-350);
    Serial.println("Current Road 1 -350 : ");
    }
    else

    if (Pin06Read == LOW)
    {
    stepper.moveTo(0);
    Serial.print("Current Road 2 zero: ");
    }
    else

    if (Pin05Read == LOW)
    {
    stepper.moveTo(350);
    Serial.print("Current Road 3 +350: ");
    }
    else

    if (Pin04Read == LOW)
    {
    stepper.moveTo(1725);
    Serial.print("Current Road 4 1725: ");

    }
    else

    if (Pin03Read == LOW)
    {
    stepper.moveTo(2090);
    Serial.print("Current Road 5 2090: ");
    }
    else

    if (Pin02Read == LOW)
    {
    stepper.moveTo(2400);
    Serial.print("Current Road 6 2400: ");
    }
    else
    {

    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);;
    Serial.print("Current steppermotor position: ");
    Serial.println(stepper.currentPosition()); // turns off the power to the stepper
    }

    }

    Still working on the overshoot/backlash section, so its in as text only at the moment.
     
  17. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018
    Anyone else seen my code and see what I’ve missed so it knows if it’s clockwise or counterclockwise ?

    If I can work that out, we can do that, I may be able to work out this backlash issue.
     
  18. Colin_W

    Colin_W Full Member

    Messages:
    230
    Likes Received:
    95
    Joined:
    Nov 19, 2016
    Hi Andy, did you sort this. I went the same way as Bob and upgraded the motor. However I have come across this video.

    The whole video is very good but checkout from 18mins onward. It may explain some of the problem (or not :lol:) but I do have the feeling as you say that there is a problem with slop in the plastic gearbox.
    Col
     
    Andy_Sollis likes this.
  19. Andy_Sollis

    Andy_Sollis Staff Member Moderator

    Messages:
    3,894
    Likes Received:
    3,621
    Joined:
    Aug 4, 2018

    No, got fed up of speaking to a number of folk on other forums promising help and I’m still waiting for a response..

    so I went route b... added two more push buttons and turned them in to nudge settings - so I can nudge the deck by a step or two each time.
     

Share This Page