/*
* This Arduino Nano code was developed by newbiely.com
*
* This Arduino Nano code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano/arduino-nano-oled
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <U8g2lib.h>
#define ONE_WIRE_BUS A0
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
// Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define LCDHeight u8g2.getDisplayHeight()
#define LCDWidth u8g2.getDisplayWidth()
#define ALIGN_CENTER(t) ((LCDWidth - (u8g2.getUTF8Width(t))) / 2)
#define ALIGN_RIGHT(t) (LCDWidth - u8g2.getUTF8Width(t))
#define ALIGN_LEFT 0
unsigned int rows, columns; // in pixels
const int dry = 639; // value for dry sensor
const int wet = 271; // value for wet sensor
unsigned long interval=500; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
String text;
// 'logo1 kura hitam 29x32', 29x32px
const unsigned char my_bitmap_logo1_kura_hitam_29x32 [] PROGMEM = {
0x00, 0x1f, 0x00, 0x00, 0x80, 0x3f, 0x7c, 0x00, 0xc0, 0xfe, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x0f,
0xe0, 0xff, 0xf8, 0x0f, 0xe0, 0x1f, 0xe0, 0x1f, 0x80, 0x0c, 0xc0, 0x03, 0x00, 0x04, 0x80, 0x03,
0x00, 0x02, 0x00, 0x07, 0x80, 0x83, 0x07, 0x06, 0xe0, 0xc1, 0x07, 0x06, 0xf0, 0x41, 0x0c, 0x0e,
0xf8, 0x01, 0x0c, 0x0c, 0xfc, 0x01, 0x0c, 0x0c, 0xfc, 0x01, 0x0c, 0x0c, 0xbe, 0x01, 0x04, 0x0c,
0x3e, 0x03, 0x06, 0x0c, 0x1f, 0x07, 0x03, 0x0c, 0x0f, 0xfe, 0x01, 0x0c, 0x0f, 0xfe, 0x00, 0x0c,
0x03, 0x06, 0x00, 0x04, 0x03, 0x06, 0x00, 0x06, 0x00, 0x0e, 0x00, 0x06, 0x00, 0x0c, 0x00, 0x03,
0x00, 0x0c, 0x00, 0x03, 0x00, 0x0e, 0x80, 0x01, 0x00, 0x1f, 0xc0, 0x03, 0x80, 0x3f, 0xe0, 0x03,
0x80, 0x77, 0xb8, 0x07, 0xc0, 0xc3, 0x0f, 0x07, 0xc0, 0x01, 0x03, 0x04, 0x40, 0x00, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 144)
const int my_bitmap_allArray_LEN = 1;
const unsigned char* my_bitmap_allArray[1] = {
my_bitmap_logo1_kura_hitam_29x32
};
void setup() {
// Serial.begin(9600);
sensors.begin();
// initialize OLED display with address 0x3C for 128x64
// if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("SSD1306 allocation failed"));
// while (true);
// }
u8g2.begin();
u8g2.setFontMode(1);
u8g2.enableUTF8Print();
// oled.clearDisplay();
// oled.setTextSize(1); // text size
// oled.setTextColor(WHITE); // text color
// // oled.setCursor(0, 10); // position to display
// // oled.println("Initalizing..."); // text to display
// // oled.display(); // show on OLED
// oledDisplayCenter("Initalizing...");
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_7x14_tr); //u8g2_font_ncenB08_tr u8g2_font_helvB14_tf
u8g2.drawXBMP(0, 0, 29, 32, my_bitmap_logo1_kura_hitam_29x32);
u8g2.sendBuffer();
delay(2000); // wait for initializing
text = "Initalizing...";
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_7x14_tr); //u8g2_font_ncenB08_tr u8g2_font_helvB14_tf
u8g2.setCursor(ALIGN_CENTER(text.c_str()), LCDHeight / 2); // write something to the internal memory
u8g2.print(text);
u8g2.sendBuffer();
delay(2000); // wait for initializing
u8g2.clearBuffer();
}
void loop() {
unsigned long currentMillis = millis(); // grab current time
// check if "interval" time has passed (xxx milliseconds)
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
// oled.clearDisplay();
// oled.setTextSize(1);
// oledDisplayHorizontalCenter(5, "TEMPERATURE");
// oled.setTextSize(2);
// sensors.requestTemperatures();
// oledDisplayHorizontalCenter(oled.getCursorY()+5, String(sensors.getTempCByIndex(0)));
// // oled.setCursor(oled.getCursorX()+5, oled.getCursorY());
// oled.cp437(true);
// oled.write(167);
// oled.print("C");
// oled.display();
// oled.setTextSize(1);
// oledDisplayHorizontalCenter(oled.getCursorY()+10, "MOISTURE");
// int sensorVal = analogRead(A1);
// int percentageHumidity = map(sensorVal, wet, dry, 100, 0);
// oled.setTextSize(2);
// oledDisplayHorizontalCenter(oled.getCursorY()+5, String(percentageHumidity) + "%");
u8g2.firstPage();
do {
text = "TEMPERATURE";
u8g2.setFont(u8g2_font_6x10_tf );
u8g2.setCursor(ALIGN_CENTER(text.c_str()),10);
u8g2.print(text);
sensors.requestTemperatures();
text = String(sensors.getTempCByIndex(0));
u8g2.setFont(u8g2_font_10x20_tf );
u8g2.setCursor(ALIGN_CENTER(text.c_str()) - 10,(u8g2.getAscent()-u8g2.getDescent())+10);
u8g2.print(text);
u8g2.setFont(u8g2_font_6x10_tf );
u8g2.print(" ");
u8g2.setFont(u8g2_font_10x20_tf );
u8g2.print("\xb0");
u8g2.print("C");
text = "MOISTURE";
u8g2.setFont(u8g2_font_6x10_tf );
u8g2.setCursor(ALIGN_CENTER(text.c_str()),u8g2.getCursorY()+15);
u8g2.print(text);
int sensorVal = analogRead(A1);
int percentageHumidity = map(sensorVal, wet, dry, 100, 0);
text = String(percentageHumidity) + " %";
u8g2.setFont(u8g2_font_10x20_tf );
u8g2.setCursor(ALIGN_CENTER(text.c_str()), u8g2.getCursorY()+(u8g2.getAscent()-u8g2.getDescent()));
u8g2.print(text);
} while( u8g2.nextPage() );
// save the "current" time
previousMillis = millis();
} // end millis()
} // end loop()
// void oledDisplayHorizontalCenter(uint16_t rows, String text) {
// int16_t x1;
// int16_t y1;
// uint16_t width;
// uint16_t height;
// oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// // display on horizontal and vertical center
// // oled.clearDisplay(); // clear display
// oled.setCursor((OLED_WIDTH - width) / 2, rows);
// oled.println(text); // text to display
// oled.display();
// }
// void oledDisplayCenter(String text) {
// int16_t x1;
// int16_t y1;
// uint16_t width;
// uint16_t height;
// oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// // display on horizontal and vertical center
// // oled.clearDisplay(); // clear display
// oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2);
// oled.println(text); // text to display
// oled.display();
// }
Loading
ds18b20
ds18b20