4TH Calculator

A small pocket calculator for programmers and engineers, a mix between HP-42 and HP-16. Fully programmable in Forth.

Similar projects worth following
I am using HP calculators for more than 45 years and wrote my very first program on a HP-41. There are many excellent HP calculator emulators for PCs and smart phones and HP still sells pocket calculators. Why should I build one by myself? The HP-15 Collector’s Edition could be the right one or even a DM42 or DM16L from SwissMicro.

A small pocket calculator for programmers (integer operations in hex, decimal, octal, and binary ) and engineers (trigonometric and exponential functions), a mix between HP-42 and HP-16. But it is also a Forth system with interpreter, compiler, editor, filesystem, RTOS, USB CDC/MSC, and BLE.

I had all the parts in my drawer, leftovers from other projects. But other parts can easily be used. The display is a bit small for my eyes, I will build another calculator with a bigger display and a 128x64 resolution, but this calculator will exceed the business card dimension.



  • RPN
  • Keyboard layout similar to HP 42
  • Stack 64 elements
  • 10 registers
  • 3 top elements (x, y, and z) are displayed
  • Floating-point
    • Single precision floating-point IEEE.754
    • Elementary arithmetic
    • trigometric and exponential functions
    • Display modes: fixed, scientific, engineering (metric unit prefix)
  • Integer
    • Hex, decimal, octal, and binary number base
    • Signed (2's complement) and unsigned numbers
    • Elementary arithmetic
    • logical and shift operations
  • Forth programmable

Standard Mecrisp-Cube Forth Features

  • 63 KiB RAM dictionary
  • 128 KiB Flash Forth dictionary
  • 50 KiB for C code
  • Serial console USB CDC / BLE Terminal-IO
  • Filesystem (FAT), Internal Flash drive 0:, 384 KiB
  • Integration in STM32 Cube Ecosystem.
    • Create C code from CubeMX for internal peripherals and use it in Forth
    • Calling C Functions from Forth and vice versa
  • RTOS
    • Forth as CMSIS-RTOS thread.
    • CMSIS-RTOS API to use FreeRTOS from Forth.
    • Buffered terminal I/O (5 KiB buffer for UART Rx). Interrupt driven and RTOS aware, key and emit block the calling thread.
  • USB
    • USB-CDC for serial communication via USB, redirect console I/O like cdc-emit, cdc-key
    • USB-MSC Device: The USB mass storage device class (also known as USB MSC or UMS). You can use your Forth system as a flash drive (also thumb drive [US], memory stick [UK], and pen drive/pendrive elsewhere)
  • BLE 5.0 GAP Peripheral Role (STM32WB)
    • DIS Device Information Service
    • HRS Heart Rate Service (heart rate depends on A0 for Nucleo and A2 for Dongle)
    • CRS Cable Replacement Server service (proprietary service from STM, similar to Classic Bluetooth SPP). Redirect console I/O like crs-emit, crs-key.
  • Floating-Point Unit FPU
    • Support for the floating-point unit FPU, single precision for M4F MPUs and double precision for M7 MPUs
  • vi editor
  • Real Time Clock RTC
  • Watchdog
  • Assertion and Logging

How to Use the Calculator

There are a few differences in operation compared to HP calculators. This applies in particular to entering numbers. Floating-point numbers must have an exponent e.g. 100e, 3.14e, 2.72e0, -10e-12, or end in a metric unit prefix like 10k, 270n, 3.3u.

ENTER reads the number and push it onto the stack. An ENTER without a number do not duplicate the x register (top of stack). If you want to duplicate, you have to use DUP. A stack element is always 32 bit, the content can be float or integer. You have to convert float to integer F→S if you want to use a float number in the integer mode or vice versa S→F.

Warning! If you enter a number with a . but without an exponent e.g. 12.34 Forth interprets this as a double cell integer and put 0 to the x register and 1234 to the y register.

R↓ is actually the Forth word rot, z→y, y→x, x→z.

STO and RCL takes x as register index 0..9.

Caution! If there is an error, the entire stack will be erased. No error message is showed on display, but you can see the error message in the terminal.

RPN crash course.

How it Works

The idea behind this is to use as much of the Forth interpreter as possible. There is already a data stack, but with 64 elements and not only x, y, z, and t registers. The calculator keyboard acts as an ordinary keyboard (terminal), but one key can send several characters (string), e.g. the key COS sends a string " fcos\n". The interpreter executes the float cosinus on the top of the stack. After the successful execution of a line, the stack content is then shown on the display. Digits (0..9, A..F) are forwarded to the display and are interpreted after hitting the ENTER key. If you connect a terminal and hit the TERM key, you can see how the interpreter works.

For implementation details see

About Forth

Forth is an interactive and extensible language, with built-in lexical analysis (tokenizer, parser) and interpreter/compiler, needs less than 20 KiB Flash and 4 KiB RAM, unbelievable for a self-contained (self-hosted) system.

Forth is perfect for embedded systems where some sort of user interactivity like CLI and extensibility (at runtime)...

Read more »


Binary for the 4TH Calculator v1.6.1

application/octet-stream - 768.00 kB - 06/25/2024 at 20:10



Portable Network Graphics (PNG) - 2.09 MB - 06/25/2024 at 10:43



Keyboard overlay, editable with Inkscape.

svg+xml - 50.05 kB - 06/25/2024 at 10:32


  • 1 × STM32WB USB Dongle Part of P-NUCLEO-WB55, DigiKey 497-18384-ND
  • 1 × Perfboard Square grid of 0.1 inches, pads on both sides (plate-through holes), 2" x 3.5"
  • 1 × OLED display 0.91" 128x32 pixels
  • 35 × Push button fit in 0.4" grid, Würth 430473035826, DigiKey #732-7021-1-ND
  • 1 × USB Connector or Charger Adafruit Micro-Lipo Charger for LiPo/LiIon Batt w/USB Type C Jack

View all 8 components

View all 2 project logs

  • 1
    Cut the perfboard

    Cut the perfboard to 2" x 3.5" (50 x 89 mm) dimension or 20 x 35 pads.

  • 2
    Solder push buttons

    Solder the 35 push buttons to the perfboard top layer. Each button is in the middle of 4 x 3 pads, the grid is 0.4".

  • 3
    Wire the buttons

    Wire the buttons to columns and rows on the perfboard bottom layer. Build a keyboard matrix without diodes. Pullups are integrated in the MCU.

View all 16 instructions

Enjoy this project?



crun wrote 07/03/2024 at 23:27 point

Excellent, excellent work!

Consider an eink display. If my HP42 has a failing, it is that the display is rather dim. Eink is both readable, and has a better absolute reflectivity than (say) sharp memory LCD, as well as zero standby current.

I would use decimal maths. There is simply no need for speed in a calculator, but on the other hand the binary/decimal errors are annoying. (I use Free42 in decimal mode).

I would also like my HP42 to be a numeric / calculator keypad for my computer. I day dream of getting the new Framework laptop, and having an HP42 numeric keypad in it.

  Are you sure? yes | no

Peter wrote 07/04/2024 at 19:34 point

Yes, I consider an eink display (250x122, 2.19"). But they are slow, a full update can take 2 s and the display flickers during the update. The partial update is much faster (about 300 ms), but but not all displays have this option.

  Are you sure? yes | no

crun wrote 07/04/2024 at 21:58 point

Most seem to, I was looking closely at the 2.1" and 2.9" to fit in my HP42 when it's display finally fails.

In a calculator you would have a lot of control around when and how you do full refreshes, as mostly it just sits there. I also imagine that you can do some kind of traveling row update, that crawls across the screen cleaning it up.

For me, I would put up with quite a bit for better readibility, zero standby power, and a display that keeps showing the last value without sleeping. 

But that's just me. I have never gotten my head around how in the '80s my HP42 calculator and OM2 camera could run for multiple years on SR44 button cells, I had a sharp pocket computer that I carried in the field doing surveying that ran for weeks on it's batteries, and my ham radio had more than a week of standby time. Now, 35 years later, none of those things go for more than a few days use. What went wrong?

  Are you sure? yes | no

crun wrote 07/05/2024 at 06:38 point

Looking at Goo-display. The "fast update" displays vary from 0.42 to 0.2s fast update times for these two.

Interestingly the flexible ones seems to be 0.3secs. This seems a lot better than I remember from a few years ago when I wanted to use them on the outside of exist equipmment cases where they would be likely to cop abit of damage.

  Are you sure? yes | no

Travis Bemann wrote 06/30/2024 at 19:35 point

This is very neat! I know this is a one-off, but I still wish I could have one.

  Are you sure? yes | no

soni wrote 06/28/2024 at 15:42 point

Please think about solar panel for increase working time.

Screen is to small. Not everyone can read small text. no problem with density. But screen must be bigger.

I like normal mechanic keyboard. You can buy small keys  (low profile keys) gateron or

  Are you sure? yes | no

Peter wrote 06/28/2024 at 19:41 point

You are right, the display is really small but it has to fit onto a business card. I submitted the project to the Hackaday Business Card Contest .

  Are you sure? yes | no

nerdu wrote 06/29/2024 at 21:04 point

In my opinion, there is a lot of space between the buttons. You can calmly fit 2 times the size of the screen. (if You not would You can add options for replace to bigest.)
And modern solar panels are tiny and give a lot.

  Are you sure? yes | no

crun wrote 07/03/2024 at 23:29 point

If you were serious about a business card calc, then you would just make a Sinclair Scientific style calc. Accuracy, meh, we've heard of it. What's with all those buttons and digits  and functions anyway?

(18 buttons,7 digits)

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates