Not getting correct reading from sensor

I bought a temperature sensor from amazon so I understand the the problem could be with the sensor. I copied the code from a tutorial, but its fairly old so I was just wondering if there’s something wrong with or needing updated in the code.

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
  Serial.begin(9600);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  
  float tempC = sensors.getTempCByIndex(0);
  float tempF = sensors.getTempFByIndex(0);
  
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print(" \xC2\xB0");
  Serial.print("C  |  ");

  Serial.print(tempF);
  Serial.print(" \xC2\xB0");
  Serial.println("F");

  delay(1000);
}

I have made sure I’m plugged into the right pin and I have all the correct pull_up resistors.

Edit: The issue that I’m having is the only reading that I get is the lowest potential reading

Basically your code does what my drying oven code does, so it's time to show us a schematic of your project and include how you are powering it and which Arduino you are using.

Paul

I test your code with a 18B20 sensor connected to my Uno and get the below as a result.

Temperature: 26.56 °C  |  79.81 °F
Temperature: 26.62 °C  |  79.93 °F
Temperature: 26.62 °C  |  79.93 °F
Temperature: 26.94 °C  |  80.49 °F
Temperature: 27.50 °C  |  81.50 °F
Temperature: 28.06 °C  |  82.51 °F
Temperature: 28.50 °C  |  83.30 °F
Temperature: 28.75 °C  |  83.75 °F
Temperature: 28.87 °C  |  83.97 °F
Temperature: 29.00 °C  |  84.20 °F
Temperature: 29.00 °C  |  84.20 °F

I am holding the sensor in my hand, heating it.

So it appears that the code is not the problem. Can you post a photo of your setup that shows all of the wiring?

Paul_KD7HB:
Basically your code does what my drying oven code does, so it's time to show us a schematic of your project and include how you are powering it and which Arduino you are using.

Paul

how do I upload an image?

Add the image as an attachment.

Here is the schematic

Sorry, i was under the (mistaken) impression that you were using a DS18B20 one wire sensor. My bad for assuming (based on your code).

The code that you posted is not going to work with a DS1621. The Tout signal is a digital (on-off) signal for a thermostat, not a temperature reading. Here is the data sheet.

The sensor communicates over the I2C bus.
There are libraries available to use that sensor with Arduino.
Here is one.
Here is another.
Both libraries have example code to get you started. Neither is available through the library manager, you will have to install them manually.

And here is a tutorial on interfacing to an Uno. Wiring to a Nano will be the same.

groundFungus:
Sorry, i was under the (mistaken) impression that you were using a DS18B20 one wire sensor. My bad for assuming (based on your code).

The code that you posted is not going to work with a DS1621. The Tout signal is a digital (on-off) signal for a thermostat, not a temperature reading. Here is the data sheet.

The sensor communicates over the I2C bus.
There are libraries available to use that sensor with Arduino.
Here is one.
Here is another.
Both libraries have example code to get you started. Neither is available through the library manager, you will have to install them manually.

And here is a tutorial on interfacing to an Uno. Wiring to a Nano will be the same.

Sorry for the confusion. I am using DS18B20 I only used the sensor in the diagram as a place holder

So I wasted my time trying to help you because you posted inaccurate data. Bad data is worse than no data.

What is the value of the pull-up resistor? Post a photo of your wiring.

GroundFungus. Kudos, you have the patience of a saint.

Steve