This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot-open:practical:hardware:sut:stm32:emb7_1 [2024/04/20 19:58] – ktokarz | en:iot-open:practical:hardware:sut:stm32:emb7_1 [2024/04/25 22:17] (current) – [STM_7: Using OLED display in STM32WB55] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== STM_7: Using OLED display ===== | ||
| + | This scenario presents how to use the OLED display connected to the STM32WB55 SoC. Our OLED display is an RGB (16bit colour, 64k colours) 1.5in, 128x128 pixels. The OLED chip is SSD1351, and it is controlled over the SPI interface using the pin configuration as described in STM32 node Hardware Reference in Table 1 STM32WB55 Node Hardware Details. | ||
| + | |||
| + | ===== Prerequisites ===== | ||
| + | There is no need to program SPI because the display is connected to the hardware SPI directly, which is handled by a library built in the STMDuino. We will use an SSD1351 display library, and a graphic abstraction layer for drawing primitives such as lines, images, text, circles, and so on: | ||
| + | <code ini> | ||
| + | lib_deps = | ||
| + | adafruit/ | ||
| + | adafruit/ | ||
| + | </ | ||
| + | Note that the graphics abstraction library (Adafruit GFX) can be loaded automatically if the | ||
| + | <code ini> | ||
| + | |||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | To generate an array of bytes representing an image in 565 format, it is easiest to use an online tool, e.g.: | ||
| + | * [[https:// | ||
| + | <note important> | ||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | |||
| + | ==== Task to be implemented ==== | ||
| + | Draw a text on the OLED display and an image of your choice (small, to fit both text and image). | ||
| + | |||
| + | ==== Start ==== | ||
| + | Perhaps you will need to use an external tool to preprocess an image to the desired size (we suggest something no bigger than 100x100 pixels) and another tool (see hint above) to convert an image to an array of bytes. | ||
| + | <note important> | ||
| + | |||
| + | Check if you can see a full OLED Display in your video stream. Book a device and create a dummy Arduino file with '' | ||
| + | |||
| + | Prepare a small bitmap and convert it to the byte array for 16-bit colour settings.\\ | ||
| + | Sample project favicon you can use is present in Figure {{ref> | ||
| + | <figure iotopenfavicon> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | ==== Steps ==== | ||
| + | Remember to include the source array in the code when drawing an image. | ||
| + | The corresponding generated C array for the logo in Figure {{ref> | ||
| + | <code c> | ||
| + | const uint16_t epd_bitmap_logo64 [] PROGMEM = { | ||
| + | 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, | ||
| + | 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xf7be, 0xbdd7, 0x8430, 0x5aeb, 0x39c7, 0x2104, 0x1082, 0x0020, 0x0020, 0x1082, | ||
| + | 0x2104, 0x39c7, 0x5aeb, 0x8430, 0xbdd7, 0xf7be, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, | ||
| + | .... | ||
| + | |||
| + | .... | ||
| + | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 | ||
| + | }; | ||
| + | </ | ||
| + | === Step 1 === | ||
| + | Include necessary libraries: | ||
| + | <code c> | ||
| + | // Libraries | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Fonts | ||
| + | #include < | ||
| + | |||
| + | // Here you can also include the file with the picture | ||
| + | #include " | ||
| + | </ | ||
| + | |||
| + | The code above also includes a font to draw text on the OLED Display. There are many fonts one can use, and a non-exhaustive list is present below (files are located in the '' | ||
| + | < | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | === Step 2 === | ||
| + | Add declarations for GPIOs, colours (to ease programming and use names instead of hexadecimal values) and screen height and width. To recall, the OLED display in our lab is square: 128x128 pixels, 16k colours (16-bit 565: RRRRRGGGGGGBBBBB colour model): | ||
| + | <code c> | ||
| + | // Pins definition for OLED | ||
| + | #define SCLK_PIN | ||
| + | #define MOSI_PIN | ||
| + | #define MISO_PIN | ||
| + | #define OLED_DC_PIN | ||
| + | #define OLED_CS_PIN | ||
| + | #define OLED_RST_PIN D10 // | ||
| + | |||
| + | // Colours definitions | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | #define CYAN 0x07FF | ||
| + | #define MAGENTA | ||
| + | #define YELLOW | ||
| + | #define WHITE | ||
| + | |||
| + | // Screen dimensions | ||
| + | #define SCREEN_WIDTH | ||
| + | #define SCREEN_HEIGHT 128 | ||
| + | </ | ||
| + | |||
| + | === Step 3 === | ||
| + | Declare an OLED controller objects: | ||
| + | <code c> | ||
| + | Adafruit_SSD1351 oled = Adafruit_SSD1351(SCREEN_WIDTH, | ||
| + | </ | ||
| + | |||
| + | === Step 4 === | ||
| + | Initialise the OLED controller object. Then clear the screen (write all black): | ||
| + | <code c> | ||
| + | pinMode(OLED_CS_PIN, | ||
| + | pinMode(OLED_RST_PIN, | ||
| + | oled.begin(); | ||
| + | oled.fillRect(0, | ||
| + | </ | ||
| + | |||
| + | === Step 5 === | ||
| + | Draw a bitmap in the left top corner of the screen (screen is 128x128px). The OLED library handles the '' | ||
| + | <code c> | ||
| + | oled.drawRGBBitmap(0, | ||
| + | </ | ||
| + | === Step 6 === | ||
| + | Write some additional text in the middle of the screen: | ||
| + | <code c> | ||
| + | oled.setFont(& | ||
| + | oled.setTextSize(1); | ||
| + | oled.setTextColor(WHITE); | ||
| + | oled.setCursor(10, | ||
| + | oled.println(" | ||
| + | </ | ||
| + | Some remarks regarding coordinates: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | <note tip>To speed up screen updating and avoid flickering, you may use a trick to clear the afore-written text: instead of clearing the whole or partial screen, write the same text in the same location but in the background colour.</ | ||
| + | |||
| + | <note tip> | ||
| + | |||
| + | Besides the functions presented above, the controller class has several other handy functions (among others): | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | ==== Result validation ==== | ||
| + | You should see the image and the text in the video stream. | ||
| + | |||
| + | ===== FAQ ===== | ||
| + | **The screen is black even if I write to it. What to do?**: Check if you have done all the steps shown in the example. Check if you used proper GPIOs to control the OLED display. Follow carefully the code example in this manual: it does work! | ||
| + | |||
| + | |||
| + | ===== Project information ===== | ||
| + | {{: | ||
| + | This Intellectual Output was implemented under the Erasmus+ KA2.\\ | ||
| + | Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, | ||
| + | Project number: 2022-1-PL01-KA220-HED-000085090. | ||
| + | |||
| + | **__Erasmus+ Disclaimer__**\\ | ||
| + | This project has been funded with support from the European Commission. \\ | ||
| + | This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. | ||
| + | |||
| + | **__Copyright Notice__**\\ | ||
| + | This content was created by the IOT-OPEN.EU Reloaded consortium, 2022, | ||
| + | The content is Copyrighted and distributed under CC BY-NC [[https:// | ||
| + | <figure label> | ||
| + | {{: | ||
| + | </ | ||
| + | |||
| + | |||