Controlling purpose built analytical instruments with Forth on an Arduino
The computer language Forth was developed in the 1970s by Charles Moore and used then for the interactive control of radio telescopes. C was introduced around the same time for more general programming tasks. Currently microcontrollers are mostly programmed in C and the programming language used with the Arduino IDE is also a derivative of C.
It may be argued that Forth is better suited for microcontroller based tasks as it has been developed for the control of hardware from the outset. Forth, in contrast to C is an interpreted language, which thus allows the user to directly interact with hardware, i.e. control valves, pumps, high voltage modules etc. from a keyboard. This interactive approach is not readily possible with compiled languages such as C. The Arduino IDE is designed to program an Arduino board to run in a standalone fashion, not to allow constant user interaction to change parameters of an experimental system.
It is, however, possible to install a Forth environment on an Arduino. A system such as the one illustrated below is then obtained, where the user can interact with the microcontroller and its attached hardware via a terminal program running on a PC. The essential elements of Forth and microcontroller hardware are quickly mastered even by novices in programming who then have a very powerful tool at their hands.
Controlling a valve with AmForth running on an Arduino Uno or Nano
As an example, let us assume that a valve is connected via a semiconductor relay to pin 13 of the Arduino (this is the pin where the LED is connected, corresponding to pin 5 of Port B of the ATmega328 controller on the board). The address of the pin can be defined as "valve" in the open source AmForth dialect with the following command sequence:
PORTB 5 portpin: valve
Now the valve can be turned on and off directly from the keyboard with the following commands:
If a timed sequence is required this can be programmed in Forth as follows:
: flush valve high 1000 ms valve low ;
The thus newly defined flush command is then immediately available for execution:
More complex applications can be built in this fashion until the desired operation sequence is defined.
Note that the Arduino Uno and Nano are both based on the ATmega328 microcontroller and the circuitries are identical. Forths for the Uno will therefore also work with the Nano. We prefer to use the Nano as it can be placed on a printed circuit board as a module (rather than having to create a so-called shield).
For the Arduino Uno/Nano a second open source Forth package with the name of Flashforth is also available.
Installing AmForth on an Arduino Uno or Nano
To get started with Forth on an Arduino is not quite as straightforward as employing the Arduino IDE, because the Arduino bootloader cannot be used, but the effort is not too troublesome.
The Forth package is loaded onto the Arduino Uno with the help of programmer, a hardware device, attached to a PC which is connected to the programming adaptor of the Arduino. Different progammers are available from 3rd party sources such as Adafruit, Sparkfun, Waveshare etc. Alternatively a second Arduino which is set up as a programmer can also be used.
A PC program is required to carry out the programming. This also is an open source program with the name of AVRDude.
AVRDude runs under Windows, Linux and macOS. The easiest way to install it on a Macintosh (which we have used) is to use the Homebrew package manager (Homebrew can be installed on the Mac following the instructions on Homebrew.) The following line must be entered into a Terminal window: "brew install avrdude".
AmForth can then be installed by copying the following command sequence into a Terminal window:
avrdude -p m328p -c avrispmkII -P usb -U efuse:w:0xFF:m -U hfuse:w:0xD9:m -U lfuse:w:0xFF:m -U flash:w:uno.hex:i -U eeprom:w:uno.eep.hex:i
This assumes that the progammer employed is a USB AVRISP XPII from Waveshare and that the two files uno.hex and uno.eep.hex are in the home directory. The files are found in the downloaded AmForth package (e.g. amforth-6.8/appl/arduino). If a different programmer is used then avrispmkII has to be changed to the appropriate name.
(Note that for earlier versions of AVRDude the e-fuse settings used had to be efuse:w:0x05:m. The actual programming of the microcontroller has not changed, just the way AVRDude works.)
Connecting to AmForth on the Arduino
The connection to the Arduino is then made with a terminal emulator program on the PC. For the Mac we have been using CoolTerm. This has to be set to 38400 Baud, 8 Data bits, no parity and 1 stop bit. The port name depends on the set-up, but on a Mac it will be something like "usbmodem1411". If on connecting the Forthduino responds with the following line you are set to go:
> amforth 6.7 ATmega328P Forthduino