PID code- trying to control a peltier to make a cooler

Hi all,

Having a bit of an issue with using PID code (just the basic BR3tt code + library). Just to help you all out here’s what I’ve got running

Hardware I’m using:
Arduino MEGA2560
LM35 temp sensor (DIY waterproofed, checked against many other thermometers, works a charm!) These run off a seperate circuit.
Peltier TEC1-12709- hooked up to a csd18533kcs TI (TO-220 style) MOSFET and 10k resistor
This is running off a 250W ATX PSU. [I’ve copied this link: for the circuit set up)

Here is the code:

 * PID Basic Example
 * Reading analog input 0 to control analog PWM output 3

#include <PID_v1.h>

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,3, DIRECT);

void setup()
  //initialize the variables we're linked to
  Input = analogRead(0);
  Setpoint = 25;

  //turn the PID on

void loop()
  Input = analogRead(0);
  Input = (5.0 * Input * 100.0)/1024.0;
  Serial.print(Input); Serial.println(Output);
  //delayy next read out

The key problem is that although the code is working- when the LM35 reads a difference between current and setpoint, the current increases, peltier goes colder and colder. However, it’s working almost in reverse to what I want- I want the Peltier to increase power when the temperature is read above the setpoint and when it’s below I want the peltier to reduce output/ turn off completely. Thinking about how PID works has led me to think that the PID code wont achieve what I want because (correct me if I’m wrong) this code only produces an output if below set point and then lets an overshoot be dealt with by turning output off and letting it drift back.

Is my target achievable with this code? If so how do I change it to make it do what I want? Where do I put the extra or tweaked code in this script?


I have no idea but, maybe you have to negate a variable.

You should set your 'direction' to "REVERSE" (like a refrigerator) instead of "DIRECT" (like a heater).