This post is about how to turn on / off the power to servo motor using MOSFET or TIP120. According to the article at the end of this post, MOSFET is the better choice due to less power consumption. However, I've observed that it takes less voltage to turn on TIP120 than IRF530 or 2N7000. So for low power devices such as RPi that outputs 3.3V and control a servo that runs on DC 6V, it may be a good idea to use TIP120 (or, there needs to be a way to level-shift the 3.3V to 5V).
Using MOSFETUsing TIP120Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* Adafruit Arduino - Lesson 14. Sweep */ #include <Servo.h> int servoPin = 9; Servo servo; int angle = 0; // servo position in degrees void setup() { servo.attach(servoPin); } void loop() { // scan from 0 to 180 degrees for(angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for(angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); } }
|
--------------------------------------------------------------------------------------------------------------------------
Below is the schematic for turning on/off a servo motor using 2N7000 and a 3.3V control signal.
--------------------------------------------------------------------------------------------------------------------------
The code below adds power control to the above example using interrupt. The power control pin is connected to pin 2 of the Arduino. When pin 2 is low, the servo will sweep from 0 to 180 and back. When pin 2 goes from low to high (RISING), the power to the servo motor is cut and the counting stops.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <Servo.h> int servoPin = 9; int powerCutDetectionPin = 2; Servo servo; int angle = 0; // servo position in degrees void setup() { servo.attach(servoPin); pinMode(powerCutDetectionPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(powerCutDetectionPin), empty_loop, RISING); }
void empty_loop() { } void loop() { // scan from 0 to 180 degrees for(angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for(angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); } }
|
Reference:http://sensitiveresearch.com/elec/DoNotTIP/index.htmlTAKING IT TO ANOTHER LEVEL: MAKING 3.3V SPEAK WITH 5V
http://hackaday.com/2016/12/05/taking-it-to-another-level-making-3-3v-and-5v-logic-communicate-with-level-shifters/
Comments
Post a Comment