Projects / Caesar cipher toy

Caesar cipher toy

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the pot

    Outer legs to 5V and GND, middle to A0.

  2. NANO

    Wire the buttons and buzzer

    D2 and D3 to GND, buzzer to D8.

  3. NANO9.2 mASSD1306

    Wire the OLED

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

  4. NANO

    Upload and open the serial monitor at 9600

    It asks for a message.

  5. NANO

    Type a message and press Enter

    It clicks and shows the scrambled text.

  6. NANO

    Turn the knob

    The scrambled text changes with the shift.

  7. NANO

    Press the break button

    Try every shift until the text is readable.

In short

How do you build a caesar cipher toy?

Type a message on a small keypad, turn a knob to choose a shift, and the Nano shows the scrambled text on a screen. A friend with the same shift can turn it back. It teaches the idea of a cipher and why this old trick is easy to break, so it is a toy and not real security. You need Arduino Nano, Potentiometer 10 kΩ, Tactile push button 6 mm × 2, SSD1306 OLED Display, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
7

Parts list

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

Wiring

FromToWhy
Pot outer legsNano 5V and GNDSupply for the shift knob
Pot middle legNano A0Shift from 0 to 25
Mode buttonNano D2 and GNDSwitch between encrypt and decrypt
Break buttonNano D3 and GNDTry the next shift from 1 to 25
Passive buzzer + and -Nano D8 and GNDA click for each letter
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 Caesar cipher moves every letter along the alphabet by a fixed shift: with a shift of 3, A becomes D and Z wraps round to C. Type a message into the serial monitor and press Enter, and the screen shows it with the scrambled version underneath. Turn the knob to choose a shift from 0 to 25, press the mode button to switch between encrypting and decrypting, and the buzzer clicks once for each letter. Because there are only 25 useful shifts, the cipher is easy to break, and that is what the break button shows: each press tries the next shift, and you will see the readable text appear when you reach the right one. This is a toy for learning the idea of a cipher, not real security, so never use it to protect anything private. Set the serial monitor to 9600 baud with "Newline" line endings. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager. Try a message that has a longer text, or a cipher that uses a different shift for each letter.

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 POT = A0;
const uint8_t BTN_MODE = 2;
const uint8_t BTN_BREAK = 3;
const uint8_t BUZZ = 8;
const uint8_t MAX_LEN = 60;

char message[MAX_LEN + 1] = "HELLO ELECTROLARK";
bool decryptMode = false;
uint8_t breakShift = 0;                  // 0 = off, 1 to 25 = trying shifts
int lastPotShift = -1;
bool prevMode = false, prevBreak = false;

char shiftChar(char c, int shift) {
  if (c < 'A' || c > 'Z') return c;      // leave spaces and digits alone
  return 'A' + ((c - 'A' + shift + 26) % 26);
}

void render(bool clicks) {
  int shift = breakShift ? breakShift : lastPotShift;
  bool dec = breakShift ? true : decryptMode;
  int s = dec ? -shift : shift;
  char out[MAX_LEN + 1];
  uint8_t n = strlen(message);
  for (uint8_t i = 0; i < n; i++) {
    out[i] = shiftChar(message[i], s);
    if (clicks && message[i] != ' ') { tone(BUZZ, 2000, 5); delay(35); }
  }
  out[n] = 0;

  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  if (breakShift) { oled.print(F("Break: try shift ")); oled.print(breakShift); }
  else { oled.print(dec ? F("DECRYPT") : F("ENCRYPT")); oled.print(F("  shift ")); oled.print(shift); }
  oled.setCursor(0, 10);
  oled.print(message);
  oled.drawFastHLine(0, 37, 128, SSD1306_WHITE);
  oled.setCursor(0, 41);
  oled.print(out);
  oled.display();
}

void setup() {
  pinMode(BTN_MODE, INPUT_PULLUP);
  pinMode(BTN_BREAK, INPUT_PULLUP);
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  Serial.println(F("Type a message and press Enter (letters, up to 60 characters)."));
  render(false);
}

void loop() {
  int potShift = map(analogRead(POT), 0, 1023, 0, 26);
  if (potShift > 25) potShift = 25;
  if (potShift != lastPotShift) {
    lastPotShift = potShift;
    breakShift = 0;                      // turning the knob leaves break mode
    render(false);
  }

  bool m = (digitalRead(BTN_MODE) == LOW);
  if (m && !prevMode) { decryptMode = !decryptMode; breakShift = 0; delay(20); render(false); }
  prevMode = m;

  bool b = (digitalRead(BTN_BREAK) == LOW);
  if (b && !prevBreak) { breakShift = breakShift % 25 + 1; delay(20); render(false); }
  prevBreak = b;

  if (Serial.available()) {              // a new message from the serial monitor
    delay(50);
    uint8_t n = 0;
    while (Serial.available() && n < MAX_LEN) {
      char c = Serial.read();
      if (c == '\n' || c == '\r') continue;
      message[n++] = toupper(c);
    }
    while (Serial.available()) Serial.read();
    message[n] = 0;
    breakShift = 0;
    render(true);
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Type a message and press Enter (letters, up to 60 characters).

Test checklist

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

CheckExpected
Knob at 3HELLO becomes KHOOR
Mode buttonSwitches to decrypt
Break buttonEach press tries the next shift
Serial messageAppears on the screen with clicks
Spaces and digitsLeft unchanged

If it does not work

Blank screen

Check the OLED wires and try address 0x3D.

Garbled serial text

Set the monitor to 9600 baud.

Shift jumps about

Check the pot wiper goes to A0.

No clicks

Check you have a passive buzzer on D8.

Lowercase not shown as typed

The sketch changes everything to capitals.

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

Ask Lark AI

Common questions

What parts do I need for the caesar cipher toy?

Arduino Nano, Potentiometer 10 kΩ, Tactile push button 6 mm × 2, SSD1306 OLED Display, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the caesar cipher toy take to build?

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

How do I know the caesar cipher toy works?

Knob at 3: HELLO becomes KHOOR. Mode button: Switches to decrypt. Break button: Each press tries the next shift.

Blank screen: what should I check?

Check the OLED wires and try address 0x3D.

Garbled serial text: what should I check?

Set the monitor to 9600 baud.

Shift jumps about: what should I check?

Check the pot wiper goes to A0.

No clicks: what should I check?

Check you have a passive buzzer on D8.