Projects / Rolling sparkline

Rolling sparkline

Beginner1.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO384 lux

    Build the light divider

    LDR from 5V to A0, 10 k from A0 to GND.

  2. NANO

    Wire the button

    One leg to D2, the other to GND.

  3. NANO12.0 mASSD1306

    Wire the OLED

    SDA to A4, SCL to A5, 5V and GND.

  4. NANO

    Upload the sketch

    A short line starts at the right edge of the screen.

  5. NANO

    Wait a minute

    The chart fills the screen and starts to scroll.

  6. NANO

    Cover and uncover the sensor

    The line drops and rises.

  7. NANO

    Press the freeze button

    HOLD appears and the chart stops.

In short

How do you build a rolling sparkline?

A ring buffer remembers the last 128 readings of a light sensor and the screen draws them as a tiny moving chart, with the newest on the right. A button freezes the chart so you can look closely. It is a neat way to see how a ring buffer reuses the same memory, and a handy display for any sensor. You need Arduino Nano, LDR Photoresistor, Resistor 10 kΩ, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 80 minutes.

Level
Beginner
Time
About 80 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LDR leg 1Nano 5VTop of the light divider
LDR leg 2 and 10 k resistorNano A0Middle of the divider
10 k resistor other endNano GNDBottom of the divider
Freeze buttonNano D2 and GNDPauses and resumes the chart
OLED SDA and SCLNano A4 and A5I2C data and clock
OLED VCC and GNDNano 5V and GNDPower for a 0.96 inch module

Code

A ring buffer is a fixed block of memory that is used round and round. Here it holds 128 readings, one for each column of the screen. Every half second the sketch writes the newest reading into the slot called head and then moves head on by one, wrapping to 0 after 127, so the oldest reading is always overwritten by the newest and the memory never grows. To draw, the sketch walks from the oldest reading to the newest, which is why the starting point is head, and joins the dots with short lines so the chart reads as one wiggly line. The chart rescales itself to the lowest and highest readings it holds, and those two numbers are printed at the top; if everything is almost flat the scale is limited so tiny wobbles do not look huge. The readings are divided by four to fit in one byte each, which keeps the whole ring to 128 bytes on the Nano. The button freezes the chart, and the word HOLD appears, so you can study a moment; press again to carry on. Cover the sensor and uncover it to see the line drop and rise, and breathe on it from a distance to see slower changes. The same code will draw any sensor you can read with analogRead, such as a temperature sensor or a knob. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const uint8_t LDR_PIN = A0;
const uint8_t BTN_PIN = 2;
const uint8_t W = 128;                    // one reading for each column
const unsigned long SAMPLE_MS = 500;      // 128 readings is about a minute

uint8_t ring[W];                          // readings scaled to 0..255
uint8_t head = 0;                         // next slot to write
uint8_t count = 0;
bool frozen = false;
bool prevBtn = false;
unsigned long lastSample = 0;

void draw() {
  uint8_t lo = 255, hi = 0;
  for (uint8_t i = 0; i < count; i++) {
    if (ring[i] < lo) lo = ring[i];
    if (ring[i] > hi) hi = ring[i];
  }
  if (count == 0) { lo = 0; hi = 255; }
  if (hi - lo < 8) hi = lo + 8;           // do not blow up tiny wobbles

  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print(F("lo "));
  oled.print(lo);
  oled.print(F("  hi "));
  oled.print(hi);
  if (frozen) { oled.setCursor(98, 0); oled.print(F("HOLD")); }

  int prevY = -1;
  for (uint8_t x = 0; x < W; x++) {
    if (x < W - count) continue;          // not filled yet
    uint8_t idx = (head + x) % W;         // oldest on the left, newest on the right
    int y = 63 - (int)(ring[idx] - lo) * 48 / (hi - lo);
    if (prevY >= 0) oled.drawLine(x - 1, prevY, x, y, SSD1306_WHITE);
    prevY = y;
  }
  oled.display();
}

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  draw();
}

void loop() {
  bool b = (digitalRead(BTN_PIN) == LOW);
  if (b && !prevBtn) { frozen = !frozen; draw(); delay(20); }
  prevBtn = b;

  unsigned long now = millis();
  if (!frozen && now - lastSample >= SAMPLE_MS) {
    lastSample = now;
    ring[head] = analogRead(LDR_PIN) >> 2;  // 0..1023 down to 0..255
    head = (head + 1) % W;
    if (count < W) count++;
    draw();
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Steady lightA flat line near the middle
Cover the sensorThe line falls
Uncover itThe line rises again
After about a minuteThe chart is full and scrolls
Freeze buttonHOLD shows and the chart stops
Press againThe chart carries on

If it does not work

Blank screen

Check the OLED wires and try address 0x3D.

Line always flat at the bottom

Check the LDR divider and the 10 k to GND.

Chart jumps around

Keep the sensor away from flickering lights.

Chart will not freeze

Check the button goes to D2 and GND.

Upload fails

Choose the Old Bootloader option for the Nano.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the rolling sparkline?

Arduino Nano, LDR Photoresistor, Resistor 10 kΩ, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the rolling sparkline take to build?

About 80 minutes for a beginner maker, in 7 steps.

How do I know the rolling sparkline works?

Steady light: A flat line near the middle. Cover the sensor: The line falls. Uncover it: The line rises again.

Blank screen: what should I check?

Check the OLED wires and try address 0x3D.

Line always flat at the bottom: what should I check?

Check the LDR divider and the 10 k to GND.

Chart jumps around: what should I check?

Keep the sensor away from flickering lights.

Chart will not freeze: what should I check?

Check the button goes to D2 and GND.