How to write analog temperature sensor to OLED display?

I am trying to make some code that writes the output of a temperature sensor to a 1.3" OLED display. So far, I am trying to rig together some sample code to make them work. How do I set the output of Serial.print(Thermistor(analogRead(A0))); to a variable, so I can have it print to the screen using the string u8g.drawStr( 0, 22, "Sample Text");

Code:

#include "U8glib.h"
#include <math.h>

int systemp = 0
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9); // SCK = 13, MOSI = 11, CS = 10, A0 = 9
void draw(void) {
 // graphic commands to redraw the complete screen should be placed here
 u8g.setFont(u8g_font_unifont);
 //u8g.setFont(u8g_font_osb21);
 u8g.drawStr( 0, 22, "Sample Text");
}
void setup(void) {
 // flip screen, if required
u8g.setRot180();
 // set SPI backup if required
 //u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
 if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
 u8g.setColorIndex(255); // white
 }
 else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
 u8g.setColorIndex(3); // max intensity
 }
 else if ( u8g.getMode() == U8G_MODE_BW ) {
 u8g.setColorIndex(1); // pixel on
 }
 else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
 u8g.setHiColorByRGB(255,255,255);
 }}
void loop(void) {
 // picture loop
 u8g.firstPage();
 do {
 draw();
 } while( u8g.nextPage() );
 // rebuild the picture after some delay
 delay(50);
}



double Thermistor(int RawADC){
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1/(0.001129148+(0.000234125+(0.0000000876741*Temp*Temp ))*Temp);
 Temp = Temp - 273.15;
 return Temp;
}
void setup() {
 Serial.begin(9600);
}
void loop(){
 Serial.print(Thermistor(analogRead(A0)));
 Serial.println("c");
 delay(1000);
}

Hi @nightoffire

welcome to the arduino-forum.

You seem to have some basic knowledge about programming as you are mentioning a variable.
Well programming requires a little bit more knowledge than a game for 3 year-old children put the magnetic parts on the iron plate.

You look into the sourcecode what type of return-value does the function Thermistor() have
define a variable of the same type to assign the value to.

Then you lookup what functions does the display-library have and what type of variable do these functions expect as parameters.

If the variabletypes do not match you have to code a type-casting.

This text is too short and uses too many technical terms to be easy to understand for beginners.
I wrote it this way to enable the experience "Ok some more knowledge is required"

If you don't want to stumble at every second line of code you should learn the basics. This will take quite some time.

basically you have two ways of using time:

  1. way (not recommended)
    doing some modifications to the code asking a new question on the forum waiting (some hours) for the answer to be posted
    doing some modifications to the code asking a new question on the forum waiting (some hours) for the answer to be posted
    ....
    repeat

After 10 to 15 days having a code that works but not really knowing how this code works

  1. way: recommended: learning the basics by working through a basic programming tutorial
    5 times 3 hours to know the basics and after that beeing able to really understand how to write the code

my recommendation is
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Once you’ve read a bit about programming, types. Functions, classes and methods (if you needed it), you could look at the GitHub for the libray you mention and they state

There will be no more development for U8glib. Please use u8g2.

So take a look at Home · olikraus/u8g2 Wiki · GitHub and the documentation

It seems they support print


Side note

Your question really was
How do I convert a number into a string? (Assuming the library does not support printing numbers)

Depending on the type of the variable you have (integral, floating point) and the hardware you use you can leverage functions like itoa(), dtostrf(), sprintf() or the String class (and the c_str() method to pass the raw buffer)

Hi Stefan,

While I thank you for your input, there was no need to patronise me. I have experience in other programming languages, just not Arduino.

Again, thanks for your input.

Ah, thanks. Docs for the screen I use dictated the use of U8glib, but I'll switch over.

I was editing my post as you replied. Make sure you take note of

Ah I see ! (or can read now). Seeing two functions void setup() and void loop() in the code you posted

together with

lead me to the assumption you are a real beginner.
Still the recommendations of the sticky note "How to get the best out of this forum" apply

Alright, no worries. I was just being lazy and using sample code instead of manually writing it is all.

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