Required preparation
hardware
One breadboard, Arduino Nano board, OLED screen of SSD1306 (I2C interface is used in this article), several wires
Library file
u8glib, U8g2 two libraries, if not, you can download them in CSDN.
Connection (hard IIC interface)
Arduino | YOU ARE |
---|---|
3V3 | VCC |
GND | GND |
A5 | SCL |
A4 | SDA |
Connection (soft IIC interface)
If I2C needs to be shared, the library of U8g2 can support ordinary IO as I2C interface to drive OLED. In this article, D10 is used as SCL interface and D11 is used as SDA interface.
Arduino | YOU ARE |
---|---|
3V3 | VCC |
GND | GND |
D10 (can be changed) | SCL |
D11 (can be changed) | SDA |
Hello word test
Based on u8glib (hard IIC interface)
In order to improve readability, irrelevant content has been deleted.
- Add the library file, open the arduino program, execute the following figure 1, 2, and 3 to find the downloaded library file.
- Execute the Demo program
Open the Arduino program and execute 1, 2, 3, 4.
If you want to continue the test and display the results, you can copy the following code to display three lines.
#include "U8glib.h"
/*I2C协议*/
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
void setup()
{
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(3); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
// u8g.setFont(u8g_font_unifont);
Serial.begin(9600);
u8g.setFont(u8g_font_osb18); /*设置字体大小*/
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
}
void loop()
{
u8g.firstPage();
do {
u8g.drawStr(0,0,"hello world!");
u8g.drawStr(0,20,"ha ha!");
u8g.drawStr(20,40,"well done!"); /*设置起始位置*/
} while( u8g.nextPage() );
delay(500);
}
Based on U8g2 (soft IIC interface)
#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_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 10, /* data=*/ 11, /* reset=*/ U8X8_PIN_NONE); // 设置D10做SCL,D11做SDA,可以改为其它接口
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 16, /* data=*/ 17); // 如果要用硬IIC接口,可以用这句
void setup(void) {
u8g2.begin();
}
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // 这里可以修改字体大小
u8g2.drawStr(0,10,"Hello World!"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
}
For more information, please refer to the relevant manual.