Arduino Temperature Sensor Read Values in Unity

Does anyone know how to take information from a temperature sensor on Arduino and render that out as U.I. text not on GUI text but the new U.I. text in Unity3D?
I would like to make a temperature digital readout in Unity that reads real environment temperature.

This may vary as the temperature may measure in solid numbers.

I’ve provided a pretty in depth script that you can play around with

using UnityEngine;
using UnityEngine.UI;

public class Example : MonoBehaviour
{
    //Seeing as the data you want will be in aother script, you will want to get the script by doing so below
    //public scriptYouWantToAccess script;
    //If you want to use that just remove the '//'
    //--Temperature sensor data--
    //This may be an int and not a float
    public float sensorData;
    //The text you'll be showing the data on
    public Text sensorText;

    void Update() //This updates everything below every frame
    {
        //Do you get what I mean by the sensorText.text? It's getting the text data from the text script
        //The .ToString() converts the data we have from the data into to a string that we can use in the text
        sensorText.text = sensorData.ToString();
        
        //If you want to access the variable from the demo, just do so as below
        //sensorData = script.theVariableStoredInTheScriptYouWant;
    }
}

You can use this as a reference, I’ve provided a lot of tips in the code itself
using UnityEngine;
using UnityEngine.UI;

public class Example : MonoBehaviour
{
    //Seeing as the data you want will be in aother script, you will want to get the script by doing so below
    //public scriptYouWantToAccess script;
    //If you want to use that just remove the '//'
    //--Temperature sensor data--
    //This may be an int and not a float
    public float sensorData;
    //The text you'll be showing the data on
    public Text sensorText;

    void Update() //This updates everything below every frame
    {
        //Do you get what I mean by the sensorText.text? It's getting the text data from the text script
        //The .ToString() converts the data we have from the data into to a string that we can use in the text
        sensorText.text = sensorData.ToString();
        
        //If you want to access the variable from the demo, just do so as below
        //sensorData = script.theVariableStoredInTheScriptYouWant;
    }
}

You’re probably gonna need a plugin that can communicate with arduino.
I’m not sure what’s your target platform, but unity documentation is pretty comprehensive about plugins.
The basic idea, is that’s it’s a native code (for example, java for android) that you can work with directly from unity, by using a plugin.

As a matter of fact, i am currently working on a project that uses an external temperature sensor.
i’ve made plugins for both windows app store and android.
so if you’ve got more specific questions, you may ask

other than that, once you have the data, you can just update a string with the data in the Update method if any script, and attack it to a Text UI element.

@WOODSY,
OK I have the Arduino code down below sending out temperature data through the Serial port, According the Serial monitor my button information is being sent to when I click the buttons but they no longer seem to respond in Unity. (See Serial Monitor Screen grab)

Arduino Code:

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS A5 //Analog Pin that the DS18B20 is connected too

OneWire oneWire_in(ONE_WIRE_BUS);

DallasTemperature sensor_incar(&oneWire_in);

int lightSensorPin = A0; //Define the Light Sensor Pin
int lightSensorValue = 0;

//int lightLevel = analogRead(0);
//int threshold = 250;
//int range = 50;

//Defines Our Button Pins
const int buttonRight = A1; 
const int buttonLeft = A2;
const int buttonUp = A3;
const int buttonDown = A4;

// Variable to hold the state of the buttons
int bRS, bLS; //ButtonRight and ButtonLeft states status holders


//Sets Pin number LED is conneted too
const byte rLed = 12; 
const byte rLed2 = 1;
const byte yLed = 11;
const byte gLed = 10;
const byte gLed2 = 5;
const byte gLed3 = 4;
const byte wLed = 3;
const byte bLed = 2;
const byte bLed2 = 13;

char myChar;         //changed the name of this variable because they are not all colurs now
const byte pulsePins[] = {6, 7, 8, 9};  //pins for a pulse output
char pulseTriggers[] = {'p', 'q','R','L'};
const int NUMBER_OF_PULSE_PINS = sizeof(pulsePins);
unsigned long pulseStarts[NUMBER_OF_PULSE_PINS];
unsigned long pulseLength = 500;

int buttonStateR = 0;       //Current State of the R button
int lastButtonStateR = 0;   //Previous State of the R button

int buttonStateL = 0;       //Current State of the L button
int lastButtonStateL = 0;   //Previous State of the L button

int buttonStateU = 0;       //Current State of the U button
int lastButtonStateU = 0;   //Previous State of the U button

int buttonStateD = 0;       //Current State of the D button
int lastButtonStateD = 0;   //Previous State of the D button

int buttonPushCounter = 0;  //Counter for number of button presses

void setup()
{
  //Serial.begin (9600);
  Serial.begin (115200);
  Serial.setTimeout(13); //Added today Sun Nov 22 ( not sure if this is needed? )

  Serial.println("Dallas Temperature Control Library Demo - TwoPin_DS18B20");

  sensor_incar.begin();

  pinMode(buttonRight, INPUT); //Defines Pin Mode
  pinMode(buttonLeft, INPUT);
  pinMode(buttonUp, INPUT);
  pinMode(buttonDown, INPUT);

  digitalWrite(buttonRight, HIGH);
  digitalWrite(buttonLeft, HIGH);
  digitalWrite(buttonUp, HIGH);
  digitalWrite(buttonDown, HIGH);
  
  pinMode(wLed, OUTPUT);
  pinMode(rLed, OUTPUT);
  pinMode(rLed2, OUTPUT);
  pinMode(yLed, OUTPUT);
  pinMode(gLed, OUTPUT);
  pinMode(gLed2, OUTPUT);
  pinMode(gLed3, OUTPUT);
  pinMode(bLed, OUTPUT);
  pinMode(bLed2, OUTPUT);
  digitalWrite(wLed, LOW);
  digitalWrite(rLed, LOW);
  digitalWrite(rLed2, LOW);
  digitalWrite(yLed, LOW);
  digitalWrite(gLed, LOW);
  digitalWrite(gLed2, LOW);
  digitalWrite(gLed3, LOW);
  digitalWrite(bLed, LOW);
  digitalWrite(bLed2, LOW);
  
  for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
  {
    pinMode(pulsePins[p], OUTPUT);
    digitalWrite(pulsePins[p], LOW);
  }
}

void loop()
{
  Serial.print("Requesting temperatures...");
  sensor_incar.requestTemperatures();
  Serial.println("done");

  Serial.print("Incar");
  Serial.println(sensor_incar.getTempCByIndex(0));
   
  // read the new light value (in range 0..1023): 
     int sensorValue = analogRead(lightSensorPin);
     // if the value changed by 10
     if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
         lightSensorValue = sensorValue; // save the new value
         float p = lightSensorValue * (100.0 / 1023.0);    // make the value to range 0..100
         // the Parentheses may be for compiler optimization idk  
         Serial.println(p); // send it to unity
     }

  

  
  if (Serial.available())              //if serial data is available
  {

    
    
    int lf = 10;

    myChar = Serial.read();             //read one character from serial
    if (myChar == 'r')                  //if it is an r
    {
      digitalWrite(rLed, !digitalRead(rLed));  //Oil Slick Toggle
    }
    
    if (myChar == 'b')
    {
      digitalWrite(bLed, !digitalRead(bLed)); //Surveillance Mode Toggle
    }

    if (myChar == 'y')
    {
      digitalWrite(yLed, !digitalRead(yLed)); //Movie Player Toggle
    }

    if (myChar == 'g')
    {
      digitalWrite(gLed, !digitalRead(gLed)); //Auto Phone Toggle
    }

    if (myChar == 's')
    {
      digitalWrite(bLed2, !digitalRead(bLed2)); //Scanner Toggle
    }

    if (myChar == 'f')
    {
      digitalWrite(gLed2, !digitalRead(gLed2)); //Fog Lights Toggle
    }

    if (myChar == 'h')
    {
      digitalWrite(gLed3, !digitalRead(gLed3)); //Head Lights Toggle
    }

    if (myChar == 'H')
    {
      digitalWrite(wLed, !digitalRead(wLed)); //High Beams Toggle
    }


    //Rear Hatch Popper
    for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    {
      if (myChar == pulseTriggers[p])
      {
        pulseStarts[p] = millis();  //save the time of receipt
        digitalWrite(pulsePins[p], HIGH);
      }
    }


    //Grappling Hook Launch
    for (int q = 0; q < NUMBER_OF_PULSE_PINS; q++)
    {
      if (myChar == pulseTriggers[q])
      {
        pulseStarts[q] = millis();  //save the time of receipt
        digitalWrite(pulsePins[q], HIGH);
      }
    }


    //Auto Doors Right Pulse
    for (int R = 0; R < NUMBER_OF_PULSE_PINS; R++)
    {
      if (myChar == pulseTriggers[R])
      {
        pulseStarts[R] = millis();  //save the time of receipt
        digitalWrite(pulsePins[R], HIGH);
      }
    }


    //Auto Doors Left Pulse
    for (int L = 0; L < NUMBER_OF_PULSE_PINS; L++)
    {
      if (myChar == pulseTriggers[L])
      {
        pulseStarts[L] = millis();  //save the time of receipt
        digitalWrite(pulsePins[L], HIGH);
      }
    }
  }

  //the following code runs each time through loop()
  for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
  {
    if (millis() - pulseStarts[p] >= pulseLength)  //has the pin been HIGH long enough ?
    {
      digitalWrite(pulsePins[p], LOW);   //take the pulse pin LOW
    }
  }

  for (int q = 0; q < NUMBER_OF_PULSE_PINS; q++)
  {
    if (millis() - pulseStarts[q] >= pulseLength)  //has the pin been HIGH long enough ?
    {
      digitalWrite(pulsePins[q], LOW);   //take the pulse pin LOW
    }
  }

 
  // read the pushbutton input pin RIGHT BUTTON
  buttonStateR = digitalRead(buttonRight);

  //compare the buttonState to its previous state
  if (buttonStateR != lastButtonStateR){
    // if the state has changed, increment the counter
    if (buttonStateR == LOW){
      // if the current state is HIGH then the button went from off to on
      buttonPushCounter++;
      Serial.println("E");
      //Serial.println(2);
      //Serial.print("number of button pushes: ");
      //Serial.println(buttonPushCounter);
    }else{
      // if the current state is LOW then the button went from on to off
      //Serial.println(0);
      Serial.println("O");
    }
    // Delay  little bit to avoid bouncing
    delay(20);
  }
    // save the current state as the last state for the next time through loop
      lastButtonStateR = buttonStateR;


   // read the pushbutton input pin LEFT BUTTON
  buttonStateL = digitalRead(buttonLeft);

  //compare the buttonState to its previous state
  if (buttonStateL != lastButtonStateL){
    // if the state has changed, increment the counter
    if (buttonStateL == LOW){
      // if the current state is HIGH then the button went from off to on
      buttonPushCounter++;
      Serial.println("W");
      //Serial.println(1);
      //Serial.print("number of button pushes: ");
      //Serial.println(buttonPushCounter);
    }else{
      // if the current state is LOW then the button went from on to off
      Serial.println("O");
      //Serial.println(0);
    }
    // Delay  little bit to avoid bouncing
    delay(20);
  }
  // save the current state as the last state for the next time through loop
  lastButtonStateL = buttonStateL;


    // read the pushbutton input pin UP BUTTON
  buttonStateU = digitalRead(buttonUp);

  //compare the buttonState to its previous state
  if (buttonStateU != lastButtonStateU){
    // if the state has changed, increment the counter
    if (buttonStateU == LOW){
      // if the current state is HIGH then the button went from off to on
      buttonPushCounter++;
      Serial.println("N");
      //Serial.println(1);
      //Serial.print("number of button pushes: ");
      //Serial.println(buttonPushCounter);
    }else{
      // if the current state is LOW then the button went from on to off
      Serial.println("O");
      //Serial.println(0);
    }
    // Delay  little bit to avoid bouncing
    delay(20);
  }
  // save the current state as the last state for the next time through loop
  lastButtonStateU = buttonStateU;


    // read the pushbutton input pin DOWN BUTTON
  buttonStateD = digitalRead(buttonDown);

  //compare the buttonState to its previous state
  if (buttonStateD != lastButtonStateD){
    // if the state has changed, increment the counter
    if (buttonStateD == LOW){
      // if the current state is HIGH then the button went from off to on
      buttonPushCounter++;
      Serial.println("S");
      //Serial.println(1);
      //Serial.print("number of button pushes: ");
      //Serial.println(buttonPushCounter);
    }else{
      // if the current state is LOW then the button went from on to off
      Serial.println("O");
      //Serial.println(0);
    }
    // Delay  little bit to avoid bouncing
    delay(20);
  }
  // save the current state as the last state for the next time through loop
  lastButtonStateD = buttonStateD;

}