Follow our illustrated blog on Embedded Software Architecture

How to build a hybrid solar/wind energy harvester?

Embedding a Basic interpreter

Embedding a Basic interpreter, 9.0 out of 10 based on 2 ratings
VN:F [1.9.22_1171]
Rating: 9.0/10 (2 votes cast)

The green4 stm32 cortex m3 microprocessor we selected is – according to today’s standards – a rather small microprocessor in Flash (128KB) and RAM (8KB).

why an interpreter

The Green4 device is running control software on top of FreeRTOS, with a couple of drivers, and exposes a communication and an information protocol. We think our users would really benefit if they have a tool with which they can write or add functionality on the fly.

Nowadays, scripting possibilities are present on lots of embedded systems, but they are not easily found on the kind of small processor we mentioned before.


However, in the early days of computing, people were working with the same low (or even lower) computing specifications and they were able to design and implement incredible things with very little RAM and ROM. One of the first languages to be developed for professionals (and later hobbyists and enthousiasts alike) was BASIC. It may come as a surprise to some (younger) people, but open source development quickly thrived as program listings were exchanged individually, or published in magazines and typed over. We refer to the example of Tiny BASIC which is a BASIC interpreter “that can fit in as little as 2 or 3KB of memory”.


The template, we based our Green4-BASIC on, is to be found on Adam Dunkels’ website: uBasic. Adam Dunkels also contributed things like proto-threads, micro-IP and the Contiki OS for embedded development.

statements and variables

The supported statements of uBasic are similar to those of Tiny Basic. We list the limited set from Tiny Basic.

relop ::=
<, >, =, <>, <=, >=

variables ::=
a, b, … x, y, z

statement ::=

PRINT expr-list
IF expression relop expression THEN statement
GOTO expression
INPUT var-list
LET var = expression
GOSUB expression

uBasic supports more ore less the same statements, except for INPUT (not supported in ubasic), while FOR loops were not supported in Tiny Basic. 26 variables (a..z) of type signed integer are supported.
in uBasic, the “GOSUB…RETURN jump” and “nested FOR…NEXT” stacks are hard code limited by defines.
We fixed some bugs and added the WHILE…WEND statement, and some Green4 specific commands to control channels and retrieve information from inputs and sensors. A simple line based editor is also provided.


All this results in a tiny, but very functional BASIC interpreter where the user has a couple of kilo bytes of space to do its own thing, and – of course – to save it on the non-volatile storage, i.e. the internal flash.


DC High power control for home automation, engine management, battery management, … ?

VN:F [1.9.22_1171]
Rating: 9.0/10 (2 votes cast)
About is a joined initiative from Gert Boddaert and Pieter Beyens.
More info: about us, our mission, contact us.

Speak Your Mind