Arduino Yun not able to read values from Analog pins

Hi,

I uploaded the plain vanilla sketch using the Cayenne wizard, to my Yun. I am unable to read data from my analog pins, which I am able to read using a regular sketch and through the command analogRead(3). Any pointers?

Welcome to Cayenne!

Can you post your code for us to look over?

  /*
Cayenne Yun Example

This sketch connects to the Cayenne server using an Arduino Yun
and runs the main communication loop.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneYun.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxxxxxxxxxxxxxxxxxx";

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
}

void loop()
{
  Cayenne.run();
  Serial.println(digitalRead(12));
}

Did you happen to go through the steps on the dashboard to add the generic ā€œdigitalā€ (but select the correct analog pin) input?

I am pretty sure I did all the stepsā€¦ do you have access to my dashboard? can you double check?

Actually for security no one has access to your dashboard but yourself. Iā€™m actually heading to sleep soon but Iā€™ll see if I can set up something similar on my Uno tomorrow.

Ok, so I added my Uno to my dashboard and added a generic digital widget mapped to A0. I hooked up my adjustable power supply to A0 and connected the grounds and I can confirm it is not working. It will read 1023 for anything from 0-5v.

Hmm, so I just disconnected the power from A0 and now it reads 373 nowā€¦ If I hook it back up it only reads ~373 now??? This is odd.

Scratch thatā€¦it does read 373 when A0 is disconnected, but I put it back in to A1, not A0 :no_mouth: Itā€™s reading 1023 again, even without a common ground connected :thinking:

I am not sure what to think of all this. Iā€™m having much more success. At first I tried the code @roccodegrazia posted here using the digital pin specified in the code (12) and had no trouble.

I then loaded analogRead code with a photoresistor plugged into AƘ and the response was as expected; high number in light, smaller number in shade, zero with +5vdc removed from circuit.

Hereā€™s the code-

      /*
    Cayenne Yun Example

    This sketch connects to the Cayenne server using an Arduino Yun
    and runs the main communication loop.

    The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

    Steps:
    1. Set the token variable to match the Arduino token from the Dashboard.
    2. Compile and upload this sketch.

    For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
    send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
    should be sent to those pins using virtualWrites. Examples for sending and receiving
    Virtual Pin data are under the Basics folder.
    */

    #define CAYENNE_DEBUG         // Uncomment to show debug messages
    #define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
    //#include <CayenneYun.h>
    #include <CayenneEthernet.h>


    // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
    char token[] = "***********";

    void setup()
    {
      Serial.begin(9600);
      Cayenne.begin(token);
    }

    void loop()
    {
      Cayenne.run();
      Serial.println(analogRead(0));
    }

Hereā€™s the dashboard, serial monitor capture, and project.



Have I misunderstood the problem?

Ian

@roccodegrazia @ats1080s @Ian If trying to read analog input from an Arduino, you should use the ā€˜Generic Analog Inputā€™ widget, or use a custom widget (value, gauge, graph) and then select ā€˜Analogā€™ when adding the custom widget.

Another thing to note is that you can convert the raw analog data to a float value by using the virtual pins in your code. You can see an example with the TMP36 analog temperature sensor, here.

Does that make sense? Iā€™ve actually never tried to read analog data using generic digital input, so Iā€™m not sure if that would work or not. I will look into that because if there are issues, then maybe we should not allow analog data on digital input widget.

I had no problem reading the analog pin using an Arduino Yun. See the image below:

It worked by using analog pins. Thanks,

1 Like