NXT servo controller

I built a vehicle that only used one RC style servo motor. To get the mindsensors servo controller just to control one servo seemed sort of pointless. I like the challenge of building circuits and programs, so I figured I’d try to build my own controller. I had gotten quite familiar with the PICAXE system, so I decided to try building the servo controller using a PICAXE 20X2.

The initial servo function tests showed great promise, so I added I2C slave and changed a couple other things in the code. And guess what! The PICAXE I2C code, hardware, and NXT program worked perfectly on the first try 🙂

The PICAXE supports up to 6 servos (when also using I2C), but I didn’t have enough header pins to build it that big. This can only control 3 servos.

Because on a solderless breadboard it didn’t have any issues with 6 outputs, I drew the schematic showing all six.

Edit (6/13/11): I was just informed that for the safety of the 20X2, it is recommended that you place a 330 ohm resistor on all the signal wires that go to the servos. I heard these protect the 20X2 from damage by plugging the servos in backwards. The schematic as follows has been updated to include these resistors.

I went to get the code I used for the PICAXE, but I can’t find it anywhere… I think it may be lost.

I rewrote the program from memory, and then modified it to be more efficient (and added a feature). I tested this code with a 20X2 that is on a breadboard, and it seems to work perfectly.

setfreq m32                      'Make sure it is running at 8mHz or 32mHz clock speed.
hi2csetup i2cslave, %00110000    'Set the I2C address.

  symbol SERVO_POS = B0          'Set up the names of the variables.
  symbol SERVO_PIN = B1          '              ''
  symbol LOOP_i    = B2          '              ''
  symbol CURRENT_FLAG = B3       '              ''
  symbol CURRENT_FLAG_REG = B4   '              ''

  pullup %10100000               'Enable internal pullups on the I2C lines.
  for LOOP_i = 0 to 5            'Loop 6 times (0-5)
    CURRENT_FLAG_REG=LOOP_i+10   'Set the current flag register number
    if LOOP_i=5 then             'If it is on it's sixth loop...
      SERVO_PIN=6                '...set the pin to B.6.
    else                         'Otherwise...
      SERVO_PIN=LOOP_i           '...set pin to loop increment.
    get LOOP_i,SERVO_POS         'Get the servo positon (from the I2C register).
    if SERVO_POS=0 then                 'If the position is 0...
      INPUT SERVO_PIN                   '...float the servo...
      put CURRENT_FLAG_REG,0            '...and set the flag to 0.
    else                                'If the position isn't 0...
      get CURRENT_FLAG_REG,CURRENT_FLAG '...check the flag.
      if CURRENT_FLAG=0 then            'If the flag is 0...
        SERVO SERVO_PIN,SERVO_POS       '...turn on the servo at the new position...
        put CURRENT_FLAG_REG,1          '...and set the flag to 1.
      else                              'If the flag is already 1...
        SERVOPOS SERVO_PIN,SERVO_POS    '...update the servo position.
  next LOOP_i                    'End of loop.
Goto main                        'Return to main (repeat forever).

Now you can give the servos a 0 to turn them off (make them float).

In a future redesign, I may add parts that would allow me to monitor the voltage of both the power supply, and the NXT power bus. That would require an LDO voltage regulator, and a voltage divider (each would only require 2 parts plus some code).

This entry was posted in Electronics, Mindstorms, NXT, PICAXE and tagged , , , , . Bookmark the permalink.

9 Responses to NXT servo controller

  1. Xander says:

    Nice write up, man. Your diagram looks great.

  2. NXTreme says:

    I don’t know if you’ve considered this option for monitoring voltage levels, but a voltage reference chip is a pretty good option. The 20X2 has one built in, or you can buy one. I have used the MAX6018 with VERY good results.

    I have a post on the Picaxe forums that explains a bit of my experience with the v-ref chip and shows a simple way of breaking it out. I pretty much just soldered two legs of the chip to two legs of headers, and then used a thin wire to solder the remaining leg to the remaining header, and soldered the required cap to the other side of the header pins. [Link]

  3. pepijndevos says:

    What does “power and jumper” do? Is this for connecting the ground of the NXT to the ground of the servo power supply?

    Looking at the third part of the guide, it seems they connect the ground of the servo power supply to the PCIAXE ground with a 100n capacitor, which I don’t see in your design.

    • mattallen37 says:

      Yes, power and jumper are for creating a common ground. Since not all servos follow the “standard” pinout (specifically the servos I was using), I needed a way to switch the power signals based on the servo requirement. I also needed to maintain a common ground.

      That 0.1uF capacitor is to reduce noise on the power lines. It isn’t critical, especially when powering the servos with batteries that can supply peaks of over an amp. I would recommend it though if you need multiple servos, and you want them to be as smooth as possible. I would also add a 100uF capacitor to the power supply if you need even less noise.

  4. pepijndevos says:

    Now that I understand your code, I wonder… Why did you use internal pullups? Why do you loop constantly, instead of using the i2cwrite interrupt?

    Also, I wonder if it would be hard to make the API compatible with the Mindsensors one, but maybe it is, I did not check.

    • mattallen37 says:

      I used internal pullups to save external parts. This was primarily because I didn’t have room for them externally. However, you can mount them externally if you want.

      I didn’t use an I2C interrupt, because I didn’t know such a thing existed. Can you please tell me how to use it?

      For basic position control, it shouldn’t be too hard. I think mindsensors used some special control features (like ramping to position) which my simple program doesn’t support.

  5. Pingback: DIY Servo Controller

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s