Getting an SG90 servo to respond

Uncle peanut butter Jul 8, 2020

  1. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    My components:
    Arduino Mega2560
    Arduino Motor Shield R3
    PCA9685
    SG90 servo on position 0 of the PCA9685
    I have these wired as instructed. I am using a 5vdc 3 amp power supply into the PCA9685
    The power light on the PCA9685 is on (red). So i feel like I have the hardware side up to speed.
    My issue:
    I have the Adafruit PCA9685 driver package in my Arduino IDE and am attempting to get the servo to respond. I also have tried a sketch that does the same thing that i downloaded from Toms Trains and Things.
    How do I send a command through my IDE to get the servo to respond? Does the sketch I downloaded need to be compiled?
    Thanks in advance
     
  2. Ash

    Ash TrainBoard Member

    106
    67
    8
    DCC++ sends commands thru the rails to decoders in the locomotives, but I did not understand that I needed a separate Arduino to decode/forward commands to control servos, lights, relays, sensors and fascia buttons.

    I am using JMRI as the front end to control accessories via CMRI. In addition to the Arduino/motor shield used for DCC++, I have 3 other Arduinos connected via RS485. This link shows the RS485 wiring:
    https://www.jmri.org/help/en/html/hardware/arduino/index.shtml#multi

    Here is a link to my progress. There is also the sketch I use on each of the arduinos used to control servos.
    https://www.trainboard.com/highball...-out-card-for-jmri.116454/page-2#post-1141569
     
    Uncle peanut butter likes this.
  3. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    So one sketch per arduino?
     
  4. Ash

    Ash TrainBoard Member

    106
    67
    8
    I think so. I'm still quite new to this. I can explain what I did -- mostly from trying to get the pca9685 and mcp23017 boards recognized from JMRI.

    This is the sketch I used, when I was only working with pca9685 and servos. You'll notice that all my servos are set to 100 or 400 -- about a 90 degree rotation. (more notes below)

    Code:
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    #include <CMRI.h>
    #include <Auto485.h>
    
    #define CMRI_ADDR 1   // unique for each Arduino
    #define DE_PIN 2
    
    int Tbit[32];
    uint8_t servonum = 0;   // servo counter
    uint8_t lastservo = 32;
    
    Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);    //setup the board address 0
    Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41);    //setup the board address 1
    Auto485 bus(DE_PIN); // Arduino pin 2 -> MAX485 DE and RE pins
    CMRI cmri(CMRI_ADDR, 24, 48, bus);
    
    
    void setup() {
      Serial.begin(9600);
      bus.begin(9600, SERIAL_8N2);
      pwm1.begin();
      pwm2.begin();
      pwm1.setPWMFreq(60);
      pwm2.setPWMFreq(60);   
    }
    
    void loop() {
       cmri.process();
       for(servonum = 0 ; servonum < lastservo; servonum ++){
    
        Tbit[servonum] = (cmri.get_bit(servonum));     
    
        if(servonum <16) {
            if (Tbit[servonum] == 1){
                pwm1.setPWM(servonum, 0, 100);
              }
            if (Tbit[servonum] == 0){
                pwm1.setPWM(servonum, 0, 400);
              }
          }
          
        if(servonum >=16 && servonum <= 31){
            if (Tbit[servonum] == 1){
                pwm2.setPWM(servonum - 16, 0, 100);
              }
            if (Tbit[servonum] == 0){
                pwm2.setPWM(servonum - 16, 0, 400);
              }
          }   
        // add additional lines for additional boards
    
      }
    
    }
    
    Notes:
    1. Before installing my servos, I cycle them several times on my test bench. Then I put the lever on the servo at +/- 45 degrees. Then I cycle the servo again to check. (I am using 0.016" music wire -- its flexibility means that I may not need to tune the settings for each servo. HO scale. My servo bracket is a 2-inch length of aluminum channel.)

    2. I use a Mega 2560 on my test bench; the pcb boards I built use Arduino Pro Mini. I think the Nano might have been simpler because (a) it has an onboard usb, and (b) I wouldn't have to contend with the odd placement of the SCL/SDA pins on the Pro Mini. If you opt for the Pro Mini, make sure to get the 5-volt version, and you will also need a TTL adapter to program it (and the adapter has a mini-USB connection -- not micro-USB).

    You may need to disconnect RX/TX and VCC when you program the Arduino, if connected to RS485 and separate power supply.

    3. Check out the translated website -- their sketch includes servo tuning and saving the results to Arduino memory. The website also has instructions to create the CMRI connections and other JMRI setup.
    https://translate.google.com/transl...8/05/criando-um-cco-no-panel-pro-do-jmri.html

    4. You could use DCC for accessories. If you go the DCC route, you might be better prepared should you decide to purchase a commercial DCC controller. (Perhaps someone will expand on other benefits to using DCC for accessories.)

    For DCC, you still have the additional Arduino to decode the DCC and relay the information. Instead of RS485, there is optoisolator circuitry to interface to the tracks, and DCC library for your sketch, etc.

    5. You could use CMRI on just one Arduino via USB. I am using RS485, as I have three Arduinos for accessory control. Each Arduino can have one PCA9685, and three MCP23017 modules -- seems to be a limitation of the CMRI/SMINI emulation. You'll see how others have designed 24/48 input/output boards. My approach is using mostly pre-built modules with pcb prototype boards for connectors and wiring.

    6. Do not use your DCC++ Arduino for CMRI testing; get an additional Arduino. JMRI will complain if it doesn't find DCC. I don't know what problems that might cause -- just suggesting that if you are happy with your DCC++ base station and your current setup in JMRI, don't break it.
     
    Uncle peanut butter likes this.
  5. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    Matriculation coming up!
    EBF5AEB7-736F-437E-BDA8-3CCEC4435D8C.jpeg
     
  6. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    @Ash
    Wonder if this will help.
     

Share This Page