Hello everyone,
I have a servo, and I am controlling it through an Arduino Uno. I have the Ethernet shield connected to it.
I am controlling a servo's Power through a transistor that I turn on and off through pin 8 from the Arduino. Pin 9
is the data to the servo. The problem is when I close the servo it works just fine, BUT when I Open it, the servo opens
but, then slowly closes. I have NO clue why this is happening. Also, I have opened up my servo and added a wire from it's potentiometer to get the position of the servo.
any help would be much appreciated, Thanks!
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Servo.h>
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
Servo myservo;
float pos = 0; //Value of For Loop
int FeedbackVal = 0; //Servo Feedback Value
int potPin = 2; //Number of pin that the Servo Feedback wire is on
int packetSize = 0;
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
myservo.attach(9);
pinMode(8, OUTPUT);
Serial.begin(9600);
Serial.print("started!");
}
void loop()
{
packetSize = Udp.parsePacket();
if(packetSize)
{
IPAddress remote = Udp.remoteIP();
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
if(strcmp(packetBuffer, "Close") == 0)
{
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
digitalWrite(8, HIGH);
float voltage= analogRead(A0) * (5.0 / 1023.0);
pos=voltage;
while(analogRead(A0) * (5.0 / 1023.0) > 1.5)
{
myservo.write(--pos);
delay(15);
}
digitalWrite(8, LOW);
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
}
if(strcmp(packetBuffer, "Open") == 0)
{
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
digitalWrite(8, HIGH);
float voltage= analogRead(A0) * (5.0 / 1023.0);
pos=voltage;
while(analogRead(A0) * (5.0 / 1023.0) < 3.0)
{
myservo.write(++pos);
delay(15);
}
digitalWrite(8, LOW);
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
}
pos = 0;
}
}
I am controlling a servo's Power through a transistor that I turn on and off through pin 8 from the Arduino.
Don't understand why people keep trying to do this. No doubt your transistor is on the ground leg of your servo. This loss of the ground flow path allows current flow thru the control pin for the servo, causing erratic servo behavior, and potential servo heating and possible damage.
Hello zoomkat, I wish I could say you were right, but sadly, even if I take out the transister and wire the servo directly to my arduino using this code:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Servo.h>
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
Servo myservo;
float pos = 0; //Value of For Loop
int FeedbackVal = 0; //Servo Feedback Value
int potPin = 2; //Number of pin that the Servo Feedback wire is on
int packetSize = 0;
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
myservo.attach(9);
pinMode(8, OUTPUT);
Serial.begin(9600);
Serial.print("started!");
}
void loop()
{
packetSize = Udp.parsePacket();
if(packetSize)
{
IPAddress remote = Udp.remoteIP();
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
if(strcmp(packetBuffer, "Close") == 0)
{
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
//digitalWrite(8, HIGH);
float voltage= analogRead(A0) * (5.0 / 1023.0);
pos=voltage;
for(pos=0;pos < 65; pos++)
{
myservo.write(pos);
delay(15);
}
//digitalWrite(8, LOW);
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
}
if(strcmp(packetBuffer, "Open") == 0)
{
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
//digitalWrite(8, HIGH);
float voltage= analogRead(A0) * (5.0 / 1023.0);
pos=voltage;
for(pos = 65;pos > 3; pos++)
{
myservo.write(pos);
delay(15);
}
//digitalWrite(8, LOW);
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
}
pos = 0;
}
}
I still get the same effect. Almost like it is sending data to the servo even if the pin is low. Now, i want to point out that i was not using the transistor to power the servo. I was using it to turn power to the servo off to prevent this. sadly it wasn't. I think i may be getting stuck in the for loops a little to long or something. I am not sure at this point.
Kevin192291:
even if I take out the transister and wire the servo directly to my arduino using this code:
This sounds like you are powering the servo from the Arduino. You shouldn't do that with or without a transistor. Dozens of problems in the Forum arise because people try to power servos from the Arduino 5v pin - it can't supply enough current and the Arduino resets repeatedly or does other strange stuff.
Power the servo with its own 5v or 6v power supply (with about 1 amp available per servo) and connect the servo power supply ground to the Arduino ground.
...R