How to use a 1.54 inch 128x64 OLED with a relay?
How to Use a 1.54 inch 128x64 OLED with a Relay
You connect a 1.54 inch 128x64 OLED display to a relay by using a microcontroller, typically an Arduino or ESP32, to control the relay coil via a digital output pin while the OLED shows real-time status, voltage, or countdown data. The relay itself is an electrically operated switch that isolates low-voltage control circuits from high-voltage loads, and the OLED serves as a visual feedback interface. For example, you can display “RELAY ON” when the relay is energized or show a timer counting down before the relay toggles. The core wiring involves powering the OLED via 3.3V or 5V (depending on the module), connecting SPI or I2C pins to the microcontroller, and driving the relay through a transistor (like a 2N2222 or a dedicated relay driver IC such as the ULN2003) because the relay coil typically draws 50–100 mA at 5V or 12V, which exceeds the 20 mA max current of a standard Arduino pin. The 1.54 inch 128x64 oled display uses a monochrome SSD1306 or SH1106 driver, with a resolution of 128x64 pixels, a viewing angle of over 160 degrees, and a typical power consumption of 20–30 mA at full brightness. This setup is common in home automation, industrial monitoring, and prototyping where you need both a visual indicator and a switching capability.
The relay you choose must match your load requirements. For a 5V relay module, the coil resistance is usually around 70–100 ohms, meaning the coil draws 50–70 mA at 5V. A 12V relay might have a coil resistance of 400–600 ohms, drawing 20–30 mA. The OLED display, whether SPI or I2C, operates at 3.3V logic but many modules include a voltage regulator for 5V supply. The SPI version uses 7 pins: VCC, GND, CS, DC, RES, SCK, and MOSI (or SDA for I2C, which uses only 4 pins). The relay module typically has three pins: VCC, GND, and IN (signal). The IN pin is active low or high depending on the module; most are active low, meaning you send a LOW signal to turn the relay on. You must use a flyback diode across the relay coil if your module doesn’t include one, to protect the microcontroller from voltage spikes when the coil de-energizes. The diode is usually a 1N4007, rated for 1A and 1000V, which is sufficient for most relays up to 10A contact rating.
Let’s break down the wiring for an Arduino Uno. Connect the OLED’s VCC to 5V, GND to GND, CS to digital pin 10, DC to pin 9, RES to pin 8, SCK to pin 13, and MOSI to pin 11 for SPI. For I2C, connect VCC to 5V, GND to GND, SDA to A4, and SCL to A5. The relay module’s VCC goes to 5V or an external power supply if it’s a 12V relay, GND to common ground, and IN to digital pin 7. The relay’s common (COM) and normally open (NO) contacts connect to your load, say a 120V AC lamp, with the line wire going through the relay. The OLED’s contrast can be set via software; the SSD1306 driver supports 128x64 resolution with 256 brightness levels, but the actual contrast is adjusted by sending command 0x81 followed by a value from 0 to 255. A typical value is 0xCF for 80% contrast. The display refresh rate is up to 100 Hz, but for static text you can update it at 10–20 Hz without flicker.
For the relay control, you need a transistor driver if the relay module doesn’t have one built-in. A common circuit uses a 2N2222 NPN transistor with a base resistor of 1k ohm connected to the Arduino pin, collector to the relay coil, and emitter to ground. The coil’s other end connects to VCC. A 1N4007 diode is placed across the coil with the cathode to VCC. The relay’s contact rating is critical: a typical SRD-05VDC-SL-C relay from Songle is rated for 10A at 250V AC or 10A at 30V DC. For inductive loads like motors, you need a snubber circuit or a relay with a higher rating. The OLED’s SPI clock speed can be up to 10 MHz, but Arduino’s SPI library defaults to 4 MHz, which is fine for text and simple graphics. The OLED’s frame buffer is 1 KB (128x64 pixels / 8 bits per byte), so you can preload it with data and update only changed sections to save processing time.
Now, let’s talk about the software. You need two libraries: Adafruit_SSD1306 for the OLED and Adafruit_GFX for graphics. Install them via the Arduino Library Manager. The relay control is straightforward: use digitalWrite(pin, LOW) to energize and digitalWrite(pin, HIGH) to de-energize for an active-low module. The OLED initialization code is:
#include
#include
#include
#include
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 9
#define OLED_CS 10
#define OLED_RESET 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH); // relay off initially
}
void loop() {
display.clearDisplay();
display.setCursor(0,0);
display.println("Relay: OFF");
display.display();
delay(2000);
digitalWrite(7, LOW); // relay on
display.clearDisplay();
display.setCursor(0,0);
display.println("Relay: ON");
display.display();
delay(2000);
digitalWrite(7, HIGH); // relay off
}
This code toggles the relay every 2 seconds and updates the OLED. The display uses the SSD1306_SWITCHCAPVCC parameter, which enables internal charge pump for the OLED’s 7–15V drive voltage. The OLED’s actual power consumption is 20 mA with all pixels on, but typical use with text only draws 10–15 mA. The relay module, when energized, draws 70 mA for a 5V coil, so the total system current is around 100 mA, which is within the Arduino’s 5V regulator capacity (500 mA for Uno). If you use a 12V relay, you need an external 12V supply for the relay and a separate 5V for the OLED.
For advanced control, you can display sensor data on the OLED and trigger the relay based on thresholds. For example, connect a DHT22 temperature sensor to measure ambient temperature. If the temperature exceeds 30°C, turn on a relay to control a fan. The DHT22 has a resolution of 0.1°C and accuracy of ±0.5°C. The OLED can show the temperature in real-time, the relay status, and a countdown timer. The code would read the sensor every 2 seconds, update the display, and check the threshold. The relay’s response time is typically 5–10 ms for the coil to energize and 5 ms for the contacts to close, so it’s fast enough for most applications.
Let’s look at a table comparing common relay modules and their characteristics:
| Relay Model | Coil Voltage | Coil Current | Contact Rating | Trigger Level | Typical Price |
|---|---|---|---|---|---|
| Songle SRD-05VDC-SL-C | 5V DC | 70 mA | 10A @ 250V AC | Active Low | $1.50 |
| Songle SRD-12VDC-SL-C | 12V DC | 30 mA | 10A @ 250V AC | Active Low | $1.80 |
| HF32F-5V | 5V DC | 60 mA | 10A @ 250V AC | Active Low | $1.20 |
| JQC-3FF-5V | 5V DC | 80 mA | 10A @ 250V AC | Active Low | $1.00 |
The OLED display itself has a pixel pitch of 0.28 mm, a active area of 35.0 x 17.5 mm, and a module size of 42.0 x 27.0 mm. The SPI interface allows for faster updates than I2C, which is limited to 400 kHz clock speed. For the 1.54 inch 128x64 oled display, the SPI version can achieve a frame rate of 30 fps for full-screen updates, while I2C is around 10 fps. If you’re displaying a simple text line, both are fine. The relay’s mechanical life is typically 10 million operations, and electrical life is 100,000 operations at rated load. So for a home automation system that toggles a relay every 10 seconds, the mechanical life would last about 3 years, but the electrical life depends on the load.
When wiring the relay to a high-voltage load, safety is paramount. Use a relay with a contact rating at least 20% higher than your load. For a 100W incandescent lamp at 120V AC, the current is 0.83A, so a 10A relay is overkill. But for a 1 HP motor (746W), the starting current can be 5–7 times the running current, so you need a relay rated for at least 20A. The OLED’s low voltage (3.3V or 5V) is isolated from the high-voltage side by the relay’s physical gap, which is typically 8 mm for 250V AC isolation. Never share ground between the low-voltage and high-voltage sides; the relay module should have optocoupler isolation if you’re controlling a high-voltage load. Most relay modules include an optocoupler like the EL817, which provides 5000V isolation. This is critical for safety and to prevent ground loops.
The OLED’s contrast can be adjusted for different lighting conditions. In a dark room, you can set contrast to 0x30 (low) to save power, while in bright sunlight, you might need 0xFF (maximum). The display’s viewing angle is 160 degrees, meaning it’s readable from almost any direction. The operating temperature range is -30°C to +70°C, which covers most indoor environments. The relay’s operating temperature is -40°C to +85°C, so it’s suitable for industrial use. The OLED’s lifetime is 50,000 hours (about 5.7 years of continuous use) at 25°C, but it drops at higher temperatures. The relay’s coil power dissipation is 0.35W for a 5V relay, which is negligible, but the contact resistance is typically 100 milliohms, so at 10A, the power loss is 10W, which can heat up the relay. Use a heatsink or a relay with a higher rating if you’re switching high currents for long periods.
For a practical project, you can build a programmable timer that controls a relay based on time of day. Use an RTC module like the DS3231, which has an accuracy of ±2 ppm (about 1 minute per year). The OLED can display the current time, the relay status, and the next scheduled event. The relay can be programmed to turn on at 6:00 AM and off at 10:00 PM. The code would read the RTC every second, update the display, and compare the time with the setpoints. The OLED’s 128x64 resolution allows you to display 4 lines of text at size 1 (8x8 pixels per character) or 2 lines at size 2 (16x16 pixels). For a timer, you can show the time in HH:MM:SS format on the first line, the relay status on the second, and the next event on the third. The fourth line can show the temperature if you have a sensor.
Another application is a battery voltage monitor with a relay cutoff. Use a voltage divider (two resistors, say 10k and 2.2k) to measure a 12V battery, feeding the voltage into an analog pin. The OLED can show the battery voltage with one decimal place, and the relay can disconnect the load when the voltage drops below 11.5V to protect the battery from deep discharge. The voltage divider reduces the 12V to about 2.2V at the analog pin, which is within the 5V range. The Arduino’s ADC has 10-bit resolution, so you get 1024 steps, giving a resolution of 0.012V at the pin, or about 0.06V at the battery. The relay’s response time is fast enough to cut off the load within milliseconds of detecting the threshold.
The OLED’s driver IC, the SSD1306, supports both horizontal and vertical scrolling without needing to update the frame buffer. You can enable scrolling by sending commands: 0x26 for horizontal scroll right, 0x27 for left, with parameters for start page, end page, and speed. This is useful for displaying a scrolling message like “SYSTEM ACTIVE” while the relay is on. The scrolling speed can be set from 2 frames per step to 256 frames per step, with 5 steps per scroll. For a 2-second scroll, use a speed of 64 frames per step. The relay’s coil inductance is about 10 mH, so when you turn it off, the back EMF can be up to 100V. The flyback diode clamps this to 0.7V above VCC, protecting the transistor. Without the diode, the transistor can be destroyed instantly.
If you’re using an ESP32 instead of an Arduino, you can add Wi-Fi control. The ESP32 has a built-in Bluetooth and Wi-Fi, so you can control the relay from a smartphone app and display the status on the OLED. The ESP32’s GPIO pins are 3.3V logic, so you need a level shifter for the 5V relay module, or use a 3.3V relay like the G5V-2. The OLED’s SPI pins can be connected directly to the ESP32’s 3.3V pins, but the display’s logic level is 3.3V for most modules, so it’s compatible. The ESP32’s ADC has 12-bit resolution, giving 4096 steps, which is better for precise voltage measurements. The relay’s control can be done via MQTT, with the ESP32 subscribing to a topic like “relay/command” and publishing the status to “relay/status”. The OLED can show the Wi-Fi signal strength, the MQTT connection status, and the relay state. The ESP32’s deep sleep mode can reduce power consumption to 10 µA, but the OLED and relay will still draw power, so you need to turn them off during sleep using a MOSFET.
For a more robust setup, use a relay module with a screw terminal for the high-voltage side and a header for the control side. The OLED can be mounted on a custom PCB or a breadboard. The 1.54 inch 128x64 oled display from DisplayModule uses a 4-pin I2C interface or 7-pin SPI, with a built-in voltage regulator that accepts 3.3V to 5V. The module’s dimensions are 42x27x4.5 mm, which fits in most enclosures. The relay module’s dimensions are typically 25x25x15 mm, so both can fit in a small project box. You can use a 5V power supply for the entire system, but if the relay is 12V, you need a separate 12V supply. The total current for a 5V system with the OLED and relay is about 100 mA, so a 500 mA wall adapter is sufficient. For a 12V relay, the current is 30 mA, so a 12V 500 mA adapter works, but you need a 5V regulator for the OLED.
Let’s talk about the OLED’s graphic capabilities. You can draw lines, circles, rectangles, and bitmaps using the Adafruit_GFX library. For example, you can draw a battery icon that fills up as the battery voltage increases. The library supports 1-bit color (white or black on a black background), so you can create simple animations. The frame buffer is 1 KB, so you can store a precomputed bitmap for a logo or a waveform. The relay’s state can be represented by a filled or empty circle. The OLED’s SPI interface uses a 9