Sim LabArduino
Writing Arduino code
Sketches and AVR C, the files you can use, libraries that are available, and a starter example.
Two ways to write code
The Code panel next to the circuit has a mode for each way of programming the board. A question can limit which one you may use.
| Mode | File | What it is |
|---|---|---|
| Arduino sketch | sketch.ino | The normal Arduino language (C++ with setup() and loop()). |
| AVR C | main.c | Plain C that works with the chip's registers directly (avr-libc). |
The editor has syntax colouring and autosaves.
Your first sketch
The starter sketch blinks the built-in LED on pin 13 and prints "on" and "off" to the serial monitor. This is the idea:
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(13, HIGH);
Serial.println("on");
delay(500);
digitalWrite(13, LOW);
Serial.println("off");
delay(500);
}Wire an LED (with a resistor) from pin 13 to ground, press Upload, and it blinks.
Files
- Most programs are a single file (
sketch.inoormain.c). A program can have up to 8 files, and the whole program can be at most 64 KB. - File names use letters, digits,
_and-, then a short extension, for examplehelpers.h. - Sketches can use
.ino,.cpp,.c,.hand.S. AVR C can use.c,.hand.S. - If you have several
.inotabs, they are joined together the way the Arduino IDE does it. Function prototypes are made for you, so you can call a function that is defined belowloop().
Libraries you can include
These are built in. You can #include them without installing anything.
| Library | For |
|---|---|
Servo | Servo motors |
LiquidCrystal | Parallel 16×2 LCD |
LiquidCrystal_I2C and LiquidCrystal_PCF8574 | I2C LCD (lcd.init(); lcd.backlight(); style sketches build) |
Wire, SPI, SoftwareSerial | They compile, but there is no device to talk to for SPI and SoftwareSerial |
EEPROM | The chip's 1 KB EEPROM |
Serial | Built into the core |
Language notes
- The board uses the standard Arduino AVR core. C++ exceptions are not available.
- Errors point to your own file and line, for example
sketch.ino:3.