Introduction
The Gcodes M42 Switch I/O pin and M280 set servo position allow direct control of some pins, or re-purposing of those pins to control PWM hobby servos.
CAUTION!
Important considerations when connecting servos!
'Hobby' servos can be connected and controlled by the Duet range of boards. However, there are limitations on the size of servo that can be powered directly, due to the current draw of the servo (particularly when it stalls, i.e. 'stall current'), and the current the servo can generate when it stops or is forced to turn ('regenerative braking'). Both of these can have a damaging effect on the Duet and/or connected peripherals, eg PanelDue.
Stall current
Stall current is the maximum current drawn when the motor is applying its maximum torque, either because it is being prevented from moving entirely or because it can no longer accelerate given the load it is under. This is 'under normal conditions', i.e. the motor isn't being asked go from max speed in one direction to max speed in the other, which can generate even higher currents.
Your servo's stall current should not exceed the Duet's available current (see Duet board 5V current limit below), and it's advisable to have some spare current capacity, so don't specify a servo that uses up all the remaining current. The effect of a servo drawing too much current will be to drop the voltage on the 5V rail, most likely leading to a Duet board reset, but may damage the 5V regulator.
Servos that are commanded beyond their movement limits usually stall. This heats up both the servo and the Duet. The servo is likely to burn out if held in this state. So:
- Ensure that the Duet can supply the stall current.
- Either be very careful not to command the servo beyond its working range, or else command the servo to move, hold it for a short time, then use M280 P# S0 to stop commanding the servo (replace # by the pin number or GpOut number as usual).
Regenerative braking
When a servo stops moving, it can pump a large amount of current into the +5v rail due to regenerative braking (the servo motor effectively becomes a generator). If unchecked, this may increase the voltage of the +5V rail above the safe limit. This applies whether the servo is connected directly to the Duet or to an attached expansion board or expansion breakout board. See 'Recommendations' below.
Very generally, there is a correlation between stall current and the amount of current a servo produces in regenerative braking. However, a servo that has a stall current close to the available 5V current from the Duet might still pump enough current into the 5V rail to be a problem.
Recommendations
- 5V rail voltage rise due to regenerative braking can be solved by connecting a Schottky diode between Duet +5V output and the servo +5V power input, and a 220uF capacitor between servo +5V and servo ground
- Both issues can be avoided by powering the servo from a separate +5 supply.
Duet board 5V current limit
Current limit is the total for 5V and 3.3v. Subtract any current draw, including the internal current consumption (usually around 200-300mA), any PanelDue or other display, connected and powered SBC and peripherals, and any endstops/Z probes that draw significant power, to find the available current for a connected servo.
5V current limit | |
Duet 2 WiFi / Ethernet / Maestro | 2.0 A |
Duet 3 Mainboard 6HC | 3.0 A |
Duet 3 Mini 5+ | 1.0 A |
Hobby servo specification examples
There are many different types of hobby servos, and we do not recommend any particular one. Check the specification of any servo you connect to your Duet, in particular the idle, running and stall current. Generally, so-called '9g' servos should be okay to connect directly (but please check your servo's specs! And see note below about Duet 3 MB6HC), but larger servos should have additional protection, or be powered independently of the Duet (see Recommendations).
Servos of similar specification to this should be fine to connect to a Duet without extra circuitry:
https://www.towerpro.com.tw/product/sg90... | TowerPro SG90 9G micro servo] |
Description | 9g micro-servo |
Idle current | 6 ± 10 mA |
Running current | 220 ± 50 mA |
Stall current | 650 ± 80 mA |
This motor would require extra circuitry (see Recommendations) or risk damage to the Duet and/or connected peripherals, particularly due to Regenerative Braking.
TowerPro MG995 | |
Description | 55g X-large servo |
Current draw at idle | 10 mA |
No load operating current draw | 170 mA |
Stall current draw | 1200 mA |
Notes on specific Duet boards
Duet 2 WiFi / Ethernet / Maestro
On the Duet 2 WiFi / Ethernet / Maestro, if a servo feeds power back (regenerative braking) then the internal 5V rail voltage will increase. This could damage other devices connected to +5V (e.g. PanelDue), also if the rise is high enough it could blow the mosfet drivers (rated at 7V) and the 3.3V regulator (rated at 6.5V). If the 3.3V regulator goes, it may take out other components on the board too.
Duet 3 Mainboard 6HC
On Duet 3 MB6HC, the internal and external 5V are separated, but the built-in 5V regulator regulates the external +5V rail (this was to provide consistent power for an attached RPi). This means that if a servo pumps power into external +5V, the internal 5V power will drop out for a short time, causing the board to reset. Again, any other devices connected to external +5V e.g. PanelDue may be damaged. Even small '9g' servos may be able to do this.
Duet 3 Mini 5+
On Duet 3 Mini 5+, the internal and external 5V rails are separated, however the built-in 5V regulator regulates the internal 5V power. So a servo pumping power into external 5V won't damage the Duet or cause it to reset, however it could damage other devices powered from external +5V e.g. PanelDue or 12864 display.
RepRapFirmware 3.x and later
In RRF 3.x and later, pins are undefined at startup. Before you can use M42 or M280, you must create a GPIO port using M950. Then in the M42 command, the P parameter is the GPIO port number, not the logical pin number. Duet 2 supports up to 10 GPIO ports, numbered 0 to 9. No GPIO ports are allocated by default.
In RRF_3, the F and I parameters are no longer supported in M42. Instead, use the F and I parameters in M950 when you create the GPIO port.
Example - RRF_2 code:
M42 P3 I1 S0.5 F500 ; set Heater 3 pin to 50% PWM at 500Hz
RRF_3 code:
M950 P0 C"exp.heater3" Q500 ; allocate GPIO port 0 to heater3 on expansion connector, 500Hz ... M42 P0 S0.5 ; set 50% PWM on GPIO port 0
For a list of pin names, see RRF3 Pin names.
RepRapFirmware 2.x and earlier
The rest of this page shows how to set up M42 and M280 in RRF 2.x.
Logical pin numbers
Starting with firmware version 1.16, pin numbers in the M42 command are logical pin numbers as listed below, not Arduino-like pin numbers. The M280 servo control command uses a subset of the same pin numbers.
Note that these logical pin numbers are not the physical pin numbers on the expansion header, or the internal pin numbering within the firmware.
To see where these pins are on the 2nd Generation Duets, see the Duet wiring diagrams
Pin numbers | Usual function | PWM capable? | Usable for servos? | Notes |
---|---|---|---|---|
0-7 | Heaters 0-7 | yes | yes | Disable the heater using M307 H# A-1 C-1 D-1 to make the pin available, where # is the heater number, and make sure that no tool is configured to use that heater. The outputs on the expansion connector are active low with 3.3V signal level. On the Duex 5 expansion board, heater channels 3-7 are available as 5V active-high signals on the PWM1 - PWM5 connectors respectively. On the DueX2 from version 0.8a onwards, heater channels 3-7 are available on PWM1 - PWM5, prior to that heater channels 3-5 only are available on PWM1 - PWM3 . Caution: the heater output terminals will also be driven by the PWM signal or servo pulses. |
20-27 | Fans 0-7 | yes | 20-22 only | Disable the fan using M106 P# I-1 to make the pin available, where # is the fan number. Fans 3-7 are on the Duex 5 and Duex 2 expansion boards. |
40-49 | Endstop connectors 0-9 | no | no | 40-44 are input-only. On the Duet 2 WiFi and Duet 2 Ethernet, if a Duex 2 or Duex 5 expansion board is connected then 45-49 are the endstop connectors on the expansion board and are input-only; else they are additional endstop connections in the expansion connector. On the Duet 0.6 and 0.8.5, 45-48 are the endstop inputs on the expansion connector. |
60- | Miscellaneous pins | varies | varies | The pins available depend on which Duet you have - see below |
100- | Pins on expansion devices | varies | varies | The pins available depend on which Duet you have - see below |
Board dependencies
Duet 2 WiFi and Duet 2 Ethernet
The miscellaneous pin numbers 60-63 correspond to the CS5-CS8 pins on the expansion connector. These were originally allocated for use as the E2-E6 endstop pins and are labelled as such on the expansion connector, but they are now used to address thermocouple and PT100 daughter boards on a Duex 2 or Duex 5 instead. Because most users don't need more than the four thermocouple and PT100 channels supported on the Duet 2, we make CS5-8 available as general purpose I/O if you do not have any daughter boards fitted to the Duex 2 or 5.
It will be helpful to refer to the wiring diagram for the Duet 2 Wifi/Ethernet and the Duex to identify the pins.
Here is the mapping between pin numbers, signal names, and the labels on the expansion connector:
Logical pin | Signal name | Expansion connector label | Expansion connector pin number | PWM capable? | Servo capable? | Notes |
---|---|---|---|---|---|---|
60 | CS5 | CS5 | 50 | no | no | |
61 | CS6 | E3_STOP | 9 | no | no | |
62 | CS7 | E4_STOP | 14 | no | no | |
63 | CS8 | E5_STOP | 19 | no | no | |
64 | ENC_SW | ENC_SW on CONN_SD connector | 7 on CONN_SD connector | no | no | |
65 | Z_PROBE_MOD | MOD on Z-probe connector | 3 on Z-probe connector | no | no | |
100-103 | GPIO pins 1-4 | yes | no | These are on the Duex 2 and Duex 5 expansion boards | ||
120-135 | SX1509B expansion I/O pins | yes | no | These are on an SX1509B-based expander board at I2C address 0x71 connected to a Duet 2 Wifi/Ethernet |
Logical pins 64 and 65 are supported in firmware 2.01 and later only. Note that the Z_PROB_MOD pin is driven automatically when some Z probe modes are selected.
Duet 2 Maestro
It would be helpful to refer to the wiring diagram for the Duet 2 Maestro to identify the pins.
Logical pin | Signal name | Expansion connector label | Expansion connector pin number | PWM capable? | Servo capable? | Notes |
---|---|---|---|---|---|---|
60 | PA21/RXD1/AD8 | EXP0 | 4 | no | no | |
61 | PA22/TXD1/AD9 | EXP1 | 5 | no | no | |
62 | PA3/TWD | TWD0 | 9 | no | no | |
63 | PA4/TWC | TWCK0 | 8 | no | no | |
64 | Z_PROBE_MOD | MOD on Z_PROBE connector, also SERVO on expansion connector | 3 on Z_PROBE connector, 11 on expansion connector | yes | yes |
Duet 0.6 and 0.8.5
The mapping between logical pin numbers for miscellaneous pins and the corresponding signal names on the expansion connector is as follows.
Logical pin number | Expansion connector signal name |
---|---|
60 | PA10/RXD0 |
61 | PA11/TXD0 |
62 | PA12/RXD1 |
63 | PA13/TXD1 |
64 | PA14/RTS1 |
65 | PB12/TWD1 |
66 | PB13/TWCK1 |
67 | PB16/DAC1 (also used as CS signal on external SD card socket) |
68 | PB21/AD14 |
69 | PC4 |
Image from wikipedia
7 Comments
Dear Sirs
How to connect Duet 2 WiFi to 3 servo drivers?
chivel - Reply
hi Tony,
Is there any way to use servo motors with Duet 3 6HC , for moving axis? Or any other way to use intermediate device to operate a servo ? Any way we can use external circuitry to use pulse and direction outputs to connect to different motor other than stepper motor?
Jayshree - Reply
Hey Jayshree. The Step/direction pins on the 6HC are not available for external step/servo drivers however we have the 1XD to connect an external stepper of servo motor:
Duet 3 Expansion 1XD
Tony -
Are we supposed to be using M571 instead of M42 on v3.0 of firmware?
Mitchell - Reply
Is there anyway to remap the z probe input pin?
Failsafe125 - Reply