DFRobot / GravityTDS

DFRobot Gravity: Analog TDS Sensor / Meter For Arduino SKU: SEN0244
GNU Lesser General Public License v2.1
39 stars 56 forks source link

ESP32 Analog Read of TDS Sensor Returns Mostly Zeros #2

Open jpagliaccio opened 5 years ago

jpagliaccio commented 5 years ago

Running this code on an ESP32 returns mostly zeros (eg: 28 zeros and a few stray values). Running the same code on and Arduino UNO returns good values. Any ideas how to get this working on the ESP32? I am using the Adafruit ESP32 Feather and powering the TDS sensor at 3.3v for both the ESP32 and UNO.

int aa = 0;

for (int n = 0; n < 30; n++) { aa = analogRead(A3); Serial.print("TDS voltage: "); Serial.println(aa); delay(40); }

I have tried adjusting the ADC on the ESP32 with:

include <driver/adc.h>

// and in setup ... adc1_config_width(ADC_WIDTH_BIT_10); adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);

Thanks in advance.

Leumas23 commented 5 years ago

Did u resolve the problem already?

zckyachmd commented 2 years ago

I had the same problem on the ESP8266 board. Is there any solution?

jpagliaccio commented 2 years ago

For the ESP32, I switched to an external I2C ADC, but the code below works. Note the "Mux" comment and remember: ADC1 works foraAnalog, but ADC2 is used by the WiFi.

/*
   ESP32_A3_Analog_Read_Example

----------------------------------------------------------------------------------------
  u //
  t //                       ---------------------
  s //                       | O |   |   |   | O |
  r //         I2C SDA D23 - |*  |-----------|  *| - D21 IO  INT_IO
  q //         I2C SCL D22 - |*  |           |  *| - D17 TX  ESP_TX
  p // (BIN2) ADC2  A6 D14 - |*  |           |  *| - D16 RX  ESP_RX
  o // (BIN1) ADC1  A7 D32 - |*  |           |  *| - D19 MI  SPI
  n // (AIN2) ADC2  A8 D15 - |*  |           |  *| - D18 MO  SPI
  m // (AIN1) ADC1  A9 D33 - |*  |           |  *| - D5  SCK SPI
  l // (MODE) ADC2 A10 D27 - |*  |           |  *| - D4  A5  ADC2
  k //        ADC2 A11 D12 - |*  -------------  *| - D36 A4  ADC1
  j // INTLED ADC2 A12 D13 - |*                 *| - D39 A3  ADC1
  i //            5VDC USB - |*                 *| - D34 A2  ADC1
  h //                  EN - |*                 *| - D25 A1  ADC2
  g //                 BAT - |*                 *| - D26 A0  ADC2
  f //                     - |*              XXX | - GND
  e //                     - |*              XXX | - NC
  d //                     - |*     -------  XXX | - VDD OUT +3.3V
  c //                     - |*     |     |      | - RST
  b //                     - | O    | USB |    O |
  a //                       ---------------------

  a //                       ---------------------
  b //                       | O    | USB |    O |
  c //                 RST - |*     |     |      |
  d //    +3.3V DC OUT VDD - |*     -------  XXX |
  e //                  NC - |*              XXX |
  f //                 GND - |*              XXX |
  g //         ADC2 A0 D26 - |*                 *| - BAT
  h //         ADC2 A1 D25 - |*                 *| - EN
  i //         ADC1 A2 D34 - |*                 *| - USB 5VDC
  j //         ADC1 A3 D39 - |*                 *| - D13 A12 ADC2 Internal LED SW3
  k //         ADC1 A4 D36 - |*  -------------  *| - D12 A11 ADC2
  l //         ADC2 A5  D4 - |*  |           |  *| - D27 A10 ADC2
  m //          SPI SCK D5 - |*  |           |  *| - D33 A9  ADC1 AIN1
  n //          SPI MO D18 - |*  |           |  *| - D15 A8  ADC2 AIN2
  o //          SPI MI D19 - |*  |           |  *| - D32 A7  ADC1 BIN1
  p //       ESP_RX RX D16 - |*  |           |  *| - D14 A6  ADC2 BIN2
  q //       ESP_TX TX D17 - |*  |           |  *| - D22 SCL I2C
  r //          INT_IO D21 - |*  |-----------|  *| - D23 SDA I2C
  s //                       | O |   |   |   | O |
  t //                       ---------------------
  u //
  v  https://learn.adafruit.com/adafruit-huzzah32-esp32-feather/pinouts
  // Remember: ADC1 works for Analog - ADC2 is used by the WiFi
  //

  // Using the Adafruit Precision LM4040 Voltage Reference
  //
  // JP 2022
  //

*/

#include <driver/adc.h>

#define EXAMPLE_ADC_CHANNEL ADC1_CHANNEL_0

const long sensorReadInterval = 120; // milliseconds
unsigned long previousSensorReadMillis = 0;

// -------------------------------------------------------------------------------
// F L O A T  M A P
// -------------------------------------------------------------------------------
float mapf(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;
}

// -------------------------------------------------------------------------------
// S E T U P
// -------------------------------------------------------------------------------
void setup() {

  Serial.begin(115200);
  Serial.println("\n\n\ ESP32_A3_Analog_Read_Example");

  adc1_config_width(ADC_WIDTH_BIT_12);
  adc1_config_channel_atten(EXAMPLE_ADC_CHANNEL, ADC_ATTEN_DB_11); // DB_11 is Full 0-3.9 VDC
}

// -------------------------------------------------------------------------------
// L O O P
// -------------------------------------------------------------------------------
void loop() {

  //--------------------------------------------------------------
  if (millis() - previousSensorReadMillis >= sensorReadInterval) {
    previousSensorReadMillis = millis();

    // The next line 2 lines stablize the mux and voltage.
    int a3 = analogRead(A3);
    delayMicroseconds(10);
    a3 = analogRead(A3);

    float v3 = mapf(a3,  0.0, 4095.0, 0.0, 3.9);

    Serial.printf(" Counts A3 %d Volts: V3 %f \n", a3, v3);
  }
}