ESP32 Sketch Uploaded but not executed

Hello all,

I have a little problem with my ESP32 DEVKIT V1
I have a sketch which should be on the board but is not executed.

All librabrys are installed and the board is also addressed during upload. However, there is no reaction.

Sketch:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int UVOUT = 15; //Output from the sensor
int REF_3V3 = 4; //3.3V power on the ESP32 board

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup()
{
Serial.begin(9600);
pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
}

//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;

for(int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;

return(runningValue);
}

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void loop()
{
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);

//Use the 3.3V power pin as a reference to get a very accurate output value from sensor
float outputVoltage = 3.3 / refLevel * uvLevel;

float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level

Serial.print("output: ");
Serial.print(refLevel);

Serial.print("ML8511 output: ");
Serial.print(uvLevel);

Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);

Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.print(uvIntensity);
Serial.println();
display.setCursor(20,0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("UV Ray Intensity");

display.setCursor(20,20); //oled display
display.setTextSize(3);
display.setTextColor(WHITE);
display.println(uvIntensity);

display.setCursor(20,45); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("mW/cm^2");
display.display();
delay(300);
display.clearDisplay();
}

Log during Upload:
esptool.py v3.0-dev
Serial port COM3
Connecting.......
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: ec:94:cb:4a:58:84
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 1394.4 kbit/s)...
Hash of data verified.
Compressed 17120 bytes to 11164...
Writing at 0x00001000... (100 %)
Wrote 17120 bytes (11164 compressed) at 0x00001000 in 0.2 seconds (effective 825.1 kbit/s)...
Hash of data verified.
Compressed 242864 bytes to 125371...
Writing at 0x00010000... (12 %)
Writing at 0x00014000... (25 %)
Writing at 0x00018000... (37 %)
Writing at 0x0001c000... (50 %)
Writing at 0x00020000... (62 %)
Writing at 0x00024000... (75 %)
Writing at 0x00028000... (87 %)
Writing at 0x0002c000... (100 %)
Wrote 242864 bytes (125371 compressed) at 0x00010000 in 1.9 seconds (effective 1005.1 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.0 seconds (effective 585.1 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

In the Serial Monitor i only get these:
Rebooting...
Guru Meditation Error: Core 1 panic'ed (IntegerDivideByZero). Exception was unhandled.
Core 1 register dump:
PC : 0x400d25b8 PS : 0x00060730 A0 : 0x800d12f0 A1 : 0x3ffb1f10
A2 : 0x3ffbdc60 A3 : 0x00000000 A4 : 0x00000001 A5 : 0x00000015
A6 : 0xffffffff A7 : 0x3ffbdc60 A8 : 0x800d25b8 A9 : 0x3ffb1ee0
A10 : 0x04c4b400 A11 : 0x00000014 A12 : 0x00000001 A13 : 0x00000001
A14 : 0x00060720 A15 : 0x00000000 SAR : 0x0000001c EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x4000c46c LEND : 0x4000c477 LCOUNT : 0x00000000

ELF file SHA256: 0000000000000000

Backtrace: 0x400d25b8:0x3ffb1f10 0x400d12ed:0x3ffb1f30 0x400d1c1e:0x3ffb1f50 0x400d0ed1:0x3ffb1f80 0x400d3d46:0x3ffb1fb0 0x400869bd:0x3ffb1fd0

Rebooting...
Guru Meditation Error: Core 1 panic'ed (IntegerDivideByZero). Exception was unhandled.
Core 1 register dump:
PC : 0x400d25b8 PS : 0x00060730 A0 : 0x800d12f0 A1 : 0x3ffb1f10
A2 : 0x3ffbdc60 A3 : 0x00000000 A4 : 0x00000001 A5 : 0x00000015
A6 : 0xffffffff A7 : 0x3ffbdc60 A8 : 0x800d25b8 A9 : 0x3ffb1ee0
A10 : 0x04c4b400 A11 : 0x00000014 A12 : 0x00000001 A13 : 0x00000001
A14 : 0x00060720 A15 : 0x00000000 SAR : 0x0000001c EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x4000c46c LEND : 0x4000c477 LCOUNT : 0x00000000

ELF file SHA256: 0000000000000000

Unfortunately, I do not have much experience with the arduinos which is why I do not understand where the error lies. Can anyone read something from this ?

Thanks in advance
Best regards

Error is division by zero. I can't see why. I suggest that you add some more serial prints, especially where you're doing division to narrow down where it fails.

I get the same crash on my "DOIT ESP32 DEVKIT V1". I added some serial output and it looks like the crash happens in:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

I would look at the Adafruit documentation for the SSD1306 display.

The i2c example in the library uses:

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

and

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

When I did the initialization that way in the sketch I no longer got the crash:

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
  Serial.begin(9600);
  pinMode(UVOUT, INPUT);
  pinMode(REF_3V3, INPUT);
  Serial.println(++Index); Serial.flush();
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
1 Like

@pxdigital, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

Can you please edit your opening post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

I tested another sketch without the display. The sensor works. i get good results over the serial monitor. So the problem must be the display integration