# DS18B20 One Wire Servo Temperature Gauge

**URL:** <https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931>\
**Category:** General Guidance\
**Created:** [February 20, 2014, 6:32pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931 "2014-02-20T18:32:16Z")\
**Posts on this page:** 15\
**Page:** 1

<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:** [February 20, 2014, 6:32pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/1 "2014-02-20T18:32:16Z")

</div>

Hi, I could do with some assistance with this issue that I'm having because I'm relatively new to coding and am trying to receive data from a DS18B20 temperature sensor and use this data to move a servo the appropriate amount and then receive the next piece of data from the sensor and then the servo move, giving me a live feed gauge of the current temperature.

```cpp
#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 180;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();

  {
   sensors.requestTemperatures(); // Send the command to get temperatures
 
int tempSens = sensors.getTempCByIndex(0);
 for(pos = 0; pos < tempSens; pos += 1);  // goes from 0 degrees to Temp value
  }
}

void loop() 
{ 
  
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  
  sensors.requestTemperatures(); // Send the command to get temperatures
 
int tempSens = sensors.getTempCByIndex(0);
 delay(200);
  
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 
  
    // goes from 0 degrees to Temp value 
  {                                  // in steps of 1 degree 
    
    myservo.write(sensors.getTempCByIndex(0) + (sensors.getTempCByIndex(0) - tempSens) );     // tell servo to go to the current temperature plus the difference between the current and old temperature
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
 delay(150);

}

```

This is the code I have so far that I've pieced together from my limited knowledge, but any help would be appreciated,

Thanks,

John

---

<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:** [February 20, 2014, 6:37pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/2 "2014-02-20T18:37:04Z")

</div>

Hi, I could do with some assistance with this issue that I'm having because I'm relatively new to coding and am trying to receive data from a DS18B20 temperature sensor and use this data to move a servo the appropriate amount and then receive the next piece of data from the sensor and then the servo move, giving me a live feed gauge of the current temperature.

```cpp
#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 180;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();

  {
   sensors.requestTemperatures(); // Send the command to get temperatures
 
int tempSens = sensors.getTempCByIndex(0);
 for(pos = 0; pos < tempSens; pos += 1);  // goes from 0 degrees to Temp value
  }
}

void loop() 
{ 
  
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  
  sensors.requestTemperatures(); // Send the command to get temperatures
 
int tempSens = sensors.getTempCByIndex(0);
 delay(200);
  
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 
  
    // goes from 0 degrees to Temp value 
  {                                  // in steps of 1 degree 
    
    myservo.write(sensors.getTempCByIndex(0) + (sensors.getTempCByIndex(0) - tempSens) );     // tell servo to go to the current temperature plus the difference between the current and old temperature
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
 delay(150);

}

```

This is the code I have so far that I've pieced together from my limited knowledge, but any help would be appreciated,

Thanks,

John

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [February 20, 2014, 6:51pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/3 "2014-02-20T18:51:32Z")

</div>

a tip before posting code - press CTRL-T - that will reformat the style and makes code more readable.

---

<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:** [February 20, 2014, 6:58pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/4 "2014-02-20T18:58:10Z")

</div>

Thanks, but it doesn't seem to make any changes to the code, only opens a new tab

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [February 20, 2014, 7:06pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/5 "2014-02-20T19:06:03Z")

</div>

which platform are you on? Mac?  
then it might be @-T -  
anyway it is under the tools menu

---

<div class="post-metadata">

**Author:** ![knut\_ny](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/knut_ny/32/1013044_2.png) [@knut\_ny](https://forum.arduino.cc/u/knut_ny)\
**Post date:** [February 20, 2014, 7:07pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/6 "2014-02-20T19:07:15Z")

</div>

code changed.  
It makes servo goto position porportional to temp (0..60 deg C)

```cpp
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
int pos;    // variable to store the servo position 
const byte factor = 3; // keep temp below 180/3 = 60 degr. C or change factor to 2 or 1

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();
  sensors.requestTemperatures(); // Send the command to get temperatures
  int tempSens = sensors.getTempCByIndex(0);
  for(pos = 0; pos < tempSens* factor; pos ++) // guess you want servo to move from 0 to 'temp' at startup  mulitply factor for bette view in this example
  {
     myservo.write(pos);
    delay(20);
  }
}

void loop() 
{ 
  sensors.requestTemperatures(); // Send the command to get temperatures
  int tempSens = sensors.getTempCByIndex(0);
  delay(200);
  Serial.print(tempSens); 
  myservo.write(tempSens*factor );     // tell servo to go to the current temperature plus the difference between the current and old temperature
  delay(150);
}

```

---

<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:** [February 20, 2014, 7:07pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/7 "2014-02-20T19:07:51Z")

</div>

Okay, thanks, I'll give it a try

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [February 20, 2014, 7:11pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/8 "2014-02-20T19:11:05Z")

</div>

topic merged - please do not cross post - is not efficient for anyone.

---

<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:** [February 21, 2014, 12:15am UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/9 "2014-02-21T00:15:17Z")

</div>

Divide the problem into three parts:

Get a number from the temperature sensor.  
Convert the temperature into a servo position.  
Make the servo move to the calculated position.

If you're having trouble with one of these, you need to explain which one you're having trouble with.

---

<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:** [February 21, 2014, 6:44am UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/10 "2014-02-21T06:44:19Z")

</div>

I've had each part of the program running independently it's just getting the servo to go to the current value of the temperature sensor that I'm struggling with

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [February 21, 2014, 11:30am UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/11 "2014-02-21T11:30:15Z")

</div>

knut\_ny's solution looks like it meets the requirement - are you trying to do something different?

---

<div class="post-metadata">

**Author:** ![knut\_ny](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/knut_ny/32/1013044_2.png) [@knut\_ny](https://forum.arduino.cc/u/knut_ny)\
**Post date:** [February 21, 2014, 2:04pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/12 "2014-02-21T14:04:20Z")

</div>

## When 'reset': Servo runs to 0 deg then swing to approx 75 deg (assuming temp 25 deg C) It will stay at that position until you heat it (cool it). Try heating 10 degs. Servo shoud add another 30 degree angle.

To see 'more action' - increase factor to 5 or more.  
If your temprange is 20..40, you can (20 deg. span) change factor to 9 and:

"myservo.write(tempSens\*factor - 180 ); "

---

<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:** [February 21, 2014, 6:53pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/13 "2014-02-21T18:53:37Z")

</div>

Knut\_ny thank you so much, it works wonderfully, this is a particularly good first experience on the Arduino forum

---

<div class="post-metadata">

**Author:** ![knut\_ny](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/knut_ny/32/1013044_2.png) [@knut\_ny](https://forum.arduino.cc/u/knut_ny)\
**Post date:** [February 21, 2014, 7:00pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/14 "2014-02-21T19:00:35Z")

</div>

good to see you 'up and running'. U see the picture now, I guess..

---

<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:** [May 5, 2021, 5:05pm UTC](https://forum.arduino.cc/t/ds18b20-one-wire-servo-temperature-gauge/213931/15 "2021-05-05T17:05:54Z")

</div>

Closed on May 5, 2021, 5:05pm UTC


