Servo Interference with Temperature sensor

New to Arduino - so please be patient.

I am working with a new Sparkfun Arduino One kit.

I am mashing up the servo example and the temperature example so that the servo would move (eg to open a cooling valve) when a certain temperature (24 deg) is exceeded.

When I run the sketch without the servo (the micro A0090) in the circuit all works fine.

(The servo script ran fine before adding the temp control BTW)

When I add the servo the temperature sensor (TMP36) gives high readings (like 20 deg C high, not a subtle interference).

I am sending the data to the serial monitor (am adding in comments here with //)

22.27 Temp OK - flow closed

22.75 Temp OK - flow closed

22.27 Temp OK - flow closed

22.27 Temp OK - flow closed

22.27 Temp OK - flow closed

22.27 Temp OK - flow closed

22.27 Temp OK - flow closed

22.27 Temp OK - flow closed

21.78 Temp OK - flow closed

22.27 Temp OK - flow closed //plug servo into board - servo cycles to closed

60.35 Temp high - flow open // way wrong temp - servo cycles to open position

33.50 Temp high - flow open // no servo change

24.22 Temp high - flow open // no servo change

23.73 Temp OK - flow closed //servo cycles to closed position

36.43 Temp high - flow open //note new artificially high temp servo cycles to open position

33.98 Temp high - flow open // etc. etc.

22.27 Temp OK - flow closed

35.94 Temp high - flow open

35.45 Temp high - flow open

23.73 Temp OK - flow closed

34.96 Temp high - flow open

36.43 Temp high - flow open

21.78 Temp OK - flow closed //unplug servo

22.27 Temp OK - flow closed

22.75 Temp OK - flow closed

22.75 Temp OK - flow closed

Code is as follows:

(yes - there is still some push button code in here i used to test setting the 2 positions of the servo)

#include <Servo.h>

Servo myservo; // create servo object to control a servo

// a maximum of eight servo objects can be created

// constants won’t change. They’re used here to

// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

//TMP36 Pin Variables

int temperaturePin = 0; //the analog pin the TMP36′s Vout (sense) pin is connected to

//the resolution is 10 mV / degree centigrade

//(500 mV offset) to make negative temperatures an option

int pos = 0; // variable to store the servo position

int flow = 90; // valve at 0 allows flow

int inject = 0; // valve at 90 allows injection

int reps = 5; // number or repitions in demo

int topen = 5; // time open in seconds

int buttonState = 0; // variable for reading the pushbutton status

int templimit = 24; //Temperature limit to trigger action

void setup()

{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

myservo.write(flow); // make sure we start allowing flow

pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:

pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input

Serial.begin(9600); //Start the serial connection with the copmuter

//to view the result open the serial monitor

//last button beneath the file bar (looks like a box with an antenae)

Serial.print(templimit);

Serial.println(" templimit");

}

void loop() {

float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor

temperature = (temperature - 0.5) * 100; //converting from 10 mv per degree with 500 mV offset to degrees volatge less 500mV times 100

Serial.print(temperature); //printing the result

delay(1000); //waiting a second

if (temperature > templimit) {

digitalWrite(ledPin, HIGH); // turn LED on

Serial.println(" Temp high - flow open");

myservo.write(flow); // tell servo to go to position flow

delay(150); // waits 15ms for the servo to reach the position

}

else {

digitalWrite(ledPin, LOW); // turn LED off

Serial.println(" Temp OK - flow closed");

myservo.write(inject); // tell servo to go to inject position

delay(150); // waits 15ms for the servo to reach the position

}

}

//

// getVoltage() – returns the voltage on the analog input defined by

// pin

//

float getVoltage(int pin){

return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range

// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts

}

I have not been able to find anyone else describing this kind of interference.

Thanks for help/ideas.

Joe

UPDATE: Bit of additional info. It looks like the servo is pulling the voltage on the board down to 3.7-3.9. I am willing to bet that is the cause. I will try this later with a better power supply, but all insights are appreciated.

Thanks

The servo supplied with that kit can pull a fair amount of current, and this can exceed the limit that the USB port will give to your Arduino by default. If it does, it’ll either reset the Arduino or will make the supply voltage fluctuate.

What’s most likely happening is the servo is actuating, and dragging the supply line low enough to make the temperature sensor unreliable.

Try and drive the circuit with the Arduino connected to an external power supply and see how you go.