digitalWrite() not working Arduino Nano ESP32

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!

Why are you using different pin numbers? The Arduino IDE takes care of mapping the pin numbers referenced on the board to whatever actual pin is used on the chip itself.

#define CORRECT_PIN 5  // Data Pin (D5)
#define WRONG_PIN 2    // unused Pin (D2)
#define OUTPUT_PIN 9  // D9

From the pins_arduino.h file for the Arduino Nano ESP32:

static constexpr uint8_t D2         = 2;
static constexpr uint8_t D3         = 3; // also CTS
static constexpr uint8_t D4         = 4; // also DSR
static constexpr uint8_t D5         = 5;
static constexpr uint8_t D6         = 6;
static constexpr uint8_t D7         = 7;
static constexpr uint8_t D8         = 8;
static constexpr uint8_t D9         = 9;

and

static constexpr uint8_t D2         = 5;
static constexpr uint8_t D3         = 6;  // also CTS
static constexpr uint8_t D4         = 7;  // also DSR
static constexpr uint8_t D5         = 8;
static constexpr uint8_t D6         = 9;
static constexpr uint8_t D7         = 10;
static constexpr uint8_t D8         = 17;
static constexpr uint8_t D9         = 18;

Which one gets used seems to be dependent on a compile time switch.

Personally, I'd stick with the Dx notation.

How have you got the pin numbering set?

Have you tried measuring the other pins, in case the one being taken high is not the one you are expecting?

I don't have a DS18B20 available at the moment, so I used a sketch based on the 'blink' example to investigate.

I used your:

#define OUTPUT_PIN 18  // D9

and got the defined pin to go alternately HIGH and LOW (and the built in LED as well).

#define OUTPUT_PIN 18  // D9
int led = 13;

void setup() { 
  pinMode(led, OUTPUT);
  pinMode(OUTPUT_PIN, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH);   
  digitalWrite(OUTPUT_PIN, HIGH);
  delay(100);               
  digitalWrite(led, LOW);    
  digitalWrite(OUTPUT_PIN, LOW);
  delay(100);               
}

I checked pin D9, and found that is wasn't being toggled, but the built in LED was blinking.
Next I checked the other digital and analog pins - I found that A1 had activity on it.

I suggest that you go and check whether pin A1 is doing what you expect D9 to be doing.

Edit:
Have a look at the pinout at docs.arduino.cc/resources/pinouts/ABX00083-full-pinout.pdf .
That confirms that pin A1 is also D18.

chrisley1337,
After re-reading sonofcy's post #4, I tried altering the pin numbering from the default 'By Arduino pin' to 'By GPIO number (legacy)'.

Guess what - it behaves like you were expecting.

Just an observation on my part using a ESP8266. One of the older IDEs will allow pin 2 to be defined as 2 and it compiled without a problem. A few years later with a newer IDE it would compile properly but would not work until I changed it to D2.

Changing the pin numbering setting worked. Thank you everyone!