Hi guys, new to the forum and almost new to programming all together. I’ve had some a few lessons in programming in c++ 2 years back and needless to say…i’m not an expert.

What i’m trying to build is basically a thermostat off an arduino uno. Basically what will happen is that temperature can be set by using a potentiometer connected to an arduino, when set temperature is detected, 5v would be sent to the relay to open the circuit and voila, we have control of the temperature.

I’ve sourced some code from the internet with the temperature sensor that i have and i was hoping someone could give me some insight on how to modify this to my needs, thanks!

void loop(){

float celsius = getTemperature();
Serial.print("Celsius: ");

float fahrenheit = (1.8 * celsius) + 32;
Serial.print("Fahrenheit: ");

delay(200); //just here to slow down the output. You can remove this

float getTemperature(){

byte MSB =;
byte LSB =;

//it’s a 12bit int, using two’s compliment for negative
int TemperatureSum = ((MSB << 8) | LSB) >> 4;

float celsius = TemperatureSum*0.0625;
return celsius;

i was hoping someone could give me some insight on how to modify this to my needs

Does that get the temperature successfully? How is the potentiometer going to come in to play?

It appears that you need to add some code to read the potentiometer, and map() the readings you get to some acceptable range of temperatures, define the set temperature.

Then, if the actual temperature is below the set temperature, activate the pin that the relay (or the transistor that controls the relay) is connected to to make some heat (or cold, if that is the objective, and the temperature is higher than the set point).

Hysteresis is a term you should investigate.

Hi, Some (not exactly Thermostat) examples on the ArduinoInfo.Info WIKI:

Start here in the "LEARN! ARDUINO - A HANDS-ON APPROACH" section:

See the example Software Sketches here:

Look at LightingControl which is similar: A potentiometer sets setpoint, reads Light Sensor, turns Lights ON/OFF.

I suggest you also look at AutomationExample which is the same exact function, but separated into the three logical parts of every Automatic system. This is a way to think about automatic systems. There are these 3 parts:

[Sensor Inputs | Software Behavior | Action Outputs]

All automatic systems, from a simple thermostat to the Mars Rover have those 3 parts.

Sensor Inputs: These can be simple like a pushbutton switch or complex like a GPS receiver. There are hundreds of possibilities for sensing things in the Physical World.

Action Outputs: These can be simple like an LED or complex like the motors and motion control of a Robot.

Software Behavior:

This is where you decide what Sensor Inputs Arduino will look at, what Decisions it will make about its Behavior, and what Action Outputs it will cause to happen. You make this actually work by writing software code statements.

So, the LOOP part of a program would look like:

void loop()   /****** LOOP: RUNS CONSTANTLY ******/

  delay(1000);     // Wait 1 second
}//--(end main loop )-

You need to decide how to Read The Sensors in your design, What Action you will Take, and what Decisions you need to make. If you put those parts of your code into separate functions it will be easier to understand what is happening.

And BTW your code will be easier to Re-Use for other things.

When it gets to where you are controlling power devices like lights and heaters look at the Arduino Power page here:

...let us know how you make out!