Hi everybody…. I´m using a device to see wind direction using analogic pins. When I was testing my sketch I read the port value and made a program to show in the serial monitor the value read, position in grade and a text to say ( north, soulth, etc ). Every think was perfect.
After this I copied the sketch to my “master” program and the value read in the same port is another, than, printed information in serial monitor appear wrong.
Everyone does it know why ?
Hi,
Which GPIO are you using to read the sensor? What were the values read before and after?
Thanks!
Sure,
The product is similar this: https://www.usinainfo.com.br/estacao-meteorologica-arduino/indicador-de-direcao-do-vento-arduino-para-estacao-meteorologica-dv10-4638.html
I´m using PIN 13 (GPIO 13 ) in the ESP32. I started testing with arduino uno, and I had the perfect informations in serial monitor. When I used ESP32 I understood that ESP has a diferrent values when analogic read are made, than I decided just read the value in the port and show these values in serial monitor with coordenate ( north, soulth, east, etc ).
Look the sketch:
int pin=13;
float valor =0;
int Winddir =0;
void setup() {
Serial.begin(115200);
pinMode ( pin, INPUT);
}
void loop() {
valor = analogRead(pin);
Serial.print(“leitura do sensor :”);
Serial.print(valor);
if (valor >= 135 && valor <= 150) {
Winddir = 315;
}
else if (valor >= 160 && valor <= 185) {
Winddir = 270;
}
else if (valor >= 200 && valor <= 220) {
Winddir = 225;
}
else if (valor >= 255 && valor <= 265) {
Winddir = 180;
}
else if (valor >= 330 && valor <= 340) {
Winddir = 135;
}
else if (valor >= 445 && valor <= 460) {
Winddir = 90;
}
else if (valor >= 660 && valor <= 680) {
Winddir = 45;
}
else {
Winddir = 0;
}
Serial.print(” Direcao a : “);
Serial.print(Winddir);
Serial.println(” graus “);
delay (5000);
}
I made many tests and the information was correct showing corret angle…… When I put these sketch inside the main sketch with informations about temperature and humidity, if there is rain ou not, if it´s day or night, the informations about wind directions was crazy, for exemple, when direction is soulth, serial monitor showing north, than the range values about south was wrong, or else, the value change.
The ESP32 analog pins values range from 0 to 4095 (it has a higher resolution)… I recommend taking a look at this Unit again and use that example first: https://rntlab.com/esp32-reading-analog-inputs/
That’s why those values for the if statements make sense for an Arduino, but with an ESP32… you need to use different values…
Note: you also need to delete that line from your code:
pinMode(pin, INPUT);
The pinMode function is only used for reading digital pins, if it’s an analog you don’t need to use pinMode.
I read in the UNIT that information about 0-4095 because in ESP32 there are more bits in the data than arduino.
will remove the command pinMode and try to made another ajustments. I understood that the informations in Analogic Pin should be the same independly anothers sensors in ESP32.