ESP32 - I can't find the reason for strange behavior in my code

Hi guys!
I have made a program for an indoor gardening project that runs on an ESP32.
Among other things, the program is supposed to run a ventilation fan when temperature or humidity readings are at certain levels. This is done with pwm and is either 0V (off), a percentage of the 12V, or 12V.
The system has one 3-way switch that when in mode 1 is set up for mushroom growing, in mode 2 is set up for greens growing, and in mode 0 is waiting for switch change.
My problem however is that the fan only runs if the switch is set to either mode 1 or 2 when the power started. And then if I use the switch to change mode, the fan stops and doesn't resume. It's not the fan, the ESP32 just doesn't output the pwm signal any more...
I'm hoping someone here can see what the cause could be?
I've been at this since this morning and am starting to loose my mind :slight_smile:
Code below:

#include <DHT.h>
#include <TimeLib.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#include <NewPing.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <time.h>

    ....
    prevbutton = 0;
    digitalWrite(moist_pin, LOW);
    digitalWrite(mushlight_pin, LOW);
    digitalWrite(growlight_pin, LOW);
    digitalWrite(pump_pin, LOW);
    digitalWrite(fan_pin, LOW);
    analogWrite(pwm_pin, 0);
    ismoisting = 0;
    isfaning = 0;
    last_measureday = 0;
    plantmeasure_before = 0;
    plant_measure = 0;
  }

  // Running oxgenating function regardless of mode setting since it's important to keep water/nutrientsolution fresh
  OxygenTimer(now_minute); 

  if (now_second != last_second) {
    last_second = now_second;
    loopTime = millis();
  }

}

Thanks!

Cannot image after writing all this code things stopped working properly. :thinking:


You are going to have to isolate sections one by one to see what’s happens.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

Use channel 3 for the PWM to see if that fixes the issue.

I just found the issue haha :slight_smile:
In the bottom of all that code I had forgot to remove this

from before when I used an Arduino instead of the ESP32.
Thanks anyway guys, sorry to have wasted your time :slight_smile:

I do have another question though:
That switch I use is connected to the 3v3 output on the ESP32 and then I read the input on pins 12 and 14 to see if one of these have been selected.
If I switch is set to mode 2 (high input on pin 12), then the system doesn't boot up when I start the power.
Does anyone of you know why that might be?

Maybe this:

Oh okay, I can try another pin for the switch and see if that helps then. Thanks :slight_smile:

Changed to pin 13 and that resolved the boot up issue :slight_smile:
Thanks LarryD, you're a legend :slight_smile:

Yes, GPIO_NUM_12 is used during boot and programing by the ESP32. During the boot process GPIO_NUM_12 must be allowed to switch states as per the CPU.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.