Compilation error(Wemos D1 32 & Mini)

Transistor: TIP122 NPN Darlington
Board: Wemos D1 32 & Mini

Error message:
Executable segment sizes:
IROM : 264008 - code in flash (default or ICACHE_FLASH_ATTR) **
IRAM : 28792 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...) **
DATA : 1316 ) - initialized variables (global, static) in RAM/HEAP **
RODATA : 936 ) / 81920 - constants (global, static) in RAM/HEAP **
BSS : 25416 ) - zeroed variables (global, static) in RAM/HEAP **
Sketch uses 295052 bytes (28%) of program storage space. Maximum is 1044464 bytes.
Global variables use 27668 bytes (33%) of dynamic memory, leaving 54252 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.8
Serial port COM8
Connecting......................................____Traceback (most recent call last):
** File "C:\Users\Lwin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py", line 65, in

** esptool.main(cmdline)

** File "C:/Users/Lwin/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool\esptool.py", line 2890, in main

** esp.connect(args.before)

** File "C:/Users/Lwin/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool\esptool.py", line 483, in connect

** raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))**
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

Here is the CODE:

//Source taken from: https://learn.linksprite.com/jpeg-camera/tutorial-of-using-linksprite-2mp-uart-jpeg-camera-with-arduino/

//CP05/06: Camera commands (byte sequence) below are for LS-Y201 camera model. You will have to modify accordingly for your Serial Camera model following the manual (Manual_for_serial_camera.pdf)

//*******************************************************

// www.linksprite.com

// Note:

// 1. SD must be formated to FAT16

// 2. As the buffer of softserial has 64 bytes,

// so the code read 32 bytes each time

// 3. Please add the libaray to the lib path

//

// * SD card attached to ESP8266 nodeMCU SPI bus as follows:

// * MOSI – D7

// * MISO – D6

// * CLK – D5

// * CS – D8

//*******************************************************

#include <SoftwareSerial.h>

#include <SPI.h>

#include <SD.h>

SoftwareSerial mySerial(D4, D3); // Set Arduino pin 4 and 5 as softserial (RxD, TxD)

byte ZERO = 0x00;

byte incomingbyte;

long int j = 0, k = 0, count = 0, i = 0x0000;

uint8_t MH, ML;

boolean EndFlag = 0;

File myFile;

void SendResetCmd()

{

  mySerial.write(0x56);

  mySerial.write(ZERO);

  mySerial.write(0x26);

  mySerial.write(ZERO);

}

/*************************************/

/* Set ImageSize :

  /* <1> 0x22 : 160*120

  /* <2> 0x11 : 320*240

  /* <3> 0x00 : 640*480

  /* <4> 0x1D : 800*600

  /* <5> 0x1C : 1024*768

  /* <6> 0x1B : 1280*960

  /* <7> 0x21 : 1600*1200

  /************************************/

void SetImageSizeCmd(byte Size)

{

  mySerial.write(0x56);

  mySerial.write(ZERO);

  mySerial.write(0x54);

  mySerial.write(0x01);

  mySerial.write(Size);

}

/*************************************/

/* Set BaudRate :

  /* <1> 0xAE : 9600

  /* <2> 0x2A : 38400

  /* <3> 0x1C : 57600

  /* <4> 0x0D : 115200

  /* <5> 0xAE : 128000

  /* <6> 0x56 : 256000

  /*************************************/

void SetBaudRateCmd(byte baudrate)

{

  mySerial.write(0x56);

  mySerial.write(ZERO);

  mySerial.write(0x24);

  mySerial.write(0x03);

  mySerial.write(0x01);

  mySerial.write(baudrate);

}

void SendTakePhotoCmd()

{

  mySerial.write(0x56);

  mySerial.write(ZERO);

  mySerial.write(0x36);

  mySerial.write(0x01);

  mySerial.write(ZERO);

}

void SendReadDataCmd()

{

  MH = i / 0x100;

  ML = i % 0x100;

  mySerial.write(0x56);

  mySerial.write(ZERO);

  mySerial.write(0x32);

  mySerial.write(0x0c);

  mySerial.write(ZERO);

  mySerial.write(0x0a);

  mySerial.write(ZERO);

  mySerial.write(ZERO);

  mySerial.write(MH);

  mySerial.write(ML);

  mySerial.write(ZERO);

  mySerial.write(ZERO);

  mySerial.write(ZERO);

  mySerial.write(0x20);

  mySerial.write(ZERO);

  mySerial.write(0x0a);

  i += 0x20;

}

void StopTakePhotoCmd()

{

  mySerial.write(0x56);

  mySerial.write(ZERO);

  mySerial.write(0x36);

  mySerial.write(0x01);

  mySerial.write(0x03);

}



void setup()
{
  Serial.begin(115200);

  Serial.print("Initializing SD card...");


}

int intNameCount = 0;

/*void loop()
  {
  digitalWrite (transistor, HIGH);




  digitalWrite (transistor, LOW);

  } */


void loop() {

  if (!SD.begin(D8)) { //D8 stands for SD_CS

    Serial.println("initialization failed!");

    return;

  }
  else
  {

    const int transistor = D2;
    pinMode (transistor, OUTPUT);
    Serial.println("initialization done.");
    Serial.println("please wait.....");
    Serial.println("Transistor online in 3sec...");
    delay(3000);
    //digitalWrite (transistor, HIGH);
    byte a[32];

    int ii;
    i = 0x0000;
    intNameCount ++;

    mySerial.begin(115200);

    delay(200);

    SendResetCmd(); //Wait 2-3 second to send take picture command

    delay(2000);

    SetBaudRateCmd(0x2A);

    delay(100);

    mySerial.begin(38400);

    delay(100);

    SetImageSizeCmd(0x11); //Change Image Bit Size (Currently: 320 * 240)

    delay(100);

    SendTakePhotoCmd();

    delay(3000);

    while (mySerial.available() > 0)

    {

      incomingbyte = mySerial.read();

    }

    myFile = SD.open(String(intNameCount) + "pic.jpg", FILE_WRITE); //<strong><span style="color: #ff0000;">The file name should not be too long</span></strong>

    while (!EndFlag)

    {

      j = 0;

      k = 0;

      count = 0;

      SendReadDataCmd();

      delay(5);

      while (mySerial.available() > 0)

      {

        incomingbyte = mySerial.read();

        k++;

        delayMicroseconds(100);

        if ((k > 5) && (j < 32) && (!EndFlag))

        {

          a[j] = incomingbyte;

          if ((a[j - 1] == 0xFF) && (a[j] == 0xD9)) //tell if the picture is finished

            EndFlag = 1;

          j++;

          count++;

        }

      }

      for (j = 0; j < count; j++)

      {

        if (a[j] < 0x10) Serial.print("0");

        Serial.print(a[j], HEX); // observe the image through serial port

        Serial.print(" ");

      }

      for (ii = 0; ii < count; ii++)

        myFile.write(a[ii]);

      Serial.println();

    }

    myFile.close();

    Serial.print("Finished writing data to file");
    StopTakePhotoCmd();
    EndFlag = 0;
    //digitalWrite (transistor, LOW);
    delay(2500);

  }


}

Hello everyone, I am a newbie to Arduino so I hope you guys can help me out. Backstory, this is a 6 months school project and I was tasked to take multiple picture of a particular area to monitor it over 10 days and it has come to my attention that the camera when it is connected to Vin PIN constantly, the battery gets drained easily. So my teacher suggested me to use a transistor to act as a switch between the Vin and the Camera. Before adding in the transistor codes, the code does not have any problem and it can complie and do it intended task which was to take multiple picture and save it into the SDCard, however after adding in the transistor, it always have compilation error unless i take out the output pin connected to the base of the transistor or the RX,TX pin of the camera.

Thank you so much for your help. :slight_smile:

Hi @lwin12 .
This error is being caused because the IDE is unable to load the sketch into ESP.
It is usually caused by a lack of communication between COM and ESP.

But it can also be caused by ESP not being in Boot mode state.

You did not inform if you are trying to load with the camera and the transistor turned on in ESP or if to load the skechtch you turn off the camera and the transistor.

Take a test. Turn off the camera and transistor and try to load the sketch and then tell us the result.

RV mineirin

Hello, thank you for your response. When I removed the Camera, and transistor from the circuit, I was able to upload the sketch without any problem

Hi
What happens is that ports 2 (D4) and 15 (D15) need to be like this for ESP to go into boot mode:
15 Pulled Low/GND (directly connected to GND, or external pull-down resistor).
2 Pull-up resistor High/VCC, or No Connection (pin has internal weak pullup, external pullup resistor is optional)

As you have the camera connected to 2(D4), it may be hindering the ESP to enter boot mode.

RV mineirin

Do you have any suggestion on what I can do?

Hi
try using one of ports 16 (D0) instead of 2 (D4).

SoftwareSerial mySerial(D0, D3); // Set Arduino pin 16 and 0 as softserial (RxD, TxD)

RV mineirin

I will try it tomorrow and update how it goes. :slight_smile:

Hi, I was able to upload the code but it could not take any picture when i changed the pin to D0 from D4.

Have you tried upload via OTA?

Hi @lwin12

Is D2 being used for something?
If not, try this way:
SoftwareSerial mySerial(D2 D3); // Set Arduino pin 4 and 0 as softserial (RxD, TxD)

RV mineirin

Hi, when I tried this, I was able to upload the code but my Serial Monitor did not show anything.

Not a suitable transistor for this purpose. Because you are switching the 5V supply, you should use a PNP or a p-channel FET. Also a Darlington will have a higher voltage drop, which may mean insufficient voltage for the camera to operate. But after that there is another problem. The esp pins output only 3.3V, which will be insufficient to switch the transistor off with 5V connected to it's emitter/source.

Maybe you could use an npn transistor to control the base of the PNP transistor.

Or switch the ground connection to the camera instead of the 5V connection using an npn transistor.

Whatever way you do it, there is the danger of phantom powering the camera through the serial connections.

Hi, yes Paul is correct. After much trouble shooting turns out it was due to not using correct transistor! This is the new configuration.

Still not correct. 2n2218 is an NPN transistor. You need PNP there, plus a pull-up resistor to 5V on its base to switch it off. Also the bc548 should be connected between the base of the PNP and ground, so it can pull down the PNP's base when the bc548 is switched on.

Yes, however it solved the issue! :slight_smile:

Then you have been lucky. Others searching for solutions to similar problems should not copy your circuit, it may not work for them.

We did read up the datasheets before implementing this!

I guess the camera module is ok with around 4.3V (~0.7V dropped by the 2n2218), but the ~1.5V dropped by the darlington was too much.

A PNP such as bc337 would only drop around 0.2V.

It was either a voltage/current issue so we hooked it up to a ULN2003 board, and we were able to upload the codes!