Hey,
I'm working on a project where I want a digital output pin to be set to HIGH when above a certain temperature and to LOW when below a certain temperature.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define CORRECT_PIN 8 // Data Pin (D5)
#define WRONG_PIN 5 // unused Pin (D2)
#define OUTPUT_PIN 18 // D9
OneWire* oneWire;
DallasTemperature* sensors;
// Sensor 2: 28696E0461240E1D with tape!
DeviceAddress outsideThermometer = { 0x28, 0x69, 0x6E, 0x04, 0x61, 0x24, 0x0E, 0x1D };
void setup(void) {
delay(2000);
// start serial port
Serial.begin(9600);
Serial.println("Initializing DS18B20 with wrong pin...");
oneWire = new OneWire(WRONG_PIN);
sensors = new DallasTemperature(oneWire);
sensors->begin();
delay(500); // Let it do "nothing" for a bit
Serial.println("Switching to correct pin...");
delete sensors;
delete oneWire;
oneWire = new OneWire(CORRECT_PIN);
sensors = new DallasTemperature(oneWire);
sensors->begin();
// define Output-Pin
pinMode(OUTPUT_PIN, OUTPUT);
Serial.println("Setup done.");
// locate devices on the bus
Serial.print("Found ");
Serial.print(sensors->getDeviceCount(), DEC);
Serial.println(" devices.");
Serial.println("Setting alarm temps...");
// alarm when temp is higher than 30C
sensors->setHighAlarmTemp(outsideThermometer, 30);
// alarm when temp is lower than 28C
sensors->setLowAlarmTemp(outsideThermometer, 27);
}
void loop(void) {
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors->requestTemperatures();
Serial.println("DONE");
float tempC = sensors->getTempC(outsideThermometer);
Serial.println(tempC);
delay(1000);
// Check for alarms
if (sensors->hasAlarm(outsideThermometer)) {
float tempC = sensors->getTempC(outsideThermometer);
if (tempC > 30.00) {
digitalWrite(OUTPUT_PIN, HIGH);
} else if (tempC < 28.00) {
digitalWrite(OUTPUT_PIN, LOW);
}
}
}
The temperature part works as intended, giving me plausible temperature values. To test if the output pin is set to high I measured the voltage with a multimeter from Pin D9 to GND. I got close to 0 V. Measuring with the same multimeter from D5 (my data pin) to GND gave me values around 3 V as expected. Then I tried measuring the D9 state with my board directly, using the following code:
#define OUTPUT_PIN 18 // D9
#define TEST 8 // D5
void setup() {
Serial.begin(9600);
pinMode(OUTPUT_PIN, OUTPUT);
pinMode(TEST, INPUT);
}
void loop() {
digitalWrite(OUTPUT_PIN, HIGH);
delay(500);
Serial.print("HIGH read: ");
Serial.println(digitalRead(TEST));
digitalWrite(OUTPUT_PIN, LOW);
delay(500);
Serial.print("LOW read: ");
Serial.println(digitalRead(TEST));
}
I connected the pins D5 and D9 using a jumper wire. Even with this simple code I couldn't measure a HIGH, the printed values were all 0.
What else have I tried:
- Using different digital Pins (same result)
- Using the digital Pins I use in the test code as the data pin for the temperature measurement (every tested digital pin gave me correct temperature values --> Shouldn't be a soldering issue, I also seem to use the right GPIO Pin numbers)
- Using a different jumper wire in case the one I used was broken (same result)
Other weird things that may correlate to my problem:
- To see if the problem is in the logic in the temperature measurement code I gave the HIGH and LOW signal to the built in red LED. It worked exactly the opposite as I intended it to work (Shining red when I set it to LOW, being off when I set it to HIGH)
- When I power cycled my board the temperature measurement wouldn't work anymore. It would work again, when I uploaded a sketch with a wrong pin number as data pin and then uploaded the sketch with the correct pin number again. Because of this I do this weird dance in the setup section where I initialitze OneWire with WRONG_PIN, delete it and then initialize it againg with CORRECT_PIN
Is there something else I can try or is my board broken (just bought it, this is my first project using this Nano ESP32)? I'm grateful for any advice!