Skip to main content

Subsection 10.1.2 Part 2: Wire It, and Light It

We’ll wire the display with four wires, flash one program, and get characters onto it. We’ll bring the oscilloscope in afterwards, in Subsection 10.1.4, once there is something on the wires worth looking at.
Power first. The display’s four connections are labeled on the board itself: +, −, D and C (Figure 10.1.4). The backpack’s pin header plugs into the breadboard, so each of the four wires goes into the breadboard column that pin lands in. + goes to your breadboard’s power rail (which is fed by the Nucleo’s 3.3 V pin), − to ground. Check that the rail is connected to 3.3 V and not to 5 V, then check it again before you plug in the USB cable: 5 V can destroy the board.
Your display may have header pins, a STEMMA QT connector (a small four-pin JST SH socket on the end of the board), or both. If it has the socket but no header pins, use the cable from your kit that has a STEMMA QT plug on one end and four separate pins on the other, and plug the pins into the same four breadboard columns. The cable is shown in Figure 10.1.3.
Figure 10.1.3. The STEMMA QT cable: a plug that fits the socket one way only, and four wires ending in pins. Black is ground, red is power, blue is SDA and yellow is SCL. The same cable connects the accelerometer later in the course.
I2C needs pull-up resistors on SCL and SDA. The backpack has them built in, so we wire none ourselves.
Check the four wires against Figure 10.1.5 before you plug the Nucleo into USB. We have not tested what happens when + and − are swapped, so check the four wires rather than finding out.
Figure 10.1.4. The display and its backpack, unpowered, with the four connections labeled on the board itself: + (supply), − (ground), D (SDA, the data line) and C (SCL, the clock line). This is a bare board, photographed before its header was fitted; yours has a pin header soldered into those four holes and sits in the breadboard, so the wires go into the breadboard column each pin lands in rather than onto the board itself. Figure 10.1.1 and Figure 10.1.5 both show it that way round. The pull-up resistors that hold both lines HIGH are already on this board, so no external resistors are needed.
The two signal wires go to PB9 (SDA, serial data) and PB8 (SCL, serial clock), which are D14 and D15 on the Arduino header. We’ll see tomorrow why those two pins and not others. Figure 10.1.5 also shows the AD2 tapping the same two wires; we’ll use the AD2 later in Subsection 10.1.4.
Figure 10.1.5. The full setup: Nucleo, display backpack and AD2 on one breadboard. Four wires run from the backpack: + to the Nucleo’s 3.3 V pin, − to GND, D to PB9 (SDA) and C to PB8 (SCL). The AD2’s channel 1 (orange) and channel 2 (blue) scope leads tap the SCL and SDA rows on the breadboard, with the white-striped minus leads at ground. Both signal lines are already pulled up on the backpack, so nothing else is needed to make the bus work.
The program is helloDisplay.c, and it is short because it uses the i2c.c library that we’ll provide. Read it before you flash it to see its shape. We’ll go through every line on Thursday. Three one-byte writes set up the display, and i2c1_memWrite() sends all ten data bytes for the four digits and colon. The loop repeatedly sends an arbitrary value to the display so that we can catch a transaction on the oscilloscope.
Every I2C device has an address, and ours is 0x70. The controller sends it first, so that the right device answers. The rest of the names below are the display chip’s own commands, taken from the HT16K33’s datasheet. We’ll work out where the numbers come from tomorrow.
#include "ES28.h"
#include "i2c.h"

#define HT16K33_ADDR         0x70   // I2C address of our backpack controller
#define HT16K33_SYSTEM_CMD   0x20   // system setup
#define HT16K33_OSC_ON       0x01
#define HT16K33_DISPLAY_CMD  0x80   // display setup
#define HT16K33_DISPLAY_ON   0x01
#define HT16K33_BLINK_OFF    0x00
#define HT16K33_BRIGHT_CMD   0xE0   // brightness, ORed with 0 to 15
#define HT16K33_ADDR_PTR     0x00   // display RAM address pointer

int main(void) {
    uint8_t data = 0b0;   // The hardware requires us to send some data, not important here.
    uint8_t display_buffer[10] = {
        0b01110110, 0x00,       // H
        0b00111111, 0x00,       // O
        0x00,       0x00,       // the colon, both dots off
        0b00111000, 0x00,       // L
        0b01110111, 0x00        // A
    };
    i2c1_init();
    // Wake the display up, then fill its display RAM
    i2c1_byteWrite(HT16K33_ADDR, HT16K33_SYSTEM_CMD | HT16K33_OSC_ON);
    i2c1_byteWrite(HT16K33_ADDR, HT16K33_DISPLAY_CMD | HT16K33_DISPLAY_ON | HT16K33_BLINK_OFF);
    i2c1_byteWrite(HT16K33_ADDR, HT16K33_BRIGHT_CMD | 0x7);
    i2c1_memWrite(HT16K33_ADDR, HT16K33_ADDR_PTR, 10, display_buffer);

    // Keep one short transaction on the wire for the oscilloscope
    while(1) {
        i2c1_byteWrite(HT16K33_ADDR, data);
        delay_ms(250);
    }
    return 0;
}
The three one-byte writes before the loop are commands to the HT16K33 (we’ll understand them in more detail tomorrow). What is worth knowing now is the order. The first of the three turns the chip’s oscillator on, and until it is running the HT16K33 accepts everything you send it and drives nothing at all. A display that has been sent the other two commands but not that one looks exactly like a display that is not plugged in.
The fourth call, i2c1_memWrite(), sends the ten bytes of display_buffer in one go. Ten bytes for four digits, because there are two per position and the colon has a position of its own; the odd-numbered bytes are 0 for a seven-segment digit, and we’ll see on Thursday why they still have to be sent.
Then the loop, whose job is not to change anything. The display is already showing what it is going to show; the loop just keeps something on the bus for us to look at with the oscilloscope. I2C has no way to address a device without also sending it something, so the shortest thing you can put on the wire is a one-byte write, and the value does not matter here. The delay_ms(250) makes that happen once every 250 ms, four transactions a second, slow enough that a single sweep catches exactly one, which is what Subsection 10.1.4 needs.

Activity 10.1.1. Wire the Display and Light It.

i2c.c, i2c.h and helloDisplay.c are in the book’s starter files.

(b)

Copy your TemplateProject to a new project named SevenSegI2CFirstSteps, put helloDisplay.c in its Src folder, and build and flash. If the build fails, double-check that you have i2c.c and i2c.h in your mylib folder.

(c)

Read what the display says. Then, without changing anything yet: which lines of the program put it there, and which line is running now?
Display still dark? Re-seat all four wires against Figure 10.1.5: + on the 3.3 V pin, − on GND, D (SDA) on PB9 and C (SCL) on PB8. D and C swapped is the one that is hardest to see and easiest to do. Still stuck? We’re here to help!