DS18B20 One Wire Servo Temperature Gauge

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.

#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

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.

#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

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

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

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

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

#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);
}

Okay, thanks, I'll give it a try

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

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.

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

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

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 ); "

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

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