How do I use the Arduino analog pins as outputs?

Discussion in 'UDOO NEO' started by Crazy Guy, Jun 9, 2016.

  1. Crazy Guy

    Crazy Guy New Member

    Joined:
    Jun 9, 2016
    Messages:
    3
    Likes Received:
    0
    Is it possible to use the Analog pins on the Neo as outputs?

    For example I've tried addressing A2 as:
    • A2
    • 14+2
    • 54+2
    ie:

    pinMode( A2, OUTPUT );
    digitalWrite( A2, HIGH );​

    and

    int ledPin = 14+2; // Arduino Uno addressing (http://forum.arduino.cc/index.php?topic=28720.0)
    //int ledPin = 54+2; // Arduino Mega adressing
    pinMode( ledPin, OUTPUT );
    digitalWrite( ledPin, HIGH );​

    But no luck :( -- I've been using the Arduino Web IDE.
     
    Last edited: Jun 9, 2016
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Not of any help for you but I can confirm that it is not working as on an Arduino UNO.
    You can't use the A0-A5 Pins as a Digital output. On an Arduino UNO it is possible:
    http://www.instructables.com/id/How-to-add-6-extra-pins-to-your-Arduino-with-no-ex/

    @Andrea Rovai, any ideas? It is not widely documented on the Arduino site but on https://www.arduino.cc/en/Reference/PinMode it is stated in the Note that
    I tried it with the blink example with pin declaration 14 and A0 (should be the same pin) but with both declarations no blinking LED.
    Example with Pin 14
    Code:
    void setup() {
      pinMode(14, OUTPUT);
    }
    
    void loop() {
      digitalWrite(14, HIGH);
      delay(300);
      digitalWrite(14, LOW);
      delay(300);
    }
     
  3. Laura

    Laura UDOOer

    Joined:
    Apr 22, 2016
    Messages:
    374
    Likes Received:
    156
    Might be an issue with the wiring_digital.c file of the Arduino board manager package. Though, I don't have enough experience to know for sure.
    Paging @ektor5
     
  4. jas-mx

    jas-mx Active Member

    Joined:
    Dec 31, 2013
    Messages:
    407
    Likes Received:
    118
    Each Analog pin/Digital pin is multiplexed by a gpio pin as declared in the dts:

    Code:
                    // Multiplexer pins for GPIO/ADC (J5)
                    MX6SX_PAD_RGMII2_TXC__GPIO5_IO_23     0x80000000 // MUX_A
                    MX6SX_PAD_RGMII2_RX_CTL__GPIO5_IO_16  0x80000000 // MUX_B
                    MX6SX_PAD_QSPI1A_SCLK__GPIO4_IO_21    0x80000000 // MUX_C
                    MX6SX_PAD_QSPI1A_SS0_B__GPIO4_IO_22   0x80000000 // MUX_D
                    MX6SX_PAD_QSPI1A_DATA2__GPIO4_IO_18   0x80000000 // MUX_E
                    MX6SX_PAD_QSPI1A_DATA3__GPIO4_IO_19   0x80000000 // MUX_F
    
    In uboot the above pins are set low to enable ADC input. As a test trying setting the pins to high from the linux side and then running your script (this may not work). What I'm not sure about is which gpio pins (the adc pins) map to on the the arduino side.
     
    Last edited: Jun 10, 2016
  5. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    As Laura already suggested digitalwrite only accepts pin 0 to 13.
     
  6. jas-mx

    jas-mx Active Member

    Joined:
    Dec 31, 2013
    Messages:
    407
    Likes Received:
    118
    Then a few more gpios like LWGPIO_MUX_PORT4_PIN24_GPIO need to be made available along with the mux pins.
     
  7. Laura

    Laura UDOOer

    Joined:
    Apr 22, 2016
    Messages:
    374
    Likes Received:
    156
  8. Crazy Guy

    Crazy Guy New Member

    Joined:
    Jun 9, 2016
    Messages:
    3
    Likes Received:
    0
  9. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    You're right @Crazy Guy, that part is missing, follow the documentation about it.
     
  10. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Someone can give the Udoo team some information in what direction they have search how to solve this issue? It seems that they are struggling with this.
     
  11. Laura

    Laura UDOOer

    Joined:
    Apr 22, 2016
    Messages:
    374
    Likes Received:
    156
    I feel it's related to the board package not including the analog pins in the default wiring digital file, which is where the digitalWrite function is sourced from. Unfortunately, I don't have a NEO to test on. If anyone wishes to try it, copy the extra pins & const array from: http://www.udoo.org/docs-neo/Arduino_M4_Processor/Use_more_digital_pins_on_M4.html
    over to the wiring_digital.c file in your board package used by Arduino IDE
     
    waltervl likes this.
  12. Crazy Guy

    Crazy Guy New Member

    Joined:
    Jun 9, 2016
    Messages:
    3
    Likes Received:
    0
    For now I will do a hardware mod to work around this problem on my project.

    @Andrea Rovai -- has Udoo added this to their to do / issues list so that this just works "out of the box" in the future?
     
  13. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
  14. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    I looked into this but i do not think it will work.
    On Arduino Uno Pin A0-A5 can also be addressed as Pin 14-19
    But in the extra pins & const array Pin 14-19 are mapped to A9 pins and the SDA/SCL pins, see also picture below.

    [​IMG]
     
  15. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    I'll give it a try.

    Edit: How to do this from Linux? I cannot do echo 1 > /gpio/pinA5/value
    Pin 16 is a pin on the A9 side. So how to adress Arduino Pin A0-A5 from Linux?

    Edit 2: hmm, going to try this: http://www.udoo.org/forum/threads/exporting-arduino-analog-pins-possible.3215/#post-15725

    Edit 3: Edit deleted.....
    Edit 4: Edit deleted.....
    Edit 5: Need to calculate again, stupid! will use A0 as in the example of jas-mx
    Edit 6: OK, got a High A0 pin by setting it by Linux, now try to get it blink by Arduino:

    # Set multiplexer to GPIO support
    echo 118 > /sys/class/gpio/export
    echo out > /sys/class/gpio/gpio118/direction
    echo 1 > /sys/class/gpio/gpio118/value

    # Set GPIO line to output
    echo 120 > /sys/class/gpio/export
    echo out > /sys/class/gpio/gpio120/direction
    echo 1 > /sys/class/gpio/gpio120/value

    Edit 7: Changing the Arduino board definition:
    changed /usr/share/arduino/hardware/UDOO/solox/variants/udooneo/wiring_digital.h
    #define ARD_NMAX_DIO 15

    changed /usr/share/arduino/hardware/UDOO/solox/variants/udooneo/wiring_digital.c
    added
    #define ARD_DIO14 (LWGPIO_PORT4 | LWGPIO_PIN24)
    #define ARD_DIO14_MUX_GPIO (LWGPIO_MUX_PORT4_PIN24_GPIO)
    and
    {ARD_DIO14, ARD_DIO14_MUX_GPIO}, // 14 = A0
    Edit 7:
    YES, I got a blinking Led with digitalWrite(14, HIGH/LOW) on pin A0!!!
    But I suppose I cannot use the AnalogRead on A0 anymore because I set multiplexer to GPIO in Step 6. Also when I reboot the sketch will fail so I will now reset it to pin 13 again.
    Edit 8:
    Tested AnalogRead(A0) but no real values as expected, just giving a approx 684 whatever voltage I put on it.
    AnalogRead(A1) gives real values between 0-1023 (0-3.3V)
     
    Last edited: Jul 14, 2016
  16. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Given my tests it is possible to address A0-A5 as a digital pin but only with some major changes from the Linux side and in wiring_digital.c/h. And after a reboot you have to do the multiplexer part again.
    Is it possible to set the multiplexer of an analog pin to GPIO within wiring_digital.c when the pinmode of an Analog pin is set to output?
     
  17. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Anyone? @Andrea Rovai ?
     

Share This Page