fun with oled display and arduino

by:LCD Mall     2020-08-06
You must have heard of OLED display technology.
It is relatively new and offers better quality than the old LCD technology.
In this tutorial, we would like to review the steps required to display data on the most common monochrome OLED display module on the market.
I will try to explain the function provided by the corresponding Adafruit library to display data on this module.
OLED modules are available in a variety of sizes and functions.
What we will be using in this tutorial is the monochrome 128x64 OLED module.
This type of module has the following sizes (
In order for you to see on the picture)
: Since all of these modules support the I2C protocol as a means of communication, the code and wiring of all modules are exactly the same.
The only difference is that you have to take into account the size shown on the code so that what you are going to display can be placed on it correctly. The inter-
Integrated circuit (IIC)
That is, what is usually called I2C (I squared C)
Developed by Philips on 80s as a data exchange bus for transferring data between central processors (CPU)
Or micro-controller unit (MCU)
A device and a peripheral chip.
It\'s basically for TV applications.
Because of its simplicity, it became so popular that after a while it became one of the main data transfer mechanisms for cpu, mcu and peripherals, these devices are not a necessary component of the same PCB board and pass through the wires (e. g.
Sensors, display modules, etc. ).
The I2C communication bus consisting of two lines supports two-way data transfer between the main device and several slave devices.
Typically, the master node is responsible for controlling the bus-this is actually done by generating a synchronous signal on the serial clock line (SCL).
This is a signal that the master will send continuously during transmission, and all other nodes connected to the bus will use it to synchronize their communication and detect the speed of the bus.
Data is transmitted between master and slave machines through serial data (SDA)line.
The transmission speed can reach 3. 4 Mbps.
All devices that wish to transfer data through I2C should have a unique address and can operate as a transmitter or receiver depending on the function of the device.
For example, the OLED display module is the receiver that accepts some data and displays them, while the temperature sensor is the transceiver that sends the capture temperature through the I2C bus.
Typically, the master device is a device that starts data transmission on the bus and generates clock signals to allow transmission.
During transmission, any device addressed by the host is treated as a slave device and the data is read.
When a node wants to send some data, the first byte of the data should be the address of the receiver and then the actual data.
This means that in order to send data to the output device using I2C (e. g.
OLED display module)
We should first find the I2C address of it, which is what we will do next.
If you are interested in learning more about the details and theory of the I2C bus, you can use the following reference: you can find the list of components you need to complete this tutorial: eBay link: Amazon.
Link: Amazon. co.
UK link: An important note about devices that support I2C is that you should connect them to the Arduino in the same way.
This is because Arduino only runs its I2C communication on a specific PIN.
In this tutorial, I use Arduino Uno.
Arduino Uno uses pin A5 as SCK and A4 as SDA.
So we can connect the OLED display module to the Arduino Uno as shown in the schematic diagram.
As you may have noticed, in the photos I took from the OLED display module, the connectors for VCC and GND are different from the schematic.
Remember to check the label of the pin on the module to make sure it is connected in the right way.
We only need 4 pins as shown below: Arduino VCC-
> OLED module vccartron GND-
> OLED module GND Arduino 4-
> OLED module SDA Arduino 5-
> The first step for an OLED module SCKAs to connect to a device that supports I2C, you need to have the address of the module.
To do this, after connecting the module to the Arduino, you should upload the attached code to the Arduino.
This code contains the Wire Library, which is a library that handles I2C communication contained in the Arduino IDE.
It tries to scan the connected I2C devices and send their addresses to your computer via a serial port.
Therefore, you can access its output through the serial monitor tool in the Arduino IDE.
Original version provided by Arduino Playground).
Also, you can view it in a more readable way in my online Arduino editor.
Do not expect anything to be displayed on the screen when this code is running.
As you can see on the picture, my module is bound to address 0x3c.
Typically, all devices in a specific product line (
For example, all 128x64 OLED modules)
The address is the same.
The address limit for I2C devices is between 1 and 126.
This code just tries to connect to each device in order (
Do not transfer any data)
Then check if the base Library reports any errors when connecting to the provided address.
If there is no error, print the address as the available module to connect.
Also, it should be noted that the first 15 addresses are reserved, so it skips them and prints only addresses that are higher than this range.
Keep in mind that the addresses of these I2C modules are hard
Encoded on the device, cannot be changed.
So, when you\'re going to put it back on the lab shelf, it\'s better to write it somewhere or tag it on the module so next time, no need to run the scanner code.
This is not, however, a complex procedure; )
The line Library can handle low level communication with I2C devices.
When you want to connect to a specific device for data to be written from/, typically you will use the library provided by the company that originally built the module.
This library handles all the I2C communication details of a given module, allowing us to focus more on our business, in which case our business displays the data the way we want it.
The company that produced the original version of such display modules, Adafruit, provides a library called Adafruit ssd136 for displaying data on these monochrome displays.
So before we start coding, we have to go through the library manager (
Accessible via sketch> Include Library> Manage Library. . . menu)in Arduino IDE.
There is also a library called Adafruit GFX that handles graphic content at a lower level and is used internally by Adafruit ssd136.
As you can see on the picture, you need to install them all on the Arduino IDE.
Drawing on the display module is wrapped in a class called adafruit_ssd306.
The definition of this class is on the Adafruit library, so we need to include this library first.
Then we must first instantiate an instance of this class.
The constructor of this class adopts the port number that can reset the display, which is pin 4 (
Connected to SCK).
This part of the code should be at the beginning of the file (out of setup()and loop()functions).
Now in settings ()
Function we should call the start function of the display object by passing our I2C address as follows (
Ssd1306_switchcapvcc is a constant value that specifies the type of power supply in the library)
: Now that the display object is ready, we can call its function (e. g. , display. drawLine, etc. ).
It is important that whenever we draw something by calling our display object, we need to call that function to actually draw at the hardware level.
This is mainly due to the drawing function we call, just update the display\'s \"in memory\" representation for performance reasons.
It actually caches changes in memory.
So we should always remember to call the monitor ()
When we finish drawing on the screen, the function.
If you try to upload the code in this step, you will notice that the Adafruit Industries logo will be displayed.
You might want to know who made it!
In fact, this is what the Adafruit library does.
It initialized the memory of the module (
Display memory representation of hardware)
There\'s a logo for this company.
If you don\'t want to see it during initialization, you can try calling display. clearDisplay()
Call immediately before calling the set function.
As its name implies, this function completely clears the display.
Based on the documentation of the adafruit_ssd1306 library, you can use the different functions provided by the class to draw or directly manipulate the pixels on the display.
In the following sections, we will try to present an example for each of them so that you can understand how it works.
Most of these examples will show only one simple static content, so we can put them in our settings ()function (
Code after initialization.
By doing so, it will run only once and stay there.
To display the text, we can use the simple functionality of the library.
It takes the text as a string and tries to display it.
It is important to know that we must tell the library where we will display the text on the display.
Each pixel on the display has a coordinate specified with X and Y.
X increases from left to right and Y increases from top to bottom.
The top left corner of the screen is (X=0, Y=0)
And the lower right corner is (X=127, Y=63).
I noticed the coordinates of the corner on the first photo.
We can use the display. setCursor()
Function to specify where we will display the text on the display.
Another property of the text is its color.
We can specify the color using display. setTextColor()
As shown in the following example.
We can also use this function to display individual characters.
It accepts the character code of uint8_t type and displays the character corresponding to that code on the string.
For example, if we want to display the same string using this function, we can use the following code snippet: black text can also be drawn with a white background.
You have to call the monitor in order to do this.
The SetTextColor function is as follows: You can also set the size of the text using display. setTextSize()function.
It takes an integer as the size.
The larger the number, the larger the text.
The minimum size is 1, which is the default size of the text.
The following code tries to write the letter \"A\" in 6 different sizes: draw basic shapes such as rectangles, circles, triangles, etc, lines or points are very easy and each point has A special function.
Drawing a line can call display. drawLine(
StartX, startY, endX, color, color).
For example, the following code draws diagonal lines on the screen so that they form large X: you can use and function to access the width and height of the display.
By doing so, your code will have nothing to do with the screen size.
Drawing a rectangle shows a function that draws a rectangle. drawRect(
Width, height, color).
Here is the code to draw three rectangles in some random places: by calling display. fillRect(
Width, height, White)
You can draw a rectangle filled with the specified color.
The third function in this example is display. drawRoundRect(
Width, height, corner radius, color)
As you can see in the picture, it is used to draw a rectangle with rounded corners.
Before the color, it accepts an additional parameter that is an integer indicating the radius of the corner.
The greater the value of the rounded corner.
It also has a corresponding fill function called display.
I think you can guess what it does.
The drawing loop display function. drawCircle(
CenterX, centerY, radius, color).
Here is an example of smiling faces.
Like shape: like a rectangle, you can use the monitor.
The FillCircle function draws circles filled with a given color.
It\'s also a function called display. drawTriangle(
Point 1x, point t1 y, point t2x, point t2y, point 3x, point 3y, color)
And the corresponding display.
Draw the fill triangle of the fill triangle.
Draw a point and you can also color a specific point (
This is called Pixel)
Display on the screen through the display. drawPixel(
Pixel, Pixel, color)function.
Drawing an image is different and a bit complicated.
Since the display module is monocolor, we need to first convert the image to a format called a monochrome bitmap (
Also called black and white).
In this format, each pixel of the image is rendered at 0 or 1.
1 indicates the existence of the color and 0 indicates the space.
At the top of this section, you can see an example of the Arduino logo in this format.
Displays the function of drawing a bitmap image. drawBitmap(
TopLeftX, topLeftY, image data, width, height, color).
The Image data parameter is an array of numbers in bytes.
There are 8 bits per byte, so each byte contains 8 pixels of data for the image.
By specifying the width and height of the image, the drawBitmap function will know which bit the next line of pixels starts from.
The solution I chose to convert the image to this format is to first use the online \"image to ASCII converter \"(e. g.
To convert my picture to a set of ASCII characters, then replace the characters used for spaces with 0 and the other characters with 1.
This is what you see below.
You can treat each 0 and 1 as pixels on the display.
Therefore, the size of the picture should not exceed our display size, that is, 128x64.
Note: Using this ASCII technique is not a recommended method because your image will be deformed due to the aspect ratio of the character (
Characters are not Square).
I try this technique just because it can convert the image to the desired format more easily.
Otherwise, it is possible to get the best results by some programming or by using some utilities that are completely out of the scope of this article.
00000000000000000000011111111111111111111110000000000000000000000000000000000000011111111111111111111111111111100000000000000000 cm 0000000000000111111111111111111111111111111111111110000000000000 cm 0000000000011111111111111111111111111111111111111111100000000000 cm 0000000001111111111111111111111111111111111111111111111000000000 cm 0000000111111111111111111111111111111111111111111111111110000000 Cm 0000011111111111111111111111111111111111111111111111111111100000 cm 0000111111111111111111111111111111111111111111111111111111110000 cm 0001111111111111111111111111111111111111111111111111111111111000 cm 0011111111111111111111111111111111111111111111111111111111111100 cm 0111111111111111000000011111111111111111100000001111111111111110 cm 011111111111000000000000000111111111100000000000000011111 1111110 cm 1111111111000000001111000000001111000000001111000000001111111111 cm 1111111110000011111111111100000110000011111111111100000111111111 cm 1111111100000111111111111111000000001111111001111110000011111111 cm 1111111100001111100000011111100000011111100000011111000011111111 cm 1111111100000111111111111111000000001111111001111110000011111111 cm 1111111110000011111111111100000110000011111111111 100000111111111 cm 1111111111000000001111000000001111000000001111100000001111111111 cm 0111111111110000000000000000111111110000000000000000111111111110 cm 0111111111111111000000001111111111111111000000001111111111111110 cm 0011111111111111111111111111111111111111111111111111111111111100 cm 0001111111111111111111111111111111111111111111111111111111111000 cm 00001111111111111111111111111111111111111 11111111111111111110000 cm 0000011111111111111111111111111111111111111111111111111111100000 cm 0000000111111111111111111111111111111111111111111111111110000000 cm 0000000011111111111111111111111111111111111111111111111100000000 cm 0000000000011111111111111111111111111111111111111111100000000000 cm 0000000000000111111111111111111111111111111111111110000000000000 cm 000000000000000011111111111111111 1111111111111110000000000000000 Cm 0000000000000000000001111111111111111111111000000000000000000000Now we should to each line of month said Bytes and will its storage in array in as follows shown in: then we can by call drawBitmap function in display on the draw it.
This is a very long tutorial, so it is very possible to have a problem.
Here are some common list of errors you may encounter when setting up an OLED display module for your project (
Some of them happened when I was preparing this tutorial).
This will not happen for many reasons, so I would suggest checking the following lists in the order in which it is possible to happen in the project: Make sure you have set them in i2c-
When setting the display object, the scanner code in the function.
This actually happened to me.
If you are using Arduino Uno then you have to check your connections again to make sure they are the same as mine.
If you are using another Arduino version (e. g.
Leonardo, wait. )
, You have to know that they may set their I2C to other pins.
You can view it in the documentation of the Wire library.
This is a software problem.
This is very common when using a drawing function to miscalculate some coordinates, so your drawing will deform, or in the worst case, it may be completely out of the scene.
Check out your calculations and try to draw them step by step to see what\'s going on.
The text is not displayed at all. You need to call setTextColor before drawing the text.
Otherwise you have no error but you will not see any display.
Also, you may have set the text color to be the same as the background color.
If you set the text size to a very large value, the character may be completely out of the visible area.
There is a compile error regarding the display size, which also happens to me, which I think will happen to most of you.
This is because the display size constant value defined in the header file adafruit_ssd136.
We include h at the top of the script.
This file is located in {your-project-folder}
\\ Libraries \\ adafruit_ssd136. h.
If you open this file, you will notice that there is a comment section below that describes the constant that you only need to uncomment to represent the size of the OLED display module.
For the 128x64 display module, the line of ssd1366 _ 28 _ 64 should be uncommented to define.
OLED displays as output modules can give you a great opportunity to provide a professional interface for your hobby projects.
You can try to start with the following idea, show meaningful data on it, or help the user know what\'s going on, or if he/she needs to do something.
For the user, reading messages on the display is much clearer than explaining the status of the item/device through some LEDs.
What you can do as a starting point may be: Don\'t forget to tell me the comments, what will you do (
Or you already did it)
Use OLED display module.
Custom message
Chat Online 编辑模式下无法使用
Chat Online inputting...