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 );
}
}