How can I read serial information and print it to an OLED?

Hello,

Yes, I seem to be one of many who might have asked such a question and I also went through a lot of time and forum pages to come to the point where I am now. I dont know much about Arduinos, OLEDs and especially coding. Though, honestly Im quite proud about how far Ive come.

I am making myself a ITX PC which includes an Arduino Nano linked to an OLED on which I want to display CPU temps, GPU temps, date and time. (Arduino is linked to the PC with a USB all the time.)

For the CPU/GPU part im thinking of following link 1 (see below). Again, Im very inexperienced. So much so that even the seemingly simplest guides are jibberish for me. Thus Im not working on whats mentioned in link 1 for now. If someone can help me with the mentioned guide, please do.

For the date and time Im using a programme called "gobetwino". Thisll be running in the background and wait for the command from the Arduino, once recieved itll sent back the current time. I figured out how to send a command and get it on the OLED in some form. (see below for the code.) However its not in the form id like it to be on the display. (Again, see below for an example.)

My questions:
How do I store the data sent from the PC?
How do I display the stored data from the Arduino displayed on the OLED?
How do I control the above each second, so the displayed time is correct?
If youre willing, the same questions but then for the CPU/GPU temps.

Hardware:
AZDelivery Arduino Nano V3.0 CH340
TZT OLED 3.12" 256x64

Link 1: Arduino PC Monitor - Arduino Project Hub

Example of displaying the data on the OLED:
CPU 60C 09-02-22
GPU 65C 19:36:21

The frankenstein code: (please note that its for displaying time only)

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1322_NHD_256X64_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 5, /* dc=*/ 3, /* reset=*/ 4);

#define U8LOG_WIDTH 8
#define U8LOG_HEIGHT 8
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
U8G2LOG u8g2log;


void setup(void) 
{
  Serial.begin(9600);

  u8g2.begin();  
  u8g2.setFont(u8g2_font_5x7_tr);
  u8g2.setFlipMode(1);
  
  u8g2log.begin(u8g2, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
  u8g2log.setLineHeightOffset(0);
  u8g2log.setRedrawMode(1);
}

void loop(void) 
{
  Serial.println("#S|T|[]#");
  delay(500);
  
  char c;
  if (Serial.available() >= 8) {
    c = Serial.read();
    u8g2log.print(c);
  }
}

What exact OLED module are you using?

What does the code you posted do, what does it show on the OLED?

There are some odd things in that code, and I don't see anything about time.

At link 1 is a simple program to receive serial info from the part we I can't help you with.

For the moment, forget the OLED and work in the PC side code… just use Serial.print instead of running the LCD which you don't have anyway.

Once you see the stuff coming from the PC and appearing in the serial monitor, then it is time time we can help display that on the OLED instead or in addition.

I usually leave such Serial printing in place, doesnt matter if there isn't anyone listening.

a7

Do you do this just to monitor two temperatures and a clock? Isn't it better to do something like that?

Thank you for your quick reply! Please see below for what hardware I am using.

The code, which I worked together with some samples, sends a command via serial to the PC (Serial.println("#S|T|[]#"); to be exact). This is picked up by this programm called "gobetwino", which in turn sends back a time in hh:mm:ss. Somehow I managed to get a code which then displays every second or so the new time. However, this is on a new line over and over.

Ill see how to set up a serial monitor, though my questions still stand. How do I use the time sent by the PC? And how do I display that in such a serial monitor.

Again, Im a total noob even though I went through quite a lot of guides.

The following is what Im using:
Arduino AZDelivery Nano V3.0 CH340 Chip Klaar Gesoldeerd, Verbeterde Versie met USB-kabel, 100% Nano V3 compatibel Inclusief E-Book! : Amazon.nl: Elektronica
OLED Tzt Real Oled scherm 3.12 "256*64 25664 Dots Grafische Lcd Module Display Screen Lcm Screen SSD1322 Controller ondersteuning Spi|LCD Modules| - AliExpress

Yes, both temps, the date and time and preferrably ordered like below since Id fill most of de display that way. Is there an explaination on how the video owner set his hardware up? I googled a bit but couldnt find much.

My example of displaying the data on the OLED:
CPU 60C 09-02-22
GPU 65C 19:36:21

Could you try this loop in place of yours and tell us what you see?

void loop(void) 
{
  Serial.println("#S|T|[]#");
  delay(500);
  
  char c;
  if (Serial.available() > 0) {
    c = Serial.read();
    u8g2log.print(c);
  }
}

That's just one small change. but it should behave the same.

I am thinking you will need two serial ports, at least during development. Which makes this into more of a challenge.

It would be nice (or maybe necessary) to have two ports on the PC to exploit during development.

Do they can worms where you live?

a7

Thanks for the suggestion, there seems to be no change when I try this. I used >0 originally but changed it to >=8.

The code I posted above starts with printing the time, then again but on the 2nd line and again until 8 lines passed. (Like mentioned in #define U8LOG_WIDTH 8 and #define U8LOG_HEIGHT 8) However, its all printed character after charater. Id like there to be a time shown on the display, and have it updated every second or minute.

Now I changed >0 to >=8 because I was hoping the code would only update when 8 characters were updated like the time notation hh:mm:ss. It didnt change much though.

I dont see how it would be possible to have two ports available. Ofcourse, theres a couple on my PC but the arduino only has one.

About the worms, quite a specific question. I guess we do but I think im unaware of it. If that is being made here its probably only used for fishing. :slight_smile:

After some tedius searching and code copying/pasting I have a code that correctly shows time on the OLED. I dont know how and why, but it works for now.

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1322_NHD_256X64_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 5, /* dc=*/ 3, /* reset=*/ 4);

int serInLen = 25;    // Max length of string returned by Gobetwino
int pId;              // Id of process started by Gobetwino

char serInString[25]; // String returned by Gobetwino
char timeString[9];   // Time Stamp extracted from returned string

void setup() 
{
  Serial.begin(9600);

  u8g2.begin();


  

}

void loop() 
{

  Serial.println("#S|T|[]#"); 
  delay(500);

  readSerialString(serInString, 5 * 1000);
  u8g2.setFont(u8g2_font_ncenB14_tr);
  u8g2.setFlipMode(1);
  u8g2.firstPage();
  do {
    u8g2.setCursor(0, 20);
    u8g2.print(serInString);
  } while ( u8g2.nextPage() );
  delay(500);

}

void readSerialString (char *strArray, long timeOut)
{
  long startTime = millis();
  // In Mikael's original sketch, i is NOT initialised to 0
  // Nevertheless, it works! I don't understand why?
  // I have initialised i to 0 for tidiness.
  int i = 0;
  while (!Serial.available())
  {
    if (millis() - startTime >= timeOut)
    {
      return;
    }
  }
  while (Serial.available() && i < serInLen)
  {
    strArray[i] = Serial.read();
    i++;
  }
  strArray[i] = '\0'; // Mikael doesn't do this either.
}


This is my last project at the moment. I haven't decided whether to write a guide for him. I use AIDA64 to send data to Arduino (about 20 parameters) and Arduino displays the data on LCD display. If you are using the WeMos D1 mini module, you don't even need a display because the data is visible in any device with Wi-Fi and a browser - like this on my Android tablet.

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