Using if statement causes fatal error - Seeed ESP32C

Hello everyone,
I am new to Arduino and having some trouble. I was hoping someone could point me in the right direction. At the moment I'm trying to set up the initial homing of a stepper motor: Move the stepper motor until a hall sensor is triggered by a magnet and then stop".

I am using the Seeed ESP32C6.

I have a sketch that turns the stepper which uploads and works just fine.

I have a separate sketch that works with the hall sensor. It also uploads and works just fine. When it runs, I get serial output showing 1 when no magnet is near and 0 when the magnet gets close enough.

When I try to merge them together, I get:
A fatal error occurred: Unable to verify flash chip connection (No serial data received.).
Failed uploading: uploading error: exit status 2

Every time I get this error, I have to hold down the Boot button and connect the device to USB again. Then I can upload the Blink sketch, and everything works fine again. (Upload speed is set to 115200). After this I can upload the sketch for the stepper and it works. I can then upload the sketch for the hall sensor and that works too. But if I try to upload the code below I'm back to the fatal error again. I have tried different USB cables and I have a 2nd Seeed ESP32C6 but both give the same result.

Here is the code I am using. Any suggestions?

//Includes the Arduino Stepper Library
#include <Stepper.h>

// initialize the stepper
const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, D5, D9, D8, D10);

//digital sensor setup
const int hallDigitalPin = D4;
int hallDigital;

void setup() {
  //setup the hall sensor
  pinMode(hallDigitalPin,INPUT);
  // set the speed at 60 rpm:
  myStepper.setSpeed(10);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  hallDigital = digitalRead(hallDigitalPin);
  Serial.print("Digital: ");
  Serial.println(hallDigital);
  if (hallDigital == 1){
  myStepper.step(1);
  }

}
1 Like

It might be bad HW, look at the following and do the debugging steps outlined there. https://github.com/espressif/esptool/issues/836

show pictures of your setup

have a look at ESP32C6 GPIO reference
check what GPIO are connected to D4 D5 D8 D9 and D10 in case there are restrictions on their use

Hi Kolaha,
Here are some pictures of what I have so far. Only lets me upload 3 since I'm new to the forum.



Horace,
I attempted to decipher that reference document but it's a bit over my head. However, since you mentioned the possibility that some of the pins may have special uses, I moved the hall sensor to pin D02 and it's working now. That's twice in a row where I couldn't get something working because of the pins I was using, and I don't understand why. I had originally tried using pins D07, 8, 9 & 10 for controlling the stepper motor and it would only turn in one direction. I had to move the connection from pin 7 to 5 to get it working. I guess I need to do some reading to wrap my brain around the different pins and what they can be used for.

Sonofcy,
I have the same result with two different Seeed ESP32C6 modules. Figure the most likely problem is lack of knowledge on my part.

There are two things you need to learn and understand. The first is how to get a datasheet where the pins with reserved or special uses are documented. This onlhy requires google, 'board number datasheet'. The second is what goes in your source code to match up to the pins your eyes are looking at. Try to find and use the Dx or GPIOx rather than just a number. It's a bit trial and error plus experience and study. for esp boards check the "random nerds tutorial" website, they have mist things esp. I have over 50 years experience, and I still make the odd mistake so not to worry.

Well, I spoke too soon. Everything was working well. The stepper moves in one direction until a magnet gets close enough to the hall sensor and it stops as intended. I rigged up a new hall sensor inside of the model I am building, and it too worked for a little while. I tested it a dozen times and then all of a sudden it stopped working. Moving the magnet near the sensor had no effect. Now I'm back to the error below whenever I try to upload a sketch.

A fatal error occurred: Unable to verify flash chip connection (No serial data received.).
Failed uploading: uploading error: exit status 2

If you get that error while trying to upload a sketch, just do the usual, try again, make sure no other windows are open and have a lock on the port, shut down the IDE as a last resort. I get similar errors a few times a week, they just need patience.

how are you powering the system? could you be overloading the ESP32 3.3V output?
upload a schematic showing connections and power supplies

if you remove all the external devices can you upload a simple program such as blink?

Horace,
I am not using the power output of the ESP32. It draws power via USB from my computer. The stepper and Hall sensor get their power from the breadboard power supply (Elegoo power MB v2) connected to a 9v battery. I created a fritzing diagram but I don't think this forum will allow me to upload another photo. Apparently new users are only allowed 3 pictures? I'll try now.

I've modified the code slightly which seems to have helped. I can now use Pin D7 for the driver board. Not sure if it is best practice to use pinMode as I am, but I saw some example code where they did that, and now I can use the pins I wanted to for the motor driver.

I still have the problem with the hall sensor though. When I upload the sketch, everything works as expected. The motor rotates until I get a magnet close to the hall sensor. If I do that 5-10 times, it eventually fails to stop the motor. It moves slower but it doesn't stop. I can see in the serial output the value on the hall output is 0, but I also see the occasional 1 which seems to explain the motor stepping but at a lower speed. I can live with that as I am not intending to use this code long term. This was just a POC to see if I could make it work at all. The real problem is that once this happens, I can no longer upload a new sketch to the EPS32C6. I get the error below and have to hold down the Boot button on the ESP32C6 and reconnect the USB cable. Then I can upload a new sketch again.

A fatal error occurred: Unable to verify flash chip connection (No serial data received.).
Failed uploading: uploading error: exit status 2

//Includes the Arduino Stepper Library
#include <Stepper.h>

// initialize the stepper
const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, D7, D9, D8, D10);

//digital sensor setup
const int hallDigitalPin = D1;
int hallDigital;

void setup() {
  //setup the hall sensor
  pinMode(hallDigitalPin,INPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);
  pinMode(D9, OUTPUT);
  pinMode(D10, OUTPUT);
  // set the speed at 60 rpm:
  myStepper.setSpeed(10);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  hallDigital = digitalRead(hallDigitalPin);
  Serial.print("Digital: ");
  Serial.println(hallDigital);
  if (hallDigital == 1){
  myStepper.step(10);
  }

}

Ultimately the question I'm trying to answer is:
Is my code junk? (well it's certainly not sophisticated, but neither is my project. :slight_smile: )
Do I have it miswired somehow and that's causing a problem for the ESP32C6?
Support for this chip for Arduino is still in "Dev". Is it just buggy?

It's just frustrating that I have to boot the device every time I edit the code.

Did you back the boards off to 2.0.17, or just use a regular esp32 dev board instead of the expensive fancy new (buggy) boards.

An 28BYJ-48 draws about 350mA. Both 9volt smoke alarm battery and breadboard supply will struggle with this, and one of them will eventually give up. Resulting in a slower spinning motor or missing steps. Measure the motor supply during use, or try three AA batteries (4.5volt) for the motor.

An ESP32 draws about 400mA peaks when WiFi is on. 400mA and 350mA for the motor is too much for common 500mA USB, so if you try to power both from USB you also will have issues.

@sonofcy Don't use 2.0.17 as a general cure. My ESP32-C3 projects work fine on 3.0.3
Leo..

Noted