Hi,
I am working on a personal project to obtain current and voltage readings form 4 batteries. Using Arduino IDE , I have developed a program to read current from two current sensors and voltage from three voltage sensors. The GPIO pins used are 32, 33, 34, 35, and 36. I have no issues reading from pins 32 and 33, but I am having trouble getting readings on pin 34. According to the ESP32 specifications, pins 34, 35, 36, and 39 require an external pull-up resistor. I have tested by applying 3.3V to pins 34, 35, and 36. What I don't understand is why only pin 34 is not getting a reading, while pins 35 and 36 show ADC readings of 4096 even without an external pull-up resistor.
For example, I am using the ACS712 current sensor to obtain current readings. Since the sensor output is in the range of 0-5V, a voltage divider (10k ohm & 20k ohm) is needed to convert the range from 0-5V to 0-3.3V. The output from the voltage divider is connected to pin D34. Is this connection correct?
Regards
The following is the code.
#include <WiFi.h> //for esp-32
#define SERVER_PORT 8000
const int V_Panel = 32;
const int V_batt = 33;
const int curr_panel = 34;
const int curr_load = 35;
const int Vcc = 36;
const int led = 4;
const char* ssid = "xxxx xxx";
const char* password = "xxxxxxxx";
WiFiServer server(SERVER_PORT);
unsigned int total;
float numReadings = 50;
float current_IN = 0.00;
float current_LOAD = 0.00;
float ADCsupplyvoltage = 0.00;
float ADCVsolarpanel = 0.00;
float ADCVbattery = 0.00;
float ADCsolarcurrent = 0.00;
float ADCloadcurrent = 0.00;
float V_solarpanel = 0.00;
float V_battery = 0.00;
float V_supply = 0.00;
float V_cc = 0.00;
float V_cc1 = 0.00;
float Vout1 = 0.00;
float Vout2 = 0.00;
float Vout3 = 0.00;
float Vout4 = 0.00;
float Vout5 = 0.00;
float Vout6 = 0.00;
float Vout7 = 0.00;
float Vout8 = 0.00;
float R1 = 10000.0;
float R2 = 20000.0;
float R5 = 1000.0;
float R6 = 2000.0;
float offset1 = 0.00;
float offset2 = 0.00;
float offset3 = 0.00;
float offset4 = 0.00;
int i, x, Vpanel, Vbatt, ipanel, iload, solarcurrent;
void setup()
{
Serial.begin(9600);
Serial.println("");
Serial.println("");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ Serial.print("->");
delay(200);
}
Serial.println("");
Serial.println("WiFi Successfully Connected");
Serial.print("NodeMCU IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("NodeMCU as a Server Role Started");
for(i=0;i<25;i++)
{ digitalWrite(led,HIGH);
delay(10);
digitalWrite(led,LOW);
delay(10);
}
}
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
uint8_t data =client.read();
switch(data) {
case 'a': {
total = 0; // reset counter
//read and averaging 50 values from A0
for (int x = 0; x < numReadings; x++) { // read sensor 50 times
total = total + (analogRead(V_Panel) + offset1); // if use with voltage divider
}
int ADCVsolarpanel = total / numReadings; // get Vsolarpanel average value
Vout1 = ADCVsolarpanel*3.3/4096;
Vout2 = Vout1*(R1+R2)/R2;
V_solarpanel= Vout2;
Serial.print("ADCPIN32:");
Serial.println(analogRead(V_Panel));
client.println(V_solarpanel);
delay(100);
break;
}
case 'b': {
total = 0; // reset counter
//read and averaging 50 values from A0
for (int x = 0; x < numReadings; x++) { // read sensor 50 times
total = total + (analogRead(V_batt) + offset2); // if use with voltage divider
}
int ADCVbattery = total / numReadings; // get Vbattery average value
Vout3 = ADCVbattery*3.3/4096;
Vout4 = Vout3*(R1+R2)/R2;
V_battery= Vout4;
Serial.print("ADCPIN33:");
Serial.println(analogRead(V_batt));
client.println(V_battery);
delay(100);
break;
}
case 'c': {
total = 0; // reset counter
//read and averaging 50 values from A0
for (int x = 0; x < numReadings; x++) { // read sensor 50 times
total = total + (analogRead(curr_panel) + offset3); // if use with voltage divider
}
int ADCsolarcurrent = total / numReadings; // get solar current average value
ADCsupplyvoltage = analogRead(Vcc); //get Vcc value from voltage divider
V_cc = ADCsupplyvoltage*3.3/ 4096;
V_cc1 = V_cc*(R1+R2)/R2;
//ACS712 at 0A @ 2.5V, the ESP ADC value is 2,072 at 1.67 V
Vout5 = ADCsolarcurrent*3.3/4096;
Vout6 = Vout5*((R5+R6)/R6);
current_IN = (Vout6-(V_cc1/2))/0.185; //Vcc/2 is the Vcc supply of the ACS712 NOT Vcc for the ESP32
//Serial.print("Vcc:");
//Serial.println(analogRead(curr_panel));
Serial.print("ADCPIN34:");
Serial.println(analogRead(ADCsolarcurrent));
client.println(current_IN);
delay(100);
break;
}
case 'd': {
total = 0; // reset counter
//read and averaging 50 values from A0
for (int x = 0; x < numReadings; x++) { // read sensor 50 times
total = total + (analogRead(curr_load) + offset4); // if use with voltage divider
}
int ADCloadcurrent = total / numReadings; // get solar current average value
ADCsupplyvoltage = analogRead(Vcc); //get Vcc value from voltage divider
V_cc = ADCsupplyvoltage*3.3/ 4096;
V_cc1 = V_cc*(R1+R2)/R2;
Vout7 = ADCloadcurrent*3.3/4096;
Vout8 = Vout7*((R5+R6)/R6);
current_LOAD = (Vout8-(V_cc1/2))/0.185;
Serial.print("ADCPIN35:");
Serial.println(analogRead(curr_load));
Serial.print("ADCPIN36:");
Serial.println(analogRead(Vcc));
client.println(current_LOAD);
delay(100);
break;
}
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}