Varying values from sensor between Arduino and Processing?

Hi! I'm currently doing a project for uni, where I'm using the TMP36 temperature sensor and serial comunication, between the Arduino UNO and Processing, to have the sensor control visuals. However, I've run into a problem, where the Arduino serial monitor gives me viable answers in celsius, between 25 degrees and 37-40 degrees when I touch it. However, when I print the values from the sensor in the Processing serial monitor, the numbers are way different - and not just in a way where they can be calculated and converted, they fluctuate way more in Processing for some reason.

Here is my Arduino code:

float temp;
int tempPin = 1;

void setup()
{
  Serial.begin(9600);
} void loop()
{
  temp = analogRead(tempPin);
  temp = temp * 0.48828125; //calculates value in fahrenheit
  temp = ((temp - 32) * 5) / 9; //converts from fahrentheit to celcius
  Serial.println(temp);
  delay(500);
}

And the values the Arduino's giving me

Processing code:

import processing.serial.*;
Serial myPort;
float tempValue;

void setup() {
  size(1200, 600);
  myPort = new Serial(this, "COM3", 9600);
}

void draw() {
  tempValue = myPort.read();
  println(tempValue);
  delay(500);
}

The values Processing is giving me

Arduino UNO and breadboard setup:

I must've done something wrong somewhere, but I hope someone can help me! :smiley:

Post the values you get in the serial monitor of the Arduino IDE and what you get in the

  • not sure

import processing sounds like python?
looked it up no not python

it is its own language for serial receiving. Never heard of it before.
Anyway I googled
import processing.serial. - Google Search*

and google came up with the website that describes "serial" of processing
and this list contains

constructor
...
Serial(parent, portName, baudRate, parity, dataBits, stopBits)

So if you see something strange the reason might be that something of the details baudrate, parity databits stopbits is different

In the Arduino and in processing there are default-values
You can configure them on both sides

For Arduino the serial.begin method can be configured

and for processing

If the two configurations match in all details and you still get different results
the problem might be inverted logic levels on the hardware-side
To analyse if this is the case
post the received characters

best regards Stefan

I've been able to send data from potentiometers and push buttons without problem, so I don't think there's issues with the deatils, however I could be mistaken, as I am definitely no expert :thinking:
I linked Imgur links to both value outputs, you should be able to click on the blue pieces of text, and it'll pop up. I'll put the links here as well, just in case it doesn't work:

Arduino values:

Processing values:

indeed strange

To narrow down the problem I would load a testprogram into the Arduino that just sends a single and the exact same value all the time

Just a single character an "A"
if this works
sending an integer "100"
if this works a float "100.0"
just a loop that does constantly
Serial.println("A");

temp = 100;
Serial.println(temp);

etc.
by the way you can simply copy & paste screenshots directly into the textwindow in the forum. The new forum-software has a lot of modern features

best regards Stefan

@muffintut, your topic has been moved to a more suitable location on the forum.

1 Like

Serial.println(temp);

This sends your data as an ascii character string representing the float value of temp.

Then in processing your read the characters, but are expecting a floating point number.

float tempValue;
tempValue = myPort.read();//read one char

You receive values like this which are the ascii representation of a floating point number
51 -> 3
53 -> 5
46 -> .
50 -> 2
57 -> 9
13 -> CR
10 -> LF

You need to realize that you reading the ascii characters and deal with it appropriately in processing.

Okay, that makes sense! I looked at processing's website, and found an example showing this:

int i = 65;
char c = char(i);
println(i + " : " + c);  // Prints "65 : A"

So I did this to my code, but I'm still getting ASCII characters, now just with a colon afterwards...

import processing.serial.*;
Serial myPort;
int tempValue;
char c = char(tempValue);

void setup() {
  size(1200, 600);
  myPort = new Serial(this, "COM3", 9600);
}

void draw() {
  tempValue = myPort.read();
  println(tempValue + ":" + c);
  delay(500);
}

Because I'm still very new to this site, I can't post anymore answers before four hours have passed. Therefore, I'm editing this, to ask you further questions @StefanL38

Okay, I tried to implement the string and convert it, according to the processing website:

import processing.serial.*;
Serial myPort;
int tempValue;
char c = char(tempValue);
String tempString = "51, 56, 10, 13";
char data[] = {51,56,10,13};
float tempFloat = Float.valueOf(tempString);

void setup() {
  size(1200, 600);
  myPort = new Serial(this, "COM3", 9600);
}

void draw() {
  tempValue = myPort.read();
  println(tempValue + ":" + c);
  delay(500);
}

However, the example I'm looking at, has a string of letters and not numbers, which is my guess as to why I'm getting the error code: 'java.lang.reflect.InvocationTargetException'
But, I'm unsure how to fix this :thinking:
I also see now, that I'm getting way more different values than the ones I picked out above, so how exactly do I get my values into the string, if I'm uncertain about the specific values?

does a conversion from type integer to char

your codeline

println(tempValue + ":" + c);

does

print tempvalue (just as before)
print a colon
print content of variavble named c

this is completely different from doing a conversion.

Your command

myPort.read();

reads a single byte

Let's say your arduino sends the float-value 345.89

So your arduino sends the following character-sequence
character 1 3
character 2 4
character 3 5
character 4 .
character 5 8
character 6 9

this means the first call of

tempValue = myPort.read();

reads a single byte with interger-value 51 which represents ASCII-code of character "3"

etc.

So what you really have to to is collect all the characters of the sequence
character 1 3
character 2 4
character 3 5
character 4 .
character 5 8
character 6 9

into a string and then do the conversion to float

best regards Stefan

You are really asking a question about how Processing deals with incoming Serial data. It's very similar to Arduino and here's the reference page
https://processing.org/reference/libraries/serial/Serial.html

I would also take a look at Robin2's Serial Input Basics tutorial for non blocking methods and robust protocols.

Alternatively you can use the blocking methods of readBytesUntil() and readStringUntil() with the '\n' of the new line as the terminating character.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.