Temperature Sensor is giving strange readings once a Servo Motor is connected.

For a project I am doing I have to connected a temperature sensor, a servo motor, an LCD screen and a piezo together so that when the temperature in the room goes above 18°C the servo motor turns 180°, the piezo sounds and a variable counts how many times the temperature has gone above 18°C (i.e. when the temperature increases above 18° the variable becomes 1, then it goes below 18° and rises above it again and the variable becomes two) and that variable and the temperature of the room is displayed on the LCD screen.

The problem that I am having is that everything works perfectly until the servo motor is connected. When the motor is connected the temperature readings start to increase then decrease rapidly. They will go from 11° to 30° and then back to 15° then up to 22° and so on and so on. I have tried connecting a resistor in between the ground on the Arduino and the ground on the servo but that stopped the servo working altogether. I have also tried placing a capacitor across the ground and power but that didn't do anything.

I am using an Arduino Uno board and the servo motor and temperature sensor I am using comes with the Arduino starter kit and all the components are connected to the 5V power supply on the Arduino board.

Here is the code that I am using:

/* Set the sensorPin variable to pin A0
   Make a variable to store the value of the sensor
   Make a variable to store the calculated voltage
   Make a variable to store the calculated temperature
*/

#include <Servo.h>
Servo CoelsServo;

int sensorPin = A1;
int sensorValue = 0;
int voltageValue = 0;
int temperatureValue = 0;

int note = 330;

int temperatureState = 0;
int lastTemperatureState = 0;
int noOfTimesTempIsAbove18C = 0;

#include <LiquidCrystal.h> // Include the library code
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the library with the numbers of the interface pins

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

  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  lcd.begin(16, 2); // Set up the LCD's number of columns and rows:
  lcd.print("Temp = "); // Print a message to the LCD to tell you what the temperature is

  lcd.setCursor(0, 1);
  lcd.print("Passed 18 C = ");

  CoelsServo.attach(7);
  delay(100);
  CoelsServo.write(0);
}

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

  sensorValue = analogRead(sensorPin); // Read the value coming from the sensor - values are between 0 and 1023
  voltageValue = (sensorValue) * (5000 / 1024); // Convert the value of the sensor to a Voltage value
  temperatureValue = ((voltageValue) - 500) / 10; // Convert the Voltage Value to a Temperature in Degrees Celcius

  lcd.setCursor(8, 0); // set the cursor to column 0, line (note: line 1 is the second row, since counting begins with 0)
  lcd.print(temperatureValue); // Print what the temperature is on the LCD screen
  lcd.print(" C");

  delay(200);

  lcd.setCursor(0, 1);
  lcd.print("Passed 18C = ");
  lcd.setCursor(14, 1);
  lcd.print(noOfTimesTempIsAbove18C);

  delay(200);

  if (temperatureValue > 18 ) {
    tone(8, 330);
    CoelsServo.write(180);
    temperatureState = 1;
  }

  if (temperatureState != lastTemperatureState) {
    if (temperatureState == 1) {
      noOfTimesTempIsAbove18C ++;
    }
    delay(100);
  }

  else if (temperatureValue <= 18) {
    noTone(8);
    CoelsServo.write(0);
    temperatureState = 0;
  }
  lastTemperatureState = temperatureState;

  delay(1000);
}

Any help will be much appreciated and if I don't reply to your comment straight away I have gone to bed because it is quite late where I am right now and I will reply in the morning.

Thanks guys!

  • Gusty

Do not power a servo with the Arduino. Use a separate power supply. A 4 AA cell battery pack is a good choice. Connect the battery pack ground (-) to Arduino ground.

voltageValue = (sensorValue) * (5000 / 1024); // bad code, assuming a TMP36 sensor

Do you think the 5volt supply is and will stay 5.000volt?

There is much better code out there that uses the more stable internal 1.1volt reference to compare sensor voltage to.
Leo..

Welcome to the group. It sounds like you have electrical noise messing with your system. Feedback via the power source is very common and troublesome. The power source impedance needs to be low enough to keep the noise in check. What are you using for a power source, it is capable of sourcing the current you need at the appropriate voltage. A power supply an Arduino is not. When using s second power supply for the motor be sure the grounds are connected.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.