# Home thermometer using a servo

**URL:** <https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420>\
**Category:** Programming\
**Created:** [January 22, 2024, 2:26pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420 "2024-01-22T14:26:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 22, 2024, 2:26pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/1 "2024-01-22T14:26:32Z")

</div>

Hi, can anybody help me with my thermometer code, please? My sketch works fine and uses a servo, with an attached pointer indicating the room temperature. However, I want to alter the scale to read from a minimum of 10`c to a maximum of 30`c. Can anybody help me  
with coding please, so I can expand the min/max scaling? my existing code as follows:-

```arduino
/* Atmega 328 (Nano)
 * sensor type DS18B20
 * 22/01/2024
 */

#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>

const int SERVO_PIN = 5; // Arduino pin connected to Servo Motor's pin
const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin
const float TEMPERATURE_THRESHOLD = 30; // °C 20

Servo servo; // create servo object to control a servo
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library
float temperature;
float servoposraw;
float servopos;

void setup() {
  Serial.begin(9600); // initialize serial
  servo.attach(SERVO_PIN); // attaches the servo on pin 5 to the servo object, pin 5? servo.write(0);
  servo.write(0);
  sensor.begin(); // initialize the sensor
}

void loop() {
  delay(1000);
  sensor.requestTemperatures(); // send the command to get temperatures
  temperature = sensor.getTempCByIndex(0); // read temperature in Celsius
  servoposraw =(180./60.)*temperature;
  servopos = 180 - servoposraw;
  servo.write(servopos);
  Serial.println(servopos);
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("°C => servo angle: ");
 
}

```

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [January 22, 2024, 2:46pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/2 "2024-01-22T14:46:47Z")

</div>

I didn't quite understand your difficulty, but see if the code below meets your needs.

```arduino
/* Atmega 328 (Nano)
    sensor type DS18B20
    22/01/2024
*/

#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>

const int SERVO_PIN = 5; // Arduino pin connected to Servo Motor's pin
const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin
const float TEMPERATURE_THRESHOLD = 30; // °C 20

Servo servo; // create servo object to control a servo
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library
float temperature;
float servoposraw;
float servopos;

void setup() {
  Serial.begin(9600); // initialize serial
  servo.attach(SERVO_PIN); // attaches the servo on pin 5 to the servo object, pin 5? servo.write(0);
  servo.write(0);
  sensor.begin(); // initialize the sensor
}

void loop() {
  delay(1000);
  sensor.requestTemperatures(); // send the command to get temperatures
  temperature = sensor.getTempCByIndex(0); // read temperature in Celsius
  if (temperature >= 10 and temperature <= 30) {
    servoposraw = (180. / 60.) * temperature;
    servopos = 180 - servoposraw;
    servo.write(servopos);
    Serial.println(servopos);
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print("°C => servo angle: ");
  }
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 22, 2024, 2:57pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/3 "2024-01-22T14:57:54Z")

</div>

> [@Jevray](#):
>
> minimum of 10`c to a maximum of 30`c.

You want the 20 degrees of temperature to play out over the 180 degrees of the servo.

You can use _map_()

```arduino
    servoAngle = map(temperature, 10, 30, 0, 180;

```

and you probably want to use _constrain_()

```arduino
   servoAngle = constrain(servoAngle, 0, 180);

```

because _map_() will cheerfully extrapolate and give servo angles out of range.

Oh, _servopos_ not _servoAngle_, sry.

HTH

a7

---

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 22, 2024, 5:26pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/4 "2024-01-22T17:26:06Z")

</div>

Many thanks for your prompt reply, must admit I struggle with code, it took me ages to put my sketch together and compile it to run. Thanks for your help I'll check it out. Regards Jevray

---

<div class="post-metadata">

**Author:** ![odometer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/odometer/32/1013044_2.png) [@odometer](https://forum.arduino.cc/u/odometer)\
**Post date:** [January 22, 2024, 5:57pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/5 "2024-01-22T17:57:28Z")

</div>

`map()` is for integers.

What I think @Jevray wants is to replace this line  
` servoposraw =(180./60.)*temperature;`  
with this  
` servoposraw =(180./20.)*(temperature-10);`  
and you are correct about the range. That can be taken care of thus:

```arduino
if (servopos < 0) servopos = 0;
if (servopos > 180) servopos = 180;

```

---

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 22, 2024, 5:58pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/6 "2024-01-22T17:58:24Z")

</div>

Thanks for your prompt reply, yes (20 degrees to play out over 180 degrees) I'll check out your snippet of code, I'm not that good when it comes to undestanding and putting together progranning code, now I see the answer I have a much better idea. Thanks again Jevray

---

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 22, 2024, 6:06pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/7 "2024-01-22T18:06:36Z")

</div>

Thank you, I'm grateful for the help you guys have come up with, It looks easy when I can see it written down, most helpful, and thanks for replying so quickly. Regards Jevray.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 22, 2024, 6:21pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/8 "2024-01-22T18:21:15Z")

</div>

> [@odometer](#):
>
> `map()` is for integers.

Yes, but you can hand it floats.

I had done, and just now googled

> When we pass a float value to a function having int argument, the Implicit typecasting is done here from float to int.

  

> [@odometer](#):
>
> What I think

Looks plausible and avoids _map_(), always a good thing, and uses simple logic instead of _constrain_().

And... works. There is no roll for _servoposraw_, it is assigned already conditioned like _map_() would have done. I used your idea thusly:

```arduino
  servopos = (180.0 / 20.0) * (temperature - 10.0);

  if (servopos < 0) servopos = 0;
  if (servopos > 180) servopos = 180;

  servo.write(servopos);

```

_map_() does make it a little easier to mess with the endpoints and stuff.

a7

---

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 22, 2024, 7:59pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/9 "2024-01-22T19:59:01Z")

</div>

Hi, I have put those few lines into my sketch, and it works fine. Thanks for your input.  
Regards Jevray

---

<div class="post-metadata">

**Author:** ![odometer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/odometer/32/1013044_2.png) [@odometer](https://forum.arduino.cc/u/odometer)\
**Post date:** [January 23, 2024, 12:12am UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/10 "2024-01-23T00:12:56Z")

</div>

> [@alto777](#):
>
> And... works. There is no roll for _servoposraw_, it is assigned already conditioned like _map_() would have done.

????!!! What about this line  
`servopos = 180 - servoposraw;`  
which was used to reverse the direction of the servo?  
Without that, @Jevray will have a backwards thermometer!

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 23, 2024, 12:16am UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/11 "2024-01-23T00:16:38Z")

</div>

> [@odometer](#):
>
> ????!!! What about this line

Yes, my bad. I had @ruilviana's idea and tossed that when I was making yours mine and ~~forgot~~ had missed the reversal line totally.

More fun was when I was sending both version's angle to one servo.

Which was after failing to attach the second servo I was used. Also caused some head scratching.

a7

---

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 23, 2024, 2:31pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/12 "2024-01-23T14:31:28Z")

</div>

Hi, hadn't noticed in which direction the servo rotates, as I'm putting a pulley system together for a vertical mode of operation. How do I alter the code to reverse the direction?  
I may or may not need to, but just in case. Regards Jevray

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 23, 2024, 2:50pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/13 "2024-01-23T14:50:20Z")

</div>

> [@Jevray](#):
>
> How do I alter the code to reverse the direction?

This is what makes it go "backwards"

```arduino
    servopos = 180 - servoposraw;

```

Simply omit that and increasing temperatures will yield increasing servo angles. Throw some _servoposraw_ numbers on the right hand side of that assignment and watch it reverse.

If you had used _map_() on _temperature_ you could see it another way

```arduino
    servoAngle = map(temperature, 10, 30, 0, 180);

```

versus

```arduino
    servoAngle = map(temperature, 10, 30, 180, 0);

```

But as shown by @odometer, it's all just some simple maths at the end, stuff from so long ago I don't remember learning it. I mean I don't remember ever not knowing it. 😑

There are a number of things that crop up going the wrong way or "upside down", like switches that are LOW when pressed - these are usually handled easily at an exact moment by a bit of code.

```arduino
    bool buttonIsPressed = !digitalRead(buttonPin);

```

where the pulled LOW wiring is immindiantly corrected with the logical not operator '!', and forever more reads right-side up.

HTH

a7

---

<div class="post-metadata">

**Author:** ![Jevray](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jevray/32/1098801_2.png) [@Jevray](https://forum.arduino.cc/u/Jevray)\
**Post date:** [January 23, 2024, 5:34pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/14 "2024-01-23T17:34:29Z")

</div>

Thanks for the info, you've been very helpful, and that's made the coding easier for me to comprehend. It took me hours to get to this stage but learned something more from the help posted here, at a later date might be adding a display! Regards Jevray

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 21, 2024, 5:34pm UTC](https://forum.arduino.cc/t/home-thermometer-using-a-servo/1214420/15 "2024-07-21T17:34:56Z")

</div>

Closed on July 21, 2024, 5:34pm UTC

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