Sony's Developer World forum

    • Home
    • Forum guidelines

    Reading analog signal

    Spresense
    2
    3
    223
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      tuan-nguyen last edited by

      Hello everyone,

      I am trying to read the analog signal from a joystick using the Spresense main board connected to the extension board.
      I had the joystick's VCC, GND, and VRx connected to extension board's VCC, GND, and A0. I am following the code bellow, which is from this link: https://github.com/sonydevworld/spresense-sketches/blob/master/analog_read/analog_read.ino

      static const uint8_t pin_l = PIN_A0;
      static const uint8_t pin_h = PIN_A4;
      
      void setup() {
        // put your setup code here, to run once:
        pinMode(pin_l, INPUT);
        pinMode(pin_h, INPUT);
        pinMode(PIN_PWM_0, OUTPUT);
        analogWrite(PIN_PWM_0, 200);
      }
      
      void loop() {
        // put your main code here, to run repeatedly:
        delay(1000);
        int ret_l = analogRead(pin_l);
        int ret_h = analogRead(pin_h);
        printf("low = %d, high = %d\n", ret_l, ret_h);
      }
      

      But after I upload the program and ran the Serial Monitor, it said:
      "ERROR: Invalid pin number [128]
      spresense dose not support using analog pin as digital."
      "ERROR: Invalid pin number [132]
      spresense dose not support using analog pin as digital."

      Can you give me any advice on this problem?
      Thank you for your help.

      Best regards,
      Tuan Nguyen

      K 1 Reply Last reply Reply Quote
      • K
        KamilTomaszewski DeveloperWorld @tuan-nguyen last edited by

        Hi @tuan-nguyen-0

        You can remove the use of pinMode on analog pins.

        The code will look like this:

        static const uint8_t pin_l = PIN_A0;
        static const uint8_t pin_h = PIN_A4;
        
        void setup() {
          // put your setup code here, to run once:
          pinMode(PIN_PWM_0, OUTPUT);
          analogWrite(PIN_PWM_0, 200);
        }
        
        void loop() {
          // put your main code here, to run repeatedly:
          delay(1000);
          int ret_l = analogRead(pin_l);
          int ret_h = analogRead(pin_h);
          printf("low = %d, high = %d\n", ret_l, ret_h);
        }
        

        Best Regards,
        Kamil Tomaszewski

        1 Reply Last reply Reply Quote
        • T
          tuan-nguyen last edited by

          Hi @KamilTomaszewski ,

          I have done what you said and it worked.
          Thank you for your help.

          Best Regards,
          Tuan Nguyen

          1 Reply Last reply Reply Quote
          • First post
            Last post
          Developer World
          Copyright © 2021 Sony Group Corporation. All rights reserved.
          • Contact us
          • Legal