ESP32 and PMW

Hi friends,

I have tried to write a project where I'm using an ESP32, a DHT11 and a 4 wire PWM caged fan.
I'm trying to log the data from the DHT11 on my blynk-app and send PWM signals to my caged fan at the same time.

the first code I have is the basic Blynk example:

#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "nabofarm";
char pass[] = "regnbuer";

#define DHTPIN 4        // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

the new one is one I did with a mix of the example and another one where I should be able to send out PWM signal to pin 16.

The example is running fine but when I upload the new one it's only online on the blink app for a short period. Does anyone know what's wrong?

/*  App project setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "nabofarm";
char pass[] = "regnbuer";

#define DHTPIN 4        // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);









const int ledpin = 16; 

const int freq= 20000; 

const int channel= 0; 

const int resolution= 8;













  
}

void setup()
{


ledcSetup (0, 20000, 8); 

ledcAttachPin (16 , 0);








  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  


if (dht.readHumidity() < 60)
  // increase the LED brightness
  {for(int dutyCycle = 0; dutyCycle = 0; ){   
    // changing the LED brightness with PWM
    ledcWrite(0, dutyCycle);}
  }
    else
    
  {
    for(int dutyCycle = 0; dutyCycle = 255; ){   
  
    // changing the LED brightness with PWM
    ledcWrite(0, dutyCycle);}
  }
  


  delay (1000);


{
  Blynk.run();
  timer.run();
}

}

This for loop is strange, missing a clause and I suspect will loop forever:

    for (int dutyCycle = 0; dutyCycle = 0; )

Hi Wildbill,

Do you have any suggestions? :slight_smile:

Best Frederik.

I'm not sure what you're trying to achieve with the for loops. Try replacing each of them with a simple call to ledcWrite with a hard coded number for the duty cycle.

Hi frederik,

you are welcomed to the forum. All I write i snot meant as offending or anything similar. It is just clear stating what I read and conclude.

You should describe in normal words what your ESP32 shall do.
From what you have coded so far I can conclude that you are a beginner.
And as you already discovered yourself your code does not what you want to achive, because you don't know (yet) enough to translate the functionality into code.

Sp please describe the functionality in normal words. To me too it is unclear what you want to achive.

your code

for(int dutyCycle = 0; dutyCycle = 255; )
for (int dutyCycle = 0; dutyCycle = 0; ) {

is still missing important parts
It is a quite simple to resolve issue. But if you haven't found it yourself this celarly shows it is better if you describe your functionality in normal words by avoiding all and any programming-terms. Because partially you would use the programming-terms in a wrong way caused by the gaps in understanding how they work.

Please look up the for-command in the Ardiuno-reference.
Blynk makes it quite easy to control a microcontroller remotely. Bit as soon as you add own functionality that goes beyond the provided examples in blynk you have to tlearn how programming works.

One important thing is add only one thing at a time.
Write a small testprogram that does nothing more than changing the pwm-duty-cycle of an IO-Pin
test it until it works reliably. It will be much easier to discover bugs in a small program than a large one.
You can invest one hor into a smal testprogram or 5 hours in tinkering with the big porgram and still not have a solution.

So it is up to you to choose a way of learning straight forward small testprograms or loosing time with guessing and tinkering

best regards Stefan