Reading analog pin only showing two values (0 and 1023), no in-between!

I am using an Arduino Mega with Arduino WiFi shield.
I am trying to transmit data from 0-1023 (actually it’s 0-5V but without the equation 5/1023, it would be the previous values).
However, the values that I receive are either 0 when connected to GND or 1023 when connected to 5 or 3.3V from the board.
This problem is absent when I don’t call the thinger.io (i.e. using only Serial.print)

Can anyone point out to me what could be the issue?

Thank you.

Hi! after searching the internet… did you tried to do the same thing while using the Wifi shield for another kind of task, like wifi Server or similar? So we can know if the problem is while powering and using the Wifi Shield, as the thinger.io client does. Maybe the shield is doing some kind of interference… So just try for example to open a sifi server, or calling an HTTP request while reading analog values in the meanwhile.

Hello.

Thank you for the reply. I tried using the WiFi Server example and it works fine i.e. it reflects the value 680 for 3.3V of input. Also actually, before I started using thinger.io, I was using the local server and was able to get the values perfectly as well. So I can say there is no problem with the WiFi shield.

Please provide further input as to how I should proceed.

Thank you again.

This is really strange! I don’t have an Arduino Mega nor Wifi Shield to test it properly. Can you please post your thinger.io Arduino sketch here so we can try different things?

Thanks! Alvaro.

Certainly.

#include SPI.h
#include WiFi.h
#include ThingerWifi.h

#define USERNAME “username”
#define DEVICE_ID “device_id”
#define DEVICE_CREDENTIAL “device_credential”

#define SSID “ssid”
#define SSID_PASSWORD “password”

double TorqueValue = 0;

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup()
{
Serial.begin(9600);
thing.add_wifi(SSID, SSID_PASSWORD);

thing[“TorqueValue”] >> (pson & out)
{
out = TorqueValue;
};

//thing[“TorqueValue”] >> outputValue(A0);
}

void loop()
{
TorqueValue = analogRead(A0);
Serial.print("sensor = ");
Serial.println(TorqueValue);
thing.handle();
}

The “#include” will have <> for the commands, I removed those as they were not showing up in preview. You can see the sketch is pretty much straightforward.
If you want, I can try a video recording for the program.
Let me know if that will be necessary.

Thank you so much.

It is not necessary a video example by the moment, but we can start by simplifying the setup and loop code:

void setup() {
  thing.add_wifi(SSID, SSID_PASSWORD);
  thing["TorqueValue"] >> analogPin(A0);
}

void loop() {
  thing.handle();
}

Try with this code. What values do you get with this code in the thinger.io console? You can see them by using the device API explorer or by creating a dashboard with a chart associated to the TorqueValue.

As you can see below, the value only juggles between 0 and 1023 regardless of whatever input I provide.

It is interesting to see that there are reads at 0, 255, and 1024. They are not common numbers!

I have prepared a sketch to try to determine the source of problems (Notice the #define _DEBUG_ at the first of the sketch)

#define _DEBUG_
#include <SPI.h>
#include <WiFi.h>
#include <ThingerWifi.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);


void setup(){
  Serial.begin(9600);

  thing.add_wifi(SSID, SSID_PASSWORD);

    // just the normal analog read
    thing["TorqueValue1"] >> analogPin(A0);

    /* define another resource with a small delay before reading
       to check if I/O affects readings in some way.
    */
    thing["TorqueValue2"] >> [](pson& out){
      // make a read just after receiving the query
      int value = analogRead(A0);
      // debug just after request value
      Serial.print("Thing read 1: ");
      Serial.println(value);
      // add a small delay
      delay(100); // change up and down if there is no difference
      // read analog value again
      value = analogRead(A0);
      // debug new value
      Serial.print("Thing read 2: ");
      Serial.println(value);
      out = value; // upload final value
    };
}

void analogTest(){
  Serial.println("Testing Reads");
  unsigned long currentTs = millis();
  while(millis()-currentTs<30000){
    int value = analogRead(A0);
    Serial.println(value);
    delay(1000);
  }
}

int state = 0;

void loop(){
  if(state==0){
    // check reads before any call to handle
    analogTest();
    // just connect to thinger
    thing.handle();
    // test analog reads again
    analogTest();
    state = 1;
  }else{
    // normal call to handler
    thing.handle();
  }
}

As I don’t have a Mega with Wifi Shield, I need that you can:

  1. Adjust some imput over the analog read so it can read some intermediary value like 700. With any sketch you know is working.

  2. Compile and update the sketch in this post, just modifying the sketch with your device credentials.

  3. Wait a minute or so, go to the dashboards, and create two different line chart widgets with a sampling interval of 1 second over resource TorqueValue1, and TorqueValue2.

  4. Keep your dashboard open, and open a serial monitor, and just reset your device to see a complete log from the start.

  5. Report both the serial log, and some screenshots of the line charts.

With this test I would like to determine if there is some influence when using the handle call, when reading analog values just after some I/O, or problems in the transmission. The first minute the sketch will not respond properly to thinger.io dashboards, as it will be testing the analog input.

Hope this code help us to determine the source of problems!

Alright, thank you so much for the reply.

Here are the screenshots from the code (I am uploading one image per post since new users can only upload 1 image in a post). I can see that the read value changes as soon as transmission starts.

Kindly see if you can determine the problem.

Thank you again.

Screenshot 1-

Screenshot 2-

Screenshot 3-

Screenshot 4-

Wow! This is quite strange! Thinger.io client libraries does not other thing than using the Wifi Shield, open a socket, and thats it. It does not use any digital or analog ping. But it keeps the connection open, that is the difference with the stock HTTP Server example.

So, in some way I think that there should be some kind of interference when using the Wifi Shield, and reading from analog ports. Not sure why. How you are using the analog input? What kind of signal are you sending? Can you send us a pic or an schematic?. The best thing we can do is to use an oscilloscope in parallel with the thinger readings, to be sure that the input is what we are expecting. But not sure you have an oscilloscope at hand.

We have been using analog reads with different hardware without problems. Do you have different hardware like a stock arduino to see if there is some difference?

Apologies for the long delay in reply.

I am just using the analog values from either an LM35 IC or directly from the Arduino for now. So the signals are normal voltage signals in the range of 0-5V and it works perfectly if the command for thing.handle() is not given. The same is reflected in the serial monitor as well.
Sorry but I do not have an oscilloscope or stock arduino at hand.
Here is the simple setup I use testing this WiFi connection:

Hello pranshushelat!
did you try to make analog read with another sensor, like an LDR?? or use an R-C circuit on signal line between sensor and analog pin?

I found the same exact error in a similar platform (Blynk):

And it seems to be related with the Internet while the device keeps a connection open (although the problem is not resolved in the forum).

My thought is that the Wifi Shield is creating some kind of interference in the analog pins of the board. Not sure if it is a hardware defect. Did you tried to test in different analog pins? The Arduino mega has 16 different analog inputs, so maybe you can check with some input that is not directly connected to the shield.

@JorgeTrincado Hello…! I tried using LDR, still the same problem remains. It changes the value to 1023 as soon as the connection is established.

@alvarolb Yes, I guess the problem must be with the WiFi shield then because I used the analog pin A10 (which is not on WiFi shield) and problem still persists.

At this point, since there doesn’t seem to be a visible solution, I am open to your suggestions regarding what I should use. Can you recommend me something that you know for a fact works for analog readings? Like another shield module or another board? The reason I am biased towards Arduino Mega 2560 is due to larger number of input pins but I am open to alternatives.

Thanks.

If you need a lot of analog pins with full range ADC, and you dont mind using WiFi instead of ethernet… I think that TI cc3200 launchpad could be perfect!
but why don`t you change the temperature sensor to a digital frontend device like a DS18B20, SI7021 or BMP180???

Actually, I’m working on getting data all sorts of sensors including but not limited to temperature, torque, rpm, acceleration and so on. That is the reason I am biased towards Arduino Mega 2560. I will take a look into CC3200 launchpad in detail, at first glance it looks quite appealing. Also, I would still prefer something which can provide WiFi capability to my existing Mega 2560. So more suggestions would be welcome.

Thank you.