Page 1 of 1

ADC causes Serial2 stops

Posted: Sat Jul 18, 2020 11:03 am
by slavendam
Hi,
I connected GPS module on Serial2 (pins 16 and 17) and it worked great on its own.
Then I added in loop analogRead on pin 34 every 10 seconds using millis() delay.

Problem is that GPS works first 10 seconds until first analogRead is done. After that GPS module stop sending data through Serial2. This means that "Serial2.available()" is "false".
When I left it turned on and blocked like that, sometimes it happens after some time (few minutes sometimes) GPS starts sending until next analogRead happens and it stops again.

When I comment analogRead line everything works great.

Does anyone had this issue?

Thanks :)

Re: ADC causes Serial2 stops

Posted: Sat Jul 18, 2020 6:04 pm
by mikemoy
Would need to see your code. You could be doing things in a non RTOS friendly way.

Re: ADC causes Serial2 stops

Posted: Sat Jul 18, 2020 6:16 pm
by slavendam
mikemoy wrote:
Sat Jul 18, 2020 6:04 pm
Would need to see your code. You could be doing things in a non RTOS friendly way.
Here it is :)
When I change line "batt = analogRead(34);" with "batt = 0;" (no analogRead) everything works fine.

Code: Select all

#include "esp32-hal-adc.h"
#include <NMEAGPS.h>


int batt;
int last_batt = 0;
float lat=0.0, lng = 0.0;
int alt=0;
#define SERIAL_PORT_HARDWARE_OPEN Serial2
#include <GPSport.h>


static NMEAGPS  gps; // This parses the GPS characters


static void doSomeWork( const gps_fix & fix );
static void doSomeWork( const gps_fix & fix )
{

  if (fix.valid.location) {
    lat = fix.latitude();
    lng = fix.longitude();
    alt = fix.altitude();
    Serial.println(lat,6);
    Serial.println(lng,6);

  } 
} // doSomeWork

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
  
  pinMode(15, OUTPUT);
  digitalWrite(15,HIGH);

  //pinMode(34, INPUT); //vlažnost zemlje
  adcAttachPin(34);
  analogReadResolution(12);
  analogSetAttenuation(ADC_11db);
  delay(100);
  gpsPort.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  if(gps.available( gpsPort )){
    doSomeWork( gps.read() );
    Serial.println("GPS");
  }

  if(millis() - last_batt > 10000){
      digitalWrite(15,HIGH);
      delay(150);
  
      batt = analogRead(34);
      Serial.print("Batt: ");
      
      float batt_volt = map(batt, 720,1120,3000,4200)/1000.0;
      Serial.print(batt_volt);
      Serial.print("V  ");
      Serial.println(batt);
      last_batt = millis();
      digitalWrite(15,LOW);
    }

    
}