[HELP] out of ports interfacing GPS with ESP32CAM over serial

Hello, I'm attempting to read the gps time from an Adafruit Ultimate GPS featherwing using an ESP32CAM. It's what my professor gave me to work with so I'm trying my best.


Not sure if this circuit will work

The problem is that the GPS communicates over serial and the Cam doesn't have any serial ports left. (SD card, camera, programmer all use one).

I've been doing some research and I think my possible solution could be one of these:
EspSoftwareSerial but I don't know how to set it up for reading a gps
Serial.swap()?
HardwareSerial somehow? I saw it in this thread:

But I wasn't sure how they got it working.

I'm just trying to get the example code for the GPS working with the ESP32CAM:

#define GPSSerial Serial1

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);
  GPSSerial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    GPSSerial.write(c);
  }
  if (GPSSerial.available()) {
    char c = GPSSerial.read();
    Serial.write(c);
  }
}

Thank you for your time

You can use a second hardware serial port on the ESP32-CAM by including (for example):

	Serial2.begin(115200,SERIAL_8N1,16,17);  //only 16 is needed to receiver GPS signals

Note the U2RXD on GPIO16:

If you don't need the SD card, some of those pins are free and you could use GPIO 14 and 15 for Serial2.

Or use Software Serial. If you look around on the web you will find several solutions.

Wow thanks for the fast reply!

I'm not sure I understand. Maybe this is too far out of my league.

I'm trying to receive the output from the gps over uart using this example code:

#define GPSSerial Serial1

void setup() {
  // make this baud rate fast enough to we aren't waiting on it
  Serial.begin(115200);

  // wait for hardware serial to appear
  while (!Serial) delay(10);

  // 9600 baud is the default rate for the Ultimate GPS
  GPSSerial.begin(9600);
}


void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    GPSSerial.write(c);
  }
  if (GPSSerial.available()) {
    char c = GPSSerial.read();
    Serial.write(c);
  }
}

This code looks like it needs to communicate both ways. How do I set it up to only use the U2RXD pin?

Also what is the point of having two Serial ports open in the code? Hopefully I didn't sound too smart in the first post. I'm a complete novice at this. Thanks

If your using the SD card in one-bit mode, which is normal, then pins 12 and 13 on the SD card are not used, althought they are connected to the SD card, so you can try connecting the GPS to those pins with;


Serial2.begin(115200,SERIAL_8N1,12,13);

Avoid using pins 16 and 17, they are used by the PSRAM.

You can use pin 4, although you probably want to remove the white LED to avoind being blinded.

And if your into soldering, you can attach a wire to pine 33, used for the red LED, and use that.

You cannot really, although if your not using the TX pin you can try giving it a number that does not exist, say 40 ?

You can use one serial port for debug messages, so you can see what your code is doing, and another serial port for the GPS.

The ESP32 has 3 serial ports, and you can assign almost any free GPIO pins to be RX or TX. You mostly have to be aware of which pins are available, and not already in use with some device, before making the choice. Unfortunately that information depends on the board manufacturer and is scattered in many bits and pieces around the web.

Since the code you posted in #3 does not use the SD card, all of those pins are available.

See this useful reference: ESP32 UART Communication Pins Explained with Example

I got out my ESP32-CAM and verified that this simple serial port transfer code works with either UART 1 or 2, using pins 14 as RX and 15 as TX.

I connected an RS232-USB 3.3V serial adapter to pins 14, 15 and GND, talking to a Windows terminal program as one serial I/O source, and used the serial monitor as the other.

//DEV MODULE selected for ESP32-CAM
// works as expected with UART2, RX=14, TX=15
// same for UART1

#include <HardwareSerial.h>
HardwareSerial SerialPort(1); // use UART1
void setup()  
{
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Serial test");
  SerialPort.begin(115200, SERIAL_8N1, 14, 15); 
} 
void loop()  
{ 
  while (Serial.available()) {
    SerialPort.write(Serial.read());
  }
  while (SerialPort.available()) {
    Serial.write(SerialPort.read());
  }
}

Thanks for all your help!

I was able to get the GPS output with this circuit:

and this code:

#include <HardwareSerial.h>

HardwareSerial GPSSerial(2);

void setup() {
  Serial.begin(115200);
  GPSSerial.begin(9600, SERIAL_8N1, 12, 13);
}

void loop() {
  if (GPSSerial.available()) {
    char c = GPSSerial.read();
    Serial.write(c);
  }
}

these examples helped a lot:

These pins worked out:

Do you think they will interfere with SD card data logging?

Yes, of course they will. So will using pins 14 and 15.

Oh, I think my professor got the esp32cam just for the SD card. I don't think I need the camera though. Is there any way I could preserve the SD card functionality along with the GPS?

Not necessarily... as you can see below there are 4 data lines that connect to the SD card (HS2_DATA0-3).
image

You can however run the SD card in 1-bit mode. If you do this only GPIO2 is used, and you are free to use the other pins. Accessing the card will be slightly slower... but I doubt you'll notice.

To do this, configure the card as follows...

  Serial.println("Initialising SD card");
 
  sdmmc_host_t host = SDMMC_HOST_DEFAULT();
 
  sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

  slot_config.width = 1;  // 1 bit mode.  Allows us to use GPIO12 & 13.
  
  esp_vfs_fat_sdmmc_mount_config_t mount_config = 
  {
    .format_if_mount_failed = false,
    .max_files = 5,
    .allocation_unit_size = 16 * 1024
  };
  
  sdmmc_card_t *card;

  esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

Note you can also use GPIO4... but this is connected to the onboard Flash LED... so it will flash when using.

Poor choice! There are many other ESP32 boards with SD card slots, and more pinouts. This one, for example, is one of the most flexible.

@red_car has a good suggestion. If you follow that, you can use pins 12 and 13 for the GPS, either UART1 or UART2.

Wow, ok. Will I need to include a library for that?

I haven't gotten to that point yet because I've been getting the MD5 error:

"A fatal error occurred: MD5 of file does not match data in flash!
Failed uploading: uploading error: exit status 2"

This happened after uploading a sketch. I've restarted windows, used a different port and everything. Do I need to download python to reset the flash on the ESP32CAM? What's the easiest way? (sorry to go off topic)

Thanks

Get SD logging working separately, starting with one of the many examples. Then work on setting 1 bit mode. Finally, add the GPS.

Sorry, no idea about the MD5 error.

One YouTube tutorial later and it's all good.

I'll look into the SD card business later because my professor just asked me to get the GPS data.

I'd just like to thank all of you that responded to this post. I was struggling and this was my first post. All of you were very helpful. Thank you

That SD card code above requires...

#include "esp_vfs_fat.h"                     // SD card library
#include "driver/sdmmc_host.h"               // SD card library

When you get to it shout out if you get stuck.

Please read again what I said in post #4;

"If your using the SD card in one-bit mode, which is normal, then pins 12 and 13 on the SD card are not used, althought they are connected to the SD card."

Pin 12,13 are DAT2, DAT3 on the SD card and not used in 1 bit mode, so whilst they are connected to the SD card they are not used...............

SD_MMC.begin("/sdcard", true); //use this line for 1 bit mode, pin 2 only, 4,12,13 not used

1 Like

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