Upto: Table of Contents of full book "Internet of Things - a techie's viewpoint"

Arduino

Resources

Specification

For the Arduino Uno "The ATmega328 has 32 KB flash memory (with 0.5 KB occupied by the bootloader). It also has 2 KB of SRAM and 1 KB of EEPROM (which can be read and written with the EEPROM library)." The flash memory is where programs are generally stored. The SRAM is the space used for stack and variable allocations. This isn't much! The largest current model in the Arduino series is the Mega 2560 with 256 KB of Flash Memory of which 8 KB is used by the bootloader, 8KB of SRAm and 4 KB of EEPROM. That would be comfortably enough to run a small O/S

C Programming

From How is programming an Arduino different than standard C? Arduino is C, except that this is inserted into every program:

      
void main() {
  setup();
  for(;;) {
    loop();
  }
}
      
    

That means you can write standard C and it is okay. But you can't blindly use the standard C libraries! The Arduino uses the AVR version of libc. From http://www.nongnu.org/avr-libc/user-manual/index.html

In general, it has been the goal to stick as best as possible to established standards while implementing this library. Commonly, this refers to the C library as described by the ANSI X3.159-1989 and ISO/IEC 9899:1990 ("ANSI-C") standard, as well as parts of their successor ISO/IEC 9899:1999 ("C99"). Some additions have been inspired by other standards like IEEE Std 1003.1-1988 ("POSIX.1"), while other extensions are purely AVR-specific (like the entire program-space string interface).

Unless otherwise noted, functions of this library are not guaranteed to be reentrant. In particular, any functions that store local state are known to be non-reentrant, as well as functions that manipulate IO registers like the EEPROM access routines. If these functions are used within both standard and interrupt contexts undefined behaviour will result. See the FAQ for a more detailed discussion.

C++ Programming

From Can I use C++ on the AVR?

Basically yes, C++ is supported (assuming your compiler has been configured and compiled to support it, of course). Source files ending in .cc, .cpp or .C will automatically cause the compiler frontend to invoke the C++ compiler. Alternatively, the C++ compiler could be explicitly called by the name avr-c++.

However, there's currently no support for libstdc++, the standard support library needed for a complete C++ implementation. This imposes a number of restrictions on the C++ programs that can be compiled. Among them are:

Programming environment

The 'standard' programming environment for creating, compiling and downloading programs to the Arduino is the Arduino IDE based on the Processing IDE. While this is fine for small projects, I am not a fan of IDEs and usually prefer to use emacs, especially for larger projects.

Using emacs (or any other editor such as vim) means you have to know what is going on at a more detailed level, and use a suitable toolchain. Sudar at Compiling Arduino sketches using Makefile has built Makefiles for various boards.

Basic programming

Any language can be used to program the Arduino as long as it compiles down to ATmega machine code. One language for which this has been done is Basic - that hoary old language from the 1980s! Apparently Basic has long been a favourite for programming microcontrollers, and well, the Arduino is just another microcontroller on a board. See BASIC programming on an Arduino by Brian Benchoff for more details.

Interacting with the Arduino through a serial port using Firmata

The normal way of using the Arduino is to write, compile and download a sketch onto the Arduino. This will then run on the Arduino until another compiled sketch is downloaded. After a sketch has been downloaded, there is no more need for it to be connected to the PC it came from: it will just run on the Arduino.

An alternative scenario is to keep the PC connected to the Arduino and to run programs on the PC which interact with the Arduino. These programs can toggle switches, read inputs, write outputs and so on, depending on the hardware configuration on the Arduino.

For this to happen, there must be a general purpose program already running on the Arduino, and there must be some protocol for the PC to communicate with the program on the Arduino.

Firmata is both a program to run on the Arduino and a protocol to talk to it. The Firmata program is a standard sketch that you download and run on the Arduino.

There are several systems in which you can program on the PC, talking to Firmata on the Arduino

A larger list is at firmata/arduino and includes Perl, Java, Ruby, Clojure, ... client libraries


Copyright © Jan Newmarch, jan@newmarch.name
Creative Commons License
"The Internet of Things - a techie's viewpoint" by Jan Newmarch is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at https://jan.newmarch.name/IoT/.

If you like this book, please donate using PayPal