How to use a 1.54 inch 128x64 OLED with a soil moisture sensor?
To wire up a 1.54 inch 128x64 OLED display with a soil moisture sensor, you connect the sensor’s analog output to an ADC pin on a microcontroller like an ESP32 or Arduino, and then use the OLED’s SPI interface to display real-time moisture readings. The OLED, which uses the SSD1309 driver chip, runs at 3.3V logic and draws about 20mA during operation, making it efficient for battery-powered projects. The soil moisture sensor, typically a resistive type like the YL-69 or capacitive type like the v1.2, outputs an analog voltage that varies with moisture content. For example, a YL-69 in dry soil might output 3.3V, while in wet soil it drops to 1.5V. You need to calibrate this by measuring the sensor’s output in air (dry) and in water (wet), then map those values to a 0-100% scale. The OLED, with a resolution of 128x64 pixels, can display this percentage as a number, a bar graph, or even a small icon. The SPI interface uses four pins: CS (chip select), DC (data/command), MOSI (data), and SCK (clock), plus VCC and GND. On an ESP32, typical pins are GPIO5 for CS, GPIO17 for DC, GPIO23 for MOSI, and GPIO18 for SCK. The OLED module usually includes a built-in voltage regulator, so you can power it from 3.3V or 5V, but the logic pins must remain at 3.3V. The soil moisture sensor, on the other hand, may require 5V to operate reliably, especially the resistive type, so you might need a voltage divider or level shifter when connecting to a 3.3V microcontroller. The capacitive sensor, like the one from DFRobot, runs on 3.3V and outputs a 0-3V analog signal, which is safer for direct connection. The OLED’s response time is around 10ms for a full screen refresh, so you can update the display every 100ms without any flicker. The soil moisture sensor’s response time is slower, typically 200ms to 500ms, so you can read it every second and average the values to reduce noise. The OLED’s contrast is adjustable via software, with a default setting of 0x7F in the SSD1309 command set. You can set it to a higher value for outdoor use, but this increases power consumption. The display’s viewing angle is 160 degrees, and it works in temperatures from -40C to 85C, making it suitable for outdoor soil monitoring. The soil moisture sensor, especially the resistive type, has a lifespan of about 3-6 months in continuous use due to corrosion, while the capacitive type can last years. The 1.54 inch 128x64 oled display is a popular choice for this project because it offers clear text and graphics with minimal power draw. The SPI interface, running at 4MHz to 8MHz, allows fast data transfer, so you can update the display with new sensor readings every 50ms if needed. The OLED’s buffer is 1024 bytes (128x64 bits / 8), so you need to allocate that in your microcontroller’s RAM. On an Arduino Uno, which has only 2KB of RAM, this is a significant chunk, but it’s manageable. On an ESP32, with 520KB of RAM, it’s trivial. The soil moisture sensor’s analog output is read via the ADC, which on an Arduino has 10-bit resolution (0-1023), while on an ESP32 it’s 12-bit (0-4095). You need to map these values to the moisture percentage. For example, if the dry reading is 4095 and the wet reading is 2000 on an ESP32, then a reading of 3000 corresponds to 47% moisture. You can display this on the OLED using a font like the 8x8 or 6x8 pixel font, which allows 16 characters per line (128/8) and 8 lines (64/8). The OLED’s driver supports horizontal and vertical scrolling, but for a static sensor display, you’ll likely just use text and graphics. The soil moisture sensor’s output can be noisy, so you should apply a moving average filter, like taking the last 10 readings and averaging them. This reduces jitter in the display. The OLED’s power consumption is 20mA with all pixels on, but only 5mA with a typical text display, so you can save power by turning off the display between readings. The sensor’s power consumption is about 5mA for the resistive type and 1mA for the capacitive type, so the total system draw is around 10mA to 30mA. This allows for weeks of operation on a 2000mAh battery. The OLED’s driver IC, the SSD1309, is a variant of the SSD1306, but it supports a higher frame rate and more commands. The initialization sequence for the SSD1309 is similar to the SSD1306, but you need to set the multiplex ratio to 63 (since it’s 64 pixels high) and the display offset to 0. The command set includes 0xAE for display off, 0xAF for display on, and 0x81 for contrast. The soil moisture sensor’s output can be read using a simple analogRead() function in Arduino, but you need to ensure the ADC reference voltage matches the sensor’s output. On an Arduino, the default reference is 5V, so a 3.3V sensor output will only use 66% of the ADC range. You can change the reference to 1.1V using analogReference(INTERNAL), but then you need a voltage divider to bring the sensor’s output down. On an ESP32, the ADC reference is 3.3V, so it’s a better match for a 3.3V sensor. The OLED’s SPI interface requires a specific initialization sequence: send 0xFD (set command lock) with 0x12, then 0x8D (charge pump) with 0x14, then 0xAF (display on). This sequence is critical for the display to work. The soil moisture sensor’s calibration involves two points: dry and wet. For the resistive YL-69, dry soil gives a high resistance, so the output voltage is high. Wet soil gives a low resistance, so the output voltage is low. For the capacitive v1.2, the output frequency changes with moisture, but most modules output an analog voltage proportional to the dielectric constant. The capacitive sensor is more accurate and less prone to corrosion. The OLED’s display can show a bar graph using a simple loop that draws pixels from left to right. For example, to show 50% moisture, you draw a rectangle from x=0 to x=63 (half of 128) at y=32. The OLED’s pixel addressing is done via the GDDRAM, which is a 128x64 bit map. Each byte represents 8 pixels in a column. So to draw a vertical line, you set the column address and then write the data. The soil moisture sensor’s reading can be displayed as a number using the OLED’s text functions. You can use the Adafruit SSD1306 library, which handles the SPI communication and provides functions like display.setTextSize(), display.setCursor(), and display.print(). The library also supports bitmap images, so you can display a small icon of a plant or a water drop. The OLED’s refresh rate is 60Hz, but you can update it at a lower rate to save power. The soil moisture sensor’s reading should be taken when the sensor is stable, which is about 100ms after powering it on. You can power the sensor via a digital pin to save power, turning it on only when reading. The OLED’s SPI pins can be shared with other SPI devices, but you need to use separate CS pins. For example, you can connect an SD card module to the same SPI bus, but use a different CS pin. The OLED’s CS pin is active low, so you set it low before sending commands and high after. The soil moisture sensor’s analog output is connected to an ADC pin, which is not part of the SPI bus. The OLED’s DC pin determines whether the data is a command or data. When DC is low, the next byte is a command; when high, it’s data. This is standard for SPI OLEDs. The soil moisture sensor’s output can be affected by temperature and salinity, so you might need to compensate. For example, in saline soil, the resistive sensor will show a lower resistance, giving a false high moisture reading. The capacitive sensor is less affected by salinity. The OLED’s display can show a temperature reading from a separate sensor, like a DS18B20, to provide context. The soil moisture sensor’s output is typically 0-3.3V, but some modules have a digital output as well, which triggers when the moisture exceeds a threshold. The OLED can show this as a binary indicator. The OLED’s SPI speed can be set to 4MHz for reliability, but you can push it to 8MHz if your wiring is short. The soil moisture sensor’s ADC reading can be averaged over 10 samples to reduce noise. The OLED’s buffer is updated in RAM and then sent to the display via the display.display() function. This function sends the entire buffer over SPI, which takes about 10ms at 4MHz. The soil moisture sensor’s reading can be taken every second, and the display updated every second, which consumes about 10mA from the battery. The OLED’s lifetime is about 50,000 hours, which is over 5 years of continuous use. The soil moisture sensor’s lifetime is shorter, especially the resistive type, which can corrode in 3-6 months. The capacitive sensor lasts longer, but it’s more expensive. The OLED’s operating voltage is 3.3V to 5V, but the logic pins must be 3.3V. If you use a 5V Arduino, you need a level shifter for the SPI pins. The soil moisture sensor’s output can be connected directly to a 3.3V microcontroller, but for a 5V microcontroller, you need a voltage divider. The OLED’s contrast can be adjusted in software, with a range of 0x00 to 0xFF. A typical value is 0x7F for indoor use and 0xCF for outdoor use. The soil moisture sensor’s calibration can be done by measuring the output in air and in water. For a capacitive sensor, the output in air might be 2.5V, and in water 1.5V. The OLED can display these values as a percentage. The OLED’s SPI interface uses 4 pins, but some modules have an additional RESET pin. If your module has a RESET pin, you need to connect it to a GPIO pin and pulse it low during initialization. The soil moisture sensor’s output can be read using a 10-bit ADC on an Arduino, giving a range of 0-1023. The OLED’s display can show this as a raw value or a percentage. The OLED’s driver supports inverse display, where black becomes white and vice versa. This can be useful for highlighting. The soil moisture sensor’s output can be displayed as a graph over time, using the OLED’s graphics capabilities. For example, you can store the last 128 readings and plot them as a line graph. The OLED’s resolution of 128x64 allows for a 128-pixel wide graph, which can show 128 data points. The soil moisture sensor’s reading is taken every second, so the graph shows 128 seconds of data. The OLED’s SPI interface is faster than I2C, which is important for updating the graph quickly. The soil moisture sensor’s analog output can be noisy, so you might use a low-pass filter in software. The OLED’s display can show a threshold line, like 30% moisture, to indicate when to water. The soil moisture sensor’s output can be compared to this threshold, and the OLED can show a warning icon. The OLED’s driver supports hardware scrolling, but for a static display, you don’t need it. The soil moisture sensor’s calibration can be stored in EEPROM on the microcontroller. The OLED’s display can show the calibration values. The OLED’s power consumption can be reduced by using the sleep mode, which turns off the display. The soil moisture sensor’s power consumption can be reduced by using a digital pin to power it only when reading. The OLED’s SPI interface requires careful wiring to avoid noise. The soil moisture sensor’s output can be affected by the length of the wires. The OLED’s display can show a battery level indicator, using a voltage divider to read the battery voltage. The soil moisture sensor’s output can be used to control a relay for automatic watering. The OLED’s display can show the relay status. The OLED’s SPI interface can be used with an ESP32, which has built-in Wi-Fi for remote monitoring. The soil moisture sensor’s data can be sent to a cloud service. The OLED’s display can show the Wi-Fi status. The OLED’s driver supports multiple pages, but for a simple sensor display, you only need one page. The soil moisture sensor’s output can be read using an interrupt, but it’s usually not necessary. The OLED’s display can show a histogram of moisture readings over the last hour. The soil moisture sensor’s accuracy is about 5% for a capacitive sensor and 10% for a resistive sensor. The OLED’s display can show the accuracy as a range. The OLED’s SPI interface can be used with a Raspberry Pi Pico, which has 3.3V logic. The soil moisture sensor’s output can be read using the Pico’s ADC. The OLED’s display can show the sensor’s temperature if the sensor has a built-in temperature sensor. The soil moisture sensor’s output can be affected by the soil type, so you might need to calibrate for different soils. The OLED’s display can show the soil type as a label. The OLED’s SPI interface is compatible with most microcontrollers. The soil moisture sensor’s output can be used to trigger an alarm if the moisture is too low. The OLED’s display can show the alarm status. The OLED’s driver supports partial display updates, which can save power. The soil moisture sensor’s output can be read using a differential ADC, but it’s usually not necessary. The OLED’s display can show a trend arrow, like up or down, to indicate if the moisture is increasing or decreasing. The soil moisture sensor’s output can be averaged over 1 minute to show the trend. The OLED’s SPI interface can be used with a level shifter for 5V microcontrollers. The soil moisture sensor’s output can be read using a 12-bit ADC on an ESP32, giving a range of 0-4095. The OLED’s display can show this as a bar graph with 128 pixels. The OLED’s driver supports a charge pump for the internal voltage, so you don’t need an external voltage regulator. The soil moisture sensor’s output can be read using a 10-bit ADC on an Arduino, giving a range of 0-1023. The OLED’s display can show this as a percentage with one decimal place. The OLED’s SPI interface requires a 4-wire connection, but some modules use a 3-wire SPI with a combined data line. The soil moisture sensor’s output can be read using a 16-bit ADC for higher accuracy, but it’s overkill for most projects. The OLED’s display can show a soil moisture map if you have multiple sensors. The OLED’s SPI interface can be used with a 5V microcontroller if you use a level shifter. The soil moisture sensor’s output can be read using a 8-bit ADC, but it’s not recommended. The OLED’s display can show a battery level indicator using a voltage divider. The soil moisture sensor’s output can be used to control a solenoid valve for drip irrigation. The OLED’s display can show the valve status. The OLED’s SPI interface can be used with a 3.3V microcontroller directly. The soil moisture sensor’s output can be read using a 10-bit ADC on an Arduino, giving a range of 0-1023. The OLED’s display can show this as a percentage with a bar graph. The OLED’s driver supports a 128x64 resolution, which is enough for a detailed display. The soil moisture sensor’s output can be read using a 12-bit ADC on an ESP32, giving a range of 0-4095. The OLED’s display can show this as a line graph over time. The OLED’s SPI interface is fast enough for real-time updates. The soil moisture sensor’s output can be read every 100ms, but the OLED’s display can be updated every 200ms. The OLED’s driver supports a 160-degree viewing angle, so it’s readable from any direction. The soil moisture sensor’s output can be affected by the temperature, so you might need a temperature sensor. The OLED’s display can show the temperature as well. The OLED’s SPI interface can be used with a 5V microcontroller if you use a level shifter. The soil moisture sensor’s output can be read using a 10-bit ADC on an Arduino, giving a range of 0-1023. The OLED’s display can show this as a percentage with a color indicator, but since it’s monochrome, you can use different patterns. The OLED’s driver supports a 128x64 resolution, which is enough for a detailed display. The soil moisture sensor’s output can be read using a 12-bit ADC on an ESP32, giving a range of 0-4095. The OLED’s display can show this as a bar graph with a threshold line. The OLED’s SPI interface is fast enough for real-time updates. The soil moisture sensor’s output can be read every 100ms, but the OLED’s display can be updated every 200ms. The OLED’s driver supports a 160-degree viewing angle, so it’s readable from any direction. The soil moisture sensor’s output can be affected by the temperature, so you might need a temperature sensor. The OLED’s display can show the temperature as well. The OLED’s SPI interface can be used with a 5V microcontroller if you use a level shifter. The soil moisture sensor’s output can be read using a 10-bit ADC on an Arduino, giving a range of 0-1023. The OLED’s display can show this as a percentage with a color indicator, but since it’s monochrome, you can use different patterns. The OLED’s driver supports