adc1_get_raw returns -1

Hasan Shadi
Posts: 26
Joined: Tue Mar 31, 2020 7:26 pm

adc1_get_raw returns -1

Postby Hasan Shadi » Tue Apr 07, 2020 6:28 am

I am trying to read an analog value from a PS2 Joystick. When I connected it correctly, I got a '-1' as a response. I have read the documentation and it says that '-1' means invalid parameter. Moreover, I am really confused about the attenuation thing. Is it making it to return the '-1'?
I have tried all the available values for the attenuation, nothing worked out. The Joystick takes 5V. However, I have tested it, and it also gives 5V from the analog output pin. About the resolution of the ADC, even if I did not use the appropriate value, at least it will give some value, not '-1'. This is the code:

Code: Select all

#include <esp_event.h>
#include <driver/gpio.h>
#include <driver/adc.h>
#include <stdio.h>

#define JS1_X 34
#define JS1_Y 35
#define JS1_BT 32

#define JS2_X 27
#define JS2_Y 26
#define JS2_BT 25

typedef struct {
    short x : 10;
    short y : 10;
} Point2D;

void app_main() {
    
    if (adc1_config_width(ADC_WIDTH_10Bit) != 0) printf("An error has occured!");
    if (adc1_config_channel_atten(ADC1_GPIO34_CHANNEL, 4) != 0) printf("An error has occured!");
    if (adc1_config_channel_atten(ADC1_GPIO35_CHANNEL, 4) != 0) printf("An error has occured!");
    
    gpio_set_direction(JS1_BT, GPIO_MODE_INPUT);
    gpio_set_pull_mode(JS1_BT, GPIO_PULLDOWN_ENABLE);

    Point2D lastPoint;
    while (true) {
        if (gpio_get_level(JS1_BT) == 1) printf("The button is pressed!\n");

        lastPoint.x = adc1_get_raw(ADC1_GPIO34_CHANNEL);
        lastPoint.y = adc1_get_raw(ADC1_GPIO35_CHANNEL);
        printf("(%d, %d)\n", lastPoint.x, lastPoint.y);

        vTaskDelay(50 / portTICK_PERIOD_MS);
    }

}
And here is the product link:

ESP_Dazz
Posts: 187
Joined: Fri Jun 02, 2017 6:50 am

Re: adc1_get_raw returns -1

Postby ESP_Dazz » Wed Apr 08, 2020 4:31 am

Which version of ESP-IDF are you using?

Hasan Shadi
Posts: 26
Joined: Tue Mar 31, 2020 7:26 pm

Re: adc1_get_raw returns -1

Postby Hasan Shadi » Wed Apr 08, 2020 7:35 am

ESP_Dazz wrote:
Wed Apr 08, 2020 4:31 am
Which version of ESP-IDF are you using?
I think I am using version "Release V4.0" of ESP-IDF which is the latest release (until the time of writing this response). I have opened "README.md" file and I found [latest release], this is how I have determined my ESP-IDF version. I have installed everything using the "esp-idf-tools-setup-2.2.exe" setup file, which is the easiest way. I will try adc2 instead of adc1 and if I found something else, I will response again to this article.

Who is online

Users browsing this forum: No registered users and 29 guests