adc_get_raw returns -1

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

adc_get_raw returns -1

Postby Hasan Shadi » Mon Apr 06, 2020 2:46 pm

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:

Who is online

Users browsing this forum: Bing [Bot] and 12 guests