Your first real code
Section 9: From blocks to C++ codeLesson 9.1 · ⏱ 7 min
Engineers program the Arduino in C++. The same ideas as the blocks, but written: setup runs once, and loop repeats. In Robo Maker, click ⌨ C++ code to type, or “Get the blocks’ code” to see your blocks' code.
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop() {
}
pinMode(13, OUTPUT);pin 13 sends electricity out.digitalWrite(13, HIGH);turn it on.- Every line ends with
;, and capital and small letters matter.

📌 Another example: on, then off
In setup: turn pin 13 on, wait two seconds, then turn it off.
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
}
void loop() {
}


✍️ Practise
Run the solved example.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
✍️ Practise
Write the line that turns it on yourself.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Write the code that turns the LED on 13 off.
👀 Show the solution🙈 Hide the solution
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop() {
}

Result: the LED is off.
Medium
Two LEDs on 13 and 12: turn them on together.
👀 Show the solution🙈 Hide the solution
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
}
void loop() {
}

Result: they light up together.
Hard
The code has a mistake: digitalwrite(13, HIGH). Fix it, then run it.
👀 Show the solution🙈 Hide the solution
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop() {
}

Result: the W is a capital: digitalWrite.

