Build it step by step
0 of 7 doneWire the knobs
Outer legs to 5V and GND, middles to A0 and A1.
Wire the button
One leg to D2, the other to GND.
Wire the OLED
SDA to A4, SCL to A5, 5V and GND.
Upload the sketch
Two scales and a sum appear on the screen.
Set about 2 and 3
The answer reads about 6.00.
Watch the hairline
It points at 6 on the top scale.
Press the button
The sum becomes a division.
How do you build a digital slide rule?
Two knobs set two numbers and the screen shows their product by adding logarithms, the same trick the old slide rules used. The screen also draws two sliding scales so you can see why adding lengths multiplies numbers. A button switches to dividing. A fun history and maths project. You need Arduino Nano, SSD1306 OLED Display, Potentiometer 10 kΩ × 2, 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
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Pot 1 outer legs | Nano 5V and GND | Sets the first number |
| Pot 1 middle leg | Nano A0 | 1.00 to 10.00 |
| Pot 2 outer legs | Nano 5V and GND | Sets the second number |
| Pot 2 middle leg | Nano A1 | 1.00 to 10.00 |
| Mode button | Nano D2 and GND | Switches between multiply and divide |
| OLED SDA and SCL | Nano A4 and A5 | I2C data and clock |
| OLED VCC and GND | Nano 5V and GND | Power for a 0.96 inch module |
Code
A slide rule multiplies by adding lengths. On a ruler marked in logarithms, the distance from 1 to a number is the logarithm of that number, and adding two logarithms multiplies the two numbers. The knobs set two numbers from 1 to 10, and the sketch works out the answer both the normal way and by adding logarithms, which agree. The screen draws two scales, a fixed one on top and a sliding one below it. To multiply, the sliding scale is moved so that its 1 sits under the second number, and the hairline on the first number of the sliding scale then points at the answer on the fixed scale. If the hairline runs off the right-hand end, the sketch uses the second 1 at the far end of the sliding scale, which is how real slide rules do it, and the answer is then read as ten times larger. The button switches to dividing, where the scales slide the other way. Compare the printed answer with the position of the hairline, and try some sums for which you already know the answer to see how it works. Engineers used slide rules until pocket calculators arrived in the 1970s. 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 int X0 = 4; // left end of a scale
const int LEN = 120; // length of one full scale, 1 to 10
bool divideMode = false;
bool prevBtn = false;
int scaleX(float logValue) { return X0 + (int)(LEN * logValue + 0.5); }
void drawTicks(int y, int shift) { // ticks for 1..10, moved right by shift
for (int n = 1; n <= 10; n++) {
int x = scaleX(log10((float)n)) + shift;
for (int pass = 0; pass < 2; pass++) {
int xx = x - pass * LEN; // the second pass is the right-hand index
if (xx >= X0 && xx <= X0 + LEN) {
oled.drawFastVLine(xx, y, 5, SSD1306_WHITE);
if (n < 10 || pass == 0) {
oled.setCursor(xx - 2, y + 7);
oled.print(n == 10 ? 1 : n);
}
}
}
}
}
void setup() {
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop() {
bool b = (digitalRead(2) == LOW);
if (b && !prevBtn) divideMode = !divideMode;
prevBtn = b;
float a = 1.0 + analogRead(A0) / 1023.0 * 9.0; // 1.00 to 10.00
float c = 1.0 + analogRead(A1) / 1023.0 * 9.0;
float la = log10(a), lc = log10(c);
float answer = divideMode ? a / c : a * c;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print(a, 2);
oled.print(divideMode ? F(" / ") : F(" x "));
oled.print(c, 2);
oled.print(F(" = "));
oled.print(answer, 2);
// Fixed scale D on top, sliding scale C below it.
oled.drawFastHLine(X0, 20, LEN + 1, SSD1306_WHITE);
drawTicks(15, 0);
int shift, hair;
if (!divideMode) {
shift = (int)(LEN * lc + 0.5); // slide C so its 1 sits over the second number
hair = scaleX(la) + shift; // the first number on C
} else {
float d = la - lc;
if (d < 0) d += 1.0; // wrap round the scale
shift = (int)(LEN * d + 0.5); // slide C so its second number sits under the first
hair = X0 + shift; // C's index
}
if (hair > X0 + LEN) hair -= LEN;
oled.drawFastHLine(X0, 40, LEN + 1, SSD1306_WHITE);
drawTicks(35, shift);
oled.drawFastVLine(hair, 12, 32, SSD1306_WHITE); // the hairline
oled.setCursor(0, 56);
oled.print(F("Press: x or /"));
oled.display();
delay(40);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Knobs at 2 and 3 | 2.00 x 3.00 = 6.00, hairline at 6 |
| Knobs at 5 and 4 | 20.00, hairline near 2 on the second pass |
| Press the button | Sign changes to a slash |
| Divide 6 by 3 | The answer is about 2.00 |
| Turn a knob | The lower scale slides smoothly |
If it does not work
Blank screen
Check the OLED wires and try address 0x3D.
Numbers jitter
Knobs are noisy, so average a few readings if you like.
Hairline in the wrong place
Check LEN and X0 are the same in every place.
Button does nothing
Check it 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 →
Common questions
What parts do I need for the digital slide rule?
Arduino Nano, SSD1306 OLED Display, Potentiometer 10 kΩ × 2, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the digital slide rule take to build?
About 80 minutes for a beginner maker, in 7 steps.
How do I know the digital slide rule works?
Knobs at 2 and 3: 2.00 x 3.00 = 6.00, hairline at 6. Knobs at 5 and 4: 20.00, hairline near 2 on the second pass. Press the button: Sign changes to a slash.
Blank screen: what should I check?
Check the OLED wires and try address 0x3D.
Numbers jitter: what should I check?
Knobs are noisy, so average a few readings if you like.
Hairline in the wrong place: what should I check?
Check LEN and X0 are the same in every place.
Button does nothing: what should I check?
Check it goes to D2 and GND.
