ESP32 AnalogRead()

lduprat
Posts: 2
Joined: Mon Sep 10, 2018 7:30 am

ESP32 AnalogRead()

Postby lduprat » Mon Sep 10, 2018 8:01 am

I am trying to write on a the pin 25, and I need to control the voltage with the pin 36. To do so, I am using the following code :

Code: Select all

int consignePin = 25;
int controlePin = 36;
int ledPin = 5;
int freq = 5000;
int ledChannel = 0;
int resolution = 8;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin,OUTPUT);
  pinMode(consignePin,OUTPUT);
  pinMode(controlePin,INPUT);
  Serial.begin(9600);
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(consignePin, ledChannel);

}

void loop() {
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
    ledcWrite(ledChannel, dutyCycle);
    analogReadResolution(10);
    Serial.println(int(analogRead(controlePin)));
    delay(100);
  }
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
    ledcWrite(ledChannel, dutyCycle);
    analogReadResolution(10);
    Serial.println(int(analogRead(controlePin)));
    delay(100);
  }
}

Here is my problem : on the serial monitor, I get complete incoherent values, like 0 when dutyCycle = 0 and 917 for all other values.
I also tried analogReadResolution 8,11 and 12 but I got the same kind of problem.
I tried to read with that too :
//adc1_config_width(ADC_WIDTH_BIT_12);
//adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_0);
//int val = adc1_get_raw(ADC1_CHANNEL_0);
but it didn't work either !

I mesured the voltage with a multimeter, and it works perfectly, so it's not a writting problem.
Do you see a solution to my problem ?

Archibald
Posts: 107
Joined: Mon Mar 05, 2018 12:44 am

Re: ESP32 AnalogRead()

Postby Archibald » Tue Sep 11, 2018 3:02 am

What do you have connected to pin 36?

lduprat
Posts: 2
Joined: Mon Sep 10, 2018 7:30 am

Re: ESP32 AnalogRead()

Postby lduprat » Tue Sep 11, 2018 7:23 am

The pin 36 is directly connected to the pin 25

Archibald
Posts: 107
Joined: Mon Mar 05, 2018 12:44 am

Re: ESP32 AnalogRead()

Postby Archibald » Tue Sep 11, 2018 8:36 am

Pin 25 with LEDC is a digital output with PWM (pulse width modulation):
https://docs.espressif.com/projects/esp ... /ledc.html

For an analogue output, use DAC (digital-to-analogue converter):
https://docs.espressif.com/projects/esp ... s/dac.html

Alternatively put a resistor-capacitor network between pins 25 and 36 so you get close to the average voltage of the digital waveform on pin 25. Be aware that if you change the duty cycle of the PWM waveform, it would take some time for the analogue voltage from the resistor-capacitor network to change.

What are you trying to achieve?

Who is online

Users browsing this forum: Baidu [Spider] and 32 guests