Skip to content

How to use a 3.2 inch 256x64 OLED display with a vibration sensor?

By admin

How to Use a 3.2 Inch 256x64 OLED Display with a Vibration Sensor

To use a 3.2 inch 256x64 OLED display with a vibration sensor, you connect the sensor to a microcontroller like an Arduino or ESP32, read its digital or analog output, and then display the vibration data on the OLED screen in real time. The 3.2 inch 256x64 oled display module typically uses SPI or I2C communication, and the vibration sensor—like a SW-420 or piezo-based model—outputs a signal that the microcontroller processes. You then map that signal to visual elements like bars, graphs, or numeric values on the OLED. This setup works for monitoring machine vibrations, detecting impacts, or logging motion in industrial or hobbyist projects. The key is ensuring the OLED’s driver (usually SSD1306 or SH1106) is correctly initialized and the sensor’s threshold is calibrated to avoid false triggers.

The 3.2 inch 256x64 OLED display is a monochrome graphic module with a resolution of 256 pixels horizontally and 64 pixels vertically. Each pixel is individually addressable, giving you full control over what’s drawn. The display uses a passive matrix OLED technology, which means each pixel emits its own light—no backlight needed. This results in a contrast ratio of over 10,000:1, a viewing angle of 160 degrees, and a response time under 10 microseconds. The module typically operates at 3.3V or 5V logic, with a typical power consumption of 20-30 mA when all pixels are lit, but it can drop to under 1 mA in sleep mode. The SPI interface runs at up to 10 MHz, allowing for fast screen updates, which is critical when displaying real-time vibration data. The driver IC is often the SSD1306, which includes 128x64 memory, but the 256x64 resolution requires a separate controller like the SH1106 or a custom driver that handles the double width. The module’s physical dimensions are roughly 89.5mm x 28.5mm, with a mounting hole pattern for easy integration into enclosures.

Vibration sensors come in two main types: digital and analog. The SW-420 is a common digital vibration sensor that uses a spring-loaded mechanism. When vibration exceeds a set threshold, the spring contacts a metal pin, closing a circuit and outputting a LOW signal (or HIGH depending on the module). Its sensitivity is adjustable via a potentiometer on the board. The response time is around 1-2 ms, and it operates at 3.3V to 5V. The typical quiescent current is 10-20 µA, rising to 50-100 µA when triggered. For analog vibration sensing, a piezo disc or accelerometer like the ADXL335 is used. The piezo disc generates a voltage proportional to the vibration amplitude, ranging from millivolts to several volts. The ADXL335 outputs analog voltages for X, Y, and Z axes, with a sensitivity of 300 mV/g and a measurement range of ±3 g. The piezo sensor’s output can be fed into a microcontroller’s analog input, but it often requires a high-value resistor (1 MΩ) to bias the signal and a capacitor to filter noise. The ADXL335, on the other hand, needs a 3.3V supply and outputs analog signals directly, but it consumes about 350 µA in active mode.

For the microcontroller, the Arduino Uno or Nano is a solid choice because of its simplicity and wide community support. The Uno runs at 16 MHz, has 32 KB of flash memory, and 2 KB of SRAM. The OLED library for SSD1306 or SH1106 takes up about 4-6 KB of flash and 200-300 bytes of RAM for the buffer. The vibration sensor reading routine adds negligible overhead. If you need more processing power or wireless connectivity, the ESP32 is better. It runs at 240 MHz, has 520 KB of SRAM, and built-in Wi-Fi and Bluetooth. The ESP32 can handle complex vibration analysis, like FFT for frequency domain display, which is impossible on an Arduino due to memory constraints. The ESP32 also supports dual-core processing, so one core can read the sensor while the other updates the OLED. Power consumption for the ESP32 is around 80 mA in active mode, but it can drop to 10 µA in deep sleep, making it suitable for battery-powered vibration monitors.

Wiring the 3.2 inch 256x64 OLED display to the microcontroller is straightforward for SPI. The OLED module typically has 7 pins: VCC, GND, MOSI, SCK, CS, DC, and RST. Connect VCC to 3.3V or 5V, GND to ground, MOSI to the microcontroller’s SPI MOSI pin (e.g., pin 11 on Uno), SCK to SPI SCK (pin 13), CS to any digital pin (e.g., pin 10), DC to another digital pin (e.g., pin 9), and RST to a third digital pin (e.g., pin 8). For I2C, the module uses only 4 pins: VCC, GND, SDA, and SCL. Connect SDA to the microcontroller’s SDA pin (A4 on Uno) and SCL to SCL (A5). The I2C address is usually 0x3C or 0x3D, depending on the module. The vibration sensor wiring depends on the type. For the SW-420, connect VCC to 5V, GND to ground, and DO to a digital input pin (e.g., pin 2). For the ADXL335, connect VCC to 3.3V, GND to ground, and X, Y, Z outputs to analog input pins (e.g., A0, A1, A2). For a piezo disc, connect one leg to an analog pin and the other to ground, with a 1 MΩ resistor in parallel to bleed off charge.

Software setup begins with installing the necessary libraries. For the OLED, use the Adafruit SSD1306 library (for SSD1306-based modules) or the u8g2 library (which supports both SSD1306 and SH1106). The u8g2 library is more versatile, supporting over 200 display controllers and multiple fonts. For the vibration sensor, no library is needed for the SW-420—just use digitalRead(). For the ADXL335, use the analogRead() function, but you’ll need to convert the raw ADC value to g-force using the formula: g = (analogValue * Vref / 1023 - offset) / sensitivity, where Vref is typically 5V for Arduino, offset is the zero-g voltage (about 1.65V for 3.3V supply), and sensitivity is 0.3 V/g. For the piezo disc, the analog reading is proportional to the vibration amplitude, but it’s not calibrated to a physical unit unless you use a reference signal.

Initializing the OLED in code requires calling the display.begin() function with the correct I2C or SPI pins. For SPI, you specify the CS, DC, and RST pins. For I2C, you specify the address. The display buffer is 256x64 bits, which is 2048 bytes (256 * 64 / 8). This buffer is stored in the microcontroller’s RAM, so ensure you have enough memory. On an Arduino Uno, 2 KB of SRAM is tight, and the buffer takes up almost all of it. This leaves little room for variables, so you might need to use a smaller buffer or update the display in sections. The ESP32, with 520 KB of SRAM, handles this easily. Once initialized, you clear the buffer with display.clearDisplay() and send it to the OLED with display.display().

Displaying vibration data involves mapping the sensor reading to a visual format. For a simple bar graph, you read the vibration value, scale it to the display height (64 pixels), and draw a rectangle from the bottom of the screen to the scaled value. For example, if the analog reading from the ADXL335 is 0-1023, you map it to 0-64 using the map() function: barHeight = map(analogValue, 0, 1023, 0, 64). Then draw a filled rectangle at x=0, y=64-barHeight, width=10, height=barHeight. For a scrolling waveform, you store the last 256 readings in an array and shift the array each time a new reading comes in. Then draw a line from each point to the next using display.drawLine(). The x-axis represents time, and the y-axis represents amplitude. The update rate should be at least 10 Hz to show meaningful vibration patterns, but the OLED’s SPI speed of 10 MHz allows for 50-100 Hz updates if the microcontroller can keep up.

Calibration is a critical step. For the SW-420, the potentiometer on the module adjusts the sensitivity. Turn it fully clockwise to make the sensor less sensitive (needs stronger vibration to trigger), or counterclockwise for more sensitivity. Test by tapping the sensor and observing the digital output. For the ADXL335, calibrate by placing the sensor on a flat surface and reading the zero-g voltage. This should be around 1.65V for a 3.3V supply, but it varies slightly per sensor. Take 100 readings and average them to get the offset. Then apply a known acceleration, like 1 g by tilting the sensor 90 degrees, and measure the voltage. The sensitivity is the difference between the 1 g voltage and the zero-g voltage. For the piezo disc, calibration is harder because the output depends on the vibration frequency and amplitude. You can use a known vibration source, like a tuning fork or a calibrated shaker, to map the ADC reading to a physical unit.

Power management is important for portable applications. The OLED consumes 20-30 mA, the vibration sensor 10-350 µA, and the microcontroller 10-80 mA. A 2000 mAh Li-ion battery can run the system for about 25 hours continuously. To extend battery life, put the microcontroller to sleep and wake it on a vibration trigger. The SW-420’s digital output can be connected to an interrupt pin, so the microcontroller wakes from deep sleep when vibration is detected. The OLED can be put to sleep with display.dim(true) or by turning off the display entirely with display.ssd1306_command(SSD1306_DISPLAYOFF). The ESP32’s deep sleep mode consumes 10 µA, and the SW-420’s quiescent current is 10 µA, so the total sleep current is under 20 µA. This gives a theoretical battery life of over 100,000 hours, or 11 years, but in practice, the battery self-discharge limits it to a few months.

Common issues include the OLED not initializing, which is often due to incorrect wiring or wrong I2C address. Check the address with an I2C scanner sketch. If the display shows garbled characters, the SPI clock speed might be too high—try reducing it to 4 MHz. The vibration sensor might give false triggers if the threshold is set too low. Use a hysteresis approach in software: only trigger if the signal stays above the threshold for a minimum time, like 10 ms. For the analog sensor, noise from the power supply can cause jittery readings. Add a 100 nF capacitor between the sensor output and ground, and use a moving average filter in software (e.g., average of 10 readings). The OLED’s refresh rate can cause flicker if the update rate is too slow. Use double buffering: write to the buffer, then call display.display() once per cycle. Avoid clearing the entire buffer each time—only update the changed pixels.

Advanced features include displaying the vibration frequency using FFT on the ESP32. The ESP32’s Arduino core includes the ArduinoFFT library, which can compute a 256-point FFT in under 10 ms. Sample the analog sensor at 500 Hz for 512 ms to get a frequency resolution of 1.95 Hz. Display the FFT magnitude as a bar graph on the OLED, with the x-axis representing frequency from 0 to 250 Hz. This is useful for identifying specific vibration frequencies in machinery, like bearing faults or imbalance. Another feature is logging vibration events to an SD card. Connect an SD card module to the microcontroller and write the timestamp and vibration amplitude to a CSV file. The OLED can show the last 10 events in a scrolling list. For wireless monitoring, use the ESP32’s Wi-Fi to send data to a web server or MQTT broker. The OLED can display the connection status and the last transmitted value.

Safety considerations: The OLED module is sensitive to static discharge, so handle it with an anti-static wrist strap. The vibration sensor should be mounted securely to the vibrating surface using double-sided tape or a screw. For industrial applications, use a metal enclosure to shield the electronics from electromagnetic interference. The OLED’s operating temperature range is -40°C to 85°C, but the vibration sensor’s range might be narrower. Check the datasheet for the specific sensor. The SW-420 operates from -10°C to 70°C, while the ADXL335 works from -40°C to 85°C. For outdoor use, ensure the enclosure is weatherproof to prevent moisture damage.

Cost breakdown: The 3.2 inch 256x64 OLED module costs around $15-25, depending on the supplier. The SW-420 sensor costs $1-2, the ADXL335 module $3-5, and the piezo disc $0.50-1. An Arduino Uno clone costs $5-10, and an ESP32 development board $3-8. A 2000 mAh Li-ion battery costs $5-10, and a charging module $1-2. Total project cost is $20-50, which is reasonable for a custom vibration monitoring system. For comparison, a commercial vibration monitor costs $100-500, so this DIY approach is cost-effective for hobbyists and small-scale industrial use.

Testing the system: Start by uploading a simple sketch that reads the vibration sensor and prints the value to the serial monitor. Verify the sensor responds to tapping. Then add the OLED initialization and display a static text. Once that works, implement the bar graph or waveform display. Test with a known vibration source, like a smartphone vibrating on a table. Adjust the scaling and threshold until the display responds correctly. For the SW-420, the digital output should toggle the OLED’s background color from black to white when vibration is detected. For the analog sensor, the waveform should show peaks and valleys corresponding to the vibration pattern. If the waveform is flat, check the sensor wiring and the analog reading range. If the OLED shows nothing, check the I2C address or SPI pins with a logic analyzer.

Performance metrics: The OLED’s pixel response time is under 10 µs, so it can show fast vibration events. The SPI bus speed of 10 MHz gives a theoretical frame rate of 10 MHz / (256 * 64 / 8) = 488 frames per second, but the microcontroller’s processing limits it to 30-60 fps. The vibration sensor’s bandwidth is crucial: the SW-420 responds to vibrations above 50 Hz, the ADXL335 has a 500 Hz bandwidth, and the piezo disc can go up to 10 kHz. For low-frequency vibrations (1-100 Hz), the ADXL335 is best. For high-frequency impacts (100 Hz-10 kHz), the piezo disc is better. The SW-420 is only suitable for detecting presence of vibration, not amplitude or frequency. The memory usage on the Arduino Uno is the bottleneck: the OLED buffer takes 2048 bytes, leaving 0 bytes for other variables. This forces you to use the PROGMEM keyword for constant data or reduce the buffer size by using a 128x64 display instead. The ESP32, with 520 KB of SRAM, has no such limitation.

Working with Quang

Have a product that needs a senior designer in the room — not in a Slack queue?

Book a Portfolio Review →