Basics · 12 min read

What Is Block Coding? How Block Programs Become Real Code

What is block coding and how does block-based coding work? Block colors, setup and loop, and an LED Blink example that turns blocks into C++ and MicroPython.

Block coding is a way of building a program by stacking colorful, puzzle-like blocks instead of typing it line by line. Each block is a single instruction, such as "set the pin as output", "turn the LED on" or "wait 100 ms". Because blocks only snap together where they fit, mistakes like a missing semicolon, an unclosed bracket or a misspelled command simply can't happen, and you can focus on the logic of the program. That's why block-based coding is widely seen as the gentlest way into programming for kids and beginners.

This article is part 1 of the From Blocks to Code series. In the next part we build the first program with blocks: blinking an LED with block coding.

But block coding is not a toy language. Behind every block program that goes to an Arduino or a Raspberry Pi Pico there is real code, and the board always runs that code. In this article you'll see what blocks are, what their colors tell you and how a block program turns into real C++ or MicroPython code, step by step, using free BlueGrays projects. At the end you'll also find the strengths and limits of block coding and how it fits together with tools like Scratch, mBlock and Tinkercad.

Blocks of the LED Blink project: setup (runs once) and loop forever
The LED Blink project as blocks: the setup block sets pin 9 as OUTPUT, and the loop forever block turns the LED on for 100 ms and off for 100 ms. Below we match this small program to its C++ code line by line.

What is block coding and why is it called "block"?

In block coding every instruction is a piece with notches and bumps on its edges. The bump at the bottom of one block sits in the notch of the block below, so the instructions run from top to bottom in order. Some blocks have an open mouth inside them: these are container blocks that hold other blocks. Others have white oval slots where you plug in a number or another value block. Since the shape of each piece decides what can go where, it's almost impossible to build a program that breaks the rules. Most of today's block editors follow the path opened by MIT's Scratch and Google's open-source Blockly library. The blocks in BlueGrays are Blockly-based too; we designed the block set ourselves for electronics projects and show it in the app's language. Keep in mind that blocks are not a new programming language. Instructions, order, conditions, loops and variables are exactly the same concepts as in text code; blocks just make them visible.

How does a block program turn into real code?

Block editors use a code generator behind the scenes: every block type has a template in text code, and as the blocks are stacked those templates are joined in order. BlueGrays is a bit stricter: the blocks of every project are built to match the real code that goes onto the board one to one. Every line in the blocks has a counterpart in the code, and every line of code has a counterpart in the blocks. In Arduino and ESP32 projects that code is C++; in Raspberry Pi Pico projects it is MicroPython. To read a block program you only need to know these five parts:

  • setup (runs once): the section that runs only once when the board powers up or resets. It matches the setup() function in Arduino, and it's usually where pins are set as INPUT or OUTPUT.
  • loop forever: the section that runs from top to bottom again and again as long as the board is on. In Arduino it's the loop() function, in MicroPython a while True: loop.
  • Command blocks: blocks like "write HIGH to pin 9" or "wait 100 ms" that are stacked inside the containers and each do one job. Most of them are a single line of text code.
  • Value blocks: numbers like 9 or 100 in the white ovals, and rounded blocks that return a result, such as "read pin 13". They plug into the slots of command blocks and become the value inside the brackets in the code.
  • Dropdowns: options like OUTPUT / INPUT or HIGH / LOW. They match the OUTPUT / INPUT and HIGH / LOW constants in the code, and since the menu only offers valid options, they can't be misspelled.

Enough theory, let's look at a real example. In the LED Blink project an LED and a resistor are connected to pin 9, and the LED blinks five times per second. In the table below we translate the blocks from the image above into code one by one. Each row is one block, and each block is one line of code:

BlockC++ codeWhat it does
setup (runs once)void setup() { … }Its contents run once when the board starts
set pin 9 as OUTPUTpinMode(9,OUTPUT);Makes pin 9 an output that can drive the LED
loop forevervoid loop() { … }Its contents repeat as long as the board runs
write HIGH to pin 9digitalWrite(9,HIGH);Puts 5 V on the pin, the LED turns on
wait 100 msdelay(100);Pauses the program for 100 milliseconds
write LOW to pin 9digitalWrite(9,LOW);Sets the pin to 0 V, the LED turns off
wait 100 msdelay(100);Waits 100 ms with the LED off
C++ (Arduino IDE)
1void setup() {
2  pinMode(9,OUTPUT);     // LED pini çıkış / LED pin as output
3}
4
5void loop() {
6  digitalWrite(9,HIGH);  // LED'i yak / Turn the LED on
7  delay(100);            // 100 ms bekle (1000 ms = 1 sn) / Wait 100 ms (1000 ms = 1 s)
8  digitalWrite(9,LOW);   // LED'i söndür / Turn the LED off
9  delay(100);            // 100 ms bekle / Wait 100 ms
10}

The code has a few things you don't see in the blocks: the word void, curly braces and semicolons. These are C++ syntax rules, and in blocks the shape itself takes care of them. The mouth of a container block stands in for the curly braces, and blocks sitting one under another stand in for the order of the lines. If you changed the 100 in the value slot to 1000, the code would say delay(1000) and the LED would blink once per second. If you picked LOW instead of HIGH from the menu, HIGH would become LOW. Thanks to this one-to-one match, after a few projects you start reading the code without looking at the blocks. You'll find the circuit and the step-by-step build of this same project in part 2 of the series: blinking an LED with block coding.

Blocks are not uploaded to the board directly. In BlueGrays the blocks make the program easier to read and understand; what goes onto the board is always the real code of the same project. For Arduino and ESP32 projects you copy the code into the Arduino IDE, for Pico projects into Thonny, and upload it from there.

What do the block colors mean?

In block editors color is not decoration; it shows which category a block comes from. When you first look at a program, the colors let you read it as "a variable is declared here, a decision is made here, there's a loop here". These are the categories you'll see in the block images of BlueGrays projects:

ColorCategoryExample blocks from the imagesCode
TealProgram structure, pin input/output, waitingsetup (runs once), loop forever, set pin … as OUTPUT, write HIGH to pin …, read pin …, wait … mssetup(), loop(), pinMode(), digitalWrite(), digitalRead(), delay()
Dark tealPico pin and timing blockspin led : GP LED_PIN, OUTPUT; turn on (on) / turn off (off) pin led; wait 0.5 secondsPin(…, Pin.OUT), led.on(), led.off(), time.sleep()
Dusty roseVariablesglobal variables (at the top), declare integer (int) … = …, set … to …int ledState = 0; ledState = …;
Blue-grayLogicif … do, ≠ and = comparisons, not …if, !=, ==, !
GreenLoopscount with i from 0 to 5 by 1for (int i = 0; i < 6; i++)
PurpleArraysarray 2 3 4 5 6 7, item i of ledPinsint ledPins[] = {2, 3, 4, 5, 6, 7}; ledPins[i]

Shapes give you hints too. Condition and comparison blocks have pointed, hexagon-like ends and fit into the condition slot of an "if" block, while number and variable blocks have rounded ends. There's one detail in the table worth noticing: in the "count with i from 0 to 5 by 1" block the upper limit is included, so i takes the values 0, 1, 2, 3, 4 and 5. In code this is written with the condition i < 6, and the loop runs six times. The LED Chaser project lights six LEDs one after another with exactly this block; we cover loop and array blocks in detail in the loops and variables part of the series.

Blocks of the Toggle an LED with a Button (On/Off) project: variable, logic and pin blocks
Toggle an LED with a Button (On/Off): dusty rose blocks are variables, blue-gray "if" blocks are decisions, and teal blocks read and write pins. Every press of the button flips the LED's state.

The nested "if" blocks in this image become nested if lines in the code. The "if button ≠ lastButton" block is the line if (button != lastButton) and runs when the button's state changes. The "if button = 1" inside it catches only the moment of the press. The "set ledState to not ledState" block is the line ledState = !ledState;, which turns 0 into 1 and 1 into 0. The "write ledState to pin 7" block that follows is digitalWrite(7, ledState);. You can see the whole project on the Toggle an LED with a Button (On/Off) page; we unpack the link between the "if" block and if/else in the buttons and conditions part of the series.

C++ on Arduino and ESP32, MicroPython on the Pico

The same block logic is translated into different languages on different boards. In Arduino and ESP32 projects the blocks map to C++ code, and the setup and loop forever blocks become setup() and loop(). For example, the blocks of the LED Blink with ESP32 project have the same structure as LED Blink; only the pin is 5 and the delay is 500 ms. In Raspberry Pi Pico projects the blocks turn into MicroPython. There is no setup() function here: whatever is inside the setup block becomes lines that run once at the top of the file, and the loop forever block becomes a while True: loop.

Blocks of the Raspberry Pi Pico external LED project: pin and timing blocks that turn into MicroPython
Raspberry Pi Pico external LED on GP14: the dark teal blocks match the Pin object and the time.sleep() command in MicroPython. The LED stays on for half a second and off for half a second.
MicroPython
1from machine import Pin
2import time
3
4# ============ PIN ============
5LED_PIN = 14                    # Boştaki herhangi bir GPIO / Any free GPIO
6led = Pin(LED_PIN, Pin.OUT)     # led adlı pin, ÇIKIŞ / pin named led, OUTPUT
7
8print("== BlueGrays: Pico Harici LED Blink ==")
9print("LED pin: GP" + str(LED_PIN))
10print("500ms yakip 500ms sonduruyor / 500ms on 500ms off")
11
12# ============ ANA DÖNGÜ / MAIN LOOP ============
13while True:
14    led.on()                    # LED'i yak / Turn the LED on
15    print("[LED] ACIK / ON")
16    time.sleep(0.5)             # 0.5 saniye bekle / Wait 0.5 s
17
18    led.off()                   # LED'i söndür / Turn the LED off
19    print("[LED] KAPALI / OFF")
20    time.sleep(0.5)             # 0.5 saniye bekle / Wait 0.5 s

You can follow the match line by line. The "declare integer (int) LED_PIN = 14" block inside "global variables (at the top)" is the line LED_PIN = 14; since Python doesn't write types, the (int) part doesn't appear in the code. The "pin led : GP LED_PIN, OUTPUT" block becomes led = Pin(LED_PIN, Pin.OUT). "turn on (on) pin led" is led.on(), "wait 0.5 seconds" is time.sleep(0.5) and "serial print" is a print() line. The "create text with" block puts the text "LED pin: GP" and the value of LED_PIN side by side with +. Watch out: on Arduino the delay is in milliseconds (100 ms), in MicroPython in seconds (0.5), and the blocks show this difference with separate "wait … ms" and "wait … seconds" blocks. For the circuit, see the Raspberry Pi Pico external LED page; if you're new to the Pico, reading what is the Raspberry Pi Pico first will make things easier.

Advantages and limits of block coding

TopicAdvantageLimit
Syntax errorsNo semicolon, bracket or upper/lower case mistakesLogic errors still happen: a wrong pin or a wrong delay is wrong in blocks too
ReadabilityColors and shapes show the structure of a program at a glanceLong programs take up a lot of screen space as blocks; text code stays more compact
Learning speedYou reach your first program in minutes and concepts become concreteIf you never leave blocks, text-code skills like reading error messages don't develop
ScopeCore building blocks like pins, waits, conditions, loops, variables and serial are readyNot every library or hardware detail has a block; advanced projects need text code
Link to the boardThe same logic looks familiar on Arduino, ESP32 and PicoThe board always runs text code, so sooner or later you need to know it

Block coding isn't the answer to everything; it's a bridge. In the first weeks it's a very powerful way to grasp the logic of a program, and the real goal is to cross that bridge at some point and look at text code with confidence. Seeing blocks and code side by side speeds up that move, because in every new project you see the same blocks turn into the same lines again and again. After meeting the "loop forever" block in ten projects, the word loop() won't feel strange anymore. When you're ready for text code, Arduino programming basics walks you through everything from variables to functions with examples.

Who is block coding for?

  • Kids and families: kids aged 9-10 and up who read comfortably can start together with an adult. With the blocks on the screen and an LED lighting up on the breadboard, coding stops being abstract.
  • Teachers and robotics clubs: explaining a project with blocks first and then moving to its code brings different levels together in the same lesson. The lesson plan we prepared for this is in the last part of the series: a robotics coding lesson plan with block coding.
  • Arduino beginners: if you just got your board and the first sketches look confusing, blocks are the fastest way to see what each line does. To get to know the board itself, start with what is Arduino.
  • Anyone who finds code hard to read: understanding a program someone else wrote is a skill in itself. Looking at the blocks of a ready-made project to grasp its structure makes it easier to see what a long piece of code does where.

Compared with Scratch, mBlock, Tinkercad and MakeCode

Most tools that come to mind for block coding are editors that run on a computer or in the browser, and each one shines in its own area. What they have in common is that you drag the blocks and build your own program. BlueGrays answers a different need: it shows the blocks of ready-made, tested projects side by side with the circuit diagram, the parts list and the real code. You can't arrange your own blocks into a program in the app (for now); instead you read how a program that is known to work is built. That's why these tools don't compete; they complement each other. For example, you can compare a traffic light sequence you built in mBlock with the blocks of the Traffic Light project, and if you don't have a board yet, simulate the circuit in Tinkercad. For details, see the mBlock with Arduino and Tinkercad Arduino simulation guides.

ToolWho arranges the blocks?Where it shines
ScratchYou, on a computerA first step into coding with animations, stories and games
mBlockYou, on a computerProgramming boards like Arduino with Scratch-style blocks and viewing the generated code
Tinkercad CircuitsYou, in the browserBuilding a circuit virtually and simulating it with block or text code
MakeCodeYou, in the browserSwitching between blocks, JavaScript and Python for boards like the micro:bit
BlueGraysReady-made, matched to the project's codeReading the blocks of tested projects side by side with real code, circuit diagram and parts list

How to use block coding in the BlueGrays app

  1. Pick a project with the block badge in Discover › Projects. You can also use the filter to show only projects with blocks. More than 100 projects in the app come with blocks, most of them Arduino projects and the rest ESP32 and Raspberry Pi Pico projects.
  2. In the project's code section, turn on the Show as blocks switch. The blocks of the same program replace the code; tap the image to open it full screen and zoom in comfortably.
  3. Switch to the Understand the blocks tab. The program is explained step by step: each step shows only that step's blocks and what they do.
  4. If you wonder what a command does, check the Code guide. Every example there is shown with blocks as well as with Arduino and MicroPython commands.
  5. Turn the switch off to get back to the real code. Copy it, paste it into the Arduino IDE (Thonny for the Pico) and upload it to your board.
  6. Reinforce what you've learned with the Daily questions: 5 questions every day at levels from A1 to C2, a weekly leaderboard and badges.

Both the blocks and the code of 31 projects are completely free; in the other projects the blocks unlock with Premium, just like the code. Some of the free projects that make a good first step: LED Blink, Two LEDs Blinking Alternately, Traffic Light, LED Chaser, LED Blink with ESP32 and Raspberry Pi Pico Onboard LED Blink. You can see all the projects with blocks by platform on the block coding page, where you can also compare an example project's blocks and code side by side.

From Blocks to Code series: the roadmap

  1. What block coding is and how a block program becomes real code (this article)
  2. The first program: blinking an LED with block coding, blocks and C++ side by side
  3. Buttons and conditions: the "if" block and if/else
  4. Loops and variables: loop forever, count, arrays and counters
  5. Reading sensors with blocks, the serial port and MicroPython on the Pico
  6. For teachers: a robotics coding lesson plan with block coding

In the next part of the series we build the LED Blink project from this article from scratch, with the circuit, the blocks and the C++ code side by side, in blinking an LED with block coding. Until then, you can open the projects mentioned here in the BlueGrays app with their circuit diagrams and code, and switch with one tap to the block version of the same program with Show as blocks or to a step-by-step explanation with Understand the blocks.

Frequently asked questions

What is block coding?

Block coding is a way of building programs by stacking colorful blocks that fit together like puzzle pieces instead of typing commands. Each block is one instruction, and the blocks run from top to bottom in order. Since syntax errors are impossible, kids and beginners can focus on the logic of the program.

What is the difference between block-based and text-based coding?

The concepts are the same: instructions, order, conditions, loops and variables exist in both. The difference is in the writing; text code needs semicolons, brackets and exact spelling, while in blocks the shape of the pieces takes care of that. For long and complex programs, text code is more practical.

What age is block coding for?

Kids aged 9-10 and up who read comfortably can start together with an adult, and there are picture-based block tools for younger children. There's no upper age limit; adults who are new to Arduino also benefit a lot from blocks in their first projects.

Which programs are used for block coding?

The best known are Scratch, mBlock, Tinkercad Circuits and MakeCode, where you drag blocks on a computer and build your own program. In the BlueGrays app you read the blocks of ready-made, tested Arduino, ESP32 and Pico projects side by side with their real code. These tools complement each other.

Can you program an Arduino with blocks?

Yes, but the board always runs text code. Tools like mBlock translate the blocks into Arduino code behind the scenes and upload it. In BlueGrays the blocks match the project's C++ code one to one; to upload, you copy the code and paste it into the Arduino IDE.

How do you move from block coding to text coding?

The most effective way is to look at the blocks and the code of the same program side by side. After following which line each block becomes in a few projects, you start reading code without the blocks. Then try making small changes directly in the code, for example changing the delay or the pin number.

See all projects
LED Blink
#1Beginner

LED Blink

The very first hello of electronics: switching a single LED on and off. The Arduino turns the LED on and off at short intervals, so the light blinks quickly. You see in the simplest way how the board

4.6(188)
1125
Two LEDs Blinking Alternately
#2Beginner

Two LEDs Blinking Alternately

Two LEDs blink quickly in turn: when one lights up the other goes off, as if they were tossing a ball back and forth. The Arduino keeps each light on for a fifth of a second and then hands over to the

4.6(105)
343
Traffic Light
#3Beginner

Traffic Light

A tiny copy of the traffic light you see on the road every day. Red stays on for two seconds, yellow flashes for a moment, green stays on for two seconds, then it goes back to red through yellow again

4.8(131)
395
LED Chaser
#5Intermediate

LED Chaser

Lights that switch on one after another, as if they were running. The Arduino lights six LEDs one at a time; each glows for a brief moment and goes out, so the light walks from start to end and then s

4.6(85)
207
Toggle an LED with a Button (On/Off)
#10Beginner

Toggle an LED with a Button (On/Off)

Press the button once and the light comes on, press it again and it goes off, just like the light switch in your room. The Arduino notices every press and flips the LED to the opposite state, waiting

4.8(100)
167
LED Blink with ESP32
#103Beginner

LED Blink with ESP32

Teaches basic GPIO output usage with ESP32. We achieve a blinking effect by turning an LED on and off from a digital pin. ESP32 version of the classic blink project on Arduino UNO; the basic blink commands in the Arduino environment are the same, but the ESP32's pin layout and 3.3 V GPIO levels differ from those of the Arduino UNO.

4.7(37)
99
Raspberry Pi Pico Onboard LED Blink (Pico and Pico W)
#147Beginner

Raspberry Pi Pico Onboard LED Blink (Pico and Pico W)

The opening project of the Raspberry Pi Pico series! We blink the green onboard LED to verify the board works — a classic "Hello World" first experiment. NO external components, just Pico and USB cable. Code is written in MicroPython (the official language backed by Raspberry Pi Foundation). Learn machine.Pin class, Pin.OUT mode, led.on()/off() methods, and time.sleep() in Thonny IDE. Follow every toggle live in the Serial (REPL) output. Also perfect to verify

5.0(1)
10
Raspberry Pi Pico: Controlling an External LED (Breadboard)
#148Beginner

Raspberry Pi Pico: Controlling an External LED (Breadboard)

The next step after Pico’s built-in LED: connecting an external LED to the breadboard and controlling it via the GP14 pin. In this project, you’ll learn how to use any of the Pico’s general-purpose GPIO pins as an output, the LED’s polarity (long leg = anode/+, short leg = cathode/–), and why a current-limiting resistor (220Ω) is required. If you don’t include the resistor, excessive current will flow through the LED and it may burn out. The circuit is very simple: Pico GP14 → 220Ω resistor → LE

4.5(2)
7