1

I am interested in measuring the voltage over an analog pin (A6). When I use the Arduino IDE to print the results through the serial I get values around 300.

Here's the code

void setup(){
   Serial.begin(57600);
}

void loop(){
   Serial.println(analogRead(A6));
   delay(50);
}

When I try to run a equivalent program using Processing and interfacing it through Firmata and the corresponding library, I get values around 900.

Here's the Processing code:

import cc.arduino.*;
import processing.serial.*;

Arduino arduino;

void setup(){
   arduino = new Arduino(this,Arduino.list()[0], 57600);
   delay(2000);
}

void draw(){
  System.out.println(arduino.analogRead(6));
  delay(50);
}

Does anybody know what could be the issue? I should probably rather trust the values given directly by the Arduino without interfacing, right?

The pin is connected to a circuit with supply voltage. I also tried reading the values directly using Serial in Processing (so without using the cc.arduino.* - library) and that showed me the values around 300. So I guess the difference is caused by the arduino.analogRead() method. I am puzzled as to whether that's a bug in the library or something else.

The problem seems to be me not properly initialising the pins. But calling

arduino.pinMode(6,Arduino.ANALOG);

in the setup method does nothing, as does Arduino.PWM, SERVO and any combination I tried. I thought, maybe Processing tried to assign D6 as analog instead of A6, but changing the value to 20 (as it's the pin number according to this) simply caused an OutOfBoundsException.

  • 1
    what is connected to the pin? – Juraj Jun 29 '18 at 18:52
  • Have you measured the value on the pin with a voltmeter? – Craig Jun 29 '18 at 21:13
  • @Craig noup, good idea. Will try that later when I can acces a voltmeter. – acester123 Jun 29 '18 at 22:07
1

You have not provided a schematic of how the Arduino is wired up.

However, if nothing is connected to the pin, i.e. it is not connected to ground or a supply voltage, then the input will float. That means that just by placing your hand near the Arduino, then analogRead() will produce a different value than when your hand in not near it.

I would hazard a guess that if you ran both sets of code multiple times then you will get different answers each time, if you start waving your hand around the Arduino.

Try that first, and then tie the input to either HIGH or LOW and then repeat the previous experiment.

In the second case the results should be consistent, and equal in the Arduino and Processing alike.


However, according to your comment, that is not your issue, as your inputs are not floating.

I think that issue is actually this:

Even with Firmata installed, the Processing application needs to define the pin mode for ALL the pins being referenced, telling the Arduino instance that xPin, yPin, and zPin are ANALOG pins.

Source: Re: Firmata problem with analogRead in Processing Library

Looking at your Processing code the pins are not defined. You need to do so.

With the Arduino code, there is no need to define the analogue pins.

  • Sorry, forgot to mention: it is connected to a circuit with supply voltage. The values are actually relatively stable around either 900 or 300. – acester123 Jun 29 '18 at 20:06
  • @JorgeAndresQuinteroSerna - Ah, that is strange then... I strongly suggest that you edit your question and put that information in your original post, to save people having to search around in comments to find it.. – Greenonline Jun 29 '18 at 20:12
  • @JorgeAndresQuinteroSerna - updated answer with possible reason. – Greenonline Jun 29 '18 at 20:17
  • Thank a lot for your help! Unfortunately, I am still not able to properly initialise the pins or that still doesn't fix the issue. – acester123 Jun 29 '18 at 21:54
  • @JorgeAndresQuinteroSerna - oh really? That is a shame. If I find a different solution, I'll try to update my answer. Otherwise if you find a solution yourself, don't forget to post it as your own answer as it will probably help others with the same issue. – Greenonline Jun 29 '18 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.