Wednesday, August 16, 2017

Arduino based 8 servo modules with frog polarization Loconet

ClubNCaldes has developed a new Loconet module to manage switches, based of course in Arduino. Our previous version (see this old post) was using a simple sensor shield that manages servo motors connected directly to Arduino PWM pins. It's been working with no problems, but we noticed some servos glitched a little bit from time to time. Probably it was because no external power source was used to feed the servo motors, and it was taking the 5v power directly from the Arduino.

Then new module and firmware uses an specific servo shield with its own PWM generator, and communicates through I2C bus, so the rest of the pins are free to use. It has an external 5Vcc connector to use a separate power source for servos. The result is a smother servo movement with no glitches or interference.

Here you have the instructions to build your own module:


PCA9685 I2C 16-Channel 12-bit PWM/Servo Drive shield For Arduino
You can find it in ebay for less than 5€.

1 x Relay board
There are 1, 2, 4, 6 or 8 channel optoisolated relay boards for arduino. Take the one you need according to the number of servos and switches you want to manage.

Dupont cable female - female
This is used to connect the relay board to the servo board.

Servo motors
In ebay you can also find cheap TowerPro 9G servos, enough to manage a N or H0 scale switch.
Servo mounting bracket
My favorite way to fix servos under the table is with aluminium servo brackets from Hobby King.

Arduino UNO
Standard Arduino, you can also use NANO or MEGA version, but the provided firmware has been only tested with UNO VERSION.
Loconet Shield (GCA185)
This is always needed in any Loconet module implemented in Arduino, you can make your own or buy a GCA185 board to Peter Giling (see Rocrail page).


Just put Servo shield on top of Loconet shield, and that last one on top of Arduino UNO.
Upload the SVLoco8ServoRelay sketch to Arduno UNO board. You can download it from:


You have to feed the servo shield with a 5Vcc power source, be careful to respect the polarity shown in the board when connecting. Servos and relays are connected to the servo shield pins as indicated in the following photo. The servo connected to pin 0 uses the relay connected to pin 8, and so on:

Keep in mind these pins on the servo shield are independent of the pins on the Arduino board. Only Arduino pins 0 and 1 are used for debugging purposes, and Arduino pins 7 and 8 communicate with the Loconet shield. The rest of Arduino pins are free to use, so you can change the firmware if you like.


Programming and adjusting is done from Rocrail as in the previous version. (NOTE: a new version using standard CV programming is planned to be released soon). Access to GCA50 configuration in Rocrail menu:

Pressing the query button it should appear the connected servo module. By default, it will have the address 81/1:

Selecting your module, you can change its Low and Sub addresses from the General tab (two modules cannot have the same Low and Sub addresses). Be careful, the RESET button of this screen will reset ALL modules connected to you Loconet network, not only the selected one:

Then you can access the Easy Setup tab, here you assign an output number to each of the 8 servos (left part of the screen marked in red), and a number to the input that tells to the sofware when a servo motor has finished its movement (servo feedback signal). Normally, the same number is assigned as in Loconet inputs and outputs do not share the same range of numbers. Set up the screen as in the following image:

To adjust the servos go to the last tab named "Servo":

Each of the ports (1 to 8) select one of the 8 servos that can be connected. When pressing the Get button it will read the configuration:

Pos1: one of the positions of the servo, with a value from 1 to 127 (1=0º, 127=180º)
Pos2: second position of the servo, with a value from 1 to 127 (1=0º, 127=180º)
V: Speed of movement from 0 (slow) to 5 (fast)

The SET button will overwrite the values and servo will move to POS1, wait one second, and will move back to POS2 so you can visually check the adjustment.

I recommend you to first set Pos1 and Pos2 values to 63. This will center the servo. Then mount the servo in place, and bit by bit reduce the value of Pos1 and increment the value of Pos2 until archive the desired servo movement.

1 comment:

  1. Hello Mister Guisado,

    thanks for your great work. I used an Arduino Nano, the PCA9685 and your code.

    All worked fine, i was able to configure with GCA50 in RocRail.
    But i am not able to use the decoder with the switches, i can't
    calculate the values for the switches,

    Decoder ist configured to 100/1, the ports are starting with 11 like
    in your screenshot.

    Can you please give me an advice?

    Thanks a lot and a happy new year