128x64 OLED code issues

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
giantpt
 
Posts: 11
Joined: Thu Feb 28, 2013 7:45 pm

128x64 OLED code issues

Post by giantpt »

Hello,

I am new around here and with arduino language.

Now I am trying to "build" a thermometer using an arduino nano, a temperature + humidity sensor and an Adafruit 1.3" 128x64 OLED graphic display.

My only problem with this project is how to print the temperature and humidity values into the display.

the variable that I receive from the sensor are:

t = temperature
h = humidity

these variables are float decimal numbers, so, when I try to add "t" and "h" into the oled, I get characters instead of numbers.

oled.drawchar(90, 4, t);

I have also tried with the command oled.drawstring but it doesn't work either.. OR... I am not realising how to do it, which is the most probable thing.

I would like to get some help on how to convert the float decimal values into char OR a way to print this float in a recognisable way for the oled display.


The entire code is posted bellow.

Thank you all


Code: Select all

 // --------------------  Thermometer DHT  -----------------------
 
  #include "DHT.h"
  #define DHTPIN 8            // what pin where the sensor is connected to
  #define DHTTYPE DHT11       // DHT 11 
  DHT dht(DHTPIN, DHTTYPE);
 
 
 // ----------   oLed   -------------------
 
  #define OLED_DC 11
  #define OLED_CS 12
  #define OLED_CLK 10
  #define OLED_MOSI 9
  #define OLED_RESET 13
 
  #include <SSD1306.h>
 
  SSD1306 oled(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
 
 
//-------------------------  SETUP  ---------------------------

   void setup() {
     
   oled.ssd1306_init(SSD1306_SWITCHCAPVCC);
     
   oled.display(); // show splashscreen
   delay(1000);
 
   Serial.begin(9600);
   dht.begin();
   pinMode(6, OUTPUT);  // Temperature warning LED ( RED ) - Dout 6
   pinMode(7, OUTPUT);  // Humidity warning LED  ( BLUE )  - Dout 7
} 

//--------------------------  LOOP  ----------------------------

   void loop() {
     
 
    
    //-------------  Sensor conf.  ---------------
     
     float h = dht.readHumidity();
     float t = dht.readTemperature();
   
   
   //--------------Sensor error condition  ---------------
   
   if (isnan(t) || isnan(h))  {
    
     oled.clear();   // clears the screen and buffer
    
   oled.drawstring(18, 1, "Weather Station"); 
   oled.drawstring(27, 4, "Sensor error");
   oled.drawstring(15, 5, "Check Connection");
   oled.display(); 
   
   
   delay(2000);
    }
    
   //-------------  Sensor information on oLed  -------------- 
    
        else {
      
           oled.clear();   // clears the screen and buffer
           
           oled.drawstring(18, 1, "Weather Station"); 
           oled.drawstring(45, 2, "Tavira");
           
           oled.drawstring(15, 4, "Temperature:");
           oled.drawchar(90, 4, t);
           oled.drawstring(15, 5, "Humidity   :");
           oled.drawchar(90, 5, h);
           oled.drawstring(15, 6, "Preassure  :");
           oled.drawstring(90, 6, "(p)");
           
           oled.display(); 
           delay(3000);
       }
       }
       

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: 128x64 OLED code issues

Post by pburgess »

Rather that drawString(), which is sort of a deprecated function, use setCursor and the print/println functions (which should handle all variable types now), i.e.:

Code: Select all

  oled.setCursor(0, 0);
  oled.println("Hello World!");
  float t = 42.0;
  oled.println(t);

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: 128x64 OLED code issues

Post by adafruit_support_rick »

Is this a Nano 3.0/ATmega328, or a Nano 2.x/ATmega168? The ATmega168 does not have enough SRAM to support the 128X64 OLED.

giantpt
 
Posts: 11
Joined: Thu Feb 28, 2013 7:45 pm

Re: 128x64 OLED code issues

Post by giantpt »

Hi...

It is the 3.0

Now its running well, after having had some help froma a friend of mine...

It is measuring the temperature, humidity and air pressure in mBar.

you can see what is done bellow... as well as all the hardware used.

If someone has better suggestions to improve the code, will be welcome.


Cheers


Code: Select all

/* 
   Temperature + Humidity Sensor with information printed in a SPI 128x64 OLED
   Including two warning leds, one for high humidity and a seccond on with 3 steps of
   high temperature warning.
   
   Blue LED - High humidity detected (>= 70%) - 2 blinks every 3 secconds
   Red LED - High temperature warning:
       - 1 blink every 3 secconds = temperature higher than 30ºC
       - 2 blink every 3 secconds = temperature higher than 33ºC
       - 3 blink every 3 secconds = temperature higher than 37ºC
  
   Connect pin 1 (on the left) of the sensor to +5V
   Connect pin 2 of the sensor to whatever your DHTPIN is
   Connect pin 4 (on the right) of the sensor to GROUND
   Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
   
   Connect oled DC to D11
   Connect oled CS to D12
   Connect oled CLK to D10
   Connect oled MOSI to D09
   Connect oled RESET to D13
   
   Barometer SDA and SCL are connected to pins A4 and A5
    PAY ATTENTION!!!!  BOAR SENSOR IS +3V
    If you connect it to +5v you will burn it
    
    Material needed:
    1x Arduino nano v.3
    1x Adafruit 128x64 1.3" oled display
    1x DHT sensor
    1x BMPo85 barometric sensor (I2C)
    2x resistor (for leds)
    1x Blue led
    1x Red led
    
    Have fun!!!!
*/
  
 // --------------------  Thermometer DHT  -----------------------
 
  #include "DHT.h"
  #define DHTPIN 8            // what pin where the sensor is connected to
  #define DHTTYPE DHT11       // DHT 11 
  DHT dht(DHTPIN, DHTTYPE);
  
  int temp_int;
  int hum_int;
  int press_int1;
  int press_int2;
     
 // ---------------------  Baro  -----------------------------
 
 #include <Wire.h>
 #define BMP085_ADDRESS 0x77  // I2C address of BMP085
 
 const unsigned char OSS = 0;  // Oversampling Setting
 
 // Calibration values
int ac1;
int ac2; 
int ac3; 
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1; 
int b2;
int mb;
int mc;
int md;

// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5; 

short temperature;
long pressure;
 
 
 // ----------   oLed   -------------------
 
  #define OLED_DC 11
  #define OLED_CS 12
  #define OLED_CLK 10
  #define OLED_MOSI 9
  #define OLED_RESET 13
 
  #include <SSD1306.h>
 
  SSD1306 oled(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
 


//-------------------------  SETUP  ---------------------------

   void setup() {
     
   oled.ssd1306_init(SSD1306_SWITCHCAPVCC);
     
   oled.display(); // show splashscreen
   delay(1000);
 
   Serial.begin(9600);
   Wire.begin();
   bmp085Calibration();
   dht.begin();
   pinMode(6, OUTPUT);  // Temperature warning LED ( RED ) - Dout 6
   pinMode(7, OUTPUT);  // Humidity warning LED  ( BLUE )  - Dout 7
} 

//--------------------------  LOOP  ----------------------------

   void loop() {
     
   // --------------- Baro ------------------------  
     
 temperature = bmp085GetTemperature(bmp085ReadUT());
  pressure = bmp085GetPressure(bmp085ReadUP());
  Serial.print("Temperature: ");
  Serial.print((temperature / 10), DEC);
  Serial.println(" C");
  Serial.print("Pressure: ");
  Serial.print(pressure, DEC);
  Serial.println(" Pa");
  Serial.println();
      
    //-------------  temp + hum sensor conf.  ---------------
     
     float h = dht.readHumidity();
     float t = dht.readTemperature();
   
   //--------------Sensor error condition on screen  ---------------
   
   if (isnan(t) || isnan(h))  {
    
     oled.clear();   // clears the screen and buffer
    
   oled.drawstring(18, 1, "Weather Station"); 
   oled.drawstring(27, 4, "Sensor error");
   oled.drawstring(15, 5, "Check Connection");
   oled.display(); 
   
   
   delay(2000);
    }
    
   //-------------  Sensors information on oLed  -------------- 
    
        else {
      
           oled.clear();   // clears the screen and buffer
           
           oled.drawstring(19, 1, "Weather Station"); 
           oled.drawstring(45, 2, "Tavira");            // Write the name of your city here
           
        oled.drawstring(17, 4, "Temperature:");
    temp_int = (int)(t);           // converter para int
    if(temp_int < 0){              // temperatura negativa
      oled.drawchar( 92, 4, '-');  // imprimir o sinal de menos
      temp_int = -temp_int;        // tornar temp positiva
    }
    else{
      oled.drawchar( 92, 4, ' ');
    }
    oled.drawchar( 92, 4, 48 + temp_int/10);
    oled.drawchar(97, 4, 48 + temp_int%10);
    oled.drawstring(102, 4, " C");
    
  
    //--------  Humidity -----------
  
     oled.drawstring(17, 5, "Humidity   :");
     hum_int = (int)(h);           // converter para int
    {
      oled.drawchar( 92, 5, ' ');
    }
    oled.drawchar( 92, 5, 48 + hum_int/10);
    oled.drawchar(97, 5, 48 + hum_int%10);
    oled.drawstring(102, 5, " %");    
    
    //--------  Air pressure -----------
    
    oled.drawstring(17, 6, "Pressure :");
    press_int1 = (int)(pressure / 10000);           // converter para int
    press_int2 = (int)((pressure / 100) - 1000);  
           
    {
      oled.drawchar(82, 6, '    ');
    }
    oled.drawchar(82, 6, 48 + press_int1/10);
    oled.drawchar(87, 6, 48 + press_int1%10);
    
    oled.drawchar(92, 6, 48 + press_int2/10);
    oled.drawchar(97, 6, 48 + press_int2%10);
    oled.drawstring(102, 6, " mB");
    
    //-----------  Graphic frame  ---------------
    
    oled.drawline(0, 4, 126, 4, 1);          // Top line
    oled.drawchar(0, 0, 213);                // Top Left corner
    oled.drawchar(121, 0, 182);              // Top Right corner
    oled.drawchar(0, 1, 185);
    oled.drawchar(121, 1, 185);
    oled.drawchar(0, 2, 185);
    oled.drawchar(121, 2, 185);
    
    oled.drawline(0, 28, 126, 28, 1);        // Center line
    oled.drawchar(0, 3, 198);                // Center Left Edge
    oled.drawchar(121, 3, 181);              // Center Right Edge
    
    oled.drawchar(0, 4, 185);
    oled.drawchar(121, 4, 185);
    oled.drawchar(0, 5, 185);
    oled.drawchar(121, 5, 185);
    oled.drawchar(0, 6, 185);
    oled.drawchar(121, 6, 185);
    
    
    oled.drawline(0, 60, 121, 60, 1);        // Bottom line
    oled.drawchar(0, 7, 210);                // Bottom Left Corner
    oled.drawchar(121, 7, 188);              // Bottom Right corner
    
    oled.display();
    
    delay(3000);   
       }
    
       
    //------------  Sensor LEDs status conditions  ---------------   
    
       //---------  Humidity  --------------
  
    if (h >=70.00) {
      
    digitalWrite(7, HIGH);
    delay(100);
    
    digitalWrite(7, LOW);
    delay(100);
    
    digitalWrite(7, HIGH);
    delay(100);
    
    digitalWrite(7, LOW);
  } 

  
     else {
     digitalWrite(7, LOW);
  }
  
       //------------  Temperature Warning  -------------------
  
    if (t >=30.00 && t <33.00) {
   
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
   
    } 
    else {
    digitalWrite(6, LOW);
  }
    if (t >=33.00 && t <37.00) {
   
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
    delay(100);
    
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
  
    } 
    else {
    digitalWrite(6, LOW);
  }
    if (t >=37.00) {
   
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
    delay(100);
    
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
    delay(100);
    
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
    delay(100);
    
    digitalWrite(6, HIGH);
    delay(100);
    
    digitalWrite(6, LOW);
    
    }
    else {
    digitalWrite(6, LOW);
  }
  
  }
  
  
  // ----------------  VOID BMP085Caçibration ---------------------------
  
  
  // Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
  ac1 = bmp085ReadInt(0xAA);
  ac2 = bmp085ReadInt(0xAC);
  ac3 = bmp085ReadInt(0xAE);
  ac4 = bmp085ReadInt(0xB0);
  ac5 = bmp085ReadInt(0xB2);
  ac6 = bmp085ReadInt(0xB4);
  b1 = bmp085ReadInt(0xB6);
  b2 = bmp085ReadInt(0xB8);
  mb = bmp085ReadInt(0xBA);
  mc = bmp085ReadInt(0xBC);
  md = bmp085ReadInt(0xBE);
}

// Calculate temperature given ut.
// Value returned will be in units of 0.1 deg C
short bmp085GetTemperature(unsigned int ut)
{
  long x1, x2;
  
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;

  return ((b5 + 8)>>4);  
}

// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
  
  b6 = b5 - 4000;
  // Calculate B3
  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  
  // Calculate B4
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
    
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
  
  return p;
}

// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
  unsigned char data;
  
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
  
  Wire.requestFrom(BMP085_ADDRESS, 1);
  while(!Wire.available())
    ;
    
  return Wire.read();
}

// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
  unsigned char msb, lsb;
  
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
  
  Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  
  return (int) msb<<8 | lsb;
}

// Read the uncompensated temperature value
unsigned int bmp085ReadUT()
{
  unsigned int ut;
  
  // Write 0x2E into Register 0xF4
  // This requests a temperature reading
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x2E);
  Wire.endTransmission();
  
  // Wait at least 4.5ms
  delay(5);
  
  // Read two bytes from registers 0xF6 and 0xF7
  ut = bmp085ReadInt(0xF6);
  return ut;
}

// Read the uncompensated pressure value
unsigned long bmp085ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
  
  // Write 0x34+(OSS<<6) into register 0xF4
  // Request a pressure reading w/ oversampling setting
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x34 + (OSS<<6));
  Wire.endTransmission();
  
  // Wait for conversion, delay time dependent on OSS
  delay(2 + (3<<OSS));
  
  // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(BMP085_ADDRESS, 3);
  
  // Wait for data to become available
  while(Wire.available() < 3)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
  
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  
  return up;
}
Attachments
IMG_0300.JPG
IMG_0300.JPG (358.57 KiB) Viewed 3962 times

giantpt
 
Posts: 11
Joined: Thu Feb 28, 2013 7:45 pm

Re: 128x64 OLED code issues

Post by giantpt »

Hi,
oled.setCursor(0, 0);
oled.println("Hello World!");
float t = 42.0;
oled.println(t);
oled.setcursor and oled.println are not supported by the SSD1306.h library

If you know a way to use it let me know.

Thank you

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: 128x64 OLED code issues

Post by adafruit_support_rick »

giantpt wrote:oled.setcursor and oled.println are not supported by the SSD1306.h library
Actually, they are supported. They are inherited from the Adafruit_GFX and Print libraries. Are they not working for you?

giantpt
 
Posts: 11
Joined: Thu Feb 28, 2013 7:45 pm

Re: 128x64 OLED code issues

Post by giantpt »

No, I've tried but it doesn't compile, saying that the library doesn't support it

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: 128x64 OLED code issues

Post by pburgess »

Make sure you've updated to the latest versions of both libraries. It should handle this.

giantpt
 
Posts: 11
Joined: Thu Feb 28, 2013 7:45 pm

Re: 128x64 OLED code issues

Post by giantpt »

Alright!!! That might be the problem because I am not using the Adafruit libraries... the libraries I have were found in the net..

I will try with the Adafruit ones

User avatar
sjn3
 
Posts: 15
Joined: Fri Nov 19, 2010 12:36 pm

Re: 128x64 OLED

Post by sjn3 »

A slightly off-topic question to those of you using the OLED (either size): is it pretty bright in the dark? I'd like to throw a lens in front of one to project on the ceiling in a dark bedroom (so: completely dark-adapted eyes) - maybe time/temp on-command (clap on/off) and definitely caller ID info so we can see without flipping on lights if it's worth answering. Thanks for any info.

giantpt
 
Posts: 11
Joined: Thu Feb 28, 2013 7:45 pm

Re: 128x64 OLED code issues

Post by giantpt »

Hi,

Well, it is bright but I don't believe it is bright enough to project the info in you roof at night...

You would need a more powerful light source for it

Unfortunately I don't have a specific lens to test it... you are free to try it.

I remember that, a lot of years ago, when the camcorders were in a 8mm tapes and later in Hi8, I have tried to do a similar thing with the camera's viewfinder (which is much brighter than this oled) and the results were very poor.

I hope I have answered your question

Cheers

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”