FORTH environment for Arduino

I've written a pretty simple FORTH environment for the Arduino platform. So far, I've tested it only on a ATMega168 Arduino, and simple things, like the LED blinker example, works fine. I'm releasing the source code under GPL2 and putting it on my Github account here:; so please fork it and help me improve it, specially if you're able to cut down the memory usage :)

Called FINF (FINF Is Not Forth), it weighs at the moment of writing, 6.5kB of program memory, and was written using the standard Arduino IDE. It is different from amforth however because words are not saved to the program memory; they stay in RAM and a simple virtual machine takes care of executing it.

As for the LED blinker example, try pasting this into Arduino's Serial Monitor:

1 13 pinmode 
: led 13 pinwrite;
: on 1 led;
: off 0 led;
: w 100 delay;
: blink 0 begin on w off w 1 + dup 10 = negate until;

This will blink the LED on pin 13 ten times, in 100ms intervals. What this does is, line by line:

  1. Set pin 13 to digital output
  2. Create a new word "led" that writes the value on stack on pin 13
  3. Create a new word "on" that pushes "1" on the stack and call the word "led"
  4. Does the same, except it pushes "0" on the stack
  5. Creates a new word "w" that delays program execution by 100ms
  6. Creates a new word "blink" that calls "on", "off", and "w", 10 times
  7. Call the word "blink" -- the LED on your Arduino should be blinking now :)

It may look weird, but FORTH is pretty fun actually. You have some pre-defined words (type "words " on the Monitor to see what you've defined and what comes with FINF; type "dis " to see the disassembly of user-defined words) and end up creating your program by defining new words and gluing them together.

There are a few things missing from FINF, however: - Stability :) - Ability to save/load things from a non-volatile memory - Testing (specially conditionals and loops) - Arrays bound checks, etc - Did I mention stability?

Well, anyway. Give it a try and shout here.



Peanut Gallery Comment: Forth never really caught on... did it.

I downloaded it, and inside a finf folder there's a file called “finf.pde” which I double clicked on and it opened the Arduino application (v22), and then did nothing else, so I wondered how to make it work and found no information on this on your “github” website. Later, however, I double clicked on it again and it then appeared in the already open Arduino application. That's a relief. However, upon setting the board to “Lilypad Arduino with Atmega 328” and my appropriate serial port “/dev/tty.usbserial-A700eXhz” and pressing the upload button, I get the following:

core.a(main.cpp.o): In function `main':
/Applications/ undefined reference to `setup'
/Applications/ undefined reference to `loop'

What do I do next?


Try removing the “inline” keyword of setup() and loop() functions; they’re the last two functions in the finf.pde sketch. I don’t know why it’s not working, but I’ve used Arduino 0017 (I’m on a flaky connection and that’s the one I could find lying around my hard drive).


Yeah, it didn’t. But this is more of a programming exercise than anything else. :slight_smile:

Very neat. FORTH may not be #1 on anyone's list of languages but it is still a great choice for interactive control on a highly-embedded system. For a flavor of what interactivity is like without the "strangeness" of FORTH give Bill Roy's Bitlash a try:

I think the "strangeness" of FORTH is charming and look forward to this FORTH project evolving (can't do much without looping :( )

-- The Rugged Motor Driver: two H-bridges, more power than an L298, fully protected

Bitlash is news to me; I liked it. Currently reading its source code to gather some ideas on how to conserve memory.

BTW, RuggedCircuits, non-nested loops works fine under FINF; but as is the case with if..else..then constructs, it only works inside a defined word. I'll fix this later.

Wrote a serial terminal mode for FINF, so it works also without Arduino's Serial Monitor. The provided shell works as you might expect, with common shortcuts such as Ctrl+L to clear the screen, Ctrl+W to delete the last word, etc. It also offers some colored output to help understand better the output. Here's a nice screenshot of a terminal program running on my computer:

(BTW, the terminal code can be easily reused if one needs this kind of thing; it's pretty small, uses only about 500bytes of Flash and 32 bytes of RAM (for the line buffer))

Forth never really caught on... did it.

It did with me. I wrote a PCB layout program in it on the Mac in the late 80's, I also did a lot of image processing research with it at my University. I also taught it for about 8 years at the University.

About 8 years ago I went to an access control company and a lot of there stuff was written in Forth. I was the only one in the company who could understand it. A great little language.

I was the only one in the company who could understand it.

Might that say something about forth in itself? Or not. ;D

Yeah, FORTH is pretty obscure, specially by today's standards. But it is simple, short, powerful and -- once you get the hang out of it -- easy to program on.

Of course, it is so different from popular languages that finding examples for it is pretty difficult; and I assume most people that uses Arduinos are learning to program, and this is a problem for them.

Being a programmer for almost 20 years (if typing BASIC programs at age 8 copied from an user's manual count as programming), I can say however that learning non-mainstream languages -- the weird ones, not only on their syntax but on how you're supposed to do things on them -- will greatly improve your problem solving abilities.

FORTH is great as in there is practically no syntax: it's like playing with LEGO, but you often have to create your own bricks. And being interactive, it remembers me the fun it was to program on BASIC on a MSX computer.

FINF is my third FORTH interpreter (it is actually a rewrite of my first FORTH environment), and so far, the one I had the most fun writing. I'll probably try something along the same line later; perhaps a dialect of BASIC, why not?

Anyway. The last couple of days I've sent some bug fixes and new features. FINF is now pretty stable and uses a lot of less memory (didn't know about the PROGMEM trick). Things that were previously not working, such as nested if..then and begin..until blocks are working; they also work while typing the program interactively, which is quite nice. The only thing really needed is a way to store your program; I might use the EEPROM for that, or add support for SD cards in the future.

So, if you've tried FINF the first time I've announced it here, try it again. Be sure to uncomment the "#define TERMINAL 1" line and use a terminal program like GNU screen (Mac / Linux) or Putty (Windows).

Have fun! :)

“FORTH never really caught on”!?!?

I guess Adobe NEVER gives any credit to Forth and they should because Postscript IS Forth!
If you ever had an HP programmable calculator, getting into Forth was easy.

Some of my favourite applications are written in Forth, Voyager 1 & 2, the desktop planetarium programme I have running on my Mac (plus!) is a conspicuous example.

Some of my favourite applications are written in Forth, Voyager 1 & 2, the desktop planetarium programme I have running on my Mac (plus!) is a conspicuous example.

I played with Forth on a Sinclair ZX81, back in the day. I recall reading somewhere that Forth was popular with astronomers who used it to control large telescopes. I suppose that enthusiasm has migrated to astronomy software today.

As an artist and not a mathematician, I always thought the phrase 'reverse polish notation' was pretty cool.

Lot's of observatories use Forth. The Arecibo radio telescope and the VLA radio telescope in New Mexico, this is not surprising, Charles Moore worked at the National Radio Astronomy Observatory (NRAO), he was a freelance programmer working mainly on control and data acquisition, which is where Forth really shines. The contempt that C programmers show Forth (and just about any other language) is amazing!

The contempt that C programmers show Forth (and just about any other language) is amazing!

Not me. I have equal contempt for all software languages. Solder is the best programming language. ;)


That's pretty impressive... I wonder if you could fit a PHP implementation into an Arduino, that'd take an Arduino webserver to a whole new level.

Hmm.. why not compile an Apache webserver with PHP module (and of course a firewall) :-/ I think it would need some extra RAM memory ;D

Freak me Lefty! I have this Mars/Lunar lander obsession, my first attempt, age 12, was all relay logic and bulbs! Then I went to DTL, after the start of the 70's I built a 6800 system, with front panel LED's and switches, you might know the one, from that issue of "Practical Electronics". Still at it, always will, till I plant that Australian flag on the Moon/Mars, arduino or not.

Is it me or does Yoda talk Forth?

'theForce withYou strong is'

Its great to see Forth on an Arduino. Well done! I spent a happy time in the 1980's writing a Forth for a TRS-80.

So many good ideas just fade away.

The contempt that C programmers show Forth (and just about any other language) is amazing!

No, it isn't.

FORTH was an interesting and clever hack that proved a useful solution to a problem that doesn't really exist any more: writing code for von Neumann-architecture machines with limited storage and no/expensive cross-development tools.

But it's definitely a "hack", not a tool for doing serious engineering: it lacks the features (most notably, function prototyping) that make it possible for compilers to catch common programmer errors before they turn into crashes, and make it easy for programmers to write re-usable code.

These days, when many targets have Harvard architectures, and you can use a cheap PC with a free version of GCC/SDCC/whatever to cross-develop for almost anything from a PIC to a supercomputer, FORTH only makes sense for a really tiny set of problems.

The reason for much of the "contempt" is the near-cult (and some would omit the "near" ::)) following it attracted, which succeeded in alienating many people who would've been quite happy to have it as one of several tools in their kit.

Ah, so one could say FORTH is really a good candidate to add to the dustbin of history? ;D