You want to control either the +5V or the ground connection on the servo with a relay or transistor etc. This site shows how to control the ground connection with a transistor.
Thanks @bko
I’ve been to my local electrical store and got an assortment of transistors and really I’m struggling to get the servo to move, the best I’ve got is a very slight whirring noise…
I've tried BC638, C556B, BC550, BUF654 and some others.
I’ve wired the Base pin of the transister to D6 on spark, the emitt pin to servo power wire and the control pin to the +6v of the external power supply which is grounded to the spark.
The signal pin of the servo is connected to D0.
here is my code,
int pos = 0; // variable to store the servo position
int tranPin = D6; //Transister base pin
void setup()
{
//myservo.attach(D0); // attaches the servo on pin 0 to the servo object
pinMode(tranPin, OUTPUT); //Set tranPin as an output
Spark.function("feedControl",feedControl);
}
void loop() {
}
int feedControl(String command)
{
Servo myservo; // create servo object to control a servo
myservo.attach(D0);
digitalWrite(tranPin, HIGH); //make tranPin high
if (command == "FEED") {
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
//myservo.detach();
digitalWrite(tranPin, LOW); //make tranPin low
return 1;
} else {
digitalWrite(tranPin, LOW); //make tranPin low
return 0;
}
}
A “low-side” NPN type switch is a bit easier but PNP will work. There is always one diode drop (around 0.7V) between base and emitter so you with PNP you need a resistor from the core pin to the transistor base. A typical value is 1kohm but other similar values are fine.
See if you can tie the base resistor to Spark +3.3V and make it work like it did before, then connect the base resistor to the core pin (D6).
I’ve hacked a really bad diagram together (this is all very new to me) does it represent what you meant? I’m not sure what you mean by using the +3.3v pin instead of the D6 though? what is the purpose of that?
Are the transistors I tried the wrong type and if so can you advise what I need to buy and I will look at ordering today.
Nice diagram! For a PNP transistor you need to drive the pin LOW to turn it on and HIGH for off.
So for testing, you can simulate this by taking the lead of the resistor connected to pin D6 and moving it to either +3.3V for off or GND for on. That is all I meant!