Servo motor malfunctioning

So, I know that my power supply (9v battery) isnt the issue. I will paste the code below. Every time one of the LED’s blinks, the servo motor pulses and I really have no clue why. I know there isnt any code in this telling the servo motor what to do but that just means it shouldnt be moving at all right? Thanks

// Adds Extra Serial Port
#include <SoftwareSerial.h>

// Assigns Serial Port RX to pin 11
SoftwareSerial portOne(10,11);

// Includes Servo Motor Library
#include <Servo.h>

Servo servo1;

// Temperature Sensor #1
int temp1;
float temp1Volt;
float temp1C;
float temp1F;

// Temperature Sensor #2
int temp2;
float temp2Volt;
float temp2C;
float temp2F;

// Humidity Sensor
int humidity;
float humidityVolt;
float RH;

// Presure Sensor
int pressure;
float pressureVolt;
float psi;

// Accelerometer X
int accelX;
float accelXVolt;
float accelXG;

// Accelerometer Z
int accelZ;
float accelZVolt;
float accelZG;

// Uplink Rate
int uplinkRate = 1;

// Time keeper
// The time stamp used when recording data points
uint32_t timeStamp = 0;

void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:

Serial.begin(9600);
portOne.begin(9600);
servo1.attach(2);

// setup the LED Visual Display
pinMode(3, OUTPUT); //Arduino on
pinMode(4, OUTPUT); //Internal Temp
pinMode(5, OUTPUT); //External Temp
pinMode(6, OUTPUT); //Humidity
pinMode(7, OUTPUT); //Pressure
pinMode(9, OUTPUT); //Accels

// turn on Arduino LED
digitalWrite(3, HIGH); // Leave on while power is on

// Print Column Headers

portOne.println(“Time,Temp1F,Temp2F,RH,Pres,AccX,AccZ”);

}

void loop() {
// put your main code here, to run repeatedly:
// put your main code here, to run repeatedly:

// Turn script running leds OFF at begining of loop
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(9, LOW);

delay(500); //Amount of time between samples (milliseconds)

// Log the time
timeStamp = millis();
portOne.print(timeStamp);

temp1 = analogRead(A0);
temp1Volt = temp1*(5.0/1023);
temp1C = (temp1Volt - 0.5)/(0.01);
temp1F = (temp1C*(9.0/5.0) + 32);
portOne.print(",");
portOne.print(temp1F, 2);
digitalWrite(4, HIGH);

temp2 = analogRead(A1);
temp2Volt = temp2*(5.0/1023);
temp2C = (temp2Volt - 0.5)/(0.01);
temp2F = (temp2C*(9.0/5.0) + 32);
portOne.print(",");
portOne.print(temp2F, 2);
digitalWrite(5, HIGH);

humidity = analogRead(A2);
humidityVolt = humidity*(5.0/1023);
RH = (((humidityVolt/5.0)-0.16)/0.0062);
portOne.print(",");
portOne.print(RH, 2);
digitalWrite(6, HIGH);

pressure = analogRead(A3);
pressureVolt = pressure*(5.0/1023);
psi = (pressureVolt-0.5)*(15.0/4.0);
portOne.print(",");
portOne.print(psi, 2);
digitalWrite(7, HIGH);

accelX = analogRead(A4);
accelXVolt = accelX*(5.0/1023);
accelXG = (accelXVolt - (3.3/2))/(0.330);
portOne.print(",");
portOne.print(accelXG,3);

accelZ = analogRead(A5);
accelZVolt = accelZ*(5.0/1023);
accelZG = (accelZVolt - (3.3/2))/(0.330);
portOne.print(",");
portOne.print(accelZG,3);
digitalWrite(9, HIGH);

portOne.println();

uplinkRate++;

if ( (uplinkRate % 20) == 0){

Serial.print(“Uplink Completed”);
Serial.println();

Serial.print(timeStamp);

temp1 = analogRead(A0);
temp1Volt = temp1*(5.0/1023);
temp1C = (temp1Volt - 0.5)/(0.01);
temp1F = (temp1C*(9.0/5.0) + 32);
Serial.print(",");
Serial.print(temp1F, 2);
digitalWrite(4, HIGH);

temp2 = analogRead(A1);
temp2Volt = temp2*(5.0/1023);
temp2C = (temp2Volt - 0.5)/(0.01);
temp2F = (temp2C*(9.0/5.0) + 32);
Serial.print(",");
Serial.print(temp2F, 2);
digitalWrite(5, HIGH);

humidity = analogRead(A2);
humidityVolt = humidity*(5.0/1023);
RH = (((humidityVolt/5.0)-0.16)/0.0062);
Serial.print(",");
Serial.print(RH, 2);
digitalWrite(6, HIGH);

pressure = analogRead(A3);
pressureVolt = pressure*(5.0/1023);
psi = (pressureVolt-0.5)*(15.0/4.0);
Serial.print(",");
Serial.print(psi, 2);
digitalWrite(7, HIGH);

accelX = analogRead(A4);
accelXVolt = accelX*(5.0/1023);
accelXG = (accelXVolt - (3.3/2))/(0.330);
Serial.print(",");
Serial.print(accelXG,3);

accelZ = analogRead(A5);
accelZVolt = accelZ*(5.0/1023);
accelZG = (accelZVolt - (3.3/2))/(0.330);
Serial.print(",");
Serial.print(accelZG,3);
digitalWrite(9, HIGH);

Serial.println();

portOne.print(“Uplink in Progress”);
portOne.println();

}

}

You wrote that your power supply is not the problem, but...

Is this the power supply for the Arduino?
Is this the power supply for the servo?
Is this the power supply for both?

It is usually a bad idea to use the same power supply for both the Arduino and for a servo (or any other motor or inductive load).
It is usually a bad idea to use a standard 9 volt battery to power a servo.

What Arduino do you have?

In the future, please use code tags when posting code.

Is it the led or is it software serial? Maybe take the software serial related stuff out for testing.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Are you using one of these 9V batteries?

If so, you do have a power supply problem.
It will not be able to provide anywhere near the current need to drive any servo.

Tom.. :slight_smile:

So, I know that my power supply (9v battery) isnt the issue

How do you know this ?

connordgrant:
So, I know that my power supply (9v battery) isnt the issue.

Interesting statement. Almost everyone else who has tried using servos and 9V PP3 type batteries found out (eventually) that the power supply WAS the problem, or at least part of it.

Steve

I know its not the power supply because I have tested the Servo motor outside of this code before with the same power supply. I believe the problem is in the code, not the hardware. I think it has something to do with the attach command because when I take the attach command out, the servo motor doesnt move.

connordgrant:
I think it has something to do with the attach command because when I take the attach command out, the servo motor doesnt move.

If you unplug the servo, it won’t move either. And that’s effectively what you’re doing by taking the attach() out.

BTW testing the servo with different code and without all those other devices connected and using power doesn’t do anything to prove that the power supply is adequate for when everything else is connected and working.

But you seem determined to ignore advice so you can just get on with it. Good luck.

Steve

connordgrant:
I know its not the power supply because I have tested the Servo motor outside of this code before with the same power supply. I believe the problem is in the code, not the hardware. I think it has something to do with the attach command because when I take the attach command out, the servo motor doesnt move.

Hi, you didn't answer post #3.

Do you have a DMM?

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Please repost your code in a new post.

Tom... :slight_smile:

// Adds Extra Serial Port 
#include <SoftwareSerial.h> 

// Assigns Serial Port RX to pin 11
SoftwareSerial portOne(10,11);

// Includes Servo Motor Library 
#include <Servo.h>

Servo servo1; 

// Temperature Sensor #1    
    int temp1;
    float temp1Volt;
    float temp1C;
    float temp1F;
    
// Temperature Sensor #2   
    int temp2;
    float temp2Volt;
    float temp2C;
    float temp2F;
   
// Humidity Sensor    
    int humidity;
    float humidityVolt;
    float RH;
    
// Presure Sensor         
    int pressure;
    float pressureVolt;
    float psi;
    
// Accelerometer X  
    int accelX;
    float accelXVolt;
    float accelXG;
    
// Accelerometer Z
    int accelZ;
    float accelZVolt;
    float accelZG;

// Uplink Rate 
   int uplinkRate = 1; 
    
// Time keeper 
// The time stamp used when recording data points
    uint32_t timeStamp = 0;  
    
void setup() {
  // put your setup code here, to run once:
// put your setup code here, to run once:

      Serial.begin(9600);
      portOne.begin(9600); 
      servo1.attach(2);  

// setup the LED Visual Display
      pinMode(3, OUTPUT);  //Arduino on
      pinMode(4, OUTPUT);  //Internal Temp
      pinMode(5, OUTPUT);  //External Temp
      pinMode(6, OUTPUT);  //Humidity
      pinMode(7, OUTPUT);  //Pressure
      pinMode(9, OUTPUT);  //Accels

// turn on Arduino LED
      digitalWrite(3, HIGH);  // Leave on while power is on 

// Print Column Headers

      portOne.println("Time,Temp1F,Temp2F,RH,Pres,AccX,AccZ");
      
}

void loop() {
  // put your main code here, to run repeatedly:
//   put your main code here, to run repeatedly:

//   Turn script running leds OFF at begining of loop
     digitalWrite(4, LOW);
     digitalWrite(5, LOW);
     digitalWrite(6, LOW);
     digitalWrite(7, LOW);
     digitalWrite(9, LOW);
     
     delay(500);  //Amount of time between samples (milliseconds)
     
// Log the time
     timeStamp = millis();
     portOne.print(timeStamp);       
             
     temp1 = analogRead(A0);
     temp1Volt = temp1*(5.0/1023);
     temp1C = (temp1Volt - 0.5)/(0.01);
     temp1F = (temp1C*(9.0/5.0) + 32);
     portOne.print(",");    
     portOne.print(temp1F, 2);
     digitalWrite(4, HIGH);
 
     temp2 = analogRead(A1);
     temp2Volt = temp2*(5.0/1023);
     temp2C = (temp2Volt - 0.5)/(0.01);
     temp2F = (temp2C*(9.0/5.0) + 32);
     portOne.print(",");    
     portOne.print(temp2F, 2);
     digitalWrite(5, HIGH);
    
     humidity = analogRead(A2);
     humidityVolt = humidity*(5.0/1023);
     RH = (((humidityVolt/5.0)-0.16)/0.0062);
     portOne.print(",");    
     portOne.print(RH, 2);
     digitalWrite(6, HIGH);

     pressure = analogRead(A3);
     pressureVolt = pressure*(5.0/1023);
     psi = (pressureVolt-0.5)*(15.0/4.0); 
     portOne.print(",");    
     portOne.print(psi, 2);
     digitalWrite(7, HIGH);
     
     accelX = analogRead(A4);
     accelXVolt = accelX*(5.0/1023);
     accelXG = (accelXVolt - (3.3/2))/(0.330);
     portOne.print(",");    
     portOne.print(accelXG,3);

     accelZ = analogRead(A5);
     accelZVolt = accelZ*(5.0/1023);
     accelZG = (accelZVolt - (3.3/2))/(0.330);
     portOne.print(",");    
     portOne.print(accelZG,3);
     digitalWrite(9, HIGH);
     
     portOne.println();

     uplinkRate++; 

     if ( (uplinkRate % 20) == 0){

      Serial.print("Uplink Completed"); 
      Serial.println(); 

      Serial.print(timeStamp);       
             
     temp1 = analogRead(A0);
     temp1Volt = temp1*(5.0/1023);
     temp1C = (temp1Volt - 0.5)/(0.01);
     temp1F = (temp1C*(9.0/5.0) + 32);
     Serial.print(",");    
     Serial.print(temp1F, 2);
     digitalWrite(4, HIGH);
 
     temp2 = analogRead(A1);
     temp2Volt = temp2*(5.0/1023);
     temp2C = (temp2Volt - 0.5)/(0.01);
     temp2F = (temp2C*(9.0/5.0) + 32);
     Serial.print(",");    
     Serial.print(temp2F, 2);
     digitalWrite(5, HIGH);
    
     humidity = analogRead(A2);
     humidityVolt = humidity*(5.0/1023);
     RH = (((humidityVolt/5.0)-0.16)/0.0062);
     Serial.print(",");    
     Serial.print(RH, 2);
     digitalWrite(6, HIGH);

     pressure = analogRead(A3);
     pressureVolt = pressure*(5.0/1023);
     psi = (pressureVolt-0.5)*(15.0/4.0); 
     Serial.print(",");    
     Serial.print(psi, 2);
     digitalWrite(7, HIGH);
     
     accelX = analogRead(A4);
     accelXVolt = accelX*(5.0/1023);
     accelXG = (accelXVolt - (3.3/2))/(0.330);
     Serial.print(",");    
     Serial.print(accelXG,3);

     accelZ = analogRead(A5);
     accelZVolt = accelZ*(5.0/1023);
     accelZG = (accelZVolt - (3.3/2))/(0.330);
     Serial.print(",");    
     Serial.print(accelZG,3);
     digitalWrite(9, HIGH);
     
     Serial.println();

      portOne.print("Uplink in Progress"); 
      portOne.println();
      
     } 

}

The servo motor is running off a 9V battery, yes. I have been running it off of a 9V for some time now, using various codes making me strongly doubt that this is power related. The fact that it moves when the LED's blink makes me believe it has to do with the code. I do not have a cad drawing of my setup. There are devices recording serial data attached to pins 0 and 1, 10 and 11. The LED's are connected to the digital pins 3, 4, 5, 6, 7, 9. The servo is attached to pin 2.

Hi,
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?

Do all the sensors run off the battery as well?

Do you have a DMM?

Are the negative of the battery and the negative of the servo connected to the gnd of the Arduino?

Tom... :slight_smile:

Hi,
Where abouts in your code do you control your servo?

Just because you are not sending commands to the servo, it still consumes current to keep the output shaft in it existing position.

What is the application of your project?

Tom... :slight_smile: