Lm35 measurement error, analogRead(), oled spi display

I am using arduino uno, a lm35 connected to A1, and a 6 pin SPI oled display (no CS pin).
the lm35 works properly, tested with a multimeter.
but when I run the program the display and the serial monitor show an increase in the real value (5°C or more). When the display is turned off, the lm35 reading is correct again.
Using libraries u8g, adafruit or with no libraries same result.
Using analogread libraries same result.

I finally have the sketch like this, but it makes blink the display. Any suggestions to correct this?

/* Sketch para el display del tacometro de la skua */
static const byte F6x8Mem [] PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,   // sp     0
   0x00, 0x00, 0x00, 0x2f,  0x00, 0x00 ,   // !       1
   0x00, 0x00, 0x07, 0x00, 0x07, 0x00 ,   // "       2
   0x00, 0x14, 0x7f,  0x14, 0x7f, 0x14 ,   // #       3
   0x00, 0x24, 0x2a, 0x7f,  0x2a, 0x12 ,   // $      4 
   0x00, 0x62, 0x64, 0x08, 0x13, 0x23 ,   // %     5
   0x00, 0x36, 0x49, 0x55, 0x22, 0x50 ,   // &      6
   0x00, 0x00, 0x05, 0x03, 0x00, 0x00 ,   // '       7 
   0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 ,   // (      8 
   0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 ,   // )       9 
   0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 ,   // *    10
   0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 ,   // +    11
   0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 ,   // ,     12
   0x00, 0x08, 0x08, 0x08, 0x08, 0x08 ,   // -     13
   0x00, 0x00, 0x60, 0x60, 0x00, 0x00 ,   // .     14
   0x00, 0x20, 0x10, 0x08, 0x04, 0x02 ,   // /     15
   0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E ,   // 0    16
   0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 ,   // 1    17
   0x00, 0x42, 0x61, 0x51, 0x49, 0x46 ,   // 2    18
   0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 ,   // 3    19
   0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 ,   // 4    20
   0x00, 0x27, 0x45, 0x45, 0x45, 0x39 ,   // 5    21
   0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 ,   // 6    22
   0x00, 0x01, 0x71, 0x09, 0x05, 0x03 ,   // 7    23
   0x00, 0x36, 0x49, 0x49, 0x49, 0x36 ,   // 8    24
   0x00, 0x06, 0x49, 0x49, 0x29, 0x1E ,   // 9    25
   0x00, 0x00, 0x36, 0x36, 0x00, 0x00 ,   // :    26
   0x00, 0x00, 0x56, 0x36, 0x00, 0x00 ,   // ;    27
   0x00, 0x08, 0x14, 0x22, 0x41, 0x00 ,   // <    28
   0x00, 0x14, 0x14, 0x14, 0x14, 0x14 ,   // =    29
   0x00, 0x00, 0x41, 0x22, 0x14, 0x08 ,   // >    30
   0x00, 0x02, 0x01, 0x51, 0x09, 0x06 ,   // ?    31
   0x00, 0x32, 0x49, 0x59, 0x51, 0x3E ,   // @    32
   0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C ,   // A    33
   0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 ,   // B    34
   0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 ,   // C    35
   0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C ,   // D    36
   0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 ,   // E    37
   0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 ,   // F    38
   0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A ,   // G    39
   0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F ,   // H    40
   0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 ,   // I    41
   0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 ,   // J    42
   0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 ,   // K    43
   0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 ,   // L    44
   0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F ,   // M    45
   0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F ,   // N    46
   0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E ,   // O    47
   0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 ,   // P    48
   0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E ,   // Q    49
   0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 ,   // R    50
   0x00, 0x46, 0x49, 0x49, 0x49, 0x31 ,   // S    51
   0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 ,   // T    52
   0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F ,   // U    53
   0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F ,   // V    54
   0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F ,   // W    55
   0x00, 0x63, 0x14, 0x08, 0x14, 0x63 ,   // X    56
   0x00, 0x07, 0x08, 0x70, 0x08, 0x07 ,   // Y    57
   0x00, 0x61, 0x51, 0x49, 0x45, 0x43 ,   // Z    58
   0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 ,   // [     59
   0x00, 0x02, 0x04 ,0x08, 0x10, 0x20 ,   // \    60
   0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 ,   // ]    61
   0x00, 0x04, 0x02, 0x01, 0x02, 0x04 ,   // ^    62
   0x00, 0x40, 0x40, 0x40, 0x40, 0x40 ,   // _    63
   0x00, 0x00, 0x01, 0x02, 0x04, 0x00 ,   // '      64
   0x00, 0x20, 0x54, 0x54, 0x54, 0x78 ,   // a      65
   0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 ,   // b      66
   0x00, 0x38, 0x44, 0x44, 0x44, 0x20 ,   // c      67
   0x00, 0x38, 0x44, 0x44, 0x48, 0x7F ,   // d      68
   0x00, 0x38, 0x54, 0x54, 0x54, 0x18 ,   // e      69
   0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 ,   // f      70
   0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C ,   // g      71
   0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 ,   // h      72
   0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 ,   // i      73
   0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 ,   // j      74
   0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 ,   // k      75
   0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 ,   // l      76
   0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 ,   // m    77
   0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 ,   // n    78
   0x00, 0x38, 0x44, 0x44, 0x44, 0x38 ,   // o      79
   0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 ,   // p      80
   0x00, 0x18, 0x24, 0x24, 0x18, 0xFC ,   // q      81
   0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 ,   // r      82
   0x00, 0x48, 0x54, 0x54, 0x54, 0x20 ,   // s      83
   0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 ,   // t      84
   0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C ,   // u      85
   0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C ,   // v      86
   0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C ,   // w     87
   0x00, 0x44, 0x28, 0x10, 0x28, 0x44 ,   // x      88
   0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C ,   // y      89
   0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 ,   // z      90
   0x0C, 0x9E, 0xFF, 0x7F, 0x3E, 0x08 ,    // {      91  =>  punto / coma
   0x00, 0x00, 0x00, 0xfe,  0x00, 0x00 ,   // |       92
   0x14, 0x14, 0x14, 0x14, 0x14, 0x14     // }      93  =>  horiz lines  
};

#define SCL_PIN     13 
#define SDA_PIN     11
#define RST_PIN     10
#define DC_PIN        9
#define compDef   2     // Compensa defazaje del display oled

#define lm35PIN                A1
//#include <ResponsiveAnalogRead.h>
//ResponsiveAnalogRead lm35(lm35PIN, true);
//#include "AnalogPin.h"
//AnalogPin INA(lm35PIN);
unsigned long tiempolm35 = 0;

void setup() {
      LEDPIN_Init();
      LED_Init();

     Serial.begin(115200);  

//      pinMode(lm35PIN,INPUT);
//      INA.setNoiseThreshold(10);
//      INA.setPrescaler(2);
      
      tiempolm35 = millis();
}

void loop() {

  if ( (tiempolm35 + 500) <= millis() ) {
    unsigned int ValSen = 0;
    //    LED_Fill(0x00);
    Set_Display_On_Off(0x00);
    
/*    lm35.update();
     for (int i = 0; i < 100; i++)  {
       ValSen = lm35.getValue(); 
     }   */
     
/*     for (int i = 0; i < 100; i++)  {
        ValSen += INA.read();
     }
*/
     for (int i = 0; i < 100; i++)  {
        ValSen += analogRead(lm35PIN);
     }
     
     ValSen = ValSen / 100;
     ValSen = ( ValSen * 500 ) / 1023;
     
   Set_Display_On_Off(0x01); 
     LED_P6x8CharMem(10,2,'T');
     LED_P6x8CharMem(18,2,'E');
     LED_P6x8CharMem(26,2,'M');
     LED_P6x8CharMem(34,2,'P');

     LED_P6x8CharMem(50,2,(((ValSen / 100) % 10) + 48));
     LED_P6x8CharMem(58,2,(((ValSen / 10) % 10) + 48));
     LED_P6x8CharMem(66,2,((ValSen % 10) + 48));
     
               Serial.print ("\nTemp = ");
               Serial.println (ValSen);
      
     tiempolm35 = millis();
  }
}

void LEDPIN_Init(void)  {
  pinMode(SCL_PIN,OUTPUT);
  pinMode(SDA_PIN,OUTPUT);
  pinMode(RST_PIN,OUTPUT);
  pinMode(DC_PIN,OUTPUT);
}

void LED_Init(void)  {
  unsigned char i;
  digitalWrite(SCL_PIN,HIGH);
  digitalWrite(RST_PIN,LOW);
  LED_DLY_ms(50);
  digitalWrite(RST_PIN,HIGH);
  Set_Display_On_Off(0x00);       // Display Off (0x00/0x01)
  Set_Segment_Remap(0x01);      // Set SEG/Column Mapping (0x00) (0x01)            // espejado o derecho
  Set_Common_Remap(0x08);     // Set COM/Row Scan Direction (0x00) (0x08)        // invertido (arriba abajo) o derecho
  SetContrastControl(0xAF);        // Set SEG Output Current                                      // brillo  0x0F a 0xFF
  Set_Display_On_Off(0x01);       // Display On (0x00/0x01)
  LED_Fill(0x00);                          //clear all
  LED_Set_Pos(0,0);
}

//################## inicio configuracion del display ######################

void LED_DLY_ms(unsigned int ms)  {
  unsigned int a;
  while(ms)  {
    a=6675;
    while(a--);
    ms--;
  }
  return;
}

void Set_Display_On_Off(unsigned char d)  {
  LED_WrCmd(0xAE|d);    // Set Display On/Off
              // Default => 0xAE
              // 0xAE (0x00) => Display Off
              // 0xAF (0x01) => Display On
}

void Set_Segment_Remap(unsigned char d)  {
  LED_WrCmd(0xA0|d);    // Set Segment Re-Map
              // Default => 0xA0
              // 0xA0 (0x00) => Column Address 0 Mapped to SEG0
              // 0xA1 (0x01) => Column Address 0 Mapped to SEG127
}

void Set_Common_Remap(unsigned char d)  {
  LED_WrCmd(0xC0|d);    // Set COM Output Scan Direction
              // Default => 0xC0
              // 0xC0 (0x00) => Scan from COM0 to 63
              // 0xC8 (0x08) => Scan from COM63 to 0
}

void SetContrastControl(unsigned char d)  {
  LED_WrCmd(0x81);      // Set Contrast Control
  LED_WrCmd(d);     // Default => 0x7F
}

//################## fin configuracion del display ######################

void LED_Fill(unsigned char bmp_data)  {
  unsigned char y,x;
  for(y=0;y<8;y++)  {
        LED_WrCmd(0xb0+y);
        LED_WrCmd(0x10);
        LED_WrCmd(0x00);
        for( x=0; x<( 128 + compDef); x++ )  {  // 128 + compensa el defasaje
              LED_WrDat(bmp_data);
  }   }
}

void LED_Set_Pos(unsigned char x, unsigned char y)  {
  x = x + compDef;                                         // posicion + compensa el defasaje
  LED_WrCmd( 0xb0 + y );                             //      (1011) "0000" el dato
  LED_WrCmd( ( ( x & 0xf0 )>>4 ) | 0x10 );   //       0010 0110   toma el mas signif    (0001) 0010
  LED_WrCmd( ( x & 0x0f ) | 0x00 );              //       0010 0110   toma el menos sign  (0000) 0110
}

void LED_WrCmd(unsigned char cmd)  {
  unsigned char i = 8;
  digitalWrite(DC_PIN,LOW);
  digitalWrite(SCL_PIN,LOW);
  while (i--)  {
    if (cmd & 0x80)  {
      digitalWrite(SDA_PIN,HIGH);
    } else {
      digitalWrite(SDA_PIN,LOW);
    }
    digitalWrite(SCL_PIN,HIGH);
    asm("nop");
    digitalWrite(SCL_PIN,LOW);
    cmd <<= 1;
  }
}

void LED_WrDat(unsigned char data)  {
  unsigned char i = 8;
  digitalWrite(DC_PIN,HIGH);
  digitalWrite(SCL_PIN,LOW);
  while (i--)  {
    if (data & 0x80)  {
      digitalWrite(SDA_PIN,HIGH);
    } else {
      digitalWrite(SDA_PIN,LOW);
    }
    digitalWrite(SCL_PIN,HIGH);
    asm("nop");
    digitalWrite(SCL_PIN,LOW);
    data <<= 1;
  }
}

void LED_P6x8CharMem(unsigned char x,unsigned char y,unsigned char ch)  {
  unsigned char c=0, i=0;
  c = ch - 32;  // posicion del vector de bytes a leer
  if ( x > 122 )  {
    x=0;
    y++;
  }
  LED_Set_Pos(x,y);
  for ( i=0; i<6; i++ )  {
    byte byteleido = pgm_read_byte ( F6x8Mem + ( (c*6) + i ) );  // recupera dato de progmem
    LED_WrDat( byteleido );
  }
}

This is not how you do unsigned integer math

  if ( (millis() - tiempolm35  >= 500) {

and

    unsigned int ValSen = 0;
 ...
     for (int i = 0; i < 100; i++)  {
        ValSen += analogRead(lm35PIN);
     }

analogRead() can produce 1023 and 1023 * 100 = 102300 which is larger than an unsigned int on a Uno. Better use unsigned long.

     ValSen = ( ValSen * 500 ) / 1023;

More potential overflow...

hello blh64.
thank you, i'll correct math and ValSen type

You may want to read this https://arduinoplusplus.wordpress.com/2018/02/04/reading-lm34-lm35-lm335-lm3x-temperature-sensors-accurately/

The extra load on the power supply when the the display is working may be dropping the voltage enough to cause the error because you are assuming that it is exactly 5V in your calculation.

Thank you marco_c
according to my multimeter, the consumption of the display lowers the voltage and "confuses" the DAC.
I will make the modifications and upload the results.

I tried both methods with the same result.
Another arduino board, new display i2c, same result.
I solve the wrong reading by applying subtraction to the result, but it is not reliable at all

Could I use an external voltage source?
How do I wire it?

/* Oled 128 x 64 i2c + lm35*/
static const byte F6x8Mem [] PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,   // sp     0
   0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 ,   // !       1
   0x00, 0x00, 0x07, 0x00, 0x07, 0x00 ,   // "       2
   0x00, 0x14, 0x7f,  0x14, 0x7f, 0x14 ,   // #       3
   0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 ,   // $      4 
   0x00, 0x62, 0x64, 0x08, 0x13, 0x23 ,   // %     5
   0x00, 0x36, 0x49, 0x55, 0x22, 0x50 ,   // &      6
   0x00, 0x00, 0x05, 0x03, 0x00, 0x00 ,   // '       7 
   0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 ,   // (      8 
   0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 ,   // )       9 
   0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 ,   // *    10
   0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 ,   // +    11
   0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 ,   // ,     12
   0x00, 0x08, 0x08, 0x08, 0x08, 0x08 ,   // -     13
   0x00, 0x00, 0x60, 0x60, 0x00, 0x00 ,   // .     14
   0x00, 0x20, 0x10, 0x08, 0x04, 0x02 ,   // /     15
   0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E ,   // 0    16
   0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 ,   // 1    17
   0x00, 0x42, 0x61, 0x51, 0x49, 0x46 ,   // 2    18
   0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 ,   // 3    19
   0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 ,   // 4    20
   0x00, 0x27, 0x45, 0x45, 0x45, 0x39 ,   // 5    21
   0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 ,   // 6    22
   0x00, 0x01, 0x71, 0x09, 0x05, 0x03 ,   // 7    23
   0x00, 0x36, 0x49, 0x49, 0x49, 0x36 ,   // 8    24
   0x00, 0x06, 0x49, 0x49, 0x29, 0x1E ,   // 9    25
   0x00, 0x00, 0x36, 0x36, 0x00, 0x00 ,   // :    26
   0x00, 0x00, 0x56, 0x36, 0x00, 0x00 ,   // ;    27
   0x00, 0x08, 0x14, 0x22, 0x41, 0x00 ,   // <    28
   0x00, 0x14, 0x14, 0x14, 0x14, 0x14 ,   // =    29
   0x00, 0x00, 0x41, 0x22, 0x14, 0x08 ,   // >    30
   0x00, 0x02, 0x01, 0x51, 0x09, 0x06 ,   // ?    31
   0x00, 0x32, 0x49, 0x59, 0x51, 0x3E ,   // @    32
   0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C ,   // A    33
   0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 ,   // B    34
   0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 ,   // C    35
   0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C ,   // D    36
   0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 ,   // E    37
   0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 ,   // F    38
   0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A ,   // G    39
   0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F ,   // H    40
   0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 ,   // I    41
   0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 ,   // J    42
   0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 ,   // K    43
   0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 ,   // L    44
   0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F ,   // M    45
   0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F ,   // N    46
   0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E ,   // O    47
   0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 ,   // P    48
   0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E ,   // Q    49
   0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 ,   // R    50
   0x00, 0x46, 0x49, 0x49, 0x49, 0x31 ,   // S    51
   0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 ,   // T    52
   0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F ,   // U    53
   0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F ,   // V    54
   0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F ,   // W    55
   0x00, 0x63, 0x14, 0x08, 0x14, 0x63 ,   // X    56
   0x00, 0x07, 0x08, 0x70, 0x08, 0x07 ,   // Y    57
   0x00, 0x61, 0x51, 0x49, 0x45, 0x43 ,   // Z    58
   0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 ,   // [     59
   0x00, 0x02, 0x04 ,0x08, 0x10, 0x20 ,   // \    60
   0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 ,   // ]    61
   0x00, 0x04, 0x02, 0x01, 0x02, 0x04 ,   // ^    62
   0x00, 0x40, 0x40, 0x40, 0x40, 0x40 ,   // _    63
   0x00, 0x00, 0x01, 0x02, 0x04, 0x00 ,   // '      64
   0x00, 0x20, 0x54, 0x54, 0x54, 0x78 ,   // a      65
   0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 ,   // b      66
   0x00, 0x38, 0x44, 0x44, 0x44, 0x20 ,   // c      67
   0x00, 0x38, 0x44, 0x44, 0x48, 0x7F ,   // d      68
   0x00, 0x38, 0x54, 0x54, 0x54, 0x18 ,   // e      69
   0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 ,   // f      70
   0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C ,   // g      71
   0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 ,   // h      72
   0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 ,   // i      73
   0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 ,   // j      74
   0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 ,   // k      75
   0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 ,   // l      76
   0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 ,   // m    77
   0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 ,   // n    78
   0x00, 0x38, 0x44, 0x44, 0x44, 0x38 ,   // o      79
   0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 ,   // p      80
   0x00, 0x18, 0x24, 0x24, 0x18, 0xFC ,   // q      81
   0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 ,   // r      82
   0x00, 0x48, 0x54, 0x54, 0x54, 0x20 ,   // s      83
   0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 ,   // t      84
   0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C ,   // u      85
   0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C ,   // v      86
   0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C ,   // w     87
   0x00, 0x44, 0x28, 0x10, 0x28, 0x44 ,   // x      88
   0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C ,   // y      89
   0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 ,   // z      90
   0x0C, 0x9E, 0xFF, 0x7F, 0x3E, 0x08 ,    // {      91  =>  punto / coma
   0x00, 0x00, 0x00, 0xfe,  0x00, 0x00 ,   // |       92
   0x14, 0x14, 0x14, 0x14, 0x14, 0x14     // }      93  =>  horiz lines  
};

#include <Wire.h>
byte direccion = 0x3C;  // (0x3c=OLED ADDRESS)

#define compDef     2   // Compensa defazaje del display oled
#define lm35PIN    A1
unsigned long tiempolm35 = 0;
unsigned int perdidaTension = 0;

void setup() {
  initialize_OLED(); //Initialize screen
  Serial.begin(9600); //Set baud for serial transmission
//  analogReference(EXTERNAL); // Wiring lm35(vcc) to aref(pin)
  analogReference(INTERNAL);

  LED_WrCmd(0xAE);  // Pantalla apagada
  perdidaTension = lecturalm35();
  delay(50);

  LED_WrCmd(0xAF);

  LED_Fill(0x0F);  // Pantalla 50%
  perdidaTension = lecturalm35() - perdidaTension;
  delay(50);

  LED_Fill(0xFF);  // Pantalla 100%
  lecturalm35();
  delay(50);

  tiempolm35 = millis();
}

void loop() {
  if ( millis() - tiempolm35 >= 500 ) {
     unsigned long ValSen = 0;
     LED_WrCmd(0xAE);  // Display off
     for (int i = 0; i < 100; i++)  {
        ValSen += analogRead(lm35PIN);
     }
     LED_WrCmd(0xAF);  // Display on
     ValSen = ValSen / 100;
               Serial.print ("\t\tDato sin pantalla = ");
               Serial.println (ValSen);

     for (int i = 0; i < 100; i++)  {
        ValSen += analogRead(lm35PIN);
     }
     ValSen = ValSen / 100;
               Serial.print ("\t\tDato con pantalla = ");
               Serial.println (ValSen);
     
     ValSen = ( ValSen * 1100 ) / 1023;
     
     LED_P6x8CharMem(10,2,'T');
     LED_P6x8CharMem(18,2,'E');
     LED_P6x8CharMem(26,2,'M');
     LED_P6x8CharMem(34,2,'P');

     LED_P6x8CharMem(50,2,(((ValSen / 100) % 10) + 48));
     LED_P6x8CharMem(58,2,(((ValSen / 10) % 10) + 48));
     LED_P6x8CharMem(66,2,((ValSen % 10) + 48));
     tiempolm35 = millis();
  }
}
 
void initialize_OLED(){
  Wire.begin();   //Initialize I2C interface
  delay(10);       //Wait 100 ms
  Wire.beginTransmission(direccion); // Start communication with slave
  Wire.write(0x00); //Command stream
  Wire.write(0xAE); //Set display Off
  Wire.write(0xA1); //Set segment re-map 
  Wire.write(0xC8); //Set COM output scan direction
  Wire.write(0x81); Wire.write(0xAF);  //Set contrast control
  Wire.write(0xAF); //Set display on
  Wire.endTransmission(); //End communicaiton with slave
}

void LED_Fill(unsigned char bmp_data)  {
  unsigned char y,x;
  for(y=0;y<8;y++)  {
        LED_WrCmd(0xb0+y);
        LED_WrCmd(0x10);
        LED_WrCmd(0x00);
        for( x=0; x<( 128 + 2); x++ )  {  // 128 + compensa el defasaje
              LED_WrDat(bmp_data);
  }   }
}

void LED_Set_Pos(unsigned char x, unsigned char y)  {
  x = x + compDef;                                         // posicion + compensa el defasaje
  LED_WrCmd( 0xb0 + y );                             //      (1011) "0000" el dato
  LED_WrCmd( ( ( x & 0xf0 )>>4 ) | 0x10 );   //       0010 0110   toma el mas signif    (0001) 0010
  LED_WrCmd( ( x & 0x0f ) | 0x00 );              //       0010 0110   toma el menos sign  (0000) 0110
}

void LED_WrCmd(unsigned char cmd)  {
  Wire.beginTransmission(direccion);   // START I2C COMMUNICATION WITH OLED 
  Wire.write(0x00);                          // WRITE I2C COMMAND WORD TO OLED
  Wire.write(cmd);                           // SEND I2C VALUE TO OLED 
  Wire.endTransmission();                 // STOP I2C COMMUNICATION
}

void LED_WrDat(unsigned char data)  {
  Wire.beginTransmission(direccion);   // START I2C COMMUNICATION WITH OLED
  Wire.write(0x40);                          //Data stream
  Wire.write(data);                          // SEND I2C VALUE TO OLED 
  Wire.endTransmission();                 // STOP I2C COMMUNICATION
}

void LED_P6x8CharMem(unsigned char x,unsigned char y,unsigned char ch)  {
  unsigned char c=0, i=0;
  c = ch - 32;  // posicion del vector de bytes a leer
  if ( x > 122 )  {
    x=0;
    y++;
  }
  LED_Set_Pos(x,y);
  for ( i=0; i<6; i++ )  {
    byte byteleido = pgm_read_byte ( F6x8Mem + ( (c*6) + i ) );  // recupera dato de progmem
    LED_WrDat( byteleido );
  }
}

int lecturalm35() {
     unsigned long ValSen = 0;
     analogRead(lm35PIN);
     analogRead(lm35PIN);
     for (int i = 0; i < 100; i++)  {
        ValSen += analogRead(lm35PIN);
     }
     ValSen = ValSen / 100;
        Serial.print ("\t\tLectura = ");  Serial.println (ValSen);
     return ValSen;
}

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