SPI version of SSD1306

In the previous chapter, we used the SSD1306 display via I2C. Now we are going to use it with SPI. In this case, we can have faster and more reliable communication at the expense of a more complex protocol.

It is quite common that a peripheral can work over SPI as well as over I2C. Most screens and sensors have this option. Note that SPI, although faster, has a limited number of slaves and also requires more wiring.

Many components are designed to be used either over SPI or I2C. We are going to look at the screen to have one example.

In principle you should only have one or two extra pins, but in some cases the board has a few more. Let's look at the different pins you can find. Note that the labels on the pins may vary a lot between manufacturers; however, the pins are always the same:

  • Vcc: to be connected to 5v.
  • Ground, GND: to be connected to Ground.
  • SCL, CLK, SCK, or D0: to be connected to the SPI clock.
  • MOSI, SDA, or D1: to be connected to the SPI data (MOSI).
  • DC, D/C, or A0: data/control pin. Used to tell the display whether we are sending data (pixels) or control (configuration). To be connected to any GPIO.
  • RS, RES, RST, or RESET: reset pin. To be connected to any GPIO. It is used to reset the configuration of the display during initialization. This process may take a few seconds.
  • CS (may not be present): Chip select. To be connected to the chip select of the bus, either CE0 or CE1.
The driver does reset the display on initialization; that is a synchronous action that takes a few seconds.

It is very important to note that some breakout circuits for this display do not have the CS pin exposed. If that is the case for your component, it will only work if it is the only peripheral on the SPI bus.

Note that some breakout circuits may not expose the CS pin. If you have one of those, you have to keep it alone on the SPI bus.

The official driver from Google only covers the I2C wiring; to use SPI we need to use a different one:

dependencies {
[...]
implementation 'com.plattysoft.things:ssd1306:+'
}

And then we make an equivalent example as the one for I2C:

val display = Ssd1306.openSpi(SPI_BUS, DC_PIN, RS_PIN, 128, 64)
for (i in 0 until display.lcdWidth) {
for (j in 0 until display.lcdHeight) {
display.setPixel(i, j, i % display.lcdHeight > j)
}
}
// Render the pixel data
display.show()
// Cleanup
display.close()

Note that we are using constants for the bus and the pin names instead of BoardDefaults just for simplicity.

The only difference with the I2C driver is the method to open the device. It now requires extra parameters for the DC and RS pins, as well as optional width and height (default display size is 128x64). The rest of the code is exactly the same as for I2C.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.71.237