AVR101 Reference: the Arduino C and C++ the simulator compiles
The AVR101 Simulator (version 1.0.1) compiles your sketch with GCC 7.3.0 (avr-gcc), as the Arduino IDE does for the Uno, so it takes the whole Arduino language: C and C++ with the Arduino functions, in one file or several, with the Wire, SPI, and Servo libraries. This page lists the commands the examples use, grouped as in the Arduino language reference, and the limits. See the AVR101 Help page for how to use the simulator. Back to the simulator.
Reference - Table of Contents
Sketch
void setup() { } | runs once when the Uno starts |
void loop() { } | runs again and again after setup() |
int add(int a, int b) { } | your own functions, with return |
#define LED 13 | a name for a value (no parameters) |
#include <Arduino.h> | the Arduino core, built in (also "Arduino.h", <avr/io.h>, <stdint.h>, <stdbool.h>, <math.h>); any other library name is an error |
#include "ace.h" | one of the sketch's own files (see Files and the preprocessor) |
// and /* */ | comments |
Control
if (x) { } else { } | choose |
for (int i = 0; i < 5; i++) { } | count |
while (x) { } | repeat while true |
do { } while (x); | repeat at least once |
switch (x) { case 1: break; default: } | choose by value |
break; continue; return x; | leave a loop, skip, return |
Types
bool boolean | true or false |
char byte | 8 bits |
int short | 16 bits, as on the Uno |
long | 32 bits |
unsigned int, unsigned long | no negative values |
uint8_t int8_t uint16_t int16_t | fixed sizes |
uint32_t int32_t word size_t | fixed sizes |
float double | decimal numbers |
const static volatile | qualifiers |
int a[5] = {1, 2}; | arrays |
Constants
HIGH LOW | pin levels, 1 and 0 |
INPUT OUTPUT INPUT_PULLUP | pin modes |
LED_BUILTIN | pin 13 |
A0 to A5 | analog pins (14 to 19) |
true false | bool values |
DEC HEX BIN OCT | number formats for Serial.print |
PI HALF_PI TWO_PI | circle constants |
DEG_TO_RAD RAD_TO_DEG | angle conversion |
Operators
= + - * / % | assign and arithmetic |
== != < > <= >= | compare |
&& || ! | logic |
& | ^ ~ << >> | bits |
++ -- += -= *= /= %= | compound |
&= |= ^= <<= >>= | compound bits |
x ? a : b | choose a value (numbers only) |
Digital I/O
pinMode(pin, mode) | INPUT, OUTPUT, or INPUT_PULLUP |
digitalWrite(pin, value) | HIGH or LOW |
digitalRead(pin) | returns HIGH or LOW |
Analog I/O
analogRead(pin) | A0 to A5, returns 0 to 1023 |
analogWrite(pin, value) | PWM 0 to 255 (pins 3, 5, 6, 9, 10, 11) |
Time
delay(ms) | wait in milliseconds |
delayMicroseconds(us) | wait in microseconds |
millis() | milliseconds since the start |
micros() | microseconds since the start |
Serial
Serial.begin(9600) | start the Serial Monitor |
Serial.print(x) Serial.print(x, HEX) | print a value or text |
Serial.println(x) | print, then a new line |
Serial.write(b) | send one byte |
Serial.available() Serial.read() | return 0 and -1 (no input) |
Serial.println("a:" ...) | name:value pairs draw the Plotter |
Math
map(x, inLow, inHigh, outLow, outHigh) | rescale a value |
constrain(x, low, high) | keep x in a range |
min(a, b) max(a, b) abs(x) | smaller, larger, size |
sq(x) sqrt(x) pow(x, y) | square, square root, power |
sin(x) cos(x) tan(x) | angles in radians |
Random
random(max) random(min, max) | a whole number below max |
randomSeed(n) | start a new random sequence |
Bits and bytes
bit(n) _BV(n) | the value of bit n |
bitRead(x, n) | bit n of x |
lowByte(x) highByte(x) | the low or high 8 bits |
Sound
tone(pin, frequency) | a square wave on the buzzer |
tone(pin, frequency, ms) | for a time |
noTone(pin) | stop the sound |
Files and the preprocessor
Name.ino, x.h, x.cpp | one sketch, up to 16 files in one folder (the file tabs above the editor) |
#include "x.h" | reads the file x.h of the sketch; "lib/x.h" finds x.h too |
#ifndef X_H #define X_H #endif | include guards; also #ifdef, #else, #undef |
#pragma once | a file is read once |
\ at the end of a line | the line goes on (long strings and defines) |
| Limits | 16 files, 8 include levels, 200,000 characters in all |
C++: classes, templates, namespaces
struct P { int x = 1; int sum() const; }; | structs and classes: fields with starting values, methods, const methods |
public: private: protected: | access labels |
Counter(int s) : n(s) { } | constructors with an initializer list; Counter c(41); |
static int sq(int v); | static members; call as M::sq(7) |
this->k, obj.field, obj.method() | members of an object |
template <uint8_t P, uint16_t T = 45> | class and function templates with type (typename T) and number parameters and defaults |
Pin<10>::high(); times<3>(5) | template arguments in a call |
namespace ns { } using namespace ns; ns::f() | namespaces |
enum, enum class, typedef, using T = ...; | named values and type names |
constexpr, inline, static_assert(c, "text"); | a false static_assert is a build error with its text |
void two(int &a) | reference parameters; default values int n = 3 |
F("text"), while (!Serial) | text in flash; the port is always ready |
class B : public A, virtual, pointers | inheritance, virtual methods, and pointers: all of C++ that avr-gcc compiles |
| Limits | the Uno's memory: 2,048 bytes of RAM and 32,256 bytes of flash |
Registers
DDRB PORTB PINB | pins 8 to 13 |
DDRC PORTC PINC | pins A0 to A5 |
DDRD PORTD PIND | pins 0 to 7 |
PORTB |= _BV(PB5); | bit names PB5, DDB5, PINB5 are 0 to 7 |
TCCR0A TCCR1A TCCR2A (and B) | timers: a COM bit (COM0A1, COM0B1, ...) puts its pin on PWM, as analogWrite() does |
OCR0A OCR0B OCR1A OCR1B OCR2A OCR2B | the PWM duty of pins 6, 5, 9, 10, 11, 3 |
ADMUX = _BV(REFS0) | ch; | chooses the analog input A0 to A5 |
ADCSRA |= _BV(ADSC); then ADC | one conversion, read as ADC (or ADCL and ADCH), as analogRead() does |
Limits
libraries (#include) | Wire, SPI, and Servo; other libraries are not included yet |
the STL (vector, map) | not part of avr-gcc, as on a real Uno |
Serial input from the keyboard | not yet: the Serial Monitor shows output only (Serial between the Uno and the ATmega328P works) |
program size | at most 32,256 bytes of flash and 2,048 bytes of RAM for the global variables, as on the Uno |
void f(int n = 3) used above it | a function with default values, or a template, used above the place it is written needs its own declaration above that use (other functions are declared for you, as in the Arduino IDE) |
attachInterrupt(), String, pointers, inheritance, virtual, and EEPROM all work: the sketch is compiled by GCC, as for a real Uno.
About this reference
- An int is 16 bits, as on the Uno: it holds −32768 to 32767, and a larger value wraps around when it is stored. Use long for larger numbers.
- Build compiles the sketch with GCC and reports the flash it uses; Upload builds and runs it. An error is GCC's own message, naming its file and line.
- Timing follows the chip's 16 MHz clock; on a slow computer the simulation may fall behind real time.
- The full Arduino language reference is at docs.arduino.cc.