Using SG90 or MG90 Servo Feedback Modification for Demonstrations With Arduino

by tekyinblack in Circuits > Arduino

153 Views, 0 Favorites, 0 Comments

Using SG90 or MG90 Servo Feedback Modification for Demonstrations With Arduino

Graph1b.png

Cheap model servos are ideal examples of a feedback mechanism but those sold are usually fully encapsulated and miss out on the potential for good feedback demonstrations as well as the additional uses the feedback signal can be put to. This instructable gives a brief description of a servo, how to modify a cheap servo to breakout the feedback signal, and the additional usefulness that signal can be put to in applications. Example code for demonstrating feedback with an Arduino and how to use it for overload protection, switch-on jerk avoidance and enhanced control is on github here.

Having a servo feedback signal is a really useful feature and these are some examples.

  1. You can detect when the servo has reached its target position. An SG90 servo moves 180 degrees in .3 second, which is a long time for a microprocessor so a program can be doing other things and checking back if the target is reached.
  2. You can prevent damage to servos by checking that a servo has not got stuck or overloaded and is overheating by say checking it's position after .3 second and if it's not at it's target position, updating the target position to the current position, effectively stopping the servo and allowing an error to be raised.
  3. Point 2 can be used for measuring how far a servo can move for calibration purposes. This can be useful for grippers, cranes etc.
  4. Servo start-up jerks can be avoided by reading the servo position before initialising the servo, and then setting the start position to the current position.

Adding a feedback wire to a servo means that the current position of a servo can be determined irrespective of what the drive signal is.

The examples given at the end are for demonstrating some of these ideas, there will be many more.

Servos with a feedback signal are available commercially and these ideas are equally applicable to those but probably with some adaption to the code.

Supplies

IMG_0259-copy.JPEG

SG90 or MG90 servos

Dupont connectors with appropriate ends. Servos are regularly supplied with female ends on their connectors but a male end can be used on the feedback lead if preferred.

Tools needed,

solder

small tipped soldering iron (2mm if you have one)

small cross point screwdriver

small flat screwdriver

wire cutters

helping hands tool (usually crocodile clips on a support :)) or a small vice

How a Servo Works

servo simple.png

This is a very simple explanation of servo operation of the type we are going to modify.

  1. The shaft of a motor is attached to a gearbox which reduces the number of revolutions of the motor and increases it's torque on the output shaft, to which the control arm of the servo is attached.
  2. A potentiometer is mounted on the output shaft of the gearbox so that it turns with the shaft.
  3. The potentiometer is connected to a reference voltage which is reduced at the wiper arm in proportion to the angle of turn of the potentiometer, so as the motor turns, so the output of the potentiometer varies. This is the feedback signal.
  4. A control signal is received, processed and compared to the feedback signal, and if there is a difference, then the motor driver is signalled to turn the motor in the required direction to eliminate the difference. When the feedback and control signal are compared and found to be the same, the motor control turns the motor off.

In this way, sending the control signal to the servo causes the servo output shaft and arm to be moved to the required position. In this instructable we are going to modify the servo to read the feedback signal so that it can be used to determine what the output shaft position is and used by the program running on the Arduino.

Dismantle the Servo

IMG_0285-copy.JPEG
IMG_0260-copy.JPEG
IMG_0261-copy.JPEG
IMG_0262-copy.JPEG

Dismantling the servo is easy but you may need a magnifying glass to see the screws properly and good light. Before dismantling, test that the servo works ok as this makes fault finding easier as you start with a known position. Test program 1 in a later step is useful for doing this.

  1. Peel the label back to reveal the join between the base and body of the servo. Unless the label has a purpose for you, it can probably be removed completely.
  2. Use a very small cross head screw driver to remove the long screws from the base. The servo shown in the picture has two screws, some SG90 servos have four, the procedure is the same.
  3. Holding onto the body of the server, not the ears, gently pull the base off to reveal the servo control board inside.

Trim a Connector Lead

IMG_0263-copy.JPEG

Chose a dupont lead of the required length, I cut one down to the length of the servo leads, making sure to keep the required connector attached, I use female ends to be compatible with the SG90 servo connector.

Strip the end of the lead, about 8mm, and tin the whole length with solder. Then cut it down further to about 2mm in length.

Attach the Connector Lead

IMG_0264-copy2.png
IMG_0293-copy.JPEG
IMG_0288-copy.JPEG

The centre pin highlighted attaches to the wiper of the servo feedback potentiometer and it is to this we need to solder a connecting wire.

Carefully attached the wire with a small blob of solder and ensure it bonds securely. Take care not ro melt the wire insulation to much. Allow to cool and then bend the wire around the circuits to align with the three other servo wires, taking care not to connect to any other circuits.

Modify the Servo Case

IMG_0270-copy.JPEG
IMG_0276-copy.JPEG

The servo case has only a gap for three wires to pass through so the gap needs to be expanded to accommodate the fourth wire.

Using a small file, widen the gap until all four wires fit comfortably. In the servos I've modified, there appears to be space next to the gap reserved for this purpose.

Reassemble the Servo

IMG_0279-copy.JPEG
IMG_0290-copy.JPEG
IMG_0292-copy.JPEG

Refit the modified base over the wires. It may be that the new connection is too high and prevents the base fitting correctly. If this is the case, the new lead will have to be de-soldered and step 3 repeated.

Insert screws and screw down the base securely.

If required, refit the labels.

Now test the servo operation.

Test and Demonstration

test circuit arduino_bb.png
graph2.png
updates.png

The circuit shown is only for demonstration and testing unloaded servos as it is not capable of delivering sufficient current. If your servo does not operate in this configuration then you may need to supply additional power, either by driving the Uno from the power jack or by powering the servo directly from a separate power source. If you do this, make sure that the negatives of both Uno and power source are connected.

There are four test/demonstration programs. Before running these programs fit a servo arm to the output shaft to better indicate the servo position.

  1. Test the servo panning from 0-180-0 degrees and reading the feedback output. The output is formatted for input to the Arduino serial plotting screen. This also verifies that the servo is working after the modification.
  2. Initialise the servo to its current position on start up. Switch off, move the servo arm and switch on so that the operation can be confirmed. Compare this to the way the servo behaves on start up with program 1.
  3. Stop the servo being overloaded. With this program running and the servo arm fitted hold the servo arm tight when it tries to turn, the servo should initially attempt to turn but then stop with an error message on the serial console
  4. Report the current servo position. The servo will move and a report will be sent to to the serial console where the arm is and when it reaches its destination.

The code for these is held on github here. No modification is required for test program 1 but the other programs require some calibration with different production runs of SG90 or MG90 servos.

To calibrate, run test program 1 with the modified servo attached and ensure it is working correctly.

View the serial output on the serial console and record the maximum and minimum values of the feedback signal. In the tests on my batch of SG90's, these were 606 and 50.

In each of the test programs, update the mapHigh and mapLow values with the values from your servo. This is to ensure correct tracking between the servo and Arduino program.

Issues

This is a basic get you going example and there may be issues in final uses. Some of these are as follows.

  1. The power supply has already been mentioned but when driving servos in the final solution it's best to have a separate smoothed power source for them with sufficient power for peak loads, think a minimum of 600mA per servo.
  2. The examples use the Arduino's power supply as the voltage reference for the analogue measurements. If precision is needed in a final solution, the Arduino should have an independent voltage reference connected and used. A TL431 is a good example of this.
  3. The feedback voltage measured here is around 3V peak, but this may vary between servo examples. Before connecting to lower voltage microcontrollers, check the output voltage with the example configuration of a multimeter to avoid damage.
  4. For some measurements it may be required to reduce the feedback voltage before measurement. This can be done with a voltage divider but ensure that if this is done, that the resistance of the divider circuit is at least 100K to avoid interfering with the servo operation.
  5. Arduino has good analogue support but other platforms may have fewer connections and require separate ADC controllers attached. The principle remains the same however.
  6. The Arduino development environment if often used with the ESP32 and if this is used in a project like this, be aware that the the ADC on some ESP32 models is not linear and may not work as expected. This will be obvious when running test program 1.

Conclusion

IMG_9285.JPEG

I hope this gives some inspiration to investigate this little used feature of servos. I'll be using this feature in the L0Cost Robot designs elsewhere on Instructables as well as with other platforms.

Have fun.