Arduino cheat sheet - Robosoc-Southampton/Tutorials GitHub Wiki

Contents

The basics

You need a setup and loop function defined, each taking no parameters and returning void.

void setup() {
    // do stuff
}

void loop() {
    // do stuff
}

setup is called once at the beginning then loop is called repeatedly after that.

Writing a value to a pin

digitalWrite(pin, value); // value is HIGH or LOW
analogWrite(pin, value); // value is 0-255 inclusive

Don't forget to use pinMode(pin, OUTPUT); first.

Reading a pin

value = digitalRead(pin);
value = analogRead(pin);

Don't forget to use pinMode(pin, INPUT); first.

Serial communication

To write things using serial, use print or println.

Serial.println("stuff");
Serial.print("stuff without a newline at the end");

To read things, use available to get the number of bytes available, and read to read a byte.

if (Serial.available()) {
    int byte = Serial.read();
}

Pausing for some time

To pause for some time, use delay.

delay(time); // time is in milliseconds