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

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.

ok, other option coud be this (based on ESP8266):

I recommend you to test with another Wifi Shield if possible (from a colleague or family) to see if there is a problem with your particular shield.

Also I recommend you to not use analog sensors if possible, as they are difficult to calibrate and are much more susceptible to noises. I prefer using digital sensors over I2C as you can connect a bunch of them in the same two digital pins, having accurate readings, without requiring any calibration process, etc. So you can use boards that are cheaper and smaller, like an Arduino MKR1000, NodeMCU, etc.

With a small NodeMCU we are reading through I2C temperature, pressure, lux, humidity, etc. using only two pins of the boards.

1 Like

@JorgeTrincado Thanks. I will be getting the ESP8266 based shield. Hopefully it will work perfectly.

@alvarolb I don’t think I’ll be able to get my hands on another shield, but I will try. As for the sensors, they are already setup and streaming. I am just trying to get that done wirelessly.

1 Like

I have got an ESP8266 based WiFi shield but then while trying to figure out the programming needed, I came across this thread.

Here, @alvarolb you mentioned that library for “Arduino + ESP8266 as Wifi bridge” is in development. Are there any updates on that?

Sorry for bugging you too much. I am also trying using CC3000. I am able to connect with the WiFi but not able to establish a connection with error message

[_SOCKET] Connecting to iot.thinger.io:25200
[_SOCKET] Error while connecting!

Here is the debug code used:

#define _DEBUG_
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <ccspi.h>
#include <ThingerCC3000.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"

    ThingerCC3000 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);


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

  thing.add_wifi(SSID, SSID_PASSWORD);

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

    /* define another resource with a small delay before reading
       to check if I/O affects readings in some way.
    */
    thing["Value2"] >> [](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();
  }
}

Can you elaborate as to why this would be happening?