chipKIT® Development Platform

Inspired by Arduino™

How to display temperature on the OLED

Created Wed, 30 May 2012 15:11:19 +0000 by Tareq


Tareq

Wed, 30 May 2012 15:11:19 +0000

Hi guys :geek:

I just got the [color=#FF0040]chip[/color]KIT Uno32 with the Basic [color=#0000FF]I/O[/color] Shield and they both work fine. I have installed the libraries and copied the sketches in place after HARD WORK. I was trying to understand the provided examples for the Basic IO Shield. Both the [color=#00BF00]OLED[/color] demo and the temperature demo work.

I was hoping one can help me write a code to display the temperature readings from the sensor on the [color=#00BF00]OLED[/color] (both are on the IO Shield) instead of the serial monitor, or both if any is even possible :mrgreen: .. It's better to seem lazy to try, but the truth is that I have run out of energy and thought trying to do this magic. Thanks.


BoH_Havoc

Sat, 02 Jun 2012 13:05:26 +0000

I couldn't test this myself as i'm not in front of my chipKIT at the moment, but this should work:

/* Note: For chipKit Max users you must manually 
** connect SDA and SCL pins(20 and 21) to pins A4 and A5 of 
** IO Shield 
**
** Note: For chipKit Uno Users you must have Jumpers JP6 and JP8 
** set in the RG3 and RG2 positions
** 
** This example also checks the status of the Alert pin. Normally this
** could be used to send some information to an external device, but
** for this example it's just checked when we check the tempereture.
** to use this part of the example connect the A pin on header JP4
** to pin 2 on your ChipKit board.
**
*/

#include <IOShieldTemp.h>
#include <IOShieldOled.h>
#include <Wire.h>

#define ALERT_PIN 2

void setup()
{
  Serial.begin(9600);
  pinMode(ALERT_PIN, INPUT);  
  //Initialize Configuration register for oneshot with 11 bit
  //resolution
  IOShieldTemp.config(IOSHIELDTEMP_ONESHOT | IOSHIELDTEMP_RES11 | IOSHIELDTEMP_ALERTHIGH);
  
  //Set the range to bring the alert pin high if it's above 78F (25.5C), alert will stay
  //high until the temp drops below 75.2F (24C).
  IOShieldTemp.setTempLimit(IOShieldTemp.convFtoC(78)); // 78.0F ~ 25.5C
  IOShieldTemp.setTempHyst(24); // 75.2F ~ 24.0C
  
  //Setup Display
  IOShieldOled.begin();
  IOShieldOled.setCharUpdate(0); //Turn automatic updating off (removes flickering)
}

void loop() 
{
  int tempF, tempC;
  
  //Get Temperature in Celsius.
  tempC = IOShieldTemp.getTemp();
  
  // Convert the result to Fahrenheit.
  tempF = IOShieldTemp.convCtoF(tempC);

  //Print Temperature to serial port
  Serial.print(tempC);
  Serial.print(" C, ");
  Serial.print(tempF);
  Serial.print(" F");
  if(digitalRead(ALERT_PIN) == HIGH)
    Serial.print(" ALERT!");
  Serial.println();
  
  
  //Print Temperature on Display
  IOShieldOled.clearBuffer();
  
  //Celsius  
  String temperature_str = "TempC:";
  temperature_str += tempC;
  char temperature_chr[temperature_str.length()+1]; //create char buffer
  temperature_str.toCharArray(temperature_chr, temperature_str.length()+1); //convert to char
  IOShieldOled.setCursor(0, 0);
  IOShieldOled.putString(temperature_chr);
  
  //Fahrenheit
  String temperature2_str = "TempF:";
  temperature2_str += tempF;
  char temperature2_chr[temperature2_str.length()+1]; //create char buffer
  temperature2_str.toCharArray(temperature2_chr, temperature2_str.length()+1); //convert to char
  IOShieldOled.setCursor(0, 1);
  IOShieldOled.putString(temperature2_chr);

  IOShieldOled.updateDisplay();

  delay(1000);  
}

Tareq

Sun, 03 Jun 2012 11:28:03 +0000

WOW, very neat indeed. Thanks for taking the trouble..

It doesn't compile until you change <float> into e.x. <int> or <unsigned>

The OLED did not turn on and did not show anything. However the serial communication is still working just like with the temp demo.

I can see the [color=#BF00BF]PIC32[/color] is gathering data and sending them. I need to learn how to show updated info on the OLED from sensors or other inputs when I am not using my computer for powering the chipKIT.

Alright, I will try to use your code and make it work. Thanks again.


BoH_Havoc

Sun, 03 Jun 2012 12:03:33 +0000

You might have to set some jumpers on the uno32 to get the oled working. (SPI mode should be set to master). If i remember correctly you can set this by J3 and J4 (at least on the max32, which i use). Just have a look at the manual.

Another guess is that the serial monitor is disturbing the oled library (i doubt it though). So if you still can't get it working after setting the uno32 to SPI master, try removing the Serial stuff.

Please note that i'm no expert on this topic and am using a different board. Also i still don't have access to my max32. So all of this is just theoretical ;)


BoH_Havoc

Sun, 03 Jun 2012 12:06:58 +0000

Wait i know why it's not working. I forgot an important line in the example i gave.

Put this line

IOShieldOled.updateDisplay();

after this line

IOShieldOled.putString(temperature2_chr);

and it should work.

I also updated my code above.


Tareq

Sun, 03 Jun 2012 15:32:51 +0000

BRAVoO! IT IS WORKING!!! for the first time in my life something actually works for me. I DID IT, I used the code and I DID IT.

TempC:51 TempF:124

the serial monitor works along with the display as well. and again, I had to define <int> instead of <float>. I'll print out the code and study it attentively. GOOD JOB BoH_Havoc.

Thanks a lot.

C:32 :mrgreen: F:89


Francois_Swart

Fri, 31 Aug 2012 19:57:57 +0000

Hi I'm very new to this, I tried using the above code but got the following errors:

sketch_aug31a.cpp: In function 'void loop()': sketch_aug31a.cpp:68:22: error: conversion from 'float' to 'const String' is ambiguous C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:39:5: note: candidates are: String::String(long unsigned int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:38:5: note: String::String(long int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:37:5: note: String::String(unsigned int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:36:5: note: String::String(int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:35:5: note: String::String(unsigned char) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:34:5: note: String::String(char) sketch_aug31a.cpp:76:23: error: conversion from 'float' to 'const String' is ambiguous C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:39:5: note: candidates are: String::String(long unsigned int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:38:5: note: String::String(long int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:37:5: note: String::String(unsigned int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:36:5: note: String::String(int, int) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:35:5: note: String::String(unsigned char) C:\Users\Francois\Downloads\mpide-0023-windows-20111221\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/WString.h:34:5: note: String::String(char)

I realy do not have a clue what anything means. Please help?


JordanR

Fri, 07 Sep 2012 21:51:35 +0000

Hello Francois_Swart,

You will need to change the float type variables to ints, as Tareq mentioned. The compiler is giving you these errors because it recognizes that it is supposed to be converting one data type into a String, but it doesn't know what to do with float data types, since the conversion method is not defined for float to string. If you take a look at the "candidates" that the compiler error is giving you, you will notice that all of the methods refer to ints unsigned ints, chars, etc., but never to float. Change the variables being converted to ints and you should have no problems.

Best Regards,

Jordan R Digilent


BoH_Havoc

Thu, 04 Oct 2012 19:34:20 +0000

I edited my code above and changed float to int. Totally forgot to do that when Tareq mentioned it. Future Copy&Pasters should not have problems anymore ;)


gwh2012

Tue, 09 Oct 2012 21:23:49 +0000

I just got my chipKIT UNO 32 and Basic I/O shield a few days ago. So far I follow the above comments in this thread except I don't see how one was supposed to know that "the conversion method is not defined for float to string". My first question is how/where could a person look that up?

My second question deals with displaying floats on the OLED. I decided I wanted to send the temperatures out on the Serial monitor as well as display them on the OLED except I wanted the numbers shown to two decimal points. My code for doing this is shown below. It works fine but my second question is how could I have done this easier? It seems like a pretty awkward way to display a simple decimal number.

Thanks for any help.

/*This program reads the temperature from the temperature sensor on the 
 chipKIT Basic I O Shield and displays it with two decimals on the 
 OLED as well as sending it the the Serial monitor via the USB cable
 */

//need this lib for the temp sensor
#include &lt;IOShieldTemp.h&gt;
//need this lib since the temp sensor and its library uses I2C
#include &lt;Wire.h&gt;
//need this lib for the OLED display
#include &lt;IOShieldOled.h&gt;

void setup()
{
  //set up the Serial monitor
  Serial.begin(9600);
  //set up the OLED display
  IOShieldOled.begin();
  IOShieldOled.setCharUpdate(0); //Turn automatic updating off (removes flickering)
  //Initialize Configuration register for oneshot with 12 bit resolution
  //The One-shot mode performs a single temperature
  //measurement and returns to Shutdown mode. 
  IOShieldTemp.config(IOSHIELDTEMP_ONESHOT | IOSHIELDTEMP_RES12);
}

void loop() 
{
  float tempF, tempC, tempCflfract, tempFflfract;
  int tempFwhole, tempCwhole, tempFfract, tempCfract;
  //Get Temperature in Celsius.
  //This function retrieves the current temp from the temp 
  //sensor and converts the returned value into a
  //signed floating point value.
  tempC = IOShieldTemp.getTemp();

  // Convert the result to Fahrenheit.
  //This routine takes in a temperature in degrees Celsius 
  //and converts it to Fahrenheit in a signed floating point value.
  tempF = IOShieldTemp.convCtoF(tempC);

  //Print Temperature to serial port
  Serial.print(tempC);
  Serial.print(" C, ");
  Serial.print(tempF);
  Serial.print(" F");

  /*the compiler doesn't have a method defined for float to string conversion
   so we must convert floats to char for display on the OLED 
   but we also want to display the fractional temperature information
   As a result, we generate int values for both the whole and fractional
   values of each temperature number.
   */

  //determine the whole and fractional parts of tempC to send to the OLED display
  tempCwhole = tempC; 
  //tempCwhole now contains the whole part of tempC in an int with the fractional part truncated
  tempCflfract = tempC - tempCwhole;
  //tempCflfract now contains the fractional part of tempC as a float
  tempCfract = tempCflfract * 100;
  //tempCfract now contains the fractional part of tempC as an int

  //determine the whole and fractional parts of tempF to send to the OLED display
  tempFwhole = tempF; 
  //tempFwhole now contains the whole part of tempF in an int with the fractional part truncated
  tempFflfract = tempF - tempFwhole;
  //tempFflfract now contains the fractional part of tempF as a float
  tempFfract = tempFflfract * 100;
  //tempFfract now contains the fractional part of tempF as an int

  //Print Temperature on Display
  //Clear the display memory buffer without updating the display
  IOShieldOled.clearBuffer();

  //Celsius 
  String temperature_str = "chipKIT reads   ";
  //adds tempCwhole to the end of the temperature_str string
  temperature_str += tempCwhole;
  temperature_str += ".";//adds the decimal point
  //need to check to see if the fractional part is under 10
  //since will have to insert a 0 in that case
  if (tempCfract &lt; 10) {
    temperature_str += "0";
  }
  temperature_str += tempCfract;//adds the fractional part
  char temperature_chr[temperature_str.length()+1]; //create char buffer

  temperature_str.toCharArray(temperature_chr, temperature_str.length()+1);//convert to char
  IOShieldOled.setCursor(0, 0);
  IOShieldOled.putString(temperature_chr);
  IOShieldOled.putString(" Degrees C");
  IOShieldOled.setCursor(0, 2);
  IOShieldOled.putString("   Which is ");
  //Fahrenheit
  String temperature2_str = "";
  temperature2_str += tempFwhole;
  temperature2_str += ".";//adds the decimal point
  //need to check to see if the fractional part is under 10
  //since will have to insert a 0 in that case
  if (tempFfract &lt; 10) {
    temperature2_str += "0";
  }
  temperature2_str += tempFfract;//adds the fractional part
  char temperature2_chr[temperature2_str.length()+1]; //create char buffer
  temperature2_str.toCharArray(temperature2_chr, temperature2_str.length()+1); //convert to char
  IOShieldOled.setCursor(0, 3);
  IOShieldOled.putString(temperature2_chr);
  IOShieldOled.putString(" Degrees F");
  IOShieldOled.updateDisplay();
  delay (5000);
}

guymc

Tue, 09 Oct 2012 22:22:41 +0000

I would think that sprintf() could do the conversion for you...


mikes

Wed, 10 Oct 2012 02:16:21 +0000

I agree sprintf is good for this job ;)

/*This program reads the temperature from the temperature sensor on the 
 chipKIT Basic I O Shield and displays it with two decimals on the 
 OLED as well as sending it the the Serial monitor via the USB cable
 */

//need this lib for the temp sensor
#include &lt;IOShieldTemp.h&gt;
//need this lib since the temp sensor and its library uses I2C
#include &lt;Wire.h&gt;
//need this lib for the OLED display
#include &lt;IOShieldOled.h&gt;

void setup()
{
  //set up the Serial monitor
  Serial.begin(9600);
  //set up the OLED display
  IOShieldOled.begin();
  IOShieldOled.setCharUpdate(0); //Turn automatic updating off (removes flickering)
  //Initialize Configuration register for oneshot with 12 bit resolution
  //The One-shot mode performs a single temperature
  //measurement and returns to Shutdown mode. 
  IOShieldTemp.config(IOSHIELDTEMP_ONESHOT | IOSHIELDTEMP_RES12);
}

void loop() 
{
  float tempF, tempC, tempCflfract, tempFflfract;
  int tempFwhole, tempCwhole, tempFfract, tempCfract;
  //Get Temperature in Celsius.
  //This function retrieves the current temp from the temp 
  //sensor and converts the returned value into a
  //signed floating point value.
  tempC = IOShieldTemp.getTemp();

  // Convert the result to Fahrenheit.
  //This routine takes in a temperature in degrees Celsius 
  //and converts it to Fahrenheit in a signed floating point value.
  tempF = IOShieldTemp.convCtoF(tempC);

  //Print Temperature to serial port
  Serial.print(tempC);
  Serial.print(" C, ");
  Serial.print(tempF);
  Serial.print(" F");

  //Print Temperature on Display
  //Clear the display memory buffer without updating the display
  IOShieldOled.clearBuffer();
  //Celsius 
  char temperature_str[60];
  sprintf(temperature_str,"chipKIT reads   %0.3f Degrees C   Which is     %0.3f Degrees F",tempC,tempF);
  IOShieldOled.setCursor(0, 0);
  IOShieldOled.putString(temperature_str);
  IOShieldOled.updateDisplay();
  delay (5000);
}

gwh2012

Fri, 12 Oct 2012 06:23:01 +0000

Thanks, I'll give the sprintf() a try this weekend.


supermac

Tue, 16 Oct 2012 04:50:21 +0000

It seems it does not like float values. I tried the program above. I get garbage values for the temp on the oled but it works in the usb serial port.

Any other suggestions.