I have been experimenting with INPUT_PULLUP on the Nano. Everything was going well and PinMode(2,INPUT_PULLUP); worked on a little sketch I modified to show how a floating pin would stay high if I designated it as INPUT_PULLUP.
But when I tried to incorporate it into a temperature sensor sketch it wouldn't work.
If I physically hook up a pullup resistor to pin 2 & 5v Vcc, it works and I get valid readings.
I have tried other pins, but no soap. I suspect it is the way the sensor is being interrogated. I am using the Onewire & DallasTemperature libraries. I can leave the resistor on the breadboard and it works, but it is messy and I would like to be able to use the pullup feature, which is new to me.
attached is my sketch. Any suggestions ? It has been a long time since I posted, and I hope I am including the code correctly.
/*
Arduino DS18B20
Created: 04/20/2016
By Gus
Modified N/A
By Gus
https://arduinomylifeup.com/arduino-ds18b20-temperature-sensor
*/
#include <OneWire.h>
#include <DallasTemperature.h>
int temp_sensor = 2; // Pin DS18B20 Sensor is connected to Digital Pin 2
float temperature = 0; //Variable to store the temperature in
OneWire oneWirePin(temp_sensor);
DallasTemperature sensors(&oneWirePin);
void setup(void){
Serial.begin(9600);
pinMode(2,INPUT); // Doesn't work if I physically remove the resistor and use pinMode(2,INPUT_PULLUP);
sensors.begin();
}
void loop(){
// Serial.print("Requesting Temperatures from sensors: ");
sensors.requestTemperatures();
// Serial.println("DONE");
temperature = sensors.getTempFByIndex(0);
int rndTemp = round(temperature);
Serial.print("Temperature is ");
//Serial.print(temperature);
Serial.print(rndTemp);
Serial.print("\xC2\xB0"); // This prints the Degree symbol
Serial.println("f");
delay(1000);
}
Check the datasheet for the microprocessor on the board you use, you will find the equivalent ohms of its internal pull-up, it is considered weak. And only spec is a range.
Some ppl, like me, are old fashioned and always use real resistors for this.
Mostly always, unless I'm just experimenting and lazy. But if something isn't working then, I go to the minor trouble of believing my own superstitions.
OneWire sets the pin to "INPUT" during initialization so an external pullup is required. Even though I have not tried this, you may be able to use the internal pullup like this:
I looked at the Nano datasheet (or maybe it is a specsheet) but I could not find anything about pullups anywhere.
but it didn't look like this.
I'll keep looking.
If you get -196.6F (rounded to -197), that would indicate an error with the communication as per DallasTemperature source. The external pullup seems inavoidable.
you misunderstand. i'm not resisting. i'm appreciative. but i am such a dunce that i have to ask dumb questions to see if there is an approach others see but i don't.
Yeah, that's an error code (see post #11). The library "thinks" your sensor is disconnected. Its thinking correctly because INPUT_PULLUP is only 30K-50K which is really really weak (high resistance) compared to the required stronger 4.7K (lower resistance) external resistor that's required.
Yeah the full data sheet will say. Note in the diagram @paulpaulson shared #5 above in the tiny print
Internal Pull Up 20-50K
As for using real resistors being "messy" there's all kindsa messiness all over every aspect of this undertaking or hobby or profession. Lotsa times it can be discretely hidden… but it be there.
If I connect my Amp meter between Nano pin 2 (set to INPUT_PULLUP) and GND, with Vcc of 4.7V, I get a current of 130 microAmps, so R = 4.7V / 0.00013A = approximately 36000 Ohms, way too high for DS18B20 pullup, use the recommended 4.7k.