Problem using LM35 for peltier element temperature control system

So I'm trying to control the temperature of a couple of thermoelectric coolers. I'm using an H-bridge and PWM signals to control the voltage accross both peltiers, and I want to sense their temperatures as I increase the applied voltage using an LM35 temperature sensor. Ultimately, I would like to implement a PID algorithm so that the temperature of the peltiers can automatically stabilize at a given temperature.

For the time being, all I'm trying to do is to get the sensor to work conjointly with the peltiers to see if it can read their temperature accurately. However I'm experiencing some difficulty with the LM35. When I send a PWM value of 0 to the H-bridge enable pin, it displays seemingly accurate values. But if I operate the peltiers on full power, the LM35 starts giving me values which are way too high. For example, I obtain these readings in the Serial monitor:

Power = 0% Temp = 26.37C
Power = 0% Temp = 26.86C
Power = 0% Temp = 26.86C
Power = 0% Temp = 25.39C
Power = 0% Temp = 25.88C
Power = 0% Temp = 25.88C
Power = 100% Temp = 87.40C
Power = 100% Temp = 104.49C
Power = 100% Temp = 106.93C
Power = 100% Temp = 106.93C
Power = 100% Temp = 106.93C
Power = 100% Temp = 104.98C

I've attached the fritzing file for the circuit and here is my code:

// Peltier temperature control

int switchpin = 13; // switch to change peltier voltage sign

int peltier1pin1 = 2; // peltier 1 logic pin 1
int peltier1pin2 = 3; // peltier 1 logic pin 2
int peltier2pin1 = 4; // peltier 2 logic pin 1
int peltier2pin2 = 5; //peltier 2 logic pin 2
int enablepin1 = 9; // H-bridge enable pin 1
int enablepin2 = 6; // H-bridge enable pin 2

int power = 0; // power level from 0 to 100%
int peltier_level = map(power, 0, 100, 0, 255); // PWM value that is given to the enable pin

int inPin = 0; // analog pin for LM35
int prevValue = 0; // previous value read that is different from current value
float celsius = 0; // temperature value

void setup()
{
  Serial.begin(9600);

  pinMode(switchpin,INPUT);
  pinMode(peltier1pin1,OUTPUT);
  pinMode(peltier1pin2,OUTPUT);
  pinMode(peltier2pin1,OUTPUT);
  pinMode(peltier2pin2,OUTPUT);
  pinMode(enablepin1,OUTPUT);
  pinMode(enablepin2,OUTPUT);

  while(!Serial);
  Serial.println("Power 0 to 100");
  Serial.println();
}


void loop()
{
  // read the state of the direction switch    
  boolean SwitchState = digitalRead(switchpin);

  // if the switch is high, set voltage polarity across peltier:
  if(SwitchState == HIGH){
    digitalWrite(peltier1pin1, HIGH);
    digitalWrite(peltier1pin2, LOW);
    digitalWrite(peltier2pin1, HIGH);
    digitalWrite(peltier2pin2, LOW);
  }

  // if the switch is low, switch voltage polarity:  
  if(SwitchState == LOW){
    digitalWrite(peltier1pin1, LOW);
    digitalWrite(peltier1pin2, HIGH);
    digitalWrite(peltier2pin1, LOW);
    digitalWrite(peltier2pin2, HIGH);
  }

  // read desired peltier power 
  if(Serial.available() > 0)
  {
    power = Serial.parseInt();

    if(power > 100) power = 100;
    if(power < 0) power = 0;

    peltier_level = map(power, 0, 100, 0, 255);
  }

  // Write value to peltier
  analogWrite(enablepin1, peltier_level);
  analogWrite(enablepin2, peltier_level);

  // measure peltier temperature
  int value = analogRead(inPin);
  if (prevValue != value) {
    float millivolts = (value / 1024.0) * 5000;
    celsius = millivolts / 10;  // sensor output is 10mV per degree Celsius
    prevValue = value;  // save current value as previous value
  }

  Serial.print("Power = ");
  Serial.print(power);
  Serial.print("%");
  Serial.print(" Temp = ");
  Serial.print(celsius);
  Serial.println("C");

  delay(500);
}

I'm very new to Arduino, so I can't determine where the problem is coming from, if it's something to do with my code or the way I have connected the sensor. Any help would be great!
Cheers

peltier_controller.fzz (9.27 KB)

Please edit your post and put the code in "code tags" -- use the # button on the editor. Fritzing files and diagrams are of little to no use.

The problem might be due to power supply fluctuations. The Peltier modules should be powered by a completely separate supply than the Arduino, one capable of delivering at least 7 amperes at 12 V, but with a common ground.

elseabs:
I'm very new to Arduino, so I can't determine where the problem is coming from, if it's something to do with my code or the way I have connected the sensor. Any help would be great!
Cheers

I think you need to measure voltages all over your circuit when the peltiers are on/off.

Post the schematic that you can get from fritzing but I suspect that you are lacking decoupling capacitors, especially the big ones.

@ jremington: i think that could well be the problem, but I'm not sure how to do that. Do I connect a different power supply to the arduino and connect pin 8 of the H-bridge to the Vin pin of the arduino?

@ fungus: I'll give that a try!

@ Grumpy_mike: why are decoupling capacitors required in this case, and where should they be inserted?

Thanks for the help!

Hi, not seeing how you have physically constructed your project, I'd would check that the wiring from the LM35 is well away from the power supply and wiring to the peltier effect unit.
PWM produces noise and that could be what is causing the temperature readings to jump, as Mike says, bypassing each analog input for a start is highly recommended.

Tom.... :slight_smile: