Arduino Yun not able to read values from Analog pins


#1

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?


#2

Welcome to Cayenne!

Can you post your code for us to look over?


#3
  /*
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));
}

#4

Did you happen to go through the steps on the dashboard to add the generic “digital” (but select the correct analog pin) input?


#5

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


#6

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.


#7

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.


#8

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:


#9

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


#10

@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.


#11

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


#12

It worked by using analog pins. Thanks,