Servo & Temp Sensor

I am trying to make a temperature sensor activate a servo motor. More or less, I want to be able to have it turn 180 degrees when the temperature gets to a certain point. Also, once the temperature falls below that level it goes back to 0.

I have been trying my best to learn and piece together some other projects but not having a lot of luck. I am able to get the temp sensor working by itself and the servo by itself but can't get the two to work together.

Currently the code I am trying to use is just showing from my understanding 0 at 50 degrees and 180 at 80 degrees. although I really want it to be any temp greater than 80F degrees 180 anything lower than 80 degrees would be 0.

Any help would be greatly appreciated because I am struggling a lot.

I am using a DS18B20 temp sensor & a SG90 servo with an Arduino Nano

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

const int SENSOR_PIN = 4; // Arduino pin connected to DS18B20 sensor's DQ pin

Servo mainServo;
int position = 0;
int previousPosition;

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library

float tempCelsius;    // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit

void setup()
{
  Serial.begin(9600); // initialize serial
  tempSensor.begin();    // initialize the sensor
  mainServo.attach(3);
  
  Serial.begin(9600);

}

void loop()
{
  tempSensor.requestTemperatures();             // send the command to get temperatures
  tempCelsius = tempSensor.getTempCByIndex(0);  // read temperature in Celsius
  tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit

  Serial.print("Temperature: ");
  Serial.print(tempCelsius);    // print the temperature in Celsius
  Serial.print("°C");
  Serial.print("  ~  ");        // separator between Celsius and Fahrenheit
  Serial.print(tempFahrenheit); // print the temperature in Fahrenheit
  Serial.println("°F");

  delay(500);
int position = map(tempFahrenheit, 50, 80, 0, 180);
Serial.println(position);
if(previousPosition != position){
    mainServo.write(position);
    delay(1000);
}}

sounds like you don't need the map at all..
try this..

//int position = map(tempFahrenheit, 50, 80, 0, 180);
if (tempFahrenheit<80){
position = 0;
} else
{  
position =180;
}
Serial.println(position);
if(previousPosition != position){
  previousPosition = position;
    mainServo.write(position);
    delay(1000);
}

simmed here..

Here's a simulation of a servo. Work it into your sketch.

[edit] old simulation died... new one here...

sketch.ino
#include <DHT.h>
#include <Servo.h> // servo library

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
const int DHTpin = 4; // Arduino pin connected to DS18B20 sensor's DQ pin
const byte servoPin = 3;

DHT dht(DHTpin, DHTTYPE);
Servo myServo;
byte wait = 15; // slow servo movement

float hOld, cOld = 0, fOld, hif, hifOld, hic, hicOld;

void setup() {
  Serial.begin(115200);

  dht.begin();

  myServo.attach(servoPin);
  myServo.write(90); // start at servo "home"
}

void loop() {
  delay(2000); // wait between DHT readings

  float h = dht.readHumidity();
  float c = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(c) || isnan(f)) {
    Serial.println(F("Failed to read DHT."));
    return;
  }

  hif = dht.computeHeatIndex(f, h);
  hic = dht.computeHeatIndex(c, h, false);


  if (h != hOld || c != cOld || f != fOld || hif != hifOld || hic != hicOld) {
    hOld = h; cOld = c; fOld = f; hifOld = hif; hicOld = hic;

    Serial.print(F("Humidity ("));
    Serial.print(h);
    Serial.print(F("%) Temperature ("));
    Serial.print(c);
    Serial.print(F("°C "));
    Serial.print(f);
    Serial.print(F("°F) Heat index: ("));
    Serial.print(hic);
    Serial.print(F("°C "));
    Serial.print(hif);
    Serial.println(F("°F)"));
  }

  int cMap = map(c, -40, 80, 0, 180);
  myServo.write(cMap);
}
diagram.json for wokwi.com
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-servo",
      "id": "servo1",
      "top": -173.8,
      "left": -46.2,
      "rotate": 270,
      "attrs": {}
    },
    {
      "type": "wokwi-text",
      "id": "legendservo3",
      "top": -134.4,
      "left": -57.6,
      "attrs": { "text": "-40C" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -56.84, "left": 0, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -9.6, "left": -19.8, "attrs": {} },
    {
      "type": "wokwi-dht22",
      "id": "dht1",
      "top": -191.7,
      "left": 119.4,
      "attrs": { "temperature": "-40", "humidity": "100" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo1",
      "top": -134.4,
      "left": 86.4,
      "attrs": { "text": "80C" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo2",
      "top": -172.8,
      "left": 86.4,
      "attrs": { "text": "60C" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo4",
      "top": -220.8,
      "left": 28.8,
      "attrs": { "text": "20C" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo5",
      "top": -201.6,
      "left": 67.2,
      "attrs": { "text": "40C" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo6",
      "top": -201.6,
      "left": -9.6,
      "attrs": { "text": "0C" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo7",
      "top": -172.8,
      "left": -48,
      "attrs": { "text": "-20C" }
    }
  ],
  "connections": [
    [ "vcc1:VCC", "servo1:V+", "red", [ "v19.2", "h105.5" ] ],
    [ "gnd1:GND", "servo1:GND", "black", [ "v-9.6", "h86.4" ] ],
    [ "nano:GND.3", "dht1:GND", "black", [ "h9.6", "v-96" ] ],
    [ "nano:5V.2", "dht1:VCC", "red", [ "v0", "h-19.2" ] ],
    [ "nano:4", "dht1:SDA", "green", [ "v-9.6", "h48", "v-67.2" ] ],
    [ "nano:3", "servo1:PWM", "green", [ "v-19.2", "h-28.8" ] ]
  ],
  "dependencies": {}
}

Thank you very much, this is exactly what I was looking for. I figured there was a simpler way but this is the first time using servos.

Tested and working properly, only minor issue is the servo keeps humming a bit after. Although I feel that’s likely because of the 10 second delay. Will do some testing with that.

Thank you for this!

you're welcome..

curious, there is no 10 second delay that I see..
there's a 1 second delay(1000) after the servo moves..
then there is a 1/2 second delay in main loop just after temp reading..
mainServo.write() blocks, meaning it doesn't return control back to you until it finishes..
double checked you only call write once, so no I don't think the humming is because of delay..
does it hum when it swings both ways or just one??
strange..

~q

Most servo problems reported on this forum are due to an inadequate servo power supply, like trying to use the Arduino 5V output to power the servo. Avoid any tutorial that suggests this is OK, as doing so can actually destroy the Arduino.

Instead, use a 6V 4xAA battery pack to power the servo. Don't forget to connect the grounds (battery negative to Arduino GND).